-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
89 lines (76 loc) · 3.03 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
var fs = require('fs');
var config = JSON.parse(fs.readFileSync('./config.cnf', 'utf8').toString());
exports.config = config;
var logger = require('./utils/logger.js'),
math = require('./utils/math.js'),
net = require('net'),
winston = require('winston'),
obj = require('./utils/obj.js'),
systemLogger = winston.loggers.get('system');
var clients = {};
var processRequest = function (client, data) {
var commands = data.toString().split(" ");
switch (commands[0]) {
case "SETUP": {
var k = JSON.parse(commands[1]).k;
var status = math.setupFFS(k);
obj.merge(client.status, status);
systemLogger.info("Client: " + client.id + " requested identification. Generating p,q,n,s,v.");
return { n: status.n, v: status.v, k: status.k }
}
case "X": {
var status = math.generateX(client.status.n);
obj.merge(client.status, status);
systemLogger.info("Client: " + client.id + " requested x generation.");
return { x: status.x };
}
case "FX": {
var status = math.generateFX(client.status.n, client.status.v);
obj.merge(client.status, status);
systemLogger.info("Generating fake x for client: " + client.id);
return { x: status.fx };
}
case "A": {
var a = JSON.parse(commands[1]);
obj.merge(client.status, a);
systemLogger.info("Client: " + client.id + " sent a.");
return a;
}
case "Y": {
var status = math.generateY(client.status.r, client.status.s, client.status.a, client.status.n);
obj.merge(client.status, status);
systemLogger.info("Client: " + client.id + " requested y generation.");
return { y: status.y };
}
case "FY": {
systemLogger.info("Sending fake y for client: " + client.id);
return { y: client.status.fy };
}
case "RESET": {
client.status = {};
systemLogger.info("Resetting client: " + client.id + " status");
return {};
}
}
}
net.createServer(function (socket) {
systemLogger.info("Client connected");
socket.write("Welcome\n");
var client = { socket: socket, status: {}, id: obj.generateId() };
clients[client.id] = client;
socket.on('data', function (data) {
try {
systemLogger.debug(data.toString());
} catch (err) { }
var response = processRequest(client, data);
socket.write(JSON.stringify(response));
});
socket.on('end', function () {
systemLogger.info("Client: " + client.id + " disconnected");
delete clients[client.id];
});
socket.on('error', function () {
systemLogger.error("Connection with client: " + client.id + " dropped");
delete clients[client.id];
});
}).listen(config.port);