-
Notifications
You must be signed in to change notification settings - Fork 0
/
testServer.js
108 lines (85 loc) · 2.84 KB
/
testServer.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
var express=require('express');
var app=express();
var bodyParser=require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
var server=require('http').createServer(app);
var io=require('socket.io').listen(server);
var fs = require('fs');
var ini = require('ini');
var config = ini.parse(fs.readFileSync('./config.ini', 'utf-8'));
/*
Initializing section
*/
var port=8081;
var timeDelta = config['excel'].step;
var askDelta = config['options'].askDelay;
///
var MongoHandler = require('./my_modules/MongoHandler/MongoHandler');
var url = "mongodb://127.0.0.1:27017/";
var MongoClient = require('mongodb').MongoClient;
var IpAnalyzer = require('./my_modules/IpAnalyzer');
var Informer = require('./my_modules/Informer');
MongoClient.connect(url, function(err, db) {
if (err) {
console.log("cant connect to Mongo")
throw err;
}
var dbo = db.db("VisualIP");
var ipanalyzer = new IpAnalyzer(dbo);
var informer = new Informer(dbo);
var currIps=[];
var refreshInfo;
var sendPercents = function(){};
app.use(express.static(__dirname +'/public'));
app.use(express.static(__dirname +'/node_modules'));
app.post('/harvest', function (req, res) {
if (req.body)
if (req.body.data)
{
var data = JSON.parse(req.body.data);
currIps=[];
for (var i=0;i<data.length;i++) {
var ip = data[i]['ip'];
var time = data[i]['time'];
console.log(ip);
console.log(time);
var info = ipanalyzer.eatIp(ip,time);
currIps.push(info);
}
refreshInfo();
}
res.send('Ok');
});
server.listen(port,function(){
console.log("server is running on port: ",port);
});
setInterval(function() {
informer.countryPercents(function(data){
sendPercents(data);
})
//sendPercents(1);
}, 10000);
io.sockets.on('connection',function(socket) {
console.log('user connected, id: ', socket.id);
io.to(socket.id).emit('sayHi', "welcome to server");
socket.on('countryInfo',function(name){
informer.countryInfo(name,function(data){
if (data){
io.to(socket.id).emit('getCountryInfo', JSON.stringify(data));
}
})
});
/* setInterval(function() {
informer.countryPercents(function(data){
io.to(socket.id).emit('getCountryPercents', JSON.stringify(data));
})
}, 10000);*/
sendPercents = function(data){
io.to(socket.id).emit('getCountryPercents', JSON.stringify(data));
};
refreshInfo = function(){
io.to(socket.id).emit('getIpStack', currIps);
}
});
});