-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
184 lines (152 loc) · 4.7 KB
/
client.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
var socket = require('socket.io-client')('https://chat-cli.herokuapp.com');
const repl = require('repl')
const chalk = require('chalk');
const figlet = require('figlet')
const { exec } = require('child_process');
var rsaWrapper = require('./encrypt_decrypt')
var fs = require('fs')
var user;
const end = "========================================================"
function eror(code){
switch(code){
case 1: return "Genral Error\n"; break;
case 2: return "Misuse of Shell builtins\n";
case 126: return "Comman invoked cannot be executed\n"
case 127: return "Command not Found\n"
case 130: return "Script termintated by Ctrl-C\n"
}
}
//Initialise the RSA keys
rsaWrapper.initLoadServerKeys(__dirname);
//To Handle disconnection
socket.on('disconnect', function() {
socket.emit('disconnect')
});
//When Client is connected to server
socket.on('connect', () => {
console.log(chalk.green(figlet.textSync('Chat CLI', { horizontalLayout: 'full'})))
console.log(chalk.red(end))
})
//Handle Connection initialization
socket.on('con', (data)=>{
console.log(chalk.green(data));
console.log(chalk.red(end))
});
//User list
socket.on('user', (data)=>{
console.log(chalk.green("=====User List====="));
for(let user of data){
console.log(chalk.red(user))
}
console.log(chalk.red(end))
});
//To Handle Command Request
socket.on('cmd', async (data) => {
//Decrypt command using server Private key
var dec_cmd = rsaWrapper.decrypt(rsaWrapper.serverPrivate, data.cmd)
//Execute the command at bash level
//stdout: Handle Output of command
//stderr: Handle Error of command
await exec(dec_cmd, (stderr, stdout)=>{
if(stderr){
var err = eror(stderr.code);
//create a json object containing encrypted Error and sender socket id
var error = {
error : rsaWrapper.encrypt(rsaWrapper.clientPub, err),
id : data.id
}
//Emit the error object
socket.emit('err', error);
}else{
//create a json object containing encrypted result and sender socket id
var success = {
res : rsaWrapper.encrypt(rsaWrapper.clientPub, stdout),
id : data.id
}
socket.emit('success', success);
}
});
})
//To send back message to the sender (ACK)
socket.on('sender_msg', (data) => {
console.log(chalk.green("Message: ",data));
console.log(chalk.red(end))
})
//To send message to reciever (Other user in case of msg, and sender user in case of command)
socket.on('reciever_msg', (data) => {
var dec_data = rsaWrapper.decrypt(rsaWrapper.serverPrivate, data)
console.log(chalk.green("Message: ",dec_data));
})
//To recive personal message
socket.on("recieve_pmsg", (data)=>{
var dec_msg = rsaWrapper.decrypt(rsaWrapper.serverPrivate, data.msg)
var title = "======" + data.from + "========"
console.log(chalk.red(title))
console.log(chalk.green(dec_msg))
})
repl.start({
prompt: '',
eval: (cmd) => {
var cmd = cmd.split(':')
var op = cmd[0];
var msg = cmd[1]
//if op is undefined or op is not equal to cmd, msg, user and con
//then it means required format is not provided so it will show usage
if((op == undefined)||((op != "cmd")&&(op != "pmsg")&&(op != "msg")&&(op != "user")&&(op != "con"))){
usage();
}else{
var log;
if(op == "cmd"){
log = "Command executed by " + user + " is " + msg ;
} else {
if(op == "msg"){
log = "Message send from " + user + " to all users is " + msg;
} else {
if(op == "user"){
log = "User requested for connected user list\n" ;
} else {
if(op == "con"){
cmd[1] = cmd[1] + " "
var posible_usr = cmd[1].split(" ")
user = posible_usr[1].replace(/(\n| )/gm, "")
log = "User requested for connection\n"
} else {
str1 = cmd[1] + " "
str = cmd[1].split(" ")
msg = ''
for(var i = 2; i < str.length; i++){
msg = msg + str[i] + " "
}
to = str[1].replace(/(\n| )/gm, "");
log = "Message sent from " + user + " to " + to + " is " + msg + "\n"
}
}
}
}
//To Write Client log
fs.appendFileSync('client.log', log);
var enc_msg = rsaWrapper.encrypt(rsaWrapper.clientPub, msg);
if(op != "pmsg"){
socket.emit(op,enc_msg)
} else {
var mesge = {
msg : enc_msg,
to : to,
from : user
}
socket.emit(op, mesge)
}
}
}
})
function usage(){
console.log(chalk.red("-------------------USAGE--------------"));
console.log("");
console.log(chalk.yellow(" cmd: <COMMAND>"));
console.log(chalk.cyan(" msg: <MESSAGE>"));
console.log(chalk.yellow(" user:"));
console.log(chalk.cyan(" pmsg: <USER_NAME> <MESSAGE>"));
console.log(chalk.yellow(" con: <USER_NAME>\n"));
console.log("");
console.log(chalk.red("--------------------Report Ends--------------"));
}