-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
196 lines (145 loc) · 5.07 KB
/
index.js
File metadata and controls
196 lines (145 loc) · 5.07 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
var http = require('http'),
querystring = require('querystring'),
request = require('request'),
fs = require('fs'),
mime = require('mime'),
URL = require('url');
var encodeFieldPart = function (boundary, name, value) {
var return_part = "--" + boundary + "\r\n";
return_part += "Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n";
return_part += value + "\r\n";
return return_part;
};
var encodeFilePart = function (boundary, type, name, filename) {
var return_part = "--" + boundary + "\r\n";
return_part += "Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\"\r\n";
return_part += "Content-Type: " + type + "\r\n\r\n";
return return_part;
};
var deathbycaptcha = {
// Get the image data from an HTTP request
decodeUrl: function(captchaURL, loopDelay, cb) {
var url = URL.parse(captchaURL);
http.get({
host: url.host,
port: url.port,
path: url.path
}, function (res) {
var imagedata = '';
res.setEncoding('binary');
res.on('data', function (chunk) {
imagedata += chunk;
});
res.on('end', function () {
deathbycaptcha.upload(imagedata, res.headers['content-type'], loopDelay, cb);
});
});
},
// Get the image data from a file
decodeFile: function(filePath, loopDelay, cb) {
var contentType = mime.lookup(filePath);
var file_reader = fs.createReadStream(filePath, {encoding: 'binary'});
var file_contents = '';
file_reader.on('data', function (data) {
file_contents += data;
});
file_reader.on('end', function () {
deathbycaptcha.upload(file_contents, contentType, loopDelay, cb);
});
},
// Send a captcha to deatchbycaptcha
upload: function(binaryContents, contentType, loopDelay, cb) {
var boundary = Math.random();
var post_data = [];
post_data.push(new Buffer(encodeFieldPart(boundary, 'username', deathbycaptcha.credentials.username), 'ascii'));
post_data.push(new Buffer(encodeFieldPart(boundary, 'password', deathbycaptcha.credentials.password), 'ascii'));
post_data.push(new Buffer(encodeFilePart(boundary, contentType, 'captchafile', 'mycaptcha.'+mime.extension(contentType) ), 'binary'));
post_data.push(new Buffer(binaryContents, 'binary'));
post_data.push(new Buffer("\r\n--" + boundary + "--"), 'ascii');
var length = 0, i;
for (i = 0; i < post_data.length; i++) {
length += post_data[i].length;
}
var post_options = {
method: 'POST',
host: 'api.dbcapi.me',
port: '80',
path: '/api/captcha',
headers : {
'Content-Type' : 'multipart/form-data; boundary=' + boundary,
'Content-Length' : length
}
};
var post_request = http.request(post_options, function (response) {
response.setEncoding('utf8');
var complete = "";
response.on('data', function (chunk) {
complete += chunk;
});
response.on('end', function () {
var results = querystring.parse(complete);
if(results.error || results.is_correct !== '1') {
cb(results, null);
return;
}
// results = { status: '0', captcha: '33064108', text: '', is_correct: '1' }
var captchaId = results.captcha;
if(loopDelay) {
deathbycaptcha.pollLoop(captchaId, loopDelay, cb);
}
else {
cb(results);
}
});
});
for (i = 0; i < post_data.length; i++) {
post_request.write(post_data[i]);
}
post_request.end();
},
// polling-loop to get decode results
pollLoop: function(captchaId, loopDelay, cb) {
setTimeout(function () {
deathbycaptcha.poll(captchaId, function(err, results) {
if (err) {
cb(err, null);
return;
}
if (results.text === "" || results.text === undefined) {
deathbycaptcha.pollLoop(captchaId, loopDelay, cb);
} else {
cb(null, results);
}
});
}, loopDelay);
},
// get results for a captcha
poll: function(captchaId, cb) {
var url = "http://api.dbcapi.me/api/captcha/" + captchaId;
request.get(url, function (error, response, body) {
var results = querystring.parse(body);
cb(error,results);
});
},
/**
* TODO
*/
report: function() {},
credit: function(cb) {
request.post("http://api.dbcapi.me/api/user", {
form: deathbycaptcha.credentials
}, function (error, response, body) {
if(error) {
cb(error, null);
return;
}
var results = querystring.parse(body);
if(results.error) {
cb(results, null);
return;
}
cb(error,results);
});
}
};
module.exports = deathbycaptcha;