-
Notifications
You must be signed in to change notification settings - Fork 102
/
gt-sdk.js
152 lines (149 loc) · 4.91 KB
/
gt-sdk.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
"use strict";
var crypto = require('crypto'),
request = require('request'),
pkg = require("./package.json");
var md5 = function (str) {
return crypto.createHash('md5').update(String(str)).digest('hex');
};
var randint = function (from, to) {
// range: from ~ to
return Math.floor(Math.random() * (to - from + 1) + from);
};
function Geetest(config) {
if (typeof config.geetest_id !== 'string') {
throw new Error('Geetest ID Required');
}
if (typeof config.geetest_key !== 'string') {
throw new Error("Geetest KEY Required");
}
if (typeof config.protocol === 'string') {
this.PROTOCOL = config.protocol;
}
if (typeof config.api_server === 'string') {
this.API_SERVER = config.api_server;
}
if (typeof config.timeout === 'number') {
this.TIMEOUT = config.timeout;
}
this.geetest_id = config.geetest_id;
this.geetest_key = config.geetest_key;
}
Geetest.prototype = {
PROTOCOL: 'http://',
API_SERVER: 'api.geetest.com',
VALIDATE_PATH: '/validate.php',
REGISTER_PATH: '/register.php',
TIMEOUT: 2000,
NEW_CAPTCHA: true,
JSON_FORMAT: 1,
register: function (data, callback) {
var that = this;
return new Promise(function (resolve, reject) {
that._register(data, function (err, data) {
if (typeof callback === 'function') {
callback(err, data);
}
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
},
_register: function (data, callback) {
data = data || {};
var that = this;
request({
url: this.PROTOCOL + this.API_SERVER + this.REGISTER_PATH,
method: 'GET',
timeout: this.TIMEOUT,
json: true,
qs: {
gt: this.geetest_id,
json_format: this.JSON_FORMAT,
sdk: 'Node_' + pkg.version,
client_type: data.client_type || 'unknown',
ip_address: data.ip_address || 'unknown'
}
}, function (err, res, data) {
var challenge;
if (err || !data || !data.challenge) {
// failback
challenge = that._make_challenge();
callback(null, {
success: 0,
challenge: challenge,
gt: that.geetest_id,
new_captcha: that.NEW_CAPTCHA
});
} else {
challenge = md5(data.challenge + that.geetest_key);
callback(null, {
success: 1,
challenge: challenge,
gt: that.geetest_id,
new_captcha: that.NEW_CAPTCHA
});
}
});
},
validate: function (failback, result, callback) {
var that = this;
return new Promise(function (resolve, reject) {
that._validate(failback, result, function (err, data) {
if (typeof callback === 'function') {
callback(err, data);
}
if (err) {
reject(err);
} else {
resolve(data);
}
});
})
},
_validate: function (failback, result, callback) {
var challenge = result.challenge || result.geetest_challenge;
var validate = result.validate || result.geetest_validate;
var seccode = result.seccode || result.geetest_seccode;
if (failback) {
if (md5(challenge) === validate) {
callback(null, true);
} else {
callback(null, false);
}
} else {
var hash = this.geetest_key + 'geetest' + challenge;
if (validate === md5(hash)) {
request({
url: this.PROTOCOL + this.API_SERVER + this.VALIDATE_PATH,
method: 'POST',
timeout: this.TIMEOUT,
json: true,
form: {
gt: this.geetest_id,
seccode: seccode,
json_format: this.JSON_FORMAT
}
}, function (err, res, data) {
if (err || !data || !data.seccode) {
callback(err);
} else {
callback(null, data.seccode === md5(seccode));
}
});
} else {
callback(null, false);
}
}
},
_make_challenge: function () {
var rnd1 = randint(0, 90);
var rnd2 = randint(0, 90);
var md5_str1 = md5(rnd1);
var md5_str2 = md5(rnd2);
return md5_str1 + md5_str2.slice(0, 2);
}
};
module.exports = Geetest;