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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
package ui
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/dialog"
"github.com/shiv/internal/logger"
)
const prefKeyDefaultBrowser = "default_browser"
// DetectedBrowser holds a browser's display name and executable path.
type DetectedBrowser struct {
Name string
Path string
}
// isFirefox returns true if the executable looks like Firefox.
func isFirefox(path string) bool {
base := strings.ToLower(filepath.Base(path))
return strings.Contains(base, "firefox")
}
// DetectBrowsers scans the system for supported browsers and returns all found.
func DetectBrowsers() []DetectedBrowser {
switch runtime.GOOS {
case "linux":
return detectBrowsersLinux()
case "darwin":
return detectBrowsersDarwin()
case "windows":
return detectBrowsersWindows()
}
return nil
}
func detectBrowsersLinux() []DetectedBrowser {
candidates := []struct{ name, bin string }{
{"Google Chrome", "google-chrome"},
{"Google Chrome (stable)", "google-chrome-stable"},
{"Chromium", "chromium"},
{"Chromium (browser)", "chromium-browser"},
{"Brave (Browser)", "brave-browser"},
{"Brave", "brave"},
{"Microsoft Edge", "microsoft-edge"},
{"Vivaldi", "vivaldi"},
{"Vivaldi (stable)", "vivaldi-Stable"},
{"Opera", "opera"},
{"Opera (stable)", "opera-stable"},
{"Opera (browser)", "opera-browser"},
{"Firefox", "firefox"},
{"Firefox ESR", "firefox-esr"},
}
var found []DetectedBrowser
seen := map[string]bool{}
for _, c := range candidates {
path, err := exec.LookPath(c.bin)
if err != nil {
continue
}
if seen[path] {
continue
}
seen[path] = true
found = append(found, DetectedBrowser{Name: c.name, Path: path})
}
return found
}
func detectBrowsersDarwin() []DetectedBrowser {
candidates := []struct{ name, app string }{
{"Google Chrome", "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"},
{"Chromium", "/Applications/Chromium.app/Contents/MacOS/Chromium"},
{"Brave", "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"},
{"Microsoft Edge", "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"},
{"Vivaldi", "/Applications/Vivaldi.app/Contents/MacOS/Vivaldi"},
{"Opera", "/Applications/Opera.app/Contents/MacOS/Opera"},
{"Firefox", "/Applications/Firefox.app/Contents/MacOS/firefox"},
}
var found []DetectedBrowser
for _, c := range candidates {
if _, err := os.Stat(c.app); err == nil {
found = append(found, DetectedBrowser{Name: c.name, Path: c.app})
}
}
return found
}
func detectBrowsersWindows() []DetectedBrowser {
candidates := []struct{ name, path string }{
{"Google Chrome", `C:\Program Files\Google\Chrome\Application\chrome.exe`},
{"Google Chrome (x86)", `C:\Program Files (x86)\Google\Chrome\Application\chrome.exe`},
{"Chromium", `C:\Program Files\Chromium\Application\chrome.exe`},
{"Brave", `C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe`},
{"Microsoft Edge", `C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe`},
{"Vivaldi", `C:\Program Files\Vivaldi\Application\vivaldi.exe`},
{"Opera", `C:\Program Files\Opera\opera.exe`},
{"Firefox", `C:\Program Files\Mozilla Firefox\firefox.exe`},
{"Firefox (x86)", `C:\Program Files (x86)\Mozilla Firefox\firefox.exe`},
}
var found []DetectedBrowser
seen := map[string]bool{}
for _, c := range candidates {
if _, err := os.Stat(c.path); err == nil {
if !seen[c.path] {
seen[c.path] = true
found = append(found, DetectedBrowser{Name: c.name, Path: c.path})
}
}
}
return found
}
// profileDir returns the persistent browser profile directory for the given browser executable.
func profileDir(fyneApp fyne.App, browserPath string) string {
root := fyneApp.Storage().RootURI().Path()
if isFirefox(browserPath) {
return filepath.Join(root, "browser-profile-firefox")
}
return filepath.Join(root, "browser-profile-chrome")
}
// setupFirefoxProfile creates a Firefox profile dir with proxy settings written into user.js.
func setupFirefoxProfile(profilePath, proxyHost string, proxyPort int) error {
if err := os.MkdirAll(profilePath, 0700); err != nil {
return fmt.Errorf("create firefox profile dir: %w", err)
}
userJS := fmt.Sprintf(`// Shiv proxy profile — auto-generated, do not edit manually.
user_pref("network.proxy.type", 1);
user_pref("network.proxy.http", "%s");
user_pref("network.proxy.http_port", %d);
user_pref("network.proxy.ssl", "%s");
user_pref("network.proxy.ssl_port", %d);
user_pref("network.proxy.no_proxies_on", "");
user_pref("browser.startup.homepage_override.mstone", "ignore");
user_pref("browser.shell.checkDefaultBrowser", false);
user_pref("extensions.enabledScopes", 0);
user_pref("extensions.autoDisableScopes", 15);
user_pref("network.stricttransportsecurity.preloadlist", false);
user_pref("security.enterprise_roots.enabled", true);
`, proxyHost, proxyPort, proxyHost, proxyPort)
return os.WriteFile(filepath.Join(profilePath, "user.js"), []byte(userJS), 0600)
}
// LaunchBrowser launches the given browser pointed at the proxy with an isolated profile.
func LaunchBrowser(fyneApp fyne.App, browser DetectedBrowser, proxyHost string, proxyPort int) error {
profile := profileDir(fyneApp, browser.Path)
proxyAddr := fmt.Sprintf("%s:%d", proxyHost, proxyPort)
var args []string
if isFirefox(browser.Path) {
if err := setupFirefoxProfile(profile, proxyHost, proxyPort); err != nil {
return err
}
args = []string{
"-profile", profile,
"-no-remote",
}
} else {
// Chromium-based — profile dir is created automatically by the browser.
args = []string{
"--proxy-server=http://" + proxyAddr,
"--user-data-dir=" + profile,
"--no-first-run",
"--no-default-browser-check",
"--disable-extensions",
"--disable-sync",
"--disable-background-networking",
"--disable-default-apps",
"--disable-component-update",
"--disable-breakpad",
"--metrics-recording-only",
"--disable-features=OptimizationHints,MediaRouter",
}
}
cmd := exec.Command(browser.Path, args...)
cmd.Stderr = nil
cmd.Stdout = nil
if err := cmd.Start(); err != nil {
return fmt.Errorf("launch browser: %w", err)
}
logger.Info("browser: launched %s (pid %d) via %s", browser.Name, cmd.Process.Pid, proxyAddr)
// Detach — we don't want to wait for the browser to exit.
go func() {
err := cmd.Wait()
if err != nil {
logger.Error("browser exited early: %v", err)
}
}()
return nil
}
// ClearBrowserProfile deletes the stored profile for the given browser executable.
func ClearBrowserProfile(fyneApp fyne.App, browserPath string) error {
profile := profileDir(fyneApp, browserPath)
if err := os.RemoveAll(profile); err != nil {
return fmt.Errorf("clear browser profile: %w", err)
}
return nil
}
// launchDefaultBrowser reads the saved default browser from prefs and launches it.
// If no default is set it shows a picker dialog first.
func launchDefaultBrowser(fyneApp fyne.App, win fyne.Window) {
prefs := fyneApp.Preferences()
proxyHost := prefs.StringWithFallback(prefKeyProxyHost, defaultProxyHost)
proxyPort := prefs.IntWithFallback(prefKeyProxyPort, defaultProxyPort)
savedPath := prefs.String(prefKeyDefaultBrowser)
if savedPath != "" {
// Verify it still exists.
if _, err := os.Stat(savedPath); err == nil {
browser := DetectedBrowser{Name: filepath.Base(savedPath), Path: savedPath}
if err := LaunchBrowser(fyneApp, browser, proxyHost, proxyPort); err != nil {
dialog.ShowError(err, win)
}
return
}
// Saved browser no longer exists — fall through to picker.
prefs.SetString(prefKeyDefaultBrowser, "")
}
browsers := DetectBrowsers()
if len(browsers) == 0 {
dialog.ShowInformation("No Browser Found",
"No supported browser was detected on your system.\nInstall Chrome, Brave, Firefox, or another Chromium-based browser.",
win)
return
}
// If only one browser found, use it directly and save as default.
if len(browsers) == 1 {
prefs.SetString(prefKeyDefaultBrowser, browsers[0].Path)
if err := LaunchBrowser(fyneApp, browsers[0], proxyHost, proxyPort); err != nil {
dialog.ShowError(err, win)
}
return
}
// Multiple browsers — show picker.
showBrowserPickerDialog(fyneApp, win, browsers, proxyHost, proxyPort)
}
// showBrowserPickerDialog shows a one-time dialog to pick a default browser.
func showBrowserPickerDialog(fyneApp fyne.App, win fyne.Window, browsers []DetectedBrowser, proxyHost string, proxyPort int) {
names := make([]string, len(browsers))
for i, b := range browsers {
names[i] = b.Name
}
selected := browsers[0]
picker := newBrowserPickerContent(names, func(name string) {
for _, b := range browsers {
if b.Name == name {
selected = b
break
}
}
})
d := dialog.NewCustomConfirm("Select Default Browser", "Launch", "Cancel", picker, func(confirmed bool) {
if !confirmed {
return
}
fyneApp.Preferences().SetString(prefKeyDefaultBrowser, selected.Path)
if err := LaunchBrowser(fyneApp, selected, proxyHost, proxyPort); err != nil {
dialog.ShowError(err, win)
}
}, win)
closeOnEscape(win, d.Dismiss)
d.Show()
}
|