-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.js
More file actions
63 lines (59 loc) · 2.32 KB
/
Copy pathdb.js
File metadata and controls
63 lines (59 loc) · 2.32 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
var sqlite3 = require("sqlite3").verbose();
var db = new sqlite3.Database("./db.db");
var request = require('request');
db.exec("BEGIN TRANSACTION;" +
"CREATE TABLE IF NOT EXISTS vpns (HostName,IP TEXT,Score INTEGER,Ping INTEGER,Speed INTEGER,CountryLong,CountryShort,NumVpnSessions,Uptime,TotalUsers,TotalTraffic,LogType,Operator,Message,OpenVPN_ConfigData_Base64, archivedAt);" +
"CREATE TABLE IF NOT EXISTS resLog (hasError, resCode, timestamp);" +
"CREATE INDEX IF NOT EXISTS vpnIPIndex ON vpns(IP);" +
"CREATE INDEX IF NOT EXISTS vpntsIndex ON vpns(archivedAt);" +
"COMMIT TRANSACTION;" +
"ANALYZE vpns;",
function(err) {
if (err) throw err;
});
var insertLogQuery = db.prepare("INSERT INTO resLog (hasError, resCode, timestamp) VALUES (?, ?, ?)");
function insertLog(hasError, resCode, timestamp, callback) {
insertLogQuery.run(hasError, resCode, timestamp, callback);
}
function insertVPNItems(arrs, timestamp) {
db.parallelize(function() {
for (var arr of arrs) {
arr.push(timestamp);
db.run("INSERT INTO vpns (HostName,IP,Score,Ping,Speed,CountryLong,CountryShort,NumVpnSessions,Uptime,TotalUsers,TotalTraffic,LogType,Operator,Message,OpenVPN_ConfigData_Base64, archivedAt) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", arr);
}
})
}
exports.parseCSVAndInsert = function() {
var url = "http://www.vpngate.net/api/iphone/";
var now = Date.now();
request(url, function(err, response, body) {
if (err) {
console.log("Error");
insertLog(true, 0, now, this.parseCSVAndInsert);
} else {
insertLog(false, response.statusCode, now, this.parseCSVAndInsert);
if (response.statusCode != 200) {
console.log("Not 200 : " + response.statusCode);
return;
}
var lines = body.split("\n");
var items = [];
for (var v of lines) {
if (v.startsWith("*") || v.startsWith("#")) continue;
if (v.trim().length == 0) continue;
items.push(v.split(","));
}
console.log(`${items.length} items inserted`);
insertVPNItems(items, now)
}
})
}
exports.searchByIP = function(ip, callback) {
var obj = [];
db.each("SELECT HostName, CountryLong, Uptime, Operator, Message, archivedAt FROM vpns WHERE IP = ?", ip, function(err, row) {
if (err) throw err;
obj.push(row);
}, function(err) {
callback(obj);
})
}