-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
63 lines (49 loc) · 1.36 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
var http = require('http');
var options = {
host: 'www.google.com',
port: 80,
path: '/recaptcha/api/verify',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
module.exports = function(privateKey, remoteIP, challenge, response, cb) {
var error = null;
if (!privateKey) {
error = 'Private key is required';
} else if (!remoteIP) {
error = 'Remote IP is required';
} else if (!challenge) {
error = 'Challenge is required';
} else if (!response) {
error = 'Response is required';
}
if (error) {
return cb(new Error(error));
}
var request = http.request(options, function(response) {
var body = '';
response.on('error', function(err) {
return cb(new Error(err));
});
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
var success = body.split('\n')[0] === "true";
var error = body.split('\n')[1];
if (!success) return cb(new Error(error));
cb(null);
});
});
request.on('error', function(err) {
return cb(new Error(err));
});
var query = 'privatekey=' + privateKey +
'&remoteip=' + remoteIP +
'&challenge=' + challenge +
'&response=' + response;
request.write(query);
request.end();
};