-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
213 lines (170 loc) · 5.77 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var express = require('express')
var chalk = require('chalk')
var PriorityQueue = require('js-priority-queue')
var app = express()
var rsaWrapper = require('./encrypt_decrypt')
var fs = require('fs')
var server = app.listen(process.env.PORT || 3000, ()=>{
console.log(`Server Listening at ${process.env.PORT || 3000}`)
});
rsaWrapper.initLoadServerKeys(__dirname);
var io = require('socket.io').listen(server)
var queue = new PriorityQueue() //Priority Messaging Queue
var user = new Array() // Array to store socket id so that user can execute command on another terminal not on its own terminal
var user_name = new Array()
var user_name_list = {}
var i = 0;
var log;
//Server Log Files
app.get('/log/server',async (req, res)=>{
var readStream = fs.createReadStream('./server.log');
// We replaced all the event handlers with a simple call to readStream.pipe()
readStream.on('open', function() {
// This just pipes the read stream to the response object (which goes to the client)
readStream.pipe(res);
});
readStream.on('error', function(err) {
res.end(err);
});
})
//Client Log files
app.get('/log/client',async (req, res)=>{
var readStream = fs.createReadStream('./client.log');
// We replaced all the event handlers with a simple call to readStream.pipe()
readStream.on('open', function() {
// This just pipes the read stream to the response object (which goes to the client)
readStream.pipe(res);
});
readStream.on('error', function(err) {
res.end(err);
});
})
io.on('connection', (socket)=>{
socket.on('con', (data)=>{
if(socket.con == undefined){
socket.join('code');
//Push Socket id to user array and write to log
var dec_data = rsaWrapper.decrypt(rsaWrapper.clientPrivate, data);
var strip_data = dec_data.replace(/(\n| )/gm, "");
var index = user_name.indexOf(strip_data)
if(index > -1){
io.to(socket.id).emit('con', "User id already taken please take a new one.\n execute user: to see used User Id\n");
} else {
log = `${strip_data} connected\n`
fs.appendFileSync('server.log', log);
user.push(socket.id);
user_name.push(strip_data)
user_name_list[strip_data] = socket.id;
socket.con = true;
io.to(socket.id).emit('con', "Connected");
}
} else {
io.to(socket.id).emit('con', "Please Disconnect from previous session to again login\n");
}
});
socket.on('user', ()=>{
io.to(socket.id).emit('user', user_name);
});
socket.on('cmd', (data)=>{
if(socket.con != undefined){
//Decrypt the command from client
var dec_data = rsaWrapper.decrypt(rsaWrapper.clientPrivate, data);
//Encrypt the command with server key
var enc_data = rsaWrapper.encrypt(rsaWrapper.serverPub, dec_data);
//Create a json object that contain command and socket id so that
//the error or result can be transmitted back to correct person
var ndata = {
id : socket.id,
cmd : enc_data
};
//Push to Priority Queue
queue.queue(ndata);
//find the a user
var index = user.indexOf(socket.id);
if(index > -1){
if(index == 0){
i = index + 1
} else {
i = index - 1
}
}
//Write to server log user which will execute a command
log = "Command will be executed on user " + (i + 1) + "\n";
fs.appendFileSync('server.log', log);
//transmitted the command to the required user
io.to(user[i]).emit('cmd', queue.dequeue());
} else {
io.to(socket.id).emit("con", "Please Provide a User name to connect");
}
});
socket.on('err', (data)=>{
var id = data.id;
var error = data.error;
//Decrypt Error Message
var dec_data = rsaWrapper.decrypt(rsaWrapper.clientPrivate, error);
var msg = "NOACK\n" + dec_data;
// Encrypt the final error message
var enc_data = rsaWrapper.encrypt(rsaWrapper.serverPub, msg);
io.to(id).emit('reciever_msg', enc_data);
});
socket.on('success', (data)=>{
var id = data.id;
var res = data.res;
var dec_data = rsaWrapper.decrypt(rsaWrapper.clientPrivate, res);
var msg = "ACK\n" + dec_data;
var enc_data = rsaWrapper.encrypt(rsaWrapper.serverPub, msg);
io.to(id).emit('reciever_msg', enc_data);
});
socket.on('msg', (data)=>{
if(socket.con != undefined){
//Decrypt Data
var dec_data = rsaWrapper.decrypt(rsaWrapper.clientPrivate, data);
//Remove unnecessary newline character
dec_data.replace(/(\r\n|\n|\r)/gm,"")
//Encrypt it back
var enc_data = rsaWrapper.encrypt(rsaWrapper.serverPub, dec_data);
queue.queue(enc_data);
//Transmit msg to all user except sender
socket.broadcast.to('code').emit('reciever_msg', queue.dequeue());
//send ACK to sender
io.to(socket.id).emit('sender_msg', "ACK");
//Write to log
log = "Message transmitted to all the clients\n"
fs.appendFileSync('server.log', log);
} else {
io.to(socket.id).emit("con", "Please Provide a User name to connect");
}
});
socket.on("pmsg", (data)=>{
if(socket.on != undefined){
//Decrypt Message
var dec_msg = rsaWrapper.decrypt(rsaWrapper.clientPrivate, data.msg)
//Encrypt it back
var enc_msg = rsaWrapper.encrypt(rsaWrapper.serverPub, dec_msg)
var snd_obj = {
msg : enc_msg,
from : data.from
}
queue.queue(snd_obj);
//find user id
var id = user_name_list[data.to]
//Transmit msg to Specific Client
io.to(id).emit("recieve_pmsg", queue.dequeue());
//Write to log
log = "Message send to Specified Client\n"
fs.appendFileSync("server.log", log);
} else {
io.to(socket.id).emit("con", "Please Provide a User name to connect");
}
});
socket.on('disconnect', ()=>{
console.log("Disconnect");
var index = user.indexOf(socket.id);
log = `${user_name[index]} Disconnected\n`
if(index > -1){
user.splice(index, 1);
user_name.splice(index, 1);
}
fs.appendFileSync('server.log', log);
});
});