Repository navigation
Expand file tree
/
Copy pathlib_usage_https_example.c
More file actions
253 lines (226 loc) · 9.33 KB
/
Copy pathlib_usage_https_example.c
File metadata and controls
253 lines (226 loc) · 9.33 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif /* _WIN32 */
#include "deathbycaptcha.h"
enum dbc_sample_token_mode {
DBC_SAMPLE_TOKEN_NONE = 0,
DBC_SAMPLE_TOKEN_V2,
DBC_SAMPLE_TOKEN_V3,
DBC_SAMPLE_TOKEN_ENTERPRISE
};
#ifndef _WIN32
static void *load_symbol(void *lib, const char *name)
{
return dlsym(lib, name);
}
#endif
static void save_outputs(const char *prefix, dbc_captcha *captcha)
{
FILE *f = fopen("id.txt", "w");
if (NULL != f) {
fprintf(f, "%u", captcha->id);
fclose(f);
}
f = fopen("answer.txt", "w");
if (NULL != f) {
fprintf(f, "%s", (NULL != captcha->text) ? captcha->text : "");
fclose(f);
}
printf("[%s] %u %s\n", prefix, captcha->id, (NULL != captcha->text) ? captcha->text : "");
}
int main(int argc, char *argv[])
{
if (argc < 3) {
printf("Usage (image): %s USERNAME PASSWORD CAPTCHA_FILE [... ]\n", argv[0]);
printf("Usage (rc v2): %s USERNAME PASSWORD --rc-v2 --sitekey SITEKEY --pageurl PAGEURL [--proxy URL] [--proxytype TYPE]\n", argv[0]);
printf("Usage (rc v3): %s USERNAME PASSWORD --rc-v3 --sitekey SITEKEY --pageurl PAGEURL [--action ACTION] [--min-score SCORE] [--proxy URL] [--proxytype TYPE]\n", argv[0]);
printf("Usage (enterprise): %s USERNAME PASSWORD --rc-enterprise --sitekey SITEKEY --pageurl PAGEURL [--proxy URL] [--proxytype TYPE]\n", argv[0]);
return EXIT_SUCCESS;
}
#ifdef _WIN32
HINSTANCE lib = LoadLibrary(".\\deathbycaptcha.dll");
if (NULL == lib) {
fprintf(stderr, "LoadLibrary(): %d\n", (int)GetLastError());
return EXIT_FAILURE;
}
#elif __APPLE__
void *lib = dlopen("./libdeathbycaptcha.dylib", RTLD_LAZY);
if (NULL == lib) {
fprintf(stderr, "dlopen(): %s\n", dlerror());
return EXIT_FAILURE;
}
#else
void *lib = dlopen("./libdeathbycaptcha.so", RTLD_LAZY);
if (NULL == lib) {
fprintf(stderr, "dlopen(): %s\n", dlerror());
return EXIT_FAILURE;
}
#endif /* _WIN32 */
int (*dbc_init_fn)(dbc_client *, const char *, const char *) = NULL;
int (*dbc_set_transport_fn)(dbc_client *, unsigned int) = NULL;
void (*dbc_close_fn)(dbc_client *) = NULL;
double (*dbc_get_balance_fn)(dbc_client *) = NULL;
int (*dbc_decode_file_fn)(dbc_client *, dbc_captcha *, FILE *, unsigned int) = NULL;
int (*dbc_decode_recaptcha_v2_fn)(dbc_client *, dbc_captcha *, const char *, const char *, const char *, const char *, unsigned int) = NULL;
int (*dbc_decode_recaptcha_v3_fn)(dbc_client *, dbc_captcha *, const char *, const char *, const char *, double, const char *, const char *, unsigned int) = NULL;
int (*dbc_decode_recaptcha_enterprise_fn)(dbc_client *, dbc_captcha *, const char *, const char *, const char *, const char *, unsigned int) = NULL;
void (*dbc_close_captcha_fn)(dbc_captcha *) = NULL;
#ifdef _WIN32
dbc_init_fn = (void *)GetProcAddress(lib, "dbc_init");
dbc_set_transport_fn = (void *)GetProcAddress(lib, "dbc_set_transport");
dbc_close_fn = (void *)GetProcAddress(lib, "dbc_close");
dbc_get_balance_fn = (void *)GetProcAddress(lib, "dbc_get_balance");
dbc_decode_file_fn = (void *)GetProcAddress(lib, "dbc_decode_file");
dbc_decode_recaptcha_v2_fn = (void *)GetProcAddress(lib, "dbc_decode_recaptcha_v2");
dbc_decode_recaptcha_v3_fn = (void *)GetProcAddress(lib, "dbc_decode_recaptcha_v3");
dbc_decode_recaptcha_enterprise_fn = (void *)GetProcAddress(lib, "dbc_decode_recaptcha_enterprise");
dbc_close_captcha_fn = (void *)GetProcAddress(lib, "dbc_close_captcha");
#else
*(void **)(&dbc_init_fn) = load_symbol(lib, "dbc_init");
*(void **)(&dbc_set_transport_fn) = load_symbol(lib, "dbc_set_transport");
*(void **)(&dbc_close_fn) = load_symbol(lib, "dbc_close");
*(void **)(&dbc_get_balance_fn) = load_symbol(lib, "dbc_get_balance");
*(void **)(&dbc_decode_file_fn) = load_symbol(lib, "dbc_decode_file");
*(void **)(&dbc_decode_recaptcha_v2_fn) = load_symbol(lib, "dbc_decode_recaptcha_v2");
*(void **)(&dbc_decode_recaptcha_v3_fn) = load_symbol(lib, "dbc_decode_recaptcha_v3");
*(void **)(&dbc_decode_recaptcha_enterprise_fn) = load_symbol(lib, "dbc_decode_recaptcha_enterprise");
*(void **)(&dbc_close_captcha_fn) = load_symbol(lib, "dbc_close_captcha");
#endif /* _WIN32 */
if (NULL == dbc_init_fn || NULL == dbc_set_transport_fn || NULL == dbc_close_fn
|| NULL == dbc_get_balance_fn || NULL == dbc_decode_file_fn
|| NULL == dbc_decode_recaptcha_v2_fn || NULL == dbc_decode_recaptcha_v3_fn
|| NULL == dbc_decode_recaptcha_enterprise_fn || NULL == dbc_close_captcha_fn) {
fprintf(stderr, "Failed resolving required symbols from shared library\n");
#ifdef _WIN32
FreeLibrary(lib);
#else
dlclose(lib);
#endif /* _WIN32 */
return EXIT_FAILURE;
}
const char *username = argv[1];
const char *password = argv[2];
enum dbc_sample_token_mode token_mode = DBC_SAMPLE_TOKEN_NONE;
const char *sitekey = NULL;
const char *pageurl = NULL;
const char *action = NULL;
const char *proxy = NULL;
const char *proxytype = NULL;
double min_score = 0.0;
for (int i = 3; i < argc; i++) {
if (!strcmp("--rc-v2", argv[i])) {
token_mode = DBC_SAMPLE_TOKEN_V2;
} else if (!strcmp("--rc-v3", argv[i])) {
token_mode = DBC_SAMPLE_TOKEN_V3;
} else if (!strcmp("--rc-enterprise", argv[i])) {
token_mode = DBC_SAMPLE_TOKEN_ENTERPRISE;
} else if (!strcmp("--sitekey", argv[i]) && i + 1 < argc) {
sitekey = argv[++i];
} else if (!strcmp("--pageurl", argv[i]) && i + 1 < argc) {
pageurl = argv[++i];
} else if (!strcmp("--action", argv[i]) && i + 1 < argc) {
action = argv[++i];
} else if (!strcmp("--proxy", argv[i]) && i + 1 < argc) {
proxy = argv[++i];
} else if (!strcmp("--proxytype", argv[i]) && i + 1 < argc) {
proxytype = argv[++i];
} else if (!strcmp("--min-score", argv[i]) && i + 1 < argc) {
min_score = atof(argv[++i]);
}
}
dbc_client *client = calloc(1, sizeof(dbc_client));
if (NULL == client) {
return EXIT_FAILURE;
}
if (dbc_init_fn(client, username, password)) {
fprintf(stderr, "dbc_init(%s, %s) failed\n", username, password);
dbc_close_fn(client);
free(client);
#ifdef _WIN32
FreeLibrary(lib);
#else
dlclose(lib);
#endif
return EXIT_FAILURE;
}
if (dbc_set_transport_fn(client, DBC_TRANSPORT_HTTPS)) {
fprintf(stderr, "Failed selecting HTTPS transport. Ensure build has libcurl support.\n");
dbc_close_fn(client);
free(client);
#ifdef _WIN32
FreeLibrary(lib);
#else
dlclose(lib);
#endif
return EXIT_FAILURE;
}
dbc_get_balance_fn(client);
printf("[https] User ID: %lu\n[https] Balance: %.3f US cents\n", client->user_id, client->balance);
int rc = 0;
if (DBC_SAMPLE_TOKEN_NONE != token_mode) {
dbc_captcha *captcha = calloc(1, sizeof(dbc_captcha));
if (NULL == captcha || NULL == sitekey || NULL == pageurl) {
rc = -1;
} else if (DBC_SAMPLE_TOKEN_V2 == token_mode) {
rc = dbc_decode_recaptcha_v2_fn(client, captcha, sitekey, pageurl, proxy, proxytype, DBC_TOKEN_TIMEOUT);
} else if (DBC_SAMPLE_TOKEN_V3 == token_mode) {
rc = dbc_decode_recaptcha_v3_fn(client, captcha, sitekey, pageurl, action, min_score, proxy, proxytype, DBC_TOKEN_TIMEOUT);
} else {
rc = dbc_decode_recaptcha_enterprise_fn(client, captcha, sitekey, pageurl, proxy, proxytype, DBC_TOKEN_TIMEOUT);
}
if (!rc && NULL != captcha) {
save_outputs("https", captcha);
} else {
fprintf(stderr, "Failed solving token-based captcha in HTTPS sample\n");
}
if (NULL != captcha) {
dbc_close_captcha_fn(captcha);
free(captcha);
}
} else {
for (int i = 3; i < argc; i++) {
if ('-' == argv[i][0] && '-' == argv[i][1]) {
if ((!strcmp(argv[i], "--sitekey") || !strcmp(argv[i], "--pageurl") ||
!strcmp(argv[i], "--action") || !strcmp(argv[i], "--proxy") ||
!strcmp(argv[i], "--proxytype") || !strcmp(argv[i], "--min-score")) && i + 1 < argc) {
i++;
}
continue;
}
FILE *f = fopen(argv[i], "rb");
if (NULL == f) {
fprintf(stderr, "fopen(%s): %d\n", argv[i], errno);
continue;
}
dbc_captcha *captcha = calloc(1, sizeof(dbc_captcha));
if (NULL == captcha) {
fclose(f);
continue;
}
rc = dbc_decode_file_fn(client, captcha, f, DBC_TIMEOUT);
if (!rc) {
save_outputs("https", captcha);
} else {
fprintf(stderr, "dbc_decode_file(%s) failed\n", argv[i]);
}
dbc_close_captcha_fn(captcha);
free(captcha);
fclose(f);
}
}
dbc_close_fn(client);
free(client);
#ifdef _WIN32
FreeLibrary(lib);
#else
dlclose(lib);
#endif /* _WIN32 */
return EXIT_SUCCESS;
}