-
Notifications
You must be signed in to change notification settings - Fork 9
/
api2captcha.hpp
669 lines (555 loc) · 20.5 KB
/
api2captcha.hpp
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
#ifndef API2CAPTCHA_HPP
#define API2CAPTCHA_HPP
#include <cassert>
#include <string>
#include <map>
#include <chrono>
#include <stdexcept>
#include <thread>
#include <vector>
namespace api2captcha
{
typedef std::map<std::string, std::string> params_t;
typedef std::map<std::string, std::string> files_t;
class exception_t : public std::runtime_error
{
public:
exception_t(const std::string &s) : std::runtime_error(s) {}
};
class api_exception_t : public exception_t
{
public:
api_exception_t(const std::string &s) : exception_t(s) {}
};
class timeout_exception_t : public exception_t
{
public:
timeout_exception_t(const std::string &s) : exception_t(s) {}
};
class network_exception_t : public exception_t
{
public:
network_exception_t(const std::string &s) : exception_t(s) {}
};
struct http_client_i
{
virtual ~http_client_i() {};
virtual int status() const = 0;
virtual std::string get(
const std::string &url, const int timeout, const params_t ¶ms) = 0;
virtual std::string post(
const std::string &url, const int timeout,
const params_t ¶ms, const files_t &files) = 0;
};
class captcha_t
{
std::string id_;
std::string code_;
mutable params_t params_;
files_t files_;
protected:
void set_param(const std::string &name, const std::string &value)
{
params_[name] = value;
}
void set_file(const std::string &name, const std::string &value)
{
files_[name] = value;
}
captcha_t() {};
public:
// make polymorphic
virtual ~captcha_t() {}
const std::string &id() const { return id_; }
const std::string &code() const { return code_; }
void set_id(const std::string &s) { id_ = s; }
void set_code(const std::string &s) { code_ = s; }
void set_proxy(const std::string &type, const std::string &url)
{
params_["proxytype"] = type;
params_["proxy"] = url;
}
void set_soft_id(const int soft_id) { params_["soft_id"] = std::to_string(soft_id); }
void set_callback(const std::string &url) { params_["pingback"] = url; }
const params_t ¶ms() const
{
if (params_["method"].empty())
{
if (!params_["body"].empty())
params_["method"] = "base64";
else
params_["method"] = "post";
}
return params_;
}
const files_t &files() const { return files_; }
};
class canvas_t : public captcha_t
{
public:
canvas_t()
{
set_param("canvas", "1");
set_param("recaptcha", "1");
}
void set_file(const std::string &path) { captcha_t::set_file("file", path); }
void set_base64(const std::string &base64) { set_param("body", base64); }
void set_previous_id(const int id) { set_param("previousID", std::to_string(id)); }
void set_can_skip(const bool can) { set_param("can_no_answer", can ? "1" : "0"); }
void set_lang(const std::string &lang) { set_param("lang", lang); }
void set_hint_text(const std::string &text) { set_param("textinstructions", text); }
void set_hint_img(const std::string &base64) { set_param("imginstructions", base64); }
void set_hint_img_file(const std::string &path) { captcha_t::set_file("imginstructions", path); }
};
class capy_t : public captcha_t
{
public:
capy_t()
{
set_param("method", "capy");
}
void set_site_key(const std::string &s) { set_param("captchakey", s); };
void set_url(const std::string &s) { set_param("pageurl", s); };
void set_api_server(const std::string &s) { set_param("api_server", s); };
};
class coordinates_t : public captcha_t
{
public:
coordinates_t()
{
set_param("coordinatescaptcha", "1");
}
coordinates_t(const std::string &path)
: coordinates_t()
{
set_file(path);
}
void set_file(const std::string &path) { captcha_t::set_file("file", path); };
void set_base64(const std::string &base64) { set_param("body", base64); }
void set_lang(const std::string &lang) { set_param("lang", lang); }
void set_hint_text(const std::string &text) { set_param("textinstructions", text); }
void set_hint_img(const std::string &base64) { set_param("imginstructions", base64); }
void set_hint_img_file(const std::string &path) { captcha_t::set_file("imginstructions", path); }
};
class funcaptcha_t : public captcha_t
{
public:
funcaptcha_t()
{
set_param("method", "funcaptcha");
}
void set_site_key(const std::string &s) { set_param("publickey", s); };
void set_url(const std::string &s) { set_param("pageurl", s); };
void set_surl(const std::string &s) { set_param("surl", s); };
void set_user_agent(const std::string &s) { set_param("userAgent", s); };
void set_data(const std::string &key, const std::string &value)
{
set_param("data[" + key + "]", value);
}
};
class geetest_t : public captcha_t
{
public:
geetest_t()
{
set_param("method", "geetest");
}
void set_gt(const std::string &s) { set_param("gt", s); };
void set_challenge(const std::string &s) { set_param("challenge", s); };
void set_url(const std::string &s) { set_param("pageurl", s); };
void set_api_server(const std::string &s) { set_param("api_server", s); };
};
class grid_t : public captcha_t
{
public:
grid_t() {}
grid_t(const std::string &path)
{
set_file(path);
}
void set_file(const std::string &path) { captcha_t::set_file("file", path); };
void set_base64(const std::string &base64) { set_param("body", base64); }
void set_rows(const int rows) { set_param("recaptcharows", std::to_string(rows)); }
void set_cols(const int cols) { set_param("recaptchacols", std::to_string(cols)); }
void set_previous_id(const int id) { set_param("previousID", std::to_string(id)); }
void set_can_skip(const bool can) { set_param("can_no_answer", can ? "1" : "0"); }
void set_lang(const std::string &lang) { set_param("lang", lang); }
void set_hint_text(const std::string &text) { set_param("textinstructions", text); }
void set_hint_img(const std::string &base64) { set_param("imginstructions", base64); }
void set_hint_img_file(const std::string &path) { captcha_t::set_file("imginstructions", path); }
};
class hcaptcha_t : public captcha_t
{
public:
hcaptcha_t()
{
set_param("method", "hcaptcha");
}
void set_site_key(const std::string &s) { set_param("sitekey", s); };
void set_url(const std::string &s) { set_param("pageurl", s); };
};
class keycaptcha_t : public captcha_t
{
public:
keycaptcha_t()
{
set_param("method", "keycaptcha");
}
void set_user_id(const int user_id) { set_user_id(std::to_string(user_id)); };
void set_user_id(const std::string &user_id) { set_param("s_s_c_user_id", user_id); };
void set_session_id(const std::string &session_id)
{
set_param("s_s_c_session_id", session_id);
}
void set_web_server_sign(const std::string &sign)
{
set_param("s_s_c_web_server_sign", sign);
}
void set_web_server_sign2(const std::string &sign)
{
set_param("s_s_c_web_server_sign2", sign);
}
void set_url(const std::string &s) { set_param("pageurl", s); };
};
class normal_t : public captcha_t
{
public:
normal_t() {}
normal_t(const std::string &path)
{
set_file(path);
}
void set_file(const std::string &path) { captcha_t::set_file("file", path); };
void set_base64(const std::string &base64) { set_param("body", base64); }
void set_phrase(const bool ph) { set_param("phrase", ph ? "1" : "0"); }
void set_case_sensitive(const bool cs) { set_param("regsense", cs ? "1" : "0"); }
void set_calc(const bool calc) { set_param("calc", calc ? "1" : "0"); }
void set_numeric(const int n) { set_param("numeric", std::to_string(n)); }
void set_min_len(const int l) { set_param("min_len", std::to_string(l)); }
void set_max_len(const int l) { set_param("max_len", std::to_string(l)); }
void set_lang(const std::string &lang) { set_param("lang", lang); }
void set_hint_text(const std::string &text) { set_param("textinstructions", text); }
void set_hint_img(const std::string &base64) { set_param("imginstructions", base64); }
void set_hint_img_file(const std::string &path) { captcha_t::set_file("imginstructions", path); }
};
class recaptcha_t : public captcha_t
{
public:
recaptcha_t()
{
set_param("method", "userrecaptcha");
}
void set_site_key(const std::string &s) { set_param("googlekey", s); };
void set_url(const std::string &s) { set_param("pageurl", s); };
void set_invisible(const bool inv) { set_param("invisible", inv ? "1" : "0"); }
void set_version(const std::string &s) { set_param("version", s); };
void set_action(const std::string &s) { set_param("action", s); };
void set_score(const double s) { set_param("min_score", std::to_string(s)); };
};
class rotate_t : public captcha_t
{
public:
rotate_t()
{
set_param("method", "rotatecaptcha");
}
rotate_t(const std::string &path)
: rotate_t()
{
set_file(path);
}
rotate_t(const std::vector<std::string> &paths)
: rotate_t()
{
set_files(paths);
}
void set_file(const std::string &path) { captcha_t::set_file("file_1", path); };
void set_files(const std::vector<std::string> &paths)
{
for (size_t i = 0; i < paths.size(); ++i)
captcha_t::set_file("file_" + std::to_string(i + 1), paths[i]);
}
void set_angle(const int a) { set_param("angle", std::to_string(a)); }
void set_lang(const std::string &lang) { set_param("lang", lang); }
void set_hint_text(const std::string &text) { set_param("textinstructions", text); }
void set_hint_img(const std::string &base64) { set_param("imginstructions", base64); }
void set_hint_img_file(const std::string &path) { captcha_t::set_file("imginstructions", path); }
};
class text_t : public captcha_t
{
public:
text_t()
{
set_param("method", "post");
}
text_t(const std::string &text)
: text_t()
{
set_text(text);
}
void set_text(const std::string &s) { set_param("textcaptcha", s); };
void set_lang(const std::string &s) { set_param("lang", s); };
};
class lemin : public captcha_t
{
public:
lemin()
{
set_param("method", "lemin");
}
void setApiServer(const std::string &s) { set_param("api_server", s); };
void setCaptchaid(const std::string &s) { set_param("captcha_id", s); };
void setUrl(const std::string &s) { set_param("pageurl", s); };
};
class client_t
{
class impl_t
{
static const constexpr char *HOST = "2captcha.com";
std::string api_key_;
int soft_id_ = 4586;
std::string callback_;
int default_timeout_ = 120; // sec
int recaptcha_timeout_ = 600; // sec
int polling_interval_ = 10; // sec
bool last_captcha_has_callback_ = false;
http_client_i *http_ = nullptr;
void check_http_response(const std::string &r)
{
const auto status = http_->status();
if (status == 0)
throw network_exception_t("Failed to send captcha");
if (status != 200)
throw api_exception_t("Bad captcha api status: " + std::to_string(status));
if (r.substr(0, 6) == "ERROR_")
throw api_exception_t("Captcha api error: " + r);
}
std::string do_http_in(
const params_t ¶ms,
const files_t &files,
const int timeout)
{
assert(is_valid());
const auto url = std::string("https://") + HOST + "/in.php";
const auto response = http_->post(url, timeout, params, files);
check_http_response(response);
return response;
}
std::string do_http_res(
const params_t ¶ms, const int timeout)
{
assert(is_valid());
const auto url = std::string("https://") + HOST + "/res.php";
const auto response = http_->get(url, timeout, params);
check_http_response(response);
return response;
}
std::string res(params_t ¶ms, int timeout = 0)
{
params["key"] = api_key_;
if (timeout <= 0)
timeout = default_timeout_;
return do_http_res(params, timeout);
}
std::string res(const std::string &action)
{
params_t params;
params["action"] = action;
return res(params);
}
void attach_default_params(params_t ¶ms)
{
params["key"] = api_key_;
if (!callback_.empty())
{
if (params.count("pingback") == 0)
{
params["pingback"] = callback_;
}
else if (params["pingback"].empty())
{
params.erase("pingback");
}
}
last_captcha_has_callback_ = params.count("pingback");
if (soft_id_ && !params.count("soft_id"))
params["soft_id"] = std::to_string(soft_id_);
}
static std::string extract_ok_response(const std::string &response)
{
if (response.substr(0, 3) != "OK|")
throw api_exception_t("Cannot recognize api response (" + response + ")");
return response.substr(3);
}
public:
impl_t() {}
bool is_valid() const { return http_; }
void set_http_client(http_client_i *http)
{
assert(http);
http_ = http;
}
void set_api_key(const std::string &s) { api_key_ = s; }
void set_soft_id(const int id) { soft_id_ = id; }
void set_callback(const std::string &url) { callback_ = url; }
void set_default_timeout(const int sec) { default_timeout_ = sec; }
void set_recaptcha_timeout(const int sec) { recaptcha_timeout_ = sec; }
void set_polling_interval(const int sec) { polling_interval_ = sec; }
void solve(captcha_t &captcha)
{
std::map<std::string, int> wait_options;
if (dynamic_cast<recaptcha_t *>(&captcha))
wait_options["timeout"] = recaptcha_timeout_;
solve(captcha, wait_options);
}
/* Can set 'timeout' and 'pollingInterval' options
*/
void solve(captcha_t &captcha, const std::map<std::string, int> &wait_options)
{
int timeout = default_timeout_;
auto i = wait_options.find("timeout");
if (i != std::end(wait_options))
timeout = i->second;
const auto id = send(captcha, timeout);
captcha.set_id(id);
if (!last_captcha_has_callback_)
wait_for_result(captcha, wait_options);
}
void wait_for_result(
captcha_t &captcha, const std::map<std::string, int> &wait_options)
{
const auto started_at = std::chrono::steady_clock::now();
int timeout = default_timeout_;
auto i = wait_options.find("timeout");
if (i != std::end(wait_options))
timeout = i->second;
int polling_interval = polling_interval_;
i = wait_options.find("pollingInterval");
if (i != std::end(wait_options))
polling_interval = i->second;
typedef std::chrono::duration<double> duration_t;
double interval = 0;
do
{
std::this_thread::sleep_for(std::chrono::seconds(polling_interval));
try
{
const auto result = get_result(captcha.id(), timeout);
if (!result.empty())
{
captcha.set_code(result);
return;
}
}
catch (network_exception_t &e)
{
// ignore network errors
}
const auto now = std::chrono::steady_clock::now();
const auto dur = std::chrono::duration_cast<duration_t>(now - started_at);
interval = dur.count();
} while (interval < (double)timeout);
throw timeout_exception_t(
"Timeout " + std::to_string(timeout) + " seconds reached");
}
std::string send(captcha_t &captcha, int timeout = 0)
{
const auto &files = captcha.files();
// copy params
auto params = captcha.params();
attach_default_params(params);
if (timeout <= 0)
timeout = default_timeout_;
const auto response = do_http_in(params, files, timeout);
return extract_ok_response(response);
}
/**
* Returns result of captcha if it was solved or empty string if result is not ready
*/
std::string get_result(const std::string &id, int timeout = 0)
{
params_t params;
params["action"] = "get";
params["id"] = id;
const auto response = res(params, timeout);
if (response == "CAPCHA_NOT_READY")
return "";
return extract_ok_response(response);
}
double get_balance()
{
const auto response = res("getbalance");
try
{
return std::stold(response, nullptr);
}
catch (...)
{
throw api_exception_t("Bad balance response (" + response + ")");
}
}
void report(const std::string &id, const bool correct)
{
params_t params;
params["id"] = id;
if (correct)
{
params["action"] = "reportgood";
}
else
{
params["action"] = "reportbad";
}
res(params);
}
};
impl_t *i_ = nullptr;
client_t(const client_t &other);
client_t &operator=(const client_t &other);
public:
client_t() : i_(new impl_t()) {}
client_t(client_t &&other) : i_(other.i_) { other.i_ = nullptr; }
client_t &operator=(client_t &&other)
{
client_t tmp(std::move(other));
std::swap(i_, tmp.i_);
return *this;
}
~client_t() { delete i_; }
bool is_valid() const { return i_->is_valid(); }
void set_http_client(http_client_i *http) { return i_->set_http_client(http); }
void set_api_key(const std::string &s) { return i_->set_api_key(s); }
void set_soft_id(const int id) { return i_->set_soft_id(id); }
void set_callback(const std::string &url) { return i_->set_callback(url); }
void set_default_timeout(const int sec) { return i_->set_default_timeout(sec); }
void set_recaptcha_timeout(const int sec) { return i_->set_recaptcha_timeout(sec); }
void set_polling_interval(const int sec) { return i_->set_polling_interval(sec); }
void solve(captcha_t &captcha) { return i_->solve(captcha); }
void solve(captcha_t &captcha, const std::map<std::string, int> &wait_options)
{
return i_->solve(captcha, wait_options);
}
void wait_for_result(
captcha_t &captcha, const std::map<std::string, int> &wait_options)
{
return i_->wait_for_result(captcha, wait_options);
}
std::string send(captcha_t &captcha, int timeout = 0)
{
return i_->send(captcha, timeout);
}
std::string get_result(const std::string &id, int timeout = 0)
{
return i_->get_result(id, timeout);
}
double get_balance()
{
return i_->get_balance();
}
void report(const std::string &id, const bool correct)
{
return i_->report(id, correct);
}
};
};
#endif /* API2CAPTCHA_HPP */