-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (64 loc) · 2.14 KB
/
main.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
package main
import (
"context"
"errors"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/chromedp"
"github.com/jarylc/go-chromedpproxy"
"log"
)
func main() {
// create result channel to signify end of program
resultChan := make(chan string)
// prepare proxy using port 9222 as Chrome debugging port and 9221 as frontend port
// also send in `chromedp.DisableGPU` as extra ExecAllocatorOption
chromedpproxy.PrepareProxy(":9222", ":9221", chromedp.DisableGPU)
targetID, err := chromedpproxy.NewTab("https://www.hcaptcha.com", chromedp.WithLogf(log.Printf))
if err != nil && !errors.Is(err, context.Canceled) {
log.Panic(err)
}
defer chromedpproxy.CloseTarget(targetID)
ctx := chromedpproxy.GetTarget(targetID)
// grab hcaptcha iframe
var iframes []*cdp.Node
err = chromedp.Run(ctx, chromedp.Tasks{
chromedp.Click(`#radio-5`, chromedp.NodeVisible),
chromedp.Nodes(`.h-captcha > iframe`, &iframes),
})
if err != nil && !errors.Is(err, context.Canceled) {
log.Panic(err)
}
// parallel steps to check if a captcha really exists before notifying
go func() {
err := chromedp.Run(ctx, chromedp.Tasks{
chromedp.Click(`#checkbox`, chromedp.NodeVisible, chromedp.FromNode(iframes[0])),
chromedp.WaitVisible(`.challenge-container`, chromedp.FromNode(iframes[0])),
chromedp.ActionFunc(func(ctx context.Context) error {
log.Print("You would normally send this via any form of notifications to a user:")
log.Printf("Recaptcha detected, please solve it here: http://127.0.0.1:9221/?id=%s", targetID)
return nil
}),
})
if err != nil && !errors.Is(err, context.Canceled) {
log.Panic(err)
}
}()
// parallel steps to find success condition
go func() {
err := chromedp.Run(ctx, chromedp.Tasks{
chromedp.WaitVisible(`.check`, chromedp.FromNode(iframes[0])),
chromedp.ActionFunc(func(ctx context.Context) error {
log.Print("Captcha solved!")
resultChan <- "OK"
return nil
}),
})
if err != nil && !errors.Is(err, context.Canceled) {
log.Panic(err)
}
resultChan <- "Failed"
}()
// you can do other stuff here like grab cookie data, etc.
// log and close
log.Print("Result: ", <-resultChan)
}