-
Notifications
You must be signed in to change notification settings - Fork 33
/
anticaptcha.js
345 lines (282 loc) · 11.6 KB
/
anticaptcha.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
var Anticaptcha = function(clientKey) {
return new function(clientKey) {
this.params = {
host: 'api.anti-captcha.com',
port: 80,
clientKey: clientKey,
// reCAPTCHA 2
websiteUrl: null,
websiteKey: null,
websiteSToken: null,
proxyType: 'http',
proxyAddress: null,
proxyPort: null,
proxyLogin: null,
proxyPassword: null,
userAgent: '',
// image
phrase: null,
case: null,
numeric: null,
math: null,
minLength: null,
maxLength: null,
softId: null,
languagePool: null
};
var connectionTimeout = 20,
firstAttemptWaitingInterval = 5,
normalWaitingInterval = 2;
this.getBalance = function (cb) {
var postData = {
clientKey: this.params.clientKey
};
this.jsonPostRequest('getBalance', postData, function (err, jsonResult) {
if (err) {
return cb(err, null, jsonResult);
}
cb(null, jsonResult.balance, jsonResult);
});
};
this.createTask = function (cb, type, taskData) {
type = typeof type == 'undefined' ? 'NoCaptchaTask' : type;
var taskPostData = this.getPostData(type);
taskPostData.type = type;
// Merge incoming and already fetched taskData, incoming data has priority
if (typeof taskData == 'object') {
for (var i in taskData) {
taskPostData[i] = taskData[i];
}
}
var postData = {
clientKey: this.params.clientKey,
task: taskPostData,
softId: this.params.softId !== null ? this.params.softId : 0
};
if (this.params.languagePool !== null) {
postData.languagePool = this.params.languagePool;
}
this.jsonPostRequest('createTask', postData, function (err, jsonResult) {
if (err) {
return cb(err, null, jsonResult);
}
// Task created
var taskId = jsonResult.taskId;
cb(null, taskId, jsonResult);
});
};
this.createTaskProxyless = function (cb) {
this.createTask(cb, 'NoCaptchaTaskProxyless');
};
this.createImageToTextTask = function (taskData, cb) {
this.createTask(cb, 'ImageToTextTask', taskData);
};
this.getTaskSolution = function (taskId, cb, currentAttempt, tickCb) {
currentAttempt = currentAttempt || 0;
var postData = {
clientKey: this.params.clientKey,
taskId: taskId
};
var waitingInterval;
if (currentAttempt == 0) {
waitingInterval = firstAttemptWaitingInterval;
} else {
waitingInterval = normalWaitingInterval;
}
console.log('Waiting %s seconds', waitingInterval);
var that = this;
setTimeout(function() {
that.jsonPostRequest('getTaskResult', postData, function (err, jsonResult) {
if (err) {
return cb(err, null, jsonResult);
}
if (jsonResult.status == 'processing') {
// Every call I'm ticki-ing
if (tickCb) {
tickCb();
}
return that.getTaskSolution(taskId, cb, currentAttempt + 1, tickCb);
} else if (jsonResult.status == 'ready') {
return cb(null, typeof jsonResult.solution.gRecaptchaResponse != 'undefined' ? jsonResult.solution.gRecaptchaResponse : jsonResult.solution.text, jsonResult);
}
});
}, waitingInterval * 1000);
};
this.getPostData = function(type) {
switch (type) {
case 'ImageToTextTask':
return {
phrase: this.params.phrase,
case: this.params.case,
numeric: this.params.numeric,
math: this.params.math,
minLength: this.params.minLength,
maxLength: this.params.maxLength
};
break;
case 'NoCaptchaTaskProxyless':
return {
websiteURL: this.params.websiteUrl,
websiteKey: this.params.websiteKey,
websiteSToken: this.params.websiteSToken
};
break;
default: // NoCaptchaTask
return {
websiteURL: this.params.websiteUrl,
websiteKey: this.params.websiteKey,
websiteSToken: this.params.websiteSToken,
proxyType: this.params.proxyType,
proxyAddress: this.params.proxyAddress,
proxyPort: this.params.proxyPort,
proxyLogin: this.params.proxyLogin,
proxyPassword: this.params.proxyPassword,
userAgent: this.params.userAgent
};
}
};
this.jsonPostRequest = function(methodName, postData, cb) {
if (typeof process === 'object' && typeof require === 'function') { // NodeJS
var http = require('http');
// http request options
var options = {
hostname: this.params.host,
port: this.params.port,
path: '/' + methodName,
method: 'POST',
headers: {
'accept-encoding': 'gzip,deflate',
'content-type': 'application/json; charset=utf-8',
'accept': 'application/json',
'content-length': Buffer.byteLength(JSON.stringify(postData))
}
};
// console.log(options);
// console.log(JSON.stringify(postData));
var req = http.request(options, function(response) { // on response
var str = '';
// another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
// the whole response has been recieved, so we just print it out here
response.on('end', function () {
// console.log(str);
try {
var jsonResult = JSON.parse(str);
} catch (err) {
return cb(err);
}
if (jsonResult.errorId) {
return cb(new Error(jsonResult.errorDescription, jsonResult.errorCode), jsonResult);
}
return cb(null, jsonResult);
});
});
// send post data
req.write(JSON.stringify(postData));
req.end();
// timeout in milliseconds
req.setTimeout(connectionTimeout * 1000);
req.on('timeout', function() {
console.log('timeout');
req.abort();
});
// After timeout connection throws Error, so we have to handle it
req.on('error', function(err) {
console.log('error');
return cb(err);
});
return req;
} else if ((typeof window !== 'undefined' || typeof chrome === 'object') && typeof $ == 'function') { // in browser or chrome extension with jQuery
$.ajax(
(window.location.protocol == 'https:' ? 'https:' : 'http:') + '//'
+ this.params.host
+ (window.location.protocol != 'https:' ? ':' + this.params.port : '')
+ '/' + methodName,
{
method: 'POST',
data: JSON.stringify(postData),
dataType: 'json',
success: function (jsonResult) {
if (jsonResult && jsonResult.errorId) {
return cb(new Error(jsonResult.errorDescription, jsonResult.errorCode), jsonResult);
}
cb(false, jsonResult);
},
error: function (jqXHR, textStatus, errorThrown) {
cb(new Error(textStatus != 'error' ? textStatus : 'Unknown error, watch console')); // should be errorThrown
}
}
);
} else {
console.error('Application should be run either in NodeJs environment or has jQuery to be included');
}
};
this.setClientKey = function (value) {
this.params.clientKey = value;
};
//proxy access parameters
this.setWebsiteURL = function (value) {
this.params.websiteUrl = value;
};
this.setWebsiteKey = function (value) {
this.params.websiteKey = value;
};
this.setWebsiteSToken = function (value) {
this.params.websiteSToken = value;
};
this.setProxyType = function (value) {
this.params.proxyType = value;
};
this.setProxyAddress = function (value) {
this.params.proxyAddress = value;
};
this.setProxyPort = function (value) {
this.params.proxyPort = value;
};
this.setProxyLogin = function (value) {
this.params.proxyLogin = value;
};
this.setProxyPassword = function (value) {
this.params.proxyPassword = value;
};
this.setUserAgent = function (value) {
this.params.userAgent = value;
};
// image
this.setPhrase = function (value) {
this.params.phrase = value;
};
this.setCase = function (value) {
this.params.case = value;
};
this.setNumeric = function (value) {
this.params.numeric = value;
};
this.setMath = function (value) {
this.params.math = value;
};
this.setMinLength = function (value) {
this.params.minLength = value;
};
this.setMaxLength = function (value) {
this.params.maxLength = value;
};
this.setSoftId = function (value) {
this.params.softId = value;
};
this.setLanguagePool = function (value) {
this.params.languagePool = value;
};
this.setHost = function (value) {
this.params.host = value;
};
this.setPort = function (value) {
this.params.port = value;
};
}(clientKey);
};
if (typeof process === 'object' && typeof require === 'function') { // NodeJS
module.exports = Anticaptcha;
}