Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
240 lines (216 loc) · 7.98 KB
/
Copy pathmain.go
File metadata and controls
240 lines (216 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package main
import (
"embed"
"log"
"os"
"time"
"snishaper/app"
"snishaper/core"
"snishaper/internal/cliapp"
"snishaper/pkg/sysproxy"
"github.com/wailsapp/wails/v3/pkg/application"
"github.com/wailsapp/wails/v3/pkg/events"
)
//go:embed all:frontend/dist
var assets embed.FS
//go:embed build/appicon.png
var trayIcon []byte
func main() {
if app.HasLaunchArg("--core") {
if err := core.RunCoreMain(); err != nil {
log.Fatal(err)
}
return
}
// SniShaperAutostart service host (the autostart "service" method): the
// SCM starts this process with --service-host in session 0, so it must
// be handled before the desktop wait and the non-interactive CLI
// fallback below, which would otherwise swallow it (the service host
// command line can carry --startup too).
if app.HasLaunchArg("--service-host") {
if err := app.RunAutoStartServiceHost(); err != nil {
log.Fatal(err)
}
return
}
// Bounded wait for the interactive desktop during autostart. In the early
// logon phase, the shell may not be ready, causing OpenInputDesktop (inside
// NonInteractiveSession) to fail. Without this wait, the app would misjudge
// the environment, fall back to CLI mode, and exit silently, leaving no
// tray icon or proxy running. We only wait when explicitly launched via
// --startup to avoid affecting normal CLI invocations or session-0 services.
if app.HasLaunchArg("--startup") && app.NonInteractiveSession() {
log.Printf("[main] Early logon phase detected, waiting for interactive desktop...")
deadline := time.Now().Add(30 * time.Second)
for time.Now().Before(deadline) {
if !app.NonInteractiveSession() {
break // Desktop is ready, proceed to GUI
}
time.Sleep(500 * time.Millisecond)
}
if app.NonInteractiveSession() {
log.Printf("[main] Timeout waiting for desktop, falling back to CLI mode")
} else {
log.Printf("[main] Interactive desktop is ready, continuing to GUI")
}
}
// Bundled CLI mode. The desktop binary doubles as the headless CLI: an
// explicit marker (--cli, --serve, any CLI verb) or a launch without an
// interactive desktop (automation, services, winget's post-install
// executable validation runs the app in session 0, where a window cannot
// exist) routes into the shared dispatcher instead of the GUI. The
// headless defaults always apply here — no arguments print the command
// help and exit 0, and the interactive TUI stays with the standalone CLI
// binary — so an automated launcher can never be left hanging on a
// terminal that will never provide input.
if app.NonInteractiveSession() || cliapp.IsCLICommand(os.Args[1:]) {
if !app.NonInteractiveSession() {
app.AttachParentConsole()
}
os.Exit(cliapp.Run(os.Args[1:], true))
}
devSession := os.Getenv("FRONTEND_DEVSERVER_URL") != ""
if devSession {
log.Printf("[main] dev session (FRONTEND_DEVSERVER_URL=%s): staying in the foreground, no elevation, no single-instance take-over", os.Getenv("FRONTEND_DEVSERVER_URL"))
}
if !devSession && !app.HasLaunchArg("--elevated") && !core.IsProcessElevated() {
// Already running? Don't trigger a UAC prompt for a second instance.
if app.IsSingleInstanceRunning("com.snishaper.desktop") {
log.Printf("[main] Instance already running, skipping auto-elevate")
} else if err := core.ElevateSelf(); err != nil {
log.Printf("[main] Auto-elevate failed: %v, continuing without admin", err)
} else {
return // elevated instance will start
}
}
app.RecoverBrokenSingleInstance("com.snishaper.desktop")
if devSession {
if app.IsSingleInstanceRunning("com.snishaper.desktop") {
log.Printf("[main] dev session: another instance is running, proxy ports may conflict until it is closed")
}
} else if app.IsSingleInstanceRunning("com.snishaper.desktop") {
// Wake the running instance; retry to handle UIPI race where first instance hasn't yet allowed cross-integrity.
woke := false
for i := 0; i < 3; i++ {
if err := app.WakeSingleInstance("com.snishaper.desktop"); err == nil {
log.Printf("[main] Woke running instance, exiting second instance")
woke = true
break
}
time.Sleep(200 * time.Millisecond)
}
if woke {
return
}
log.Printf("[main] Failed to wake running instance, killing stale instance and taking over")
app.KillSingleInstance("com.snishaper.desktop")
}
a := app.NewApp()
defer func() {
if r := recover(); r != nil {
log.Printf("[main] Panic recovered, forcing cleanup: %v", r)
}
a.ForceCleanup()
// Force-disable system proxy even if ForceCleanup missed it — but
// only when it is the proxy this app configured; a user's own or
// third-party proxy setting must survive our exit.
if a.IsManagedSystemProxy() {
_ = sysproxy.DisableSystemProxy()
}
}()
opts := application.Options{
Name: "snishaper",
Description: "SniShaper - Cloudflare IP Shaper",
Assets: application.AssetOptions{
Handler: application.BundledAssetFileServer(assets),
},
Services: []application.Service{
application.NewService(a),
},
Icon: trayIcon,
Logger: a.FrameworkLogger(),
// The app owns the window lifecycle on every desktop platform: a
// window that disappears must not end the process, because closing to
// the tray and hibernating both leave the app running with a tray icon.
Windows: application.WindowsOptions{
DisableQuitOnLastWindowClosed: true,
},
Linux: application.LinuxOptions{
DisableQuitOnLastWindowClosed: true,
},
}
if !devSession {
// The framework exits a second instance outright, which would end the
// `wails3 dev` run task (and its Vite server) the moment an instance is
// already running, so dev sessions opt out of the handshake entirely.
opts.SingleInstance = &application.SingleInstanceOptions{
UniqueID: "com.snishaper.desktop",
OnSecondInstanceLaunch: func(data application.SecondInstanceData) {
log.Printf("[single-instance] OnSecondInstanceLaunch args=%v", data.Args)
for _, arg := range data.Args {
if arg == "--tray-rebuild" {
log.Printf("[single-instance] tray rebuild requested by second launch")
a.RebuildSystemTray()
return
}
}
a.RevealMainWindow()
},
ExitCode: 0,
}
}
wailsApp := application.New(opts)
a.SetWailsApp(wailsApp)
if !devSession {
a.StartInstanceListener("com.snishaper.desktop")
app.AllowSingleInstanceCrossIntegrity("com.snishaper.desktop")
go func() {
for i := 0; i < 10; i++ {
app.AllowSingleInstanceCrossIntegrity("com.snishaper.desktop")
time.Sleep(100 * time.Millisecond)
}
}()
}
// Create Tray. The app owns the tray lifecycle so it can rebuild the icon
// when the shell drops it.
a.BuildSystemTray(wailsApp, trayIcon)
// Create Main Window
hidden := a.ShouldStartHidden()
log.Printf("[startup] ShouldStartHidden=%v Hidden=%v", hidden, hidden)
mainWindow := wailsApp.Window.NewWithOptions(application.WebviewWindowOptions{
Name: "main",
Title: "SniShaper",
Width: 1024,
Height: 768,
URL: "/",
Frameless: true,
Hidden: hidden,
BackgroundColour: application.NewRGB(27, 38, 54),
})
a.AttachMainWindowHandlers(mainWindow)
mainWindow.OnWindowEvent(events.Common.WindowRuntimeReady, func(event *application.WindowEvent) {
if !a.ShouldStartHidden() {
a.RevealMainWindow()
}
})
a.SetMainWindow(mainWindow)
log.Printf("[startup] ShouldStartHidden=%v closeToTray=%v hibernate=%v", a.ShouldStartHidden(), a.GetCloseToTray(), a.GetHibernateOnClose())
go func() {
time.Sleep(800 * time.Millisecond)
shouldHide := a.ShouldStartHidden()
vis := a.IsMainWindowVisible()
hib := a.IsHibernated()
log.Printf("[startup] post-check shouldHide=%v isVisible=%v hibernated=%v", shouldHide, vis, hib)
if !shouldHide && !vis {
log.Printf("[startup] post-check: window not visible but should be, calling RevealMainWindow")
a.RevealMainWindow()
time.Sleep(300 * time.Millisecond)
vis2 := a.IsMainWindowVisible()
log.Printf("[startup] after Reveal isVisible=%v", vis2)
}
}()
err := wailsApp.Run()
if err != nil {
log.Fatal(err)
}
}