-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
270 lines (233 loc) · 5.51 KB
/
options.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
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
package browser
import (
"errors"
"io/ioutil"
"os"
"strings"
"time"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/devices"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/launcher/flags"
"github.com/go-rod/rod/lib/proto"
"github.com/sohaha/zlsgo/zcli"
"github.com/sohaha/zlsgo/zerror"
"github.com/sohaha/zlsgo/zfile"
"github.com/sohaha/zlsgo/zstring"
"github.com/sohaha/zlsgo/ztype"
"github.com/sohaha/zlsgo/zutil"
)
type Options struct {
browser *Browser
Hijack HijackProcess
Flags map[string]string
Bin string
WSEndpoint string
UserAgent string
UserDataDir string
AcceptLanguage string
ProxyUrl string
DefaultDevice devices.Device
Envs []string
Scripts []string
Extensions []string
SlowMotion time.Duration
Timeout time.Duration
Headless bool
Incognito bool
UserMode bool
Devtools bool
IgnoreCertError bool
autoKill bool
Stealth bool
Leakless bool
Debug bool
}
func (b *Browser) init() (err error) {
if b == nil {
return errors.New("browser is nil")
}
if b.options.UserMode {
b.launcher = launcher.NewUserMode()
b.options.Headless = false
} else {
b.launcher = launcher.New()
}
for _, v := range []func(b *Browser){
setBin,
setDebug,
setLeakless,
setDefaultDevice,
setUserDataDir,
setEnv,
setFlags,
setExtensions,
} {
v(b)
}
b.launcher.Headless(b.options.Headless)
if b.options.ProxyUrl != "" {
_ = b.client.SetProxyUrl(b.options.ProxyUrl)
}
if b.options.UserAgent != "" || b.options.AcceptLanguage != "" {
ua := &proto.NetworkSetUserAgentOverride{
AcceptLanguage: "en-US,en;q=0.9",
}
if b.options.AcceptLanguage != "" {
ua.AcceptLanguage = b.options.AcceptLanguage
}
if b.options.UserAgent != "" {
ua.UserAgent = b.options.UserAgent
}
b.userAgent = ua
}
if b.options.WSEndpoint == "" {
b.options.WSEndpoint, err = b.launcher.Logger(ioutil.Discard).Launch()
if err != nil {
if strings.Contains(err.Error(), "Failed to launch the browser") {
errMsg := "Failed to launch the browser"
if zutil.IsLinux() {
if isDebian() {
errMsg += `: sudo apt-get install --no-install-recommends -y libnss3 libxss1 libasound2t64 libxtst6 libgtk-3-0 libgbm1 ca-certificates fonts-liberation fonts-noto-color-emoji fonts-noto-cjk`
} else {
errMsg += ": https://pptr.dev/troubleshooting#chrome-doesnt-launch-on-linux"
}
}
return errors.New(errMsg)
}
return err
}
} else {
b.isCustomWSEndpoint = true
}
b.id = ztype.DecimalToAny(int(zstring.UUID()), 64)
b.Browser = rod.New().ControlURL(b.options.WSEndpoint)
for _, v := range b.before {
v()
}
if err = b.Browser.Connect(); err != nil {
return err
}
if b.options.Incognito {
b.Browser, err = b.Browser.Incognito()
if err != nil {
return err
}
}
if b.options.IgnoreCertError {
_ = b.Browser.IgnoreCertErrors(true)
}
for _, v := range b.after {
v()
}
return nil
}
func setEnv(b *Browser) {
b.launcher.Env(b.options.Envs...)
}
func setExtensions(b *Browser) {
extensions := strings.Join(b.options.handerExtension(), ",")
if extensions == "" {
return
}
b.launcher.Set("load-extension", extensions)
}
func setFlags(b *Browser) {
for n, v := range b.options.Flags {
_ = zerror.TryCatch(func() error {
if v == "" {
b.launcher = b.launcher.Set(flags.Flag(n))
} else {
b.launcher = b.launcher.Set(flags.Flag(n), v)
}
return nil
})
}
if b.options.ProxyUrl != "" {
b.launcher = b.launcher.Set(flags.ProxyServer, b.options.ProxyUrl)
}
}
func setLeakless(b *Browser) {
if b.id != "" {
return
}
b.launcher.Leakless(b.options.Leakless)
go func() {
<-zcli.SingleKillSignal()
if b.launcher.PID() != 0 {
p, err := os.FindProcess(b.launcher.PID())
if err == nil {
_ = p.Kill()
}
}
_ = b.Close()
b.Cleanup()
os.Exit(0)
}()
}
func setDefaultDevice(b *Browser) {
b.after = append(b.after, func() {
if b.options.DefaultDevice.Title == "" {
b.Browser.NoDefaultDevice()
} else {
b.Browser.DefaultDevice(b.options.DefaultDevice)
}
if v, err := b.Browser.Version(); err == nil {
if b.userAgent == nil {
userAgent := strings.Replace(v.UserAgent, "Headless", "", -1)
b.userAgent = &proto.NetworkSetUserAgentOverride{UserAgent: userAgent}
}
b.client.SetUserAgent(func() string {
if b.userAgent == nil {
return strings.Replace(v.UserAgent, "Headless", "", -1)
}
return b.userAgent.UserAgent
})
}
})
}
// setBin 优先使用本地浏览器
func setBin(b *Browser) {
b.launcher.Bin(getBin(b.options.Bin))
}
func getBin(path string) string {
if path == "" {
if p, exists := launcher.LookPath(); exists {
path = p
}
}
if !zfile.FileExist(path) {
browser := launcher.NewBrowser()
browser.Logger = newLogger()
bin, err := browser.Get()
if err == nil {
return bin
}
}
return path
}
// setDebug 调试模式
func setDebug(b *Browser) {
debug := b.options.Debug
if b.options.Devtools {
debug = true
b.launcher.Devtools(true)
}
if debug {
b.after = append(b.after, func() {
b.Browser.Trace(true)
b.Browser.SlowMotion(b.options.SlowMotion)
b.Browser.Logger(newLogger())
})
}
}
// setUserDataDir 用户数据保存目录
func setUserDataDir(b *Browser) {
if b.options.UserMode {
return
}
if b.options.UserDataDir == "" {
b.options.UserDataDir = zfile.TmpPath() + "/browser/" + zstring.Rand(8)
}
b.launcher.UserDataDir(zfile.RealPath(b.options.UserDataDir))
}