-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathcaptcha.go
143 lines (141 loc) · 4.35 KB
/
captcha.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
package sydney
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/stealth"
"github.com/google/uuid"
"log/slog"
"os"
"path/filepath"
"strings"
"sydneyqt/sydney/internal/hex"
"sydneyqt/util"
"time"
)
func (o *Sydney) ResolveCaptcha(stopCtx context.Context) (err error) {
defer func() {
if err0 := recover(); err0 != nil {
slog.Warn("Error resolving captcha", "err", err0)
err = err0.(error)
}
}()
iframeID := uuid.New().String()
l := launcher.NewUserMode().Context(stopCtx).
Leakless(true).
UserDataDir(filepath.Join(os.TempDir(), "rod-user-data-"+uuid.New().String())).
Set("disable-default-apps").
Set("no-first-run").Headless(false)
defer l.Cleanup()
u := l.MustLaunch()
browser := rod.New().Context(stopCtx).NoDefaultDevice().ControlURL(u).MustConnect()
defer browser.MustClose()
var cookies []*proto.NetworkCookie
for k, v := range o.cookies {
cookies = append(cookies, &proto.NetworkCookie{
Name: k,
Value: v,
Domain: ".bing.com",
Path: "/",
Expires: proto.TimeSinceEpoch(time.Now().Add(1 * time.Hour).Unix()),
})
}
browser.MustSetCookies(cookies...)
page := stealth.MustPage(browser)
page.MustNavigate("https://www.bing.com/turing/captcha/challenge?" +
"q=&iframeid=local-gen-" + iframeID)
page.MustElement("body")
page.MustEval("()=>{let info=document.createElement('h3');" +
"info.textContent='↑ Please help click if this cannot be processed automatically!';" +
"document.body.appendChild(info);}")
router := page.HijackRequests()
waitCh := make(chan struct{}, 16)
defer close(waitCh)
var resCookies map[string]string
router.MustAdd("https://www.bing.com/challenge/verify*", func(hijack *rod.Hijack) {
hijack.MustLoadResponse()
for key, values := range hijack.Response.Headers() {
if strings.ToLower(key) != "set-cookie" {
continue
}
var arr []string
for _, v := range values {
arr = append(arr, strings.Split(v, ";")[0])
}
resCookies = util.ParseCookiesFromString(strings.Join(arr, "; "))
}
waitCh <- struct{}{}
})
go router.Run()
defer router.Stop()
select {
case <-time.Tick(60 * time.Second):
return errors.New("timeout verifying challenge token")
case <-stopCtx.Done():
return stopCtx.Err()
case <-waitCh:
}
slog.Info("Captcha resCookies", "v", resCookies)
if err := o.postprocessCaptchaCookies(resCookies); err != nil {
return err
}
return nil
}
func (o *Sydney) BypassCaptcha(stopCtx context.Context, conversationID string, messageID string) error {
if o.bypassServer == "" {
return errors.New("no bypass server specified")
}
_, client, err := util.MakeHTTPClient(o.proxy, 60*time.Second)
if err != nil {
return err
}
req := BypassCaptchaRequest{
IG: hex.NewUpperHex(32),
Cookies: util.FormatCookieString(o.cookies),
IFrameID: "local-gen-" + uuid.New().String(),
ConvID: conversationID,
RID: messageID,
}
slog.Debug("Bypass CAPTCHA request", "v", req)
resp, err := client.R().SetContext(stopCtx).SetBody(req).Post(o.bypassServer)
if err != nil {
return fmt.Errorf("cannot communicate with captcha bypass server: %w", err)
}
slog.Debug("Bypass captcha response body", "v", resp.String())
var response BypassCaptchaResponse
err = json.Unmarshal(resp.Bytes(), &response)
if err != nil {
return fmt.Errorf("cannot unmarshal json from captcha bypass server: %w", err)
}
if response.Error != "" {
return errors.New("bypass captcha error: " + response.Error)
}
cookies := util.ParseCookiesFromString(response.Result.Cookies)
if err := o.postprocessCaptchaCookies(cookies); err != nil {
return fmt.Errorf("%w; screenshot: "+
strings.TrimSuffix(o.bypassServer, "/")+
response.Result.ScreenShot, err)
}
return nil
}
func (o *Sydney) UpdateModifiedCookies(modifiedCookies map[string]string) {
for k, v := range modifiedCookies { // keep the map pointer
o.cookies[k] = v
}
err := util.UpdateCookiesFile(o.cookies)
if err != nil {
slog.Warn("Cannot update cookies file: ", "err", err)
}
}
func (o *Sydney) postprocessCaptchaCookies(modifiedCookies map[string]string) error {
if _, ok := modifiedCookies["cct"]; !ok {
return errors.New("captcha cookies not valid: no cookie named cct found")
}
slog.Info("postprocessCaptchaCookies", "cookies", modifiedCookies)
o.UpdateModifiedCookies(modifiedCookies)
return nil
}