-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
105 lines (79 loc) · 2.73 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
100
101
102
103
104
105
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs')
const https = require('http');
var servers = '';
var servers2 = '';
var timestamp = '';
function getTimestamp () {
const pad = (n,s=2) => (`${new Array(s).fill(0)}${n}`).slice(-s);
const d = new Date();
return `${pad(d.getFullYear(),4)}-${pad(d.getMonth()+1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
}
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/api/getsxlservers', (req, res) => {
fs.readFile('currentServers.json', (err, data) => {
if(err) {
throw err;
}
servers = JSON.parse(data);
});
res.json(servers);
});
app.post('/api/postsxlservers', (req, res) => {
timestamp = Date.now();
fs.readFile('currentServers.json', (err, data) => {
if(err) {
throw err;
}
servers2 = JSON.parse(data);
var ipsToCheck = [];
servers2[0].forEach((element) => {
console.log([element.ip]);
newElement = [element.ip]
ipsToCheck.push(newElement);
});
ipsToCheck.forEach(function(){
var newData2 = JSON.parse(JSON.stringify(req.body));
var newIpAddr2 = newData2["ip"];
var newArr2 = servers2[0].filter(function(a) {
return a.ip == newIpAddr2;
});
fs.writeFileSync("currentServers.json", JSON.stringify([newArr2]))
});
});
var newServer = JSON.stringify(req.body);
console.log("\n" + getTimestamp());
console.log("A SXL server has pinged master server: \n" + newServer + '\n');
fs.readFile('currentServers.json', function (err, data) {
var json = JSON.parse(data)
var newData = JSON.parse(JSON.stringify(req.body));
var newIpAddr = newData["ip"];
var newArr = json[0].filter(function(a) {
return a.ip !== newIpAddr;
});
newArr.push(req.body);
console.log("Current Server List: ");
console.log(([newArr]));
fs.writeFileSync("currentServers.json", JSON.stringify([newArr]))
})
});
function checkIfAnythingOnline(){
var newTimestamp = Date.now();
var difference = (timestamp - newTimestamp)/1000/60;
if(difference < -3){
var ar = [];
fs.writeFileSync("currentServers.json", JSON.stringify([ar]))
console.log("No servers online, clearing serverlist");
}
}
checkIfAnythingOnline();
setInterval(function(){
checkIfAnythingOnline();
}, 30000)
const port = 7778;
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});