refactor: вынести солвер captcha в internal/captcha
Вытаскивает auto-captcha пайплайн из client/main и даёт ему узкий публичный интерфейс, чтобы main.go больше не держал ~1.2k строк обработки VK-челленджей. Новые пакеты под client/internal/: - browserprofile: типы Profile / Saved плюс Load / Save / Random и инжекторы заголовков ApplyHTTP / ApplyFhttp (перенесены из profiles.go + main.go). manual_captcha.go тоже переключён на него. - captcha: package-level entrypoint Solve(ctx, *Error, ...), плюс Error / ParseError / IsCaptcha (переименованы из VkCaptchaError / ParseVkCaptchaError / IsCaptchaError). Сессия, scoring слайдера, синтез курсора и PoW теперь неэкспортируемая деталь реализации. solveVkCaptcha в main.go сжимается до обёртки над captcha.Solve с загрузкой SavedProfile.
This commit is contained in:
1 parent
130d5e9298
commit
ac3a6031d0
6 files changed
+172
-169
No files matched your search
@@ -0,0 +1,126 @@
|
||||
package browserprofile
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
fhttp "github.com/bogdanfinn/fhttp"
|
||||
)
|
||||
|
||||
type Profile struct {
|
||||
UserAgent string
|
||||
SecChUa string
|
||||
SecChUaMobile string
|
||||
SecChUaPlatform string
|
||||
}
|
||||
|
||||
type Saved struct {
|
||||
Profile
|
||||
DeviceJSON string
|
||||
BrowserFp string
|
||||
}
|
||||
|
||||
const profileFile = "vk_profile.json"
|
||||
|
||||
func Load() (*Saved, error) {
|
||||
data, err := os.ReadFile(profileFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var sp Saved
|
||||
if err := json.Unmarshal(data, &sp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &sp, nil
|
||||
}
|
||||
|
||||
func Save(sp Saved) error {
|
||||
data, err := json.MarshalIndent(sp, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(profileFile, data, 0644)
|
||||
}
|
||||
|
||||
func ApplyHTTP(req *http.Request, profile Profile) {
|
||||
req.Header.Set("User-Agent", profile.UserAgent)
|
||||
req.Header.Set("sec-ch-ua", profile.SecChUa)
|
||||
req.Header.Set("sec-ch-ua-mobile", profile.SecChUaMobile)
|
||||
req.Header.Set("sec-ch-ua-platform", profile.SecChUaPlatform)
|
||||
req.Header.Set("Accept-Language", "en-US,en;q=0.9")
|
||||
req.Header.Set("DNT", "1")
|
||||
}
|
||||
|
||||
func ApplyFhttp(req *fhttp.Request, profile Profile) {
|
||||
req.Header.Set("User-Agent", profile.UserAgent)
|
||||
req.Header.Set("sec-ch-ua", profile.SecChUa)
|
||||
req.Header.Set("sec-ch-ua-mobile", profile.SecChUaMobile)
|
||||
req.Header.Set("sec-ch-ua-platform", profile.SecChUaPlatform)
|
||||
req.Header.Set("Accept-Language", "en-US,en;q=0.9")
|
||||
req.Header.Set("DNT", "1")
|
||||
}
|
||||
|
||||
// profiles contain paired User-Agent and Client Hints strings to harden bot detection.
|
||||
// Used only as a fallback if no saved profile exists.
|
||||
var profileList = []Profile{
|
||||
{
|
||||
UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36",
|
||||
SecChUa: `"Chromium";v="146", "Not-A.Brand";v="24", "Google Chrome";v="146"`,
|
||||
SecChUaMobile: "?0",
|
||||
SecChUaPlatform: `"Windows"`,
|
||||
},
|
||||
{
|
||||
UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36",
|
||||
SecChUa: `"Chromium";v="145", "Not-A.Brand";v="99", "Google Chrome";v="145"`,
|
||||
SecChUaMobile: "?0",
|
||||
SecChUaPlatform: `"Windows"`,
|
||||
},
|
||||
{
|
||||
UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36",
|
||||
SecChUa: `"Chromium";v="144", "Not-A.Brand";v="8", "Google Chrome";v="144"`,
|
||||
SecChUaMobile: "?0",
|
||||
SecChUaPlatform: `"Windows"`,
|
||||
},
|
||||
{
|
||||
UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0",
|
||||
SecChUa: `"Chromium";v="146", "Not-A.Brand";v="24", "Microsoft Edge";v="146"`,
|
||||
SecChUaMobile: "?0",
|
||||
SecChUaPlatform: `"Windows"`,
|
||||
},
|
||||
{
|
||||
UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36 Edg/145.0.0.0",
|
||||
SecChUa: `"Chromium";v="145", "Not-A.Brand";v="99", "Microsoft Edge";v="145"`,
|
||||
SecChUaMobile: "?0",
|
||||
SecChUaPlatform: `"Windows"`,
|
||||
},
|
||||
{
|
||||
UserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36",
|
||||
SecChUa: `"Chromium";v="146", "Not-A.Brand";v="24", "Google Chrome";v="146"`,
|
||||
SecChUaMobile: "?0",
|
||||
SecChUaPlatform: `"macOS"`,
|
||||
},
|
||||
{
|
||||
UserAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36",
|
||||
SecChUa: `"Chromium";v="145", "Not-A.Brand";v="99", "Google Chrome";v="145"`,
|
||||
SecChUaMobile: "?0",
|
||||
SecChUaPlatform: `"macOS"`,
|
||||
},
|
||||
{
|
||||
UserAgent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36",
|
||||
SecChUa: `"Chromium";v="146", "Not-A.Brand";v="24", "Google Chrome";v="146"`,
|
||||
SecChUaMobile: "?0",
|
||||
SecChUaPlatform: `"Linux"`,
|
||||
},
|
||||
{
|
||||
UserAgent: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36",
|
||||
SecChUa: `"Chromium";v="144", "Not-A.Brand";v="8", "Google Chrome";v="144"`,
|
||||
SecChUaMobile: "?0",
|
||||
SecChUaPlatform: `"Linux"`,
|
||||
},
|
||||
}
|
||||
|
||||
func Random() Profile {
|
||||
return profileList[rand.Intn(len(profileList))]
|
||||
}
|
||||
Reference in new issue
Block a user