-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
295 lines (274 loc) · 11.2 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const fs = require('fs');
const path = require('path');
const imageToBase64 = require('image-to-base64');
const stream = require('stream');
const BPromise = require('bluebird');
const get = require('lodash/get');
class CapchaService {
constructor(extra_options = {}) {
this.Stream = new stream.Stream();
let {
captcha_service,
captcha_key,
imageCaptchaPath,
renameImage,
captchaType,
websiteURL,
websiteKey,
captchaOptions,
} = extra_options;
this.captchaOptions = captchaOptions || {};
this.captchaType = captchaType ? captchaType : 'image';
switch (this.captchaType) {
case 'image':
if (!imageCaptchaPath) throw new Error('Please input imageCaptchaPath');
if (!captcha_key) throw new Error('captcha_key missing');
this.imageCaptchaPath = path.resolve(imageCaptchaPath);
this.captchaFileName = path.basename(this.imageCaptchaPath);
this.renameImage = (typeof(renameImage) === "boolean")?renameImage:false;
break;
case 'recaptchav2':
if (!websiteURL) throw new Error('Please input websiteURL');
if (!websiteKey) throw new Error('Please input websiteKey');
this.websiteURL = websiteURL;
this.websiteKey = websiteKey;
break;
default:
throw new Error(`Captcha type: ${this.captchaType} is not supported`);
break;
}
this.captcha_service = captcha_service || 'twocaptcha';
this.captcha_key = (typeof (captcha_key) == "object") ? captcha_key || [''] : [captcha_key || ''];
switch (captcha_service) {
case 'azcaptcha':
this.solver = require(path.normalize(__dirname + '/./services/solveCaptchaWithAzCaptcha'));
this.solver.setApiKey(this.captcha_key[0]);
break;
case 'twocaptcha':
this.solver = require(path.normalize(__dirname + '/./services/twocaptcha'));
this.solver.setApiKey(this.captcha_key[0]);
break;
case 'anticaptcha':
this.solver = require(path.normalize(__dirname + '/./services/solveCaptchaWithAnti'));
this.solver.setApiKey(this.captcha_key[0]);
this.solver.Stream.on('log', data => this.Stream.emit('log', data));
break;
case 'deathbycaptcha':
this.solver = require(path.normalize(__dirname + '/./services/deathbycaptcha2'));
this.solver.setApiKey(this.captcha_key[0]);
break;
default:
this.solver = require(path.normalize(__dirname + '/./services/solveCaptchaWithAzCaptcha'));
this.solver.setApiKey(this.captcha_key[0]);
break;
}
}
changeCaptchaImageFileName(solvedCaptchaString, imageCaptchaPath) {
return new Promise((resolve, reject) => {
try {
solvedCaptchaString = solvedCaptchaString || this.solvedCaptchaString || '';
imageCaptchaPath = path.resolve(imageCaptchaPath || this.imageCaptchaPath);
const captchaFileName = path.basename(imageCaptchaPath);
const imageExtname = path.extname(captchaFileName);
this.Stream.emit('log', `Changing captcha file name: ${captchaFileName}`);
const captcha_folder = path.dirname(imageCaptchaPath);
let RecaptchaFileName = path.format({
name: solvedCaptchaString.trim() || 'captcha',
ext: imageExtname
});
let imageCaptchaPathRename = path.format({
dir: captcha_folder,
base: RecaptchaFileName
}) || './captcha.jpg';
fs.renameSync(imageCaptchaPath, imageCaptchaPathRename);
this.Stream.emit('log', `Captcha file name has been changed: from ${captchaFileName} --> ${RecaptchaFileName}`);
} catch (e) {
this.Stream.emit('log', `Change Captcha File Name ERROR: ${e.message}`);
} finally {
this.Stream.emit('log', `Captcha resolved`);
resolve();
}
});
}
solveCaptchaWithCaptchaService(captcha_index = 0) {
return new Promise(async (resolve, reject) => {
try {
this.Stream.emit('log', `Solve captcha with captcha_index: ${captcha_index}`);
this.solver.balanceZero(async (err, isZero) => {
if (err) return reject(err);
try {
if (isZero == true) {
if (captcha_index == (this.captcha_key.length - 1)) {
reject(new Error(`ERROR_USER_BALANCE_ZERO`));
} else {
this.solver.setApiKey(this.captcha_key[captcha_index + 1]);
this.solveCaptchaWithCaptchaService(captcha_index + 1).then(data => resolve(data)).catch(err => reject(err));
}
} else {
switch (this.captcha_service) {
case 'deathbycaptcha':
this.solver.decodeFile(this.imageCaptchaPath, 10000, (err, result) => {
if (err) {
reject(new Error(`Deathbycaptcha Error: ${(err || '').toString()}`));
} else {
this.Stream.emit('log', `Deathbycaptcha: ${JSON.stringify(result)}`);
const textResult = get(result, 'text', '');
resolve(textResult);
}
});
break;
case 'twocaptcha':
try {
const captchaBASE64 = await imageToBase64(this.imageCaptchaPath);
await this.solver.decode(captchaBASE64, this.captchaOptions, (err, result) => {
this.Stream.emit('log', `TwoCaptcha: ${JSON.stringify(result)}`);
const textResult = get(result, 'text', '');
resolve(textResult);
});
} catch (err) {
reject(err);
} finally {
break;
}
case 'anticaptcha':
try {
const captchaBASE64 = await imageToBase64(this.imageCaptchaPath);
this.solver.decode(captchaBASE64, (err, result) => {
if (err) {
reject(new Error(`anticaptcha Error: ${(err || '').toString()}`));
} else {
this.Stream.emit('log', `anticaptcha: ${JSON.stringify(result)}`);
const textResult = get(result, 'text', '');
resolve(textResult);
}
});
} catch (err) {
reject(err);
} finally {
break;
}
default:
try {
const captchaBASE64 = await imageToBase64(this.imageCaptchaPath);
this.solver.decode(captchaBASE64, {
pollingInterval: 10000
}, (err, result, invalid) => {
if (err) {
reject(new Error(`AZCaptcha Error: ${(err || '').toString()}`));
} else {
this.Stream.emit('log', `AZCaptcha: ${JSON.stringify(result)}`);
const textResult = get(result, 'text', '');
resolve(textResult);
}
});
} catch (err) {
reject(err);
} finally {
break;
}
}
}
} catch (e) {
reject(e);
}
});
} catch (e) {
reject(e);
}
});
}
solveRecapchaV2(captcha_index = 0) {
return new Promise(async (resolve, reject) => {
try {
this.Stream.emit('log', `Solve captcha with captcha_index: ${captcha_index}`);
this.solver.balanceZero(async (err, isZero) => {
if (err) return reject(err);
try {
if (isZero == true) {
if (captcha_index == (this.captcha_key.length - 1)) {
reject(new Error(`ERROR_USER_BALANCE_ZERO`));
} else {
this.solver.setApiKey(this.captcha_key[captcha_index + 1]);
this.solveRecapchaV2(captcha_index + 1).then(data => resolve(data)).catch(err => reject(err));
}
} else {
switch (this.captcha_service) {
case 'anticaptcha':
try {
this.solver.solveRecaptchaV2({
websiteKey: this.websiteKey,
websiteURL: this.websiteURL,
}, (err, result) => {
if (err) {
reject(new Error(`anticaptcha Error: ${(err || '').toString()}`));
} else {
this.Stream.emit('log', `anticaptcha: ${JSON.stringify(result)}`);
resolve(get(result, 'text', ''));
}
});
} catch (err) {
reject(err);
} finally {
break;
}
default:
reject(new Error(`ERROR_CAPTCHA_SERVICE_NOT_SUPPORTED`));
break;
}
}
} catch (e) {
reject(e);
}
});
} catch (e) {
reject(e);
}
});
}
solveCaptcha(trytime = 1) {
return new Promise(async (resolve, reject) => {
try {
this.Stream.emit('log', `Solving ${this.captchaType} captcha trytime=${trytime}`);
let response = null;
switch (this.captchaType) {
case 'image':
this.solvedCaptchaString = await this.solveCaptchaWithCaptchaService();
this.Stream.emit('log', `Captcha result: ${this.solvedCaptchaString} | ${this.captchaFileName}`);
if (this.renameImage) await this.changeCaptchaImageFileName();
response = this.solvedCaptchaString;
break;
case 'recaptchav2': {
const solvedCaptchaString = await this.solveRecapchaV2();
this.Stream.emit('log', `Captcha result: ${solvedCaptchaString}`);
response = solvedCaptchaString;
break;
}
default:
break;
}
resolve(response);
} catch (err) {
const errMsg = get(err, 'message', String(err));
this.Stream.emit('log', `Captcha error: ${errMsg}`);
if (errMsg.includes('ERROR_USER_BALANCE_ZERO')) {
reject(err);
} else if (errMsg.includes('ERROR_WRONG_USER_KEY')) {
reject(err);
} else {
if (trytime == 0) {
reject(new Error('Can not solve captcha'));
} else {
try {
await BPromise.delay(1000);
const retryData = await this.solveCaptcha(trytime - 1);
resolve(retryData);
} catch (e) {
reject(e);
}
}
}
}
});
}
}
module.exports = CapchaService;