-
Notifications
You must be signed in to change notification settings - Fork 2
/
browser.go
192 lines (174 loc) · 5.31 KB
/
browser.go
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
package slackauth
import (
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
)
// newBrwsrLauncher creates a new incognito browser launcher with the given
// headless mode.
func (c *Client) newBrwsrLauncher(headless bool) *launcher.Launcher {
l := launcher.New().Headless(headless).Leakless(isLeaklessEnabled).Devtools(false)
if binpath, ok := c.opts.browserPath(); ok {
l = l.Bin(binpath)
}
return l
}
// usrBrwsrLauncher creates a new user-mode browser launcher.
func (c *Client) usrBrwsrLauncher() *launcher.Launcher {
l := launcher.NewUserMode().Headless(false).Leakless(isLeaklessEnabled).Devtools(false)
if binpath, ok := c.opts.browserPath(); ok {
l = l.Bin(binpath)
}
return l
}
// browserPath returns the path to the browser executable and a boolean
// indicating whether the path is valid.
func (o options) browserPath() (path string, ok bool) {
if o.useBundledBrwsr && !o.forceUser {
// bundled browser can't operate in the user mode. forceUser overrides
// useBundledBrwsr.
return "", false
}
if o.localBrowser != "" {
if p, err := exec.LookPath(o.localBrowser); err == nil {
return p, true
}
}
return lookPath()
}
var ErrNoBrowsers = fmt.Errorf("no browsers found")
// ListBrowsers returns a list of browsers that are installed on the system.
func ListBrowsers() ([]LocalBrowser, error) {
LocalBrowsers, ok := discover()
if !ok {
return nil, ErrNoBrowsers
}
return LocalBrowsers, nil
}
const (
bChrome = "Google Chrome"
bChromium = "Chromium"
bEdge = "Microsoft Edge"
bBrave = "Brave"
)
// LocalBrowser represents a browser that is installed on the system.
type LocalBrowser struct {
Name string
Path string
}
// discover returns the list of browsers that are installed on the system and a
// boolean indicating whether any browsers were found.
func discover() (found []LocalBrowser, has bool) {
for _, br := range browserList {
var err error
p, err := exec.LookPath(br.Path)
if err == nil {
found = append(found, LocalBrowser{br.Name, p})
}
}
return found, len(found) > 0
}
var browserList = map[string][]LocalBrowser{
"darwin": {
{bBrave, "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"},
{bEdge, "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"},
{bChrome, "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"},
{bChromium, "/Applications/Chromium.app/Contents/MacOS/Chromium"},
{bChrome, "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary"},
{bChrome, "/usr/bin/google-chrome-stable"},
{bChrome, "/usr/bin/google-chrome"},
{bChromium, "/usr/bin/chromium"},
{bChromium, "/usr/bin/chromium-browser"},
},
"linux": {
{bBrave, "brave-browser"},
{bChrome, "chrome"},
{bChrome, "google-chrome"},
{bChrome, "/usr/bin/google-chrome"},
{bBrave, "/usr/bin/brave-browser"},
{bEdge, "microsoft-edge"},
{bEdge, "/usr/bin/microsoft-edge"},
{bChromium, "chromium"},
{bChromium, "chromium-browser"},
{bChrome, "/usr/bin/google-chrome-stable"},
{bChromium, "/usr/bin/chromium"},
{bChromium, "/usr/bin/chromium-browser"},
{bChromium, "/snap/bin/chromium"},
{bChromium, "/data/data/com.termux/files/usr/bin/chromium-browser"},
},
"openbsd": {
{bChrome, "chrome"},
{bChromium, "chromium"},
},
"windows": append(
[]LocalBrowser{{bEdge, "edge"}, {bBrave, "brave"}, {bChrome, "chrome"}},
expandWindowsExePaths(
LocalBrowser{bEdge, `Microsoft\Edge\Application\msedge.exe`},
LocalBrowser{bBrave, `BraveSoftware\Brave-Browser\Application\brave.exe`},
LocalBrowser{bChrome, `Google\Chrome\Application\chrome.exe`},
LocalBrowser{bChromium, `Chromium\Application\chrome.exe`},
)...),
}[runtime.GOOS]
// lookPath is extended launcher.LookPath that includes support for Brave
// browser.
//
// (c) MIT license: Copyright 2019 Yad Smood
func lookPath() (found string, has bool) {
for _, b := range browserList {
var err error
found, err = exec.LookPath(b.Path)
has = err == nil
if has {
break
}
}
return
}
// expandWindowsExePaths is based on the same function from rod's browser.go.
//
// (c) MIT license: Copyright 2019 Yad Smood
func expandWindowsExePaths(list ...LocalBrowser) []LocalBrowser {
newList := []LocalBrowser{}
for _, p := range list {
newList = append(
newList,
LocalBrowser{p.Name, filepath.Join(os.Getenv("ProgramFiles"), p.Path)},
LocalBrowser{p.Name, filepath.Join(os.Getenv("ProgramFiles(x86)"), p.Path)},
LocalBrowser{p.Name, filepath.Join(os.Getenv("LocalAppData"), p.Path)},
)
}
return newList
}
func setCookies(browser *rod.Browser, cookies []*http.Cookie) error {
if len(cookies) == 0 {
return nil
}
for _, c := range cookies {
if err := browser.SetCookies([]*proto.NetworkCookieParam{
{Name: c.Name, Value: c.Value, Domain: c.Domain, Path: c.Path, Expires: proto.TimeSinceEpoch(c.Expires.Unix())},
}); err != nil {
return fmt.Errorf("failed to set cookies: %w", err)
}
}
return nil
}
// RemveBundled removes the bundled browser from the system.
func RemoveBrowser() error {
bpath := launcher.DefaultBrowserDir
if bpath == "" {
return nil
}
if _, err := os.Stat(bpath); os.IsNotExist(err) {
return nil
}
if err := os.RemoveAll(bpath); err != nil {
return fmt.Errorf("failed to remove bundled browser: %w", err)
}
return nil
}