263 lines
8.9 KiB
Go
263 lines
8.9 KiB
Go
package libs
|
|
|
|
import (
|
|
browserfingerprint "go-account-register/libs/browser-fingerprint"
|
|
paramsTypes "go-account-register/types"
|
|
"go-account-register/utils"
|
|
"go-account-register/utils/external"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/corpix/uarand"
|
|
"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/proto"
|
|
"github.com/go-rod/stealth"
|
|
)
|
|
|
|
type Chrome struct{}
|
|
|
|
func (c *Chrome) ConnectBitBrowser(url string) (*rod.Browser, string, *rod.Page) {
|
|
log.Println("浏览器的URL", url)
|
|
browser := rod.New().
|
|
ControlURL(url).
|
|
MustConnect().NoDefaultDevice()
|
|
browser.MustIgnoreCertErrors(true)
|
|
|
|
// var page *rod.Page
|
|
// pages := browser.MustPages()
|
|
// if len(pages) >= 2 {
|
|
// page = pages[1]
|
|
// } else {
|
|
|
|
// }
|
|
page := browser.MustPage()
|
|
go browser.EachEvent(func(e *proto.TargetTargetCreated) {
|
|
if e.TargetInfo.Type != proto.TargetTargetInfoTypePage {
|
|
return
|
|
}
|
|
browser.MustPageFromTargetID(e.TargetInfo.TargetID).MustEvalOnNewDocument(stealth.JS)
|
|
})()
|
|
|
|
return browser, url, page
|
|
}
|
|
|
|
func (c *Chrome) Create(accountId int, params *Fingerprint) (*rod.Browser, string, *rod.Page) {
|
|
var accountIdStr = strconv.Itoa(accountId)
|
|
userDataDir := "user-data/" + accountIdStr
|
|
absPath, _ := filepath.Abs(userDataDir)
|
|
log.Printf("用户数据目录: %s", absPath)
|
|
path, _ := launcher.LookPath() // 获取当前系统的浏览器目录
|
|
// 判断是否传递代理
|
|
// var proxyHandle utils.ProxyHandle
|
|
// protocol, username, password, ip, port, errProxy := proxyHandle.ParseProxy(params.Proxy)
|
|
|
|
ipInfo := external.GetIpInfo(strings.TrimSpace(string(params.Proxy)))
|
|
|
|
l := launcher.New().
|
|
Bin(path).
|
|
// Proxy("210.51.27.121:50003"). // 直接传入完整认证URL
|
|
Delete("use-mock-keychain"). // delete flag "--use-mock-keychain"
|
|
Set("disable-blink-features", "AutomationControlled"). // 绕过自动化检测
|
|
// Set("incognito"). // 无痕模式
|
|
Set("user-data-dir", absPath). // 数据持久化目录
|
|
Set("window-size", "1920,1480"). // 窗口尺寸
|
|
Set("disable-infobars", "true"). // 隐藏自动化提示栏
|
|
Set("no-sandbox", "true").
|
|
Set("excludeSwitches", "enable-automation").
|
|
Set("enable-gpu"). // 启用 GPU 加速
|
|
Set("ignore-certificate-errors"). // 忽略证书错误
|
|
Set("use-fake-ui-for-media-stream"). // 允许媒体流
|
|
Set("autoplay-policy", "no-user-gesture-required"). // 自动播放
|
|
Set("ignore-certificate-errors").
|
|
Set("disable-application-cache").
|
|
Set("disable-dev-shm-usage").
|
|
// 禁用webrtc
|
|
Set("disable-webrtc", "true"). // 核心禁用参数
|
|
Set("disable-features", "WebRtcHideLocalIpsWithMdns"). // 隐藏本地IP
|
|
Set("webrtc-ip-handling-policy", "disable_non_proxied_udp"). // 禁用非代理UDP
|
|
Set("force-webrtc-ip-handling-policy").
|
|
Headless(false) // 关闭无头模式
|
|
|
|
// 修改语言
|
|
l.Set("lang", "en-US")
|
|
l.Set("accept-lang", "en-US")
|
|
|
|
// 修改时区
|
|
l.Set("timezone", ipInfo.Timezone)
|
|
l.Set("disable-geolocation", "true") // 启用地理定位API
|
|
l.Set("disable-web-security", "true") // 允许跨域(某些网站需要)
|
|
l.Set("disable-notifications", "true")
|
|
|
|
// 设置环境变量 - 核心时区设置
|
|
l.Env(append(os.Environ(), "TZ="+ipInfo.Timezone)...)
|
|
l.
|
|
Set("timezone", ipInfo.Timezone).
|
|
Set("geolocation", ipInfo.Lat+","+ipInfo.Lon)
|
|
|
|
// if errProxy == nil {
|
|
// if strings.Contains(protocol, "http") {
|
|
// log.Println(protocol + "://" + ip + ":" + port)
|
|
// l = l.Proxy(ip + ":" + port)
|
|
// }
|
|
// }
|
|
|
|
uri := l.MustLaunch()
|
|
|
|
browser := rod.New().
|
|
ControlURL(uri).
|
|
MustConnect().NoDefaultDevice()
|
|
browser.MustIgnoreCertErrors(true)
|
|
// if errProxy == nil {
|
|
// if strings.Contains(protocol, "http") {
|
|
// log.Println(username, password)
|
|
// go browser.MustHandleAuth(username, password)()
|
|
// }
|
|
// }
|
|
page := stealth.MustPage(browser)
|
|
// page := browser.MustPage()
|
|
|
|
go browser.EachEvent(func(e *proto.TargetTargetCreated) {
|
|
if e.TargetInfo.Type != proto.TargetTargetInfoTypePage {
|
|
return
|
|
}
|
|
browser.MustPageFromTargetID(e.TargetInfo.TargetID).MustEvalOnNewDocument(stealth.JS)
|
|
})()
|
|
|
|
// 在页面加载前注入时区覆盖代码
|
|
var browserFingerprint browserfingerprint.BrowserFingerprint
|
|
page = browserFingerprint.SetBrowserFingerprint(page, ¶msTypes.BrowserFingerprintParams{
|
|
Canvas: true,
|
|
TimeZone: ipInfo.Timezone, // 太平洋时间 (UTC-8)
|
|
Language: "en-US",
|
|
GeoLocation: ipInfo.Lat + "," + ipInfo.Lon, // 洛杉矶坐标
|
|
})
|
|
page.MustSetViewport(1920, 1480, 1.0, false)
|
|
|
|
return browser, uri, page
|
|
}
|
|
|
|
func (c *Chrome) TemporaryCreate(params *Fingerprint) (*rod.Browser, string, *rod.Page) {
|
|
userDataDir := "user-data/" + utils.RandString(5)
|
|
absPath, _ := filepath.Abs(userDataDir)
|
|
// path, _ := launcher.LookPath() // 获取当前系统的浏览器目录
|
|
// 判断是否传递代理
|
|
// var proxyHandle utils.ProxyHandle
|
|
// protocol, username, password, ip, port, errProxy := proxyHandle.ParseProxy(params.Proxy)
|
|
// 加载扩展
|
|
expandPath, _ := filepath.Abs("expand/general-auto")
|
|
|
|
// 验证扩展目录
|
|
if _, err := os.Stat(expandPath); os.IsNotExist(err) {
|
|
log.Fatal("扩展目录不存在:", expandPath)
|
|
}
|
|
manifestPath := filepath.Join(expandPath, "manifest.json")
|
|
if _, err := os.Stat(manifestPath); os.IsNotExist(err) {
|
|
log.Fatal("manifest.json 不存在:", manifestPath)
|
|
}
|
|
|
|
ipInfo := external.GetIpInfo(strings.TrimSpace(string(params.Proxy)))
|
|
|
|
l := launcher.New().
|
|
// Bin(path).
|
|
Set("load-extension", expandPath).
|
|
// Proxy("210.51.27.121:50003"). // 直接传入完整认证URL
|
|
Delete("use-mock-keychain"). // delete flag "--use-mock-keychain"
|
|
Set("disable-blink-features", "AutomationControlled"). // 绕过自动化检测
|
|
// Set("incognito"). // 无痕模式
|
|
Set("user-data-dir", absPath). // 数据持久化目录
|
|
Set("window-size", "1920,1480"). // 窗口尺寸
|
|
Set("disable-infobars", "true"). // 隐藏自动化提示栏
|
|
Set("no-sandbox", "true").
|
|
Set("excludeSwitches", "enable-automation").
|
|
Set("enable-gpu"). // 启用 GPU 加速
|
|
Set("ignore-certificate-errors"). // 忽略证书错误
|
|
Set("use-fake-ui-for-media-stream"). // 允许媒体流
|
|
Set("autoplay-policy", "no-user-gesture-required"). // 自动播放
|
|
Set("ignore-certificate-errors").
|
|
Set("disable-application-cache").
|
|
Set("disable-dev-shm-usage").
|
|
// 禁用webrtc
|
|
Set("disable-webrtc", "true"). // 核心禁用参数
|
|
Set("disable-features", "WebRtcHideLocalIpsWithMdns"). // 隐藏本地IP
|
|
Set("webrtc-ip-handling-policy", "disable_non_proxied_udp"). // 禁用非代理UDP
|
|
Set("force-webrtc-ip-handling-policy").
|
|
Headless(false) // 关闭无头模式
|
|
|
|
// 修改语言
|
|
l.Set("lang", "en-US")
|
|
l.Set("accept-lang", "en-US")
|
|
|
|
// 修改时区
|
|
l.Set("timezone", ipInfo.Timezone)
|
|
l.Set("disable-geolocation", "true") // 启用地理定位API
|
|
l.Set("disable-web-security", "true") // 允许跨域(某些网站需要)
|
|
l.Set("disable-notifications", "true")
|
|
|
|
// 设置环境变量 - 核心时区设置
|
|
l.Env(append(os.Environ(), "TZ="+ipInfo.Timezone)...)
|
|
l.
|
|
Set("timezone", ipInfo.Timezone).
|
|
Set("geolocation", ipInfo.Lat+","+ipInfo.Lon)
|
|
|
|
ua := uarand.GetRandom()
|
|
screen := devices.Device{
|
|
Title: "Laptop with MDPI screen",
|
|
Capabilities: []string{"touch", "mobile"},
|
|
UserAgent: ua,
|
|
AcceptLanguage: "en-US,en;q=0.9",
|
|
Screen: devices.Screen{
|
|
DevicePixelRatio: 1,
|
|
Horizontal: devices.ScreenSize{
|
|
Width: 1920,
|
|
Height: 1480,
|
|
},
|
|
},
|
|
}
|
|
// if errProxy == nil {
|
|
// if strings.Contains(protocol, "http") {
|
|
// log.Println(protocol + "://" + ip + ":" + port)
|
|
// l = l.Proxy(ip + ":" + port)
|
|
// }
|
|
// }
|
|
|
|
uri := l.MustLaunch()
|
|
|
|
browser := rod.New().
|
|
ControlURL(uri).
|
|
MustConnect().DefaultDevice(screen)
|
|
browser.MustIgnoreCertErrors(true)
|
|
|
|
// if errProxy == nil {
|
|
// if strings.Contains(protocol, "http") {
|
|
// log.Println(username, password)
|
|
// go browser.MustHandleAuth(username, password)()
|
|
// }
|
|
// }
|
|
page := stealth.MustPage(browser)
|
|
// page := browser.MustPage()
|
|
|
|
go browser.EachEvent(func(e *proto.TargetTargetCreated) {
|
|
if e.TargetInfo.Type != proto.TargetTargetInfoTypePage {
|
|
return
|
|
}
|
|
browser.MustPageFromTargetID(e.TargetInfo.TargetID).MustEvalOnNewDocument(stealth.JS)
|
|
})()
|
|
|
|
// 在页面加载前注入时区覆盖代码
|
|
var browserFingerprint browserfingerprint.BrowserFingerprint
|
|
page = browserFingerprint.SetBrowserFingerprint(page, ¶msTypes.BrowserFingerprintParams{
|
|
Canvas: true,
|
|
TimeZone: ipInfo.Timezone, // 太平洋时间 (UTC-8)
|
|
Language: "en-US",
|
|
GeoLocation: ipInfo.Lat + "," + ipInfo.Lon, // 洛杉矶坐标
|
|
})
|
|
page.MustSetViewport(1920, 1480, 1.0, false)
|
|
|
|
return browser, uri, page
|
|
}
|