-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathipServer.js
More file actions
97 lines (82 loc) · 2.58 KB
/
ipServer.js
File metadata and controls
97 lines (82 loc) · 2.58 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
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
const Alpine = require('alpine');
const {createReadStream} = require('fs');
const request = require('request-promise');
const alpine = new Alpine();
let Coordinates = require('./model/coordinates');
function findIpAddresses() {
console.log('in find ip addresses');
let ipAddresses = [];
alpine.parseReadStream(
createReadStream('access.log', {encoding: 'utf8'}).on('end', () => {
console.log('qui');
let uniqueIpsFromLog = [...new Set(ipAddresses)];
Coordinates.findAll({
raw: true,
attributes: ['ip']
})
.then(ipsFromDB => {
let diff = makeDiff(uniqueIpsFromLog, ipsFromDB);
if (diff.length > 100) {
return makeApiCall(diff.splice(100, diff.length));
}
console.log(JSON.stringify(diff));
return makeApiCall(diff);
})
.then(function(response) {
console.log('mandata get');
storeIps(response);
// SALVARE NEL DB
})
.catch(function(err) {
console.log(err);
});
}),
data => {
if (data.request === 'POST /service/meter/ HTTP/1.1') {
ipAddresses.push(data.remoteHost);
}
}
);
}
function makeDiff(ipsFromLog, ipsFromDB) {
//console.log(array);
let arrayFromDb = ipsFromDB.map(item => item.ip); // da db
let difference = ipsFromLog
.filter(item => !arrayFromDb.includes(item))
.map(value => ({
query: value,
fields: 'query,lat,lon'
}));
// differences è un array di ip che non sono presenti nel db ma solo sul access.log
//console.log(difference);
return difference;
}
function makeApiCall(unlocalizedIps) {
//console.log('array' + array)
//let array1 = JSON.stringify(array);
const options = {
method: 'POST',
uri: 'http://ip-api.com/batch/',
body: unlocalizedIps,
json: true // Automatically stringifies the body to JSON
};
return request(options);
}
// TODO: bulka instead of looping
function storeIps(localizedIps) {
for (let i = 0; i < localizedIps.length; i++) {
console.log(localizedIps[i].ip);
console.log(localizedIps[i].lat);
console.log(localizedIps[i].longitude);
Coordinates.create({
ip: localizedIps[i].query,
latitude: localizedIps[i].lat,
longitude: localizedIps[i].lon
}).then(coordiantes => {
console.log('inserted');
});
}
}
// fare la chiamata in batch ip-api con al massimo cento
// inserire il tutto nel database,
setInterval(findIpAddresses, 15000, 'funky');