-
Notifications
You must be signed in to change notification settings - Fork 2
/
fraud_score.js
93 lines (82 loc) · 2.5 KB
/
fraud_score.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
const connect = require('./connect');
const ProxyAgent = require('proxy-agent');
const http = require('https');
async function getScore(proxy) {
var data = await connect.test(proxy);
if (data.valid == true) {
if(data.data.ip) {
const proxyUri = proxy;
const agent = new ProxyAgent(proxyUri);
const options = {
hostname: 'scamalytics.com',
port: 443,
agent: agent,
rejectUnauthorized: false,
path: '/ip/'+data.data.ip,
method: 'GET'
};
const result = await new Promise((resolve, reject) => {
const req = http.get(options, res => {
const statusCode = res.statusCode;
const headers = res.headers;
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.on('end', () => {
var response = {
valid: false,
status: '',
statusCode: statusCode,
data: data
};
resolve(response);
});
});
req.on('error', err => {
console.log(err)
resolve({statusCode:true})
});
req.end();
});
var response = {};
switch (result.statusCode) {
case 200:
response.valid = true;
response.status = 'Connection Successful';
const html = result.data;
const ipAddressRegex = /"ip":"([\d.]+)"/;
const fraudScoreRegex = /"score":"(\d+)"/;
const riskLevelRegex = /"risk":"(\w+)"/;
const ipAddressMatch = html.match(ipAddressRegex);
const fraudScoreMatch = html.match(fraudScoreRegex);
const riskLevelMatch = html.match(riskLevelRegex);
const extractedData = {
ip: ipAddressMatch ? ipAddressMatch[1] : null,
fraudScore: fraudScoreMatch ? parseInt(fraudScoreMatch[1]) : null,
riskLevel: riskLevelMatch ? riskLevelMatch[1] : null,
};
response.data = extractedData;
break;
case 401:
response.status = 'Authentication Invalid';
break;
case 403:
response.status = 'Authentication Failed';
break;
case 302:
response.status = 'Redirected';
break;
case 502:
response.status = 'Bad Gateway';
break;
default:
response.status = 'Connection Error';
break;
}
if(!response.valid) {response.valid = false}
return response;
}
} else return data;
}
module.exports.getScore = getScore;