-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.js
61 lines (57 loc) · 1.59 KB
/
tools.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
module.exports.randomNumberSeed=function(seed,from,to){
var a=Math.sin(seed)*100000;
a=Math.abs(a-Math.floor(a));
var d=Math.floor(a*(to-from+1));
return from+d;
}
module.exports.randomNumber=function(from,to){
var d=Math.floor(Math.random()*(to-from+1));
return from+d;
}
module.exports.recaptcha_v3=function(response,callback){
if(!global.config.recaptcha_v3.active){
callback(false,true);
return;
}
const https = require('https');
var options = {
host: 'www.google.com',
port: 443,
method: 'POST',
path: '/recaptcha/api/siteverify'
,headers:{'content-type' : 'application/x-www-form-urlencoded'}
};
var req = https.request(options, function(res) {
// console.log('STATUS: ' + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
var bodyChunks = [];
res.on('data', function(chunk) {
bodyChunks.push(chunk);
}).on('end', function() {
var body = Buffer.concat(bodyChunks);
// console.log('BODY: ' + body);
if(callback){
try{
var json=JSON.parse(body);
}catch(e){
callback(e,body);
return;
}
callback(false,json['success']);
}
})
});
req.on('error', function(e) {
console.log('ERROR: ' + e.message);
callback(e);
});
req.write('secret='+encodeURIComponent(global.config.recaptcha_v3.secret)+'&response='+encodeURIComponent(response));
req.end();
}
module.exports.randomText=function(size){
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < size; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}