-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
257 lines (227 loc) · 6.81 KB
/
app.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
package main
import (
"flag"
"net/http"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/charmbracelet/log"
"github.com/gin-gonic/gin"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
)
type QueryRequest struct {
URL string `json:"url"`
UserAgent string `json:"userAgent"`
Cookies map[string]string `json:"cookies"`
}
type QueryResponse struct {
Message string `json:"message"`
ElapsedTime int `json:"elapsed_time"`
Solution SolutionResponse `json:"solution"`
}
type SolutionResponse struct {
Url string `json:"url"`
Status int `json:"status"`
Response string `json:"response"`
Cookies []*proto.NetworkCookie `json:"cookies"`
}
var (
listenAddress string
apiKey string
twoCaptchaApiKey string
proxyDsn string
proxyParsed *url.URL
browser *rod.Browser
)
func main() {
flag.StringVar(&listenAddress, "listen-address", "0.0.0.0:2316", "HTTP server listen address")
flag.StringVar(&apiKey, "api-key", "", "API key for accessing this service")
flag.StringVar(&proxyDsn, "proxy-dsn", "", "Proxy dsn, example: http://user:pass@host:port")
flag.StringVar(&twoCaptchaApiKey, "twocaptcha-api-key", "", "2captcha api key")
flag.Parse()
if apiKey == "" {
log.Fatal("API key is required")
}
if twoCaptchaApiKey == "" {
log.Fatal("2captcha api key is required")
}
if proxyDsn == "" {
log.Fatal("proxy dsn is required")
} else {
proxyParse, err := url.Parse(proxyDsn)
if err != nil {
log.Fatalf("invalid proxy dsn: %s", err)
}
proxyParsed = proxyParse
}
launchUrl := launcher.New().
UserDataDir("/tmp/rod").
Headless(false).
NoSandbox(true).
Set("window-size", "1920,1080").
Set("disable-setuid-sandbox").
Set("disable-dev-shm-usage").
Set("no-first-run").
Set("disable-blink-features", "AutomationControlled").
Set("excludeSwitches", "enable-automation").
MustLaunch()
browser = rod.New().ControlURL(launchUrl).MustConnect()
browser.MustVersion()
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.Use(authMiddleware())
router.GET("/health", healthCheck)
router.POST("/bypass", handleBypass)
srv := &http.Server{
Addr: listenAddress,
Handler: router,
}
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-quit
if err := srv.Close(); err != nil {
log.Fatalf("failed to shutdown server: %s", err)
}
}()
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("failed to start server: %s", err)
}
log.Info("server is shutting down")
}
func authMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
auth := c.GetHeader("Authorization")
if auth != "Bearer "+apiKey {
c.JSON(http.StatusUnauthorized, gin.H{"message": "Unauthorized"})
c.Abort()
return
}
c.Next()
}
}
func healthCheck(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
}
func handleBypass(c *gin.Context) {
var req QueryRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": "Invalid request"})
return
}
bypassUrl, err := url.Parse(req.URL)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"message": errors.Wrap(err, "invalid url").Error()})
return
}
startTime := time.Now()
logger := log.NewWithOptions(os.Stdout, log.Options{
ReportCaller: true,
ReportTimestamp: true,
Prefix: "🕷️ " + req.URL,
})
page := browser.MustPage("").Context(c)
defer page.MustClose()
page.MustSetUserAgent(&proto.NetworkSetUserAgentOverride{
UserAgent: req.UserAgent,
})
var cookies []*proto.NetworkCookieParam
for cookieName, cookieValue := range req.Cookies {
cookies = append(cookies, &proto.NetworkCookieParam{
Name: cookieName,
Value: cookieValue,
Domain: bypassUrl.Hostname(),
})
}
page.MustSetCookies(cookies...)
var statusCode int
var interceptedParams string
interceptedDone := make(chan struct{})
go page.EachEvent(func(e *proto.RuntimeConsoleAPICalled) bool {
for _, arg := range e.Args {
value := arg.Value.String()
if strings.Contains(value, "intercepted-params:") {
interceptedParams = strings.TrimPrefix(value, "intercepted-params:")
close(interceptedDone)
return true
}
}
return false
}, func(e *proto.NetworkResponseReceived) {
statusCode = e.Response.Status
})()
page.MustNavigate(req.URL)
page.MustEval(`() => {
const i = setInterval(() => {
if (window.turnstile) {
clearInterval(i)
window.turnstile.render = (a, b) => {
let params = {
sitekey: b.sitekey,
pageurl: window.location.href,
data: b.cData,
pagedata: b.chlPageData,
action: b.action,
json: 1
}
console.log('intercepted-params:' + JSON.stringify(params))
window.cfCallback = b.callback
}
}
}, 10)
}
`)
title := page.MustInfo().Title
if title == "Just a moment..." {
logger.Info("met cloudflare turnstile")
select {
case <-interceptedDone:
logger.Info("start solving captcha")
proxyPassword, _ := proxyParsed.User.Password()
solveResult, err := SolveCaptcha(&TurnstileTask{
Type: "TurnstileTask",
WebsiteURL: req.URL,
WebsiteKey: gjson.Get(interceptedParams, "sitekey").String(),
UserAgent: req.UserAgent,
Action: gjson.Get(interceptedParams, "action").String(),
Data: gjson.Get(interceptedParams, "data").String(),
PageData: gjson.Get(interceptedParams, "pagedata").String(),
ProxyType: proxyParsed.Scheme,
ProxyAddress: proxyParsed.Hostname(),
ProxyPort: proxyParsed.Port(),
ProxyLogin: proxyParsed.User.Username(),
ProxyPassword: proxyPassword,
})
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"message": errors.Wrap(err, "failed to solve captcha").Error()})
return
}
logger.Infof("captcha solved, cost: %s, usedTime(s): %d, solveCount: %d", solveResult.Cost, solveResult.EndTime-solveResult.CreateTime, solveResult.SolveCount)
redirectWait := page.MustWaitNavigation()
page.MustEval(`() => window.cfCallback("` + solveResult.Solution.Token + `")`)
redirectWait()
case <-time.After(30 * time.Second):
logger.Warn("captcha solving timeout")
c.JSON(http.StatusInternalServerError, gin.H{"message": "captcha solving timeout"})
return
}
}
resp := QueryResponse{
Message: "",
ElapsedTime: int(time.Since(startTime).Seconds()),
Solution: SolutionResponse{
Url: page.MustInfo().URL,
Status: statusCode,
Response: page.MustHTML(),
Cookies: page.MustCookies(req.URL),
},
}
c.PureJSON(http.StatusOK, resp)
}