-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrod_base.go
301 lines (257 loc) · 7.57 KB
/
rod_base.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package rod_helper
import (
"github.com/WQGroup/logger"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"github.com/pkg/errors"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
"time"
)
func NewBrowserBase(tmpRootFolder, browserFPath, httpProxyURL string, loadAdblock, loadPic bool) (*BrowserInfo, error) {
var err error
// 随机的 rod 子文件夹名称
nowUserData := filepath.Join(GetRodTmpRootFolder(tmpRootFolder), RandStringBytesMaskImprSrcSB(20))
err = os.MkdirAll(nowUserData, os.ModePerm)
if err != nil {
return nil, err
}
var browser *rod.Browser
// 如果没有指定 chrome 的路径,则使用 rod 自行下载的 chrome
err = rod.Try(func() {
var nowLancher *launcher.Launcher
purl := ""
if loadAdblock == true {
nowLancher = launcher.New().
Delete("disable-extensions").
// 这里要写的是缓存的根目录,不是 Browser 的目录
Set("load-extension", GetADBlockLocalPath(tmpRootFolder, httpProxyURL)).
Proxy(httpProxyURL).
Headless(false). // 插件模式需要设置这个
UserDataDir(nowUserData)
//XVFB("--server-num=5", "--server-args=-screen 0 1600x900x16").
//XVFB("-ac :99", "-screen 0 1280x1024x16").
} else {
nowLancher = launcher.New().
Proxy(httpProxyURL).
UserDataDir(nowUserData)
}
if loadPic == false {
nowLancher.Set("blink-settings", "imagesEnabled=false")
}
if browserFPath != "" {
// 指定浏览器启动
nowLancher = nowLancher.Bin(browserFPath)
}
purl = nowLancher.MustLaunch()
browser = rod.New().ControlURL(purl).MustConnect()
})
if err != nil {
_ = os.RemoveAll(nowUserData)
return nil, err
}
// 忽略证书错误
err = browser.IgnoreCertErrors(true)
if err != nil {
return nil, err
}
return NewBrowserInfo(browser, nowUserData), nil
}
func NewPage(browser *rod.Browser) (*rod.Page, error) {
page, err := browser.Page(proto.TargetCreateTarget{URL: ""})
if err != nil {
return nil, err
}
return page, err
}
func PageNavigate(page *rod.Page, randomUA bool, desURL string, timeOut time.Duration) (*rod.Page, *proto.NetworkResponseReceived, error) {
if randomUA == true {
ua := RandomUserAgent()
err := page.SetUserAgent(&proto.NetworkSetUserAgentOverride{
UserAgent: ua,
})
if err != nil {
if page != nil {
_ = page.Close()
}
return nil, nil, err
}
}
var e proto.NetworkResponseReceived
wait := page.WaitEvent(&e)
err := rod.Try(func() {
page.Timeout(timeOut).MustNavigate(desURL)
wait()
})
if err != nil {
return page, &e, err
}
if page == nil {
return nil, nil, errors.New("page is nil")
}
return page, &e, nil
}
// NewPageHijackRouter 需要手动启动 休要开启协程 Run() 和 释放 Stop
func NewPageHijackRouter(page *rod.Page, loadBody bool, httpClient *http.Client) *rod.HijackRouter {
router := page.HijackRequests()
router.MustAdd("*", func(ctx *rod.Hijack) {
err := ctx.LoadResponse(httpClient, loadBody)
if err != nil {
return
}
})
return router
}
// ContainedWords 返回的页面是否包含关键词
func ContainedWords(pageContent string, failedWords []string) (bool, int) {
for i, word := range failedWords {
if strings.Contains(strings.ToLower(pageContent), strings.ToLower(word)) == true {
return true, i
}
}
return false, -1
}
// ContainedWordsRegex 返回的页面是否包含关键词正则表达式
func ContainedWordsRegex(pageContent string, failedWordsRegex []string) (bool, int) {
for i, wordRegex := range failedWordsRegex {
failedRegex := regexp.MustCompile(wordRegex)
matches := failedRegex.FindAllString(pageContent, -1)
if matches == nil || len(matches) == 0 {
// 没有找到匹配的内容,那么认为是成功的
} else {
return true, i
}
}
return false, -1
}
// PageStatusCodeCheck 页面状态码检查
func PageStatusCodeCheck(e *proto.NetworkResponseReceived, statusCodeInfo []StatusCodeInfo) (PageCheck, error) {
doWhat := func(index int, codeInfo StatusCodeInfo) (PageCheck, error) {
defer func() {
logger.Warningln("PageStatusCodeCheck", codeInfo.Operator, codeInfo.Codes[index], codeInfo.WillDo)
}()
return codeInfo.WillDo, nil
}
if e != nil && e.Response != nil {
for _, codeInfo := range statusCodeInfo {
switch codeInfo.Operator {
case Match:
// 等于的情况
for index, code := range codeInfo.Codes {
if e.Response.Status == code {
return doWhat(index, codeInfo)
}
}
case GreatThan:
// 大于的情况
for index, code := range codeInfo.Codes {
if e.Response.Status > code {
return doWhat(index, codeInfo)
}
}
case LessThan:
// 小于的情况
for index, code := range codeInfo.Codes {
if e.Response.Status < code {
return doWhat(index, codeInfo)
}
}
default:
// 其他情况跳过
continue
}
}
logger.Infoln("PageStatusCodeCheck", Success)
// 都没踩中,那么就继续下面的逻辑吧
return Success, nil
} else {
// 这个事件收不到,那么就是无法使用 page 获取 HTML 以及查询元素的操作的,会卡住一直等着,所以这里需要设置一下,跳过这个代理
logger.Warningln("Skip, Response is nil")
return Repeat, nil
}
}
func GetPublicIP(page *rod.Page, timeOut time.Duration, customDectIPSites []string) (string, error) {
defPublicIPSites := []string{
"https://myip.biturl.top/",
"https://ip4.seeip.org/",
"https://ipecho.net/plain",
"https://api-ipv4.ip.sb/ip",
"https://api.ipify.org/",
"http://myexternalip.com/raw",
}
customPublicIPSites := make([]string, 0)
if customDectIPSites != nil {
customPublicIPSites = append(customPublicIPSites, customDectIPSites...)
} else {
customPublicIPSites = append(customPublicIPSites, defPublicIPSites...)
}
for _, publicIPSite := range customPublicIPSites {
publicIPPage, _, err := PageNavigate(page, true, publicIPSite, timeOut)
if err != nil {
return "", err
}
html, err := publicIPPage.HTML()
if err != nil {
return "", err
}
matcheds := ReMatchIP.FindAllString(html, -1)
if html != "" && matcheds != nil && len(matcheds) >= 1 {
return matcheds[0], nil
}
}
return "", errors.New("get public ip failed")
}
// HasPageLoaded 通过一个 Element 的 XPath 判断是否页面加载完毕
func HasPageLoaded(page *rod.Page, targetElementXPaths []string, timeOut int) bool {
for {
for _, targetElementXPath := range targetElementXPaths {
foundEle, _, _ := page.Timeout(300 * time.Millisecond).HasX(targetElementXPath)
if foundEle == true {
return true
}
}
// Timeout check
if timeOut < 1 {
break
}
timeOut--
time.Sleep(1 * time.Second)
}
return false
}
const regMatchIP = `(?m)((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d))).){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))`
var ReMatchIP = regexp.MustCompile(regMatchIP)
type BrowserInfo struct {
Browser *rod.Browser // 浏览器
UserDataDir string // 这里实例的缓存文件夹
}
func NewBrowserInfo(browser *rod.Browser, userDataDir string) *BrowserInfo {
return &BrowserInfo{Browser: browser, UserDataDir: userDataDir}
}
func (bi *BrowserInfo) Close() {
needClearFolder := bi.UserDataDir
if bi.Browser != nil {
_ = bi.Browser.Close()
bi.Browser = nil
}
if needClearFolder != "" {
if IsDir(needClearFolder) == false {
return
}
logger.Infoln("try clear UserDataDir:", needClearFolder)
time.AfterFunc(5*time.Second, func() {
err := os.RemoveAll(needClearFolder)
if err != nil {
logger.Errorln("clear UserDataDir failed:", err)
} else {
logger.Infoln("clear UserDataDir success")
}
})
} else {
logger.Warningln("UserDataDir is empty")
}
}