-
Notifications
You must be signed in to change notification settings - Fork 58
/
challenge.go
232 lines (199 loc) · 6 KB
/
challenge.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
package goinsta
import (
"encoding/json"
"strings"
)
type ChallengeStepData struct {
Choice string `json:"choice"`
FbAccessToken string `json:"fb_access_token"`
BigBlueToken string `json:"big_blue_token"`
GoogleOauthToken string `json:"google_oauth_token"`
Email string `json:"email"`
SecurityCode string `json:"security_code"`
ResendDelay interface{} `json:"resend_delay"`
ContactPoint string `json:"contact_point"`
FormType string `json:"form_type"`
}
// Challenge is a status code 400 error, usually prompting the user to perform
// some action.
type Challenge struct {
insta *Instagram
LoggedInUser *Account `json:"logged_in_user,omitempty"`
UserID int64 `json:"user_id"`
Status string `json:"status"`
Errors []string `json:"errors"`
URL string `json:"url"`
ApiPath string `json:"api_path"`
Context *ChallengeContext `json:"challenge_context"`
FlowRenderType int `json:"flow_render_type"`
HideWebviewHeader bool `json:"hide_webview_header"`
Lock bool `json:"lock"`
Logout bool `json:"logout"`
NativeFlow bool `json:"native_flow"`
TwoFactorRequired bool
TwoFactorInfo TwoFactorInfo
}
// Checkpoint is just like challenge, a status code 400 error.
// Usually used to prompt the user to accept cookies.
type Checkpoint struct {
insta *Instagram
URL string `json:"checkpoint_url"`
Lock bool `json:"lock"`
FlowRenderType int `json:"flow_render_type"`
}
type ChallengeContext struct {
TypeEnum string `json:"challenge_type_enum"`
IsStateless bool `json:"is_stateless"`
Action string `json:"action"`
NonceCode string `json:"nonce_code"`
StepName string `json:"step_name"`
StepData ChallengeStepData `json:"step_data"`
UserID int64 `json:"user_id"`
}
type challengeResp struct {
*Challenge
}
func newChallenge(insta *Instagram) *Challenge {
return &Challenge{
insta: insta,
}
}
// updateState updates current data from challenge url
func (c *Challenge) updateState() error {
insta := c.insta
ctx, err := json.Marshal(c.Context)
if err != nil {
return err
}
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: c.insta.challengeURL,
Query: map[string]string{
"guid": insta.uuid,
"device_id": insta.dID,
"challenge_context": string(ctx),
},
},
)
if err == nil {
resp := challengeResp{}
err = json.Unmarshal(body, &resp)
if err == nil {
*c = *resp.Challenge
c.insta = insta
}
}
return err
}
// selectVerifyMethod selects a way and verify it (Phone number = 0, email = 1)
func (challenge *Challenge) selectVerifyMethod(choice string, isReplay ...bool) error {
insta := challenge.insta
url := challenge.insta.challengeURL
if len(isReplay) > 0 && isReplay[0] {
url = strings.Replace(url, "/challenge/", "/challenge/replay/", -1)
}
data, err := json.Marshal(
map[string]string{
"choice": choice,
"guid": insta.uuid,
"device_id": insta.dID,
"_uuid": insta.uuid,
"_uid": toString(insta.Account.ID),
})
if err != nil {
return err
}
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: url,
Query: generateSignature(data),
IsPost: true,
},
)
if err == nil {
resp := challengeResp{}
err = json.Unmarshal(body, &resp)
if err == nil {
*challenge = *resp.Challenge
challenge.insta = insta
}
}
return err
}
// sendSecurityCode sends the code received in the message
func (challenge *Challenge) SendSecurityCode(code string) error {
insta := challenge.insta
url := challenge.insta.challengeURL
data, err := json.Marshal(map[string]string{
"security_code": code,
"guid": insta.uuid,
"device_id": insta.dID,
"_uuid": insta.uuid,
"_uid": toString(insta.Account.ID),
})
if err != nil {
return err
}
body, _, err := insta.sendRequest(
&reqOptions{
Endpoint: url,
IsPost: true,
Query: generateSignature(data),
},
)
if err == nil {
resp := challengeResp{}
err = json.Unmarshal(body, &resp)
if err == nil {
*challenge = *resp.Challenge
challenge.insta = insta
challenge.LoggedInUser.insta = insta
insta.Account = challenge.LoggedInUser
}
}
return err
}
// deltaLoginReview process with choice (It was me = 0, It wasn't me = 1)
func (c *Challenge) deltaLoginReview() error {
return c.selectVerifyMethod("0")
}
func (c *Challenge) ProcessOld(apiURL string) error {
c.insta.challengeURL = apiURL[1:]
if err := c.updateState(); err != nil {
return err
}
switch c.Context.StepName {
case "select_verify_method":
return c.selectVerifyMethod(c.Context.StepData.Choice)
case "delta_login_review":
return c.deltaLoginReview()
}
return ErrChallengeProcess{StepName: c.Context.StepName}
}
// Process will open up the challenge url in a chromium browser and
// take a screenshot. Please report the screenshot and printed out struct so challenge
// automation can be build in.
func (c *Challenge) Process() error {
insta := c.insta
insta.warnHandler("Encountered a captcha challenge, goinsta will attempt to open the challenge in a headless chromium browser, and take a screenshot. Please report the details in a github issue.")
err := insta.openChallenge(c.URL)
err = checkHeadlessErr(err)
return err
}
// Process will open up the url passed as a checkpoint response (not a challenge)
// in a headless browser. This method is experimental, please report if you still
// get a /privacy/checks/ checkpoint error.
func (c *Checkpoint) Process() error {
insta := c.insta
if insta.privacyRequested.Get() {
panic("Privacy request again, it hus failed, panicing")
}
insta.privacyRequested.Set(true)
err := insta.acceptPrivacyCookies(c.URL)
err = checkHeadlessErr(err)
if err != nil {
return err
}
insta.privacyCalled.Set(true)
return nil
}