-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
99 lines (78 loc) · 3.4 KB
/
server.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
//libraries
let express = require('express');
let MongoClient = require('mongodb').MongoClient;
let app = express();
let server = require('http').createServer(app);
let io = require('socket.io').listen(server);
let bodyParser = require('body-parser');
//config file
let config = require('./config.json');
//my modules
let NetworkHandler = require('./my_modules/NetworkHandler/NetworkHandler').NetworkHandler;
let DataPrepareModule = require('./my_modules/DataPrepareModule/DataPrepareModule').DataPrepareModule;
let DataLogReceiver = require('./my_modules/DataLogReceiver/DataLogReceiver').DataLogReceiver;
let MongoHandler = require('./my_modules/MongoHandler/MongoHandler').MongoHandler;
let IpInformer = require('./my_modules/IpInformer/IpInformer').IpInformer;
//my network
let networkHandler = new NetworkHandler();
let dataPrepareModule = new DataPrepareModule(['time_local', 'status', 'body_bytes_sent', 'http_user_agent']);
networkHandler.createNetworkFromLog(__dirname + config.network.trainingFile, () => {
console.log("done training");
networkHandler.checkNetwork(__dirname + '/data/logs/tactravels/access.txt');
});
app.use("/node_modules", express.static(__dirname + "/node_modules/"));
app.use("/", express.static(__dirname + "/public/"));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
server.listen(config.server.port, function () {
console.log("server is running on port: " + config.server.port);
});
MongoClient.connect(config.mongo.url, function (err, connection) {
let db = connection.db(config.mongo.db);
let ipsColl = db.collection("Ips");
let mongoHandler = new MongoHandler(db);
let dataLogReceiver = new DataLogReceiver(dataPrepareModule, networkHandler, mongoHandler);
let ipInformer = new IpInformer(ipsColl);
if (err) {
console.log("cant connect to Mongo");
throw err;
} else {
console.log("connected to db");
}
app.get('/getCountryInfo', (request, response) => {
let code = request.query.name;
if (code){
mongoHandler.getCountryInfo(code, (obj)=>{
response.status(200).send(JSON.stringify(obj));
})
} else{
response.status(503).send("Country required")
}
})
io.sockets.on('connection', function (socket) {
console.log('user connected, id: ', socket.id);
io.to(socket.id).emit('sayHi', "welcome to server");
socket.on('networkInfo', function () {
let data = networkHandler.networkAnalyzer.getFullInfo();
io.to(socket.id).emit('getNetworkInfo', JSON.stringify(data));
});
socket.on('getIpInfo', function (ip) {
if (ip && ip !== ""){
ipInformer.getFullIpInfo(ip, (data) => {
io.to(socket.id).emit('ipInfo', JSON.stringify(data));
})
}
})
app.post("/saveLog", function (req, res) {
if (req.body.data) {
dataLogReceiver.saveLog(req, (fullInfo) => {
io.sockets.emit("getNewSignals", JSON.stringify(fullInfo));
})
}
res.status(200).send({"message": "Ok"});
});
app.get("/saveLog", function (req, res) {
res.status(200).send({"message": "Ok"});
});
});
});