-
Notifications
You must be signed in to change notification settings - Fork 58
/
headless.go
273 lines (239 loc) · 6.38 KB
/
headless.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package goinsta
import (
"context"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/emulation"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
)
type headlessOptions struct {
// seconds
timeout int64
showBrowser bool
tasks chromedp.Tasks
}
// Wait until page gets redirected to instagram home page
func waitForInstagram(b *bool) chromedp.ActionFunc {
return chromedp.ActionFunc(
func(ctx context.Context) error {
for {
select {
case <-time.After(time.Millisecond * 250):
var l string
err := chromedp.Location(&l).Do(ctx)
if err != nil {
return err
}
if l == "https://www.instagram.com/" {
*b = true
return nil
}
case <-ctx.Done():
return nil
}
}
})
}
// Wait until page gets redirected to instagram home page
func printButtons(insta *Instagram) chromedp.Action {
return chromedp.ActionFunc(
func(ctx context.Context) error {
var nodes []*cdp.Node
err := chromedp.Nodes("button", &nodes, chromedp.ByQueryAll).Do(ctx)
if err != nil {
return err
}
for _, p := range nodes {
if len(p.Children) > 0 {
insta.infoHandler(
fmt.Sprintf("Found button on challenge page: %s\n",
p.Children[0].NodeValue,
))
}
}
return nil
})
}
func takeScreenshot(fn string) chromedp.Action {
return chromedp.ActionFunc(
func(ctx context.Context) error {
var buf []byte
err := chromedp.FullScreenshot(&buf, 90).Do(ctx)
if err != nil {
return err
}
if err := os.WriteFile(fn, buf, 0o644); err != nil {
return err
}
return nil
})
}
func (insta *Instagram) acceptPrivacyCookies(url string) error {
// Looks for the "Allow All Cookies button"
selector := `//button[contains(text(),"Allow All Cookies")]`
// This value is not actually used, since its headless, the browser cannot
// be closed easily. If the process is unsuccessful, it will return a timeout error.
success := false
return insta.runHeadless(
&headlessOptions{
timeout: 60,
showBrowser: false,
tasks: chromedp.Tasks{
chromedp.Navigate(url),
// wait a second after elemnt is visible, does not work otherwise
chromedp.WaitVisible(selector),
chromedp.Sleep(time.Second * 1),
chromedp.Click(selector, chromedp.BySearch),
waitForInstagram(&success),
},
},
)
}
func (insta *Instagram) openChallenge(url string) error {
fname := fmt.Sprintf("challenge-screenshot-%d.png", time.Now().Unix())
success := false
err := insta.runHeadless(
&headlessOptions{
timeout: 300,
showBrowser: true,
tasks: chromedp.Tasks{
chromedp.Navigate(url),
// Wait for a few seconds, and screenshot the page after
chromedp.Sleep(time.Second * 5),
printButtons(insta),
takeScreenshot(fname),
// Wait until page gets redirected to instagram home page
waitForInstagram(&success),
},
},
)
if err != nil {
return err
}
insta.infoHandler(
fmt.Sprintf(
"Saved a screenshot of the challenge '%s' to %s, please report it in a github issue so the challenge can be solved automatiaclly.\n",
url,
fname,
))
if !success {
return ErrChallengeFailed
}
return nil
}
// runHeadless takes a list of chromedp actions to perform, wrapped around default
// actions that will need to be run for every headless request, such as setting
// the cookies and user-agent.
func (insta *Instagram) runHeadless(options *headlessOptions) error {
if insta.privacyCalled.Get() {
return errors.New("Accept privacy cookie method has already been called. Did it not work? please report on a github issue")
}
if options.timeout <= 0 {
options.timeout = 60
}
// Extract required headers as cookies
cookies := map[string]string{}
cookie_list := []string{
"x-mid",
"authorization",
"ig-u-shbid",
"ig-u-shbts",
"ig-u-ds-user-id",
"ig-u-rur",
}
insta.headerOptions.Range(
func(key, value interface{}) bool {
header := strings.ToLower(key.(string))
for _, cookie_name := range cookie_list {
if cookie_name == header {
cookies[cookie_name] = value.(string)
}
}
return true
},
)
userAgent := fmt.Sprintf(
"Mozilla/5.0 (Linux; Android %d; %s/%s; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/95.0.4638.50 Mobile Safari/537.36 %s",
insta.device.AndroidRelease,
insta.device.Model,
insta.device.Chipset,
insta.userAgent,
)
opts := append(
chromedp.DefaultExecAllocatorOptions[:],
chromedp.UserAgent(userAgent),
)
if insta.proxy != "" {
opts = append(opts, chromedp.ProxyServer(insta.proxy))
}
if insta.proxyInsecure {
opts = append(opts, chromedp.Flag("ignore-certificate-errors", true))
}
if options.showBrowser {
opts = append(opts, chromedp.Flag("headless", false))
}
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
// create chrome instance
ctx, cancel = chromedp.NewContext(
ctx,
// chromedp.WithDebugf(log.Printf),
)
defer cancel()
// create a timeout
ctx, cancel = context.WithTimeout(ctx, time.Duration(options.timeout)*time.Second)
defer cancel()
// Size for custom device
// res := strings.Split(strings.ToLower(insta.device.ScreenResolution), "x")
// width, err := strconv.Atoi(res[0])
// if err != nil {
// return err
// }
// height, err := strconv.Atoi(res[1])
// if err != nil {
// return err
// }
default_actions := chromedp.Tasks{
// Set custom device type
chromedp.Tasks{
emulation.SetUserAgentOverride(userAgent),
// emulation.SetDeviceMetricsOverride(int64(width), int64(height), 1.000000, true).
// WithScreenOrientation(&emulation.ScreenOrientation{
// Type: emulation.OrientationTypePortraitPrimary,
// Angle: 0,
// }),
emulation.SetTouchEmulationEnabled(true),
},
// Set custom cookie
chromedp.ActionFunc(func(ctx context.Context) error {
expr := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour))
for key, val := range cookies {
err := network.SetCookie(key, val).
WithExpires(&expr).
WithDomain("i.instagram.com").
// WithHTTPOnly(true).
Do(ctx)
if err != nil {
return err
}
}
return nil
}),
// Set custom headers
network.Enable(),
network.SetExtraHTTPHeaders(
network.Headers(
map[string]interface{}{
"X-Requested-With": "com.instagram.android",
},
),
),
}
err := chromedp.Run(ctx, append(default_actions, options.tasks))
return err
}