Skip to content

Commit 43e5b53

Browse files
authored
Create central-server.js
1 parent 0afa5fd commit 43e5b53

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

central-server.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
const Express = require('express');
2+
3+
var server = Express();
4+
server.listen(1338, '0.0.0.0');
5+
6+
const MAX_ELEMENTS_PER_IP = 10;
7+
const MAX_TIME_TO_KEEP_MS = 1000 * 60 * 3;
8+
const CLEANUP_INTERVAL_MS = 1000 * 60 * 1;
9+
10+
server.use(Express.json({
11+
strict: false,
12+
type: 'application/x-www-form-urlencoded'
13+
}));
14+
15+
const respondWithJSON = (request, response, data) => {
16+
if (arguments.length < 3) data = 0;
17+
response.header('Access-Control-Allow-Origin', '*');
18+
response.end(JSON.stringify(data));
19+
};
20+
21+
const mappingTable = {
22+
23+
}
24+
25+
server.post('/getHosts', (request, response) => {
26+
27+
// const remoteAddress = request.socket.remoteAddress;
28+
// if (typeof remoteAddress !== 'string' || !remoteAddress) return respondWithJSON(request, response);
29+
30+
const browserId = request.body.browserId;
31+
if (typeof browserId !== 'string' || !browserId) return;
32+
33+
34+
if (!mappingTable.hasOwnProperty(browserId)) return respondWithJSON(request, response);
35+
if (!mappingTable[browserId].length) return respondWithJSON(request, response);
36+
respondWithJSON(request, response, mappingTable[browserId].map(x => x.deviceIp));
37+
});
38+
39+
server.post('/retropie', (request, response) => {
40+
response.end();
41+
42+
43+
console.info(request.socket.remoteAddress, request.socket.remotePort)
44+
45+
46+
const browserId = request.body.browserId;
47+
if (typeof browserId !== 'string' || !browserId) return;
48+
49+
const deviceIp = request.body.deviceIp;
50+
if (typeof deviceIp !== 'string' || !deviceIp) return;
51+
52+
53+
const addresses = (
54+
mappingTable.hasOwnProperty(browserId) ?
55+
mappingTable[browserId] :
56+
mappingTable[browserId] = []
57+
);
58+
59+
60+
const existingIndex = addresses.findIndex(item => item.deviceIp === deviceIp);
61+
const when = Date.now();
62+
63+
if (existingIndex !== -1) {
64+
const existingAddress = addresses[existingIndex];
65+
addresses.splice(existingIndex, 1);
66+
addresses.push({ ...existingAddress, when });
67+
} else {
68+
addresses.push({ when, deviceIp });
69+
}
70+
71+
mappingTable[browserId] = addresses.slice(-MAX_ELEMENTS_PER_IP);
72+
73+
74+
});
75+
76+
77+
const checkExpiredAddresses = () => {
78+
console.info('checkExpiredAddresses:start');
79+
for (const browserId in mappingTable) {
80+
81+
mappingTable[browserId] = mappingTable[browserId].filter(address => {
82+
const isExpired = (Date.now() - address.when >= MAX_TIME_TO_KEEP_MS);
83+
if (isExpired) console.info(`expired ${browserId} -> ${JSON.stringify(address)}`);
84+
return !isExpired;
85+
});
86+
87+
if (!mappingTable[browserId].length) {
88+
console.info(`remove ${browserId} from table`);
89+
delete mappingTable[browserId];
90+
}
91+
92+
}
93+
94+
console.info('checkExpiredAddresses:finish');
95+
setTimeout(checkExpiredAddresses, CLEANUP_INTERVAL_MS);
96+
97+
};
98+
99+
100+
checkExpiredAddresses();

0 commit comments

Comments
 (0)