-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
136 lines (113 loc) · 4.27 KB
/
app.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var geohash = require("ngeohash");
const config = require("./config.json");
const axios = require("axios");
const Influx = require("influx");
// our metrics
const metricPort = 3001;
const express = require('express')
const app = express()
const Prometheus = require('prom-client')
const apiCallsTotal = new Prometheus.Counter({
name: 'api_calls_total',
help: 'Total number of geo-ip calls',
labelNames: ['status']
});
const apiRequestDurationMicroseconds = new Prometheus.Histogram({
name: 'api_request_duration_ms',
help: 'Duration of api requests in ms',
labelNames: ['response_time'],
buckets: [0.10, 5, 15, 50, 100, 200, 300, 400, 500] // buckets for response time from 0.1ms to 500ms
})
app.get('/metrics', (req, res) => {
res.set('Content-Type', Prometheus.register.contentType)
res.end(Prometheus.register.metrics())
})
console.log("config: " + JSON.stringify(config));
console.log("config.influxHost=" + config.influxHost);
console.log("config.influxDatabase=" + config.influxDatabase);
// TCP handles
const net = require('net');
const port = 8080;
const host = '0.0.0.0';
const server = net.createServer();
server.listen(port, host, () => {
console.log('TCP Server is running on port ' + port + '.');
});
// // Runs before each requests
// server.use((req, res, next) => {
// res.locals.startEpoch = Date.now()
// next()
// })
// // Runs after each requests
// server.use((req, res, next) => {
// const responseTimeInMs = Date.now() - res.locals.startEpoch
// requestDurationMicroseconds
// .labels('response_time')
// .observe(responseTimeInMs)
// next()
// })
// InfluxDB Initialization.
const influx = new Influx.InfluxDB({
host: config.influxHost,
database: config.influxDatabase
});
let sockets = [];
const metricsServer = app.listen(metricPort, () => {
console.log(`Metrics app listening on port ${metricPort}!`)
})
server.on('connection', function(sock) {
console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
sockets.push(sock);
sock.on('data', function(data) {
console.log("Received data: " + data);
try {
let message = JSON.parse("" + data)
// API Initialization.
const instance = axios.create({
baseURL: "http://ip-api.com/json"
});
instance
.get(`/${message.ip}?fields=status,lat,lon`)
.then(function(response) {
const apiResponse = response.data;
console.log("ip-api.com response: ");
console.log(" status: " + apiResponse.status);
const success = apiResponse.status === 'success' ? 'success' : 'failure';
apiCallsTotal.inc({
status: success
})
console.log(" lat : " + apiResponse.lat);
console.log(" lon : " + apiResponse.lon);
console.log("geohash: "+ geohash.encode(apiResponse.lat, apiResponse.lon));
influx.writePoints(
[{
measurement: "geossh",
fields: {
value: 1
},
tags: {
geohash: geohash.encode(apiResponse.lat, apiResponse.lon),
username: message.username,
port: message.port,
ip: message.ip
}
}]
);
console.log("Intruder added")
})
.catch(function(error) {
console.log(error);
});
} catch(error) {
console.log(error);
};
});
// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
let index = sockets.findIndex(function(o) {
return o.remoteAddress === sock.remoteAddress && o.remotePort === sock.remotePort;
})
if (index !== -1) sockets.splice(index, 1);
console.log('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
});
});