Skip to content

Instantly share code, notes, and snippets.

@Nerixyz

Nerixyz/main.ts Secret

Created December 14, 2019 22:38
Show Gist options
  • Save Nerixyz/7f65bc19e5e666865a2682fb509a5d83 to your computer and use it in GitHub Desktop.
Save Nerixyz/7f65bc19e5e666865a2682fb509a5d83 to your computer and use it in GitHub Desktop.
Instagram web challenge
async function challengeIndex(url: string, params?: any) {
return request({
url: `https://i.instagram.com${url}`,
method: 'POST',
form: {
...params,
},
resolveWithFullResponse: true,
simple: false,
// transform: requestTransform,
strictSSL: false,
gzip: true,
headers: {
Host: 'i.instagram.com',
Connection: 'close',
Origin: 'https://i.instagram.com',
'X-IG-WWW-Claim': '0',
'Accept': '*/*',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': `Mozilla/5.0 (Linux; Android 8.0.0; Custom Build/OPR6.170623.017; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/58.0.3029.125 Mobile Safari/537.36 Instagram ${ig.state.appVersion} Android (${ig.state.deviceString}; ${ig.state.language}; ${ig.state.appVersionCode})`,
'X-CSRFToken': ig.state.cookieCsrfToken,
'X-IG-App-ID': ig.state.fbOrcaApplicationId,
'Referer': `https://i.instagram.com${url}`,
// X-Mid, X-Instagram-AJAX
},
jar: ig.state.cookieJar,
});
}
async function login() {
ig.state.generateDevice(process.env.IG_USERNAME);
ig.state.proxyUrl = process.env.IG_PROXY;
ig.request.end$.subscribe(async () => {
const serialized = await ig.state.serialize();
delete serialized.constants;
await promises.writeFile(sessionPath, JSON.stringify(serialized), { encoding: 'utf8' });
});
if (existsSync(sessionPath)) {
await ig.state.deserialize(await promises.readFile(sessionPath, { encoding: 'utf8' }));
}
return Bluebird.try(() => ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD)).catch(IgCheckpointError, async (e) => {
// await ig.challenge.auto(true);
console.log('challenge');
const url = e.response.body.challenge.api_path;
// get info
console.log(await challengeIndex(url));
// default option
console.log(await challengeIndex(url, { choice: '1' }));
// get code
const { code } = await inquirer.prompt([{
type: 'input',
name: 'code',
message: 'Enter code',
}]);
console.log(await challengeIndex(url, { security_code: code }));
// get password
const { password } = await inquirer.prompt([{
type: 'input',
name: 'password',
message: 'Enter password',
}]);
console.log(await challengeIndex(url, { new_password1: password, new_password2: password }));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment