fix(captcha): адаптация под новую версию виджета
This commit is contained in:
1 parent
760c511fe8
commit
7c28498f23
5 files changed
+73
-6
No files matched your search
@@ -231,3 +231,7 @@ func ApplyFhttp(req *fhttp.Request, profile Profile) {
|
||||
req.Header.Set("Priority", "u=1, i")
|
||||
req.Header[fhttp.HeaderOrderKey] = chromeHeaderOrder
|
||||
}
|
||||
|
||||
func ApplyProxyFhttp(req *fhttp.Request) {
|
||||
req.Header[fhttp.HeaderOrderKey] = chromeHeaderOrder
|
||||
}
|
||||
@@ -35,10 +35,10 @@ const (
|
||||
captchaAPIOrigin = "https://id.vk.ru"
|
||||
captchaDomain = "vk.ru"
|
||||
|
||||
// captchaDebugInfoFallback - константа виджета из not_robot_captcha.js 1.1.1384.
|
||||
// captchaDebugInfoFallback - константа виджета из not_robot_captcha.js 1.1.1387.
|
||||
// Ротируется вместе с версией бандла; используется, только если бандл не скачался
|
||||
// (UUID тут был бы аномалией - VK всегда шлёт 64-hex).
|
||||
captchaDebugInfoFallback = "153e72fb620892a5aa1580b0f30f1fe007b20f8e01bf5c03023f87e73e958749"
|
||||
captchaDebugInfoFallback = "b50d237c4ab98510721070cdbafe5dd88691e8883aa2458968040d70f870d91f"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -103,6 +103,7 @@ type captchaSession struct {
|
||||
// sensorsStart - момент ответа settings: с него виджет начинает тикать
|
||||
// таймером телеметрии.
|
||||
sensorsStart time.Time
|
||||
onCompromised func()
|
||||
}
|
||||
|
||||
func (s *captchaSession) logger() logx.Logger {
|
||||
@@ -120,6 +121,7 @@ func Solve(
|
||||
client tlsclient.HttpClient,
|
||||
profile browserprofile.Profile,
|
||||
log logx.Logger,
|
||||
onCompromised func(),
|
||||
) (string, error) {
|
||||
if captchaErr == nil || captchaErr.SessionToken == "" {
|
||||
return "", fmt.Errorf("no session_token in redirect_uri")
|
||||
@@ -136,6 +138,7 @@ func Solve(
|
||||
log: l,
|
||||
browserFP: profile.VisitorID,
|
||||
sensors: defaultSensorConfig(),
|
||||
onCompromised: onCompromised,
|
||||
}
|
||||
|
||||
for attempt := 1; attempt <= captchaMaxAttempts; attempt++ {
|
||||
@@ -250,6 +253,9 @@ func (s *captchaSession) escalate(sessionToken string, initContent captchaConten
|
||||
if content.Value == "" {
|
||||
return "", cause
|
||||
}
|
||||
if s.onCompromised != nil {
|
||||
s.onCompromised()
|
||||
}
|
||||
s.logger().Debugf("[Captcha] escalated to slider in-session (content source=%s)", content.Source)
|
||||
// Перерисовка виджета плюс пауза на осознание.
|
||||
if err := s.dwell(500, 1100); err != nil {
|
||||
@@ -503,9 +509,9 @@ func (s *captchaSession) sendComponentDone(sessionToken string) error {
|
||||
{"session_token", sessionToken},
|
||||
{"domain", s.domain},
|
||||
{"adFp", ""},
|
||||
{"access_token", ""},
|
||||
{"browser_fp", s.browserFP},
|
||||
{"device", s.profile.DeviceJSON},
|
||||
{"access_token", ""},
|
||||
}); err != nil {
|
||||
return fmt.Errorf("captcha componentDone failed: %w", err)
|
||||
}
|
||||
|
||||
@@ -444,7 +444,7 @@ func solveViaProxy(ctx context.Context, redirectURI string, dialer net.Dialer, p
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("captcha proxy client: %w", err)
|
||||
}
|
||||
transport := &loggingTransport{rt: personanet.RoundTripper(client, profile)}
|
||||
transport := &loggingTransport{rt: personanet.ProxyRoundTripper(client)}
|
||||
|
||||
proxy := &httputil.ReverseProxy{
|
||||
Transport: transport,
|
||||
|
||||
@@ -153,3 +153,58 @@ func (rt *personaRoundTripper) RoundTrip(req *http.Request) (*http.Response, err
|
||||
Request: req,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func ProxyRoundTripper(client tlsclient.HttpClient) http.RoundTripper {
|
||||
return &proxyRoundTripper{client: client}
|
||||
}
|
||||
|
||||
type proxyRoundTripper struct {
|
||||
client tlsclient.HttpClient
|
||||
}
|
||||
|
||||
func (rt *proxyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
var body io.Reader
|
||||
if req.Body != nil {
|
||||
body = req.Body
|
||||
}
|
||||
out, err := fhttp.NewRequestWithContext(req.Context(), req.Method, req.URL.String(), body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out.Host = req.Host
|
||||
for name, values := range req.Header {
|
||||
if http.CanonicalHeaderKey(name) == "Accept-Encoding" {
|
||||
continue
|
||||
}
|
||||
for _, v := range values {
|
||||
out.Header.Add(name, v)
|
||||
}
|
||||
}
|
||||
|
||||
// Важное отличие: задаём только порядок, не трогаем User-Agent
|
||||
browserprofile.ApplyProxyFhttp(out)
|
||||
|
||||
resp, err := rt.client.Do(out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
header := make(http.Header, len(resp.Header))
|
||||
for name, values := range resp.Header {
|
||||
header[http.CanonicalHeaderKey(name)] = append([]string(nil), values...)
|
||||
}
|
||||
if resp.Uncompressed {
|
||||
header.Del("Content-Encoding")
|
||||
header.Del("Content-Length")
|
||||
}
|
||||
return &http.Response{
|
||||
Status: resp.Status,
|
||||
StatusCode: resp.StatusCode,
|
||||
Proto: resp.Proto,
|
||||
ProtoMajor: resp.ProtoMajor,
|
||||
ProtoMinor: resp.ProtoMinor,
|
||||
Header: header,
|
||||
Body: resp.Body,
|
||||
ContentLength: resp.ContentLength,
|
||||
Request: req,
|
||||
}, nil
|
||||
}
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
tlsclient "github.com/bogdanfinn/tls-client"
|
||||
)
|
||||
|
||||
func (*Client) defaultAutoSolve(
|
||||
func (c *Client) defaultAutoSolve(
|
||||
ctx context.Context,
|
||||
captchaErr *captcha.Error,
|
||||
streamID int,
|
||||
@@ -27,7 +27,9 @@ func (*Client) defaultAutoSolve(
|
||||
return "", fmt.Errorf("no redirect_uri for auto-solve")
|
||||
}
|
||||
|
||||
successToken, err := captcha.Solve(ctx, captchaErr, streamID, client, profile, log)
|
||||
successToken, err := captcha.Solve(ctx, captchaErr, streamID, client, profile, log, func() {
|
||||
c.burnPersona(streamID)
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user