fst – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | const geoip = require('geoip-lite') |
2 | const moment = require('moment') |
||
3 | const SyncData = require('./SyncData.js') |
||
4 | |||
5 | var Metrics = function () { |
||
6 | // Public |
||
7 | this.peers = [] |
||
8 | this.attacked_peers = {} |
||
9 | this.attackers = {} |
||
10 | this.geo = {} |
||
11 | this.ports = {} |
||
12 | |||
13 | // Update the metrics using a message. |
||
14 | this.update = function (syncMessage) { |
||
15 | if (!(syncMessage instanceof SyncData)) |
||
16 | throw 'Argument exception' |
||
17 | |||
18 | var peer = syncMessage.peer |
||
19 | var ip = syncMessage.ip |
||
20 | var port = syncMessage.port |
||
21 | |||
22 | // Update peers. |
||
23 | if (!this.peers.includes(peer)) { |
||
24 | this.peers.push(peer) |
||
25 | } |
||
26 | |||
27 | // Update attacks. |
||
28 | if (!(peer in this.attacked_peers)) { |
||
29 | this.attacked_peers[peer] = {} |
||
30 | this.attacked_peers[peer].count = 1 |
||
31 | this.attacked_peers[peer].stamp = moment() |
||
32 | } |
||
33 | else { |
||
34 | ++this.attacked_peers[peer].count |
||
35 | this.attacked_peers[peer].stamp = moment() |
||
36 | } |
||
37 | |||
38 | // Update the list of attackers. |
||
39 | if (!(ip in this.attackers)) { |
||
40 | this.attackers[ip] = {} |
||
41 | this.attackers[ip].count = 1 |
||
42 | this.attackers[ip].stamp = moment() |
||
43 | } |
||
44 | else { |
||
45 | ++this.attackers[ip].count |
||
46 | this.attackers[ip].stamp = moment() |
||
47 | } |
||
48 | |||
49 | // Update ports. |
||
50 | if(!(port in this.ports)) { |
||
51 | this.ports[port] = 1 |
||
52 | } |
||
53 | else { |
||
54 | ++this.ports[port] |
||
55 | } |
||
56 | |||
57 | // Update geoip. |
||
58 | var geo = geoip.lookup(ip) |
||
59 | if (!(geo.country in this.geo)) { |
||
60 | this.geo[geo.country] = 1 |
||
61 | } else { |
||
62 | ++this.geo[geo.country] |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | |||
67 | // Deserialize metrics from JSON. |
||
68 | Metrics.fromJSON = function (data) { |
||
69 | const payload = JSON.parse(data) |
||
70 | |||
71 | if (payload === null || typeof payload === undefined) |
||
72 | throw 'Deserialization failed' |
||
73 | |||
74 | for (var key in new Metrics()) { |
||
75 | if (typeof Metrics[key] !== 'undefined' && !payload.hasOwnProperty(key)) { |
||
76 | throw 'Deserialization failed' |
||
77 | } |
||
78 | } |
||
79 | |||
80 | var metrics = new Metrics() |
||
81 | metrics.peers = payload.peers |
||
82 | metrics.attacked_peers = payload.attacked_peers |
||
83 | metrics.attackers = payload.attackers |
||
84 | metrics.geo = payload.geo |
||
85 | metrics.ports = payload.ports |
||
86 | |||
87 | return metrics |
||
88 | } |
||
89 | |||
90 | module.exports = Metrics |