-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
261 lines (232 loc) · 7 KB
/
index.js
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
const request = require("request");
const encryptLogin = require("./modules/Google").encryptLogin;
const path = require("path");
const fs = require("fs-extra");
const config = require("./config");
const async = require("async");
const download = require("download");
class Extractor {
constructor({ email, password, phone, devid }) {
this.gmail = email;
this.passw = password;
this.devid = devid;
this.celnumbr = phone;
this.pkg = config["app"]["pkg"];
this.sig = config["app"]["sig"];
this.client_pkg = config["client"]["pkg"];
this.client_sig = config["client"]["sig"];
this.client_ver = config["client"]["ver"];
this.bearer = null;
this.workerQueue = [];
this.downloadBaseDirectory = ".";
}
setDownloadBaseDirectory(path = ".") {
this.downloadBaseDirectory = path.replace("/", path.sep);
}
async getGoogleAccountTokenFromAuth() {
const payload = {
Email: this.gmail,
EncryptedPasswd: encryptLogin(this.gmail, this.passw),
app: this.client_pkg,
client_sig: this.client_sig,
parentAndroidId: this.devid,
};
try {
const opts = {
url: "https://android.clients.google.com/auth",
form: payload,
headers: {
"User-Agent":
"WhatsApp/2.19.291 Android/5.1.1 Device/samsung-SM-N950W",
},
};
const results = await this.fetch(opts, true);
const match = /Token=(.*?)\n/g.exec(results);
if (match.length < 1)
throw new Error(
"[!] getGoogleAccountTokenFromAuth: no auth token found"
);
return match[1];
} catch (error) {
if (typeof error === "object")
throw new Error(
`[!] getGoogleAccountTokenFromAuth status: ${error.body}`
);
throw new Error(`[!] getGoogleAccountTokenFromAuth: ${error}`);
}
}
async getGoogleDriveToken(token) {
const payload = {
Token: token,
app: this.pkg,
client_sig: this.sig,
device: this.devid,
google_play_services_version: this.client_ver,
service:
"oauth2:https://www.googleapis.com/auth/drive.appdata https://www.googleapis.com/auth/drive.file",
has_permission: "1",
};
const opts = {
url: "https://android.clients.google.com/auth",
form: payload,
headers: {
"User-Agent": "WhatsApp/2.19.291 Android/5.1.1 Device/samsung-SM-N950W",
},
};
try {
let response = await this.fetch(opts, true);
if (response.substring(response.length - 1, response.length) !== "\n")
response = `${response}\n`;
const match = /Auth=(.*?)\n/g.exec(response);
if (match === null || match.length < 1)
throw new Error(
`[!] getGoogleDriveToken: unable to parse bearer token ${response}`
);
this.bearer = match[1];
return this.bearer;
} catch (error) {
throw new Error(`[!] getGoogleDriveToken error ${error}`);
}
}
async rawGoogleDriveRequest(url) {
try {
const opts = {
url,
headers: {
Authorization: `Bearer ${this.bearer}`,
"User-Agent":
"WhatsApp/2.19.291 Android/5.1.1 Device/samsung-SM-N950W",
"Content-Type": "application/json; charset=UTF-8",
Connection: "Keep-Alive",
},
};
const results = await this.fetch(opts);
return results;
} catch (error) {
throw new Error(`[!] rawGoogleDriveRequest: ${error}`);
}
}
async gDriveFileMap(nextPageToken) {
try {
const data = await this.gDriveFileMapRequest(this.bearer, nextPageToken);
const jres = JSON.parse(data);
let incomplete_backup_marker = false;
const description_url = `https://backup.googleapis.com/v1/clients/wa/backups/${this.celnumbr}`;
const description = await this.rawGoogleDriveRequest(description_url);
if (typeof jres["files"] === "undefined")
throw new Error("Unable to locate Google Drive Whatsapp backup.");
if (description["title"].includes("invisible")) {
for (const p in result["properties"]) {
if (
p["key"] === "incomplete_backup_marker" &&
p["value"].toString() === "true"
)
incomplete_backup_marker = true;
}
}
if (jres["files"].length === 0) {
if (incomplete_backup_marker)
throw new Error(
"[!] incomplete backup. it may be corrupted. try and backup again."
);
else throw new Error("[!] no backup files found.");
}
if (typeof jres["nextPageToken"] !== "undefined")
this.gDriveFileMap(jres["nextPageToken"]);
this.workerQueue = jres["files"].map((file) => {
return file["name"];
});
} catch (error) {
throw new Error(`[!] gDriveFileMap error ${error}`);
}
}
async gDriveFileMapRequest(bearer, nextPageToken) {
const url = `https://backup.googleapis.com/v1/clients/wa/backups/${this.celnumbr}/files?pageToken=${nextPageToken}&pageSize=5000`;
try {
const opts = {
url,
headers: {
Authorization: `Bearer ${bearer}`,
"User-Agent":
"WhatsApp/2.19.291 Android/5.1.1 Device/samsung-SM-N950W",
"Content-Type": "application/json; charset=UTF-8",
Connection: "Keep-Alive",
},
};
const results = await this.fetch(opts);
return results;
} catch (error) {
throw new Error(`[!] gDriveFileMapRequest: ${error}`);
}
}
getFileList() {
return this.workerQueue;
}
async downloadAll(output = ".") {
console.log("[@] trying to download all the files now..");
if (this.workerQueue.length < 1) throw new Error("[!] Nothing to download");
console.log(`[@] ${this.workerQueue.length} files to download..`);
let totalItems = this.workerQueue.length;
let currentIndex = 1;
async.eachSeries(
this.workerQueue,
async (item, callback) => {
let downloadFilePath = path.join(
this.downloadBaseDirectory,
"Whatsapp",
item.replace("/", path.sep)
);
fs.ensureDirSync(path.dirname(downloadFilePath));
if (fs.pathExistsSync(downloadFilePath)) {
const stat = fs.lstatSync(downloadFilePath);
if (stat.isFile()) fs.unlinkSync(downloadFilePath);
}
try {
await download(
`https://backup.googleapis.com/v1/${item}?alt=media`,
path.dirname(downloadFilePath),
{
headers: {
Authorization: `Bearer ${this.bearer}`,
"User-Agent":
"WhatsApp/2.19.291 Android/5.1.1 Device/samsung-SM-N950W",
"Content-Type": "application/json; charset=UTF-8",
Connection: "Keep-Alive",
},
filename: path.basename(downloadFilePath),
}
);
console.log(
`[-] downloaded ${downloadFilePath} (${currentIndex}/${totalItems})`
);
currentIndex++;
} catch (error) {
console.error(error);
return callback();
}
},
(error) => {
throw new Error(error);
}
);
}
async fetch(opts, isPost = false) {
return new Promise((resolve, reject) => {
if (isPost) {
return request.post(opts, function (error, response, body) {
if (error) return reject(error);
if (response.statusCode !== 200)
return reject({ status: response.statusCode, body });
return resolve(body);
});
}
request(opts, function (error, response, body) {
if (error) return reject(error);
if (response.statusCode !== 200)
return reject({ status: response.statusCode, body });
return resolve(body);
});
});
}
}
module.exports = Extractor;