-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (123 loc) · 3.54 KB
/
index.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
'use strict';
const config = require('./config');
const readLine = require('readline');
const Socket = require('./socket');
const socket = new Socket(config.host, config.port);
let clientSocket = socket.connect();
//clientSocket.pause();
let user = {'name': 'ttgsgmafe'};
let isAuth = false;
let hbLast = 0;
let rl = readLine.createInterface({
input: process.stdin,
output: process.stdout
});
clientSocket.on('data', (data) => {
let payload = {};
//Validate JSON
if(jsonVerify(data.toString())) {
try{
payload = JSON.parse(data.toString());
if(payload.type === "welcome") {
//console.log(payload.msg);
} else if(payload.type === "heartbeat") {
if ( (payload.epoch - hbLast) > 2 && hbLast !== 0){
if(isAuth) {
clientSocket.destroy();
}
} else {
//testing results
//console.log(payload.type);
}
hbLast = payload.epoch;
}
} catch(err) {
//console.log("jsonerr");
}
} else {
//console.log("Inval");
}
// try{
// payload = JSON.parse(data.toString());
// if(payload.type === 'welcome') {
// logger(payload.msg);
// } else if(payload.type === 'heartbeat') {
// if ( (payload.epoch - hbLast) > 2 && hbLast !== 0){
// clientSocket.destroy();
// } else {
// //testing results
// //console.log(payload);
// }
// hbLast = payload.epoch;
// }
// } catch(err) {
// console.log(`\nInvalid JSON. Skipping`);
// } finally {
// }
});
clientSocket.on('close', () => {
//logger('Connection has been closed. Reauthenticating.');
clientSocket = socket.connect();
authenticate();
//auth();
});
let logger = function (msg) {
// process.stdout.clearLine();
// process.stdout.cursorTo(0);
process.stdout.write(msg);
}
// rl.on('json', (json) => {
// let data;
// try{
// data = JSON.parse(json);
// clientSocket.write(JSON.stringify(data), (err)=> {
// console.log("senderr");
// });
// } catch (err) {
// console.log("invaljson");
// }
// });
let userInput = function() {
rl.question("Enter a JSON formatted command: ", (jsoncmd) => {
try {
} catch(err) {
}
if(jsonVerify(jsoncmd)) {
console.log("Valid");
} else {
console.log(`JSON invalid`);
userInput();
}
});
}
//If allowing automatic authentication
let authenticate = function() {
clientSocket.write(JSON.stringify(user), (err)=> {
if(err) {
console.log(err);
} else {
isAuth = true;
userInput();
}
});
}
authenticate();
//If allowing user to authenticate via console
// let auth = function() {
// rl.question("Enter a username: ", (uname) => {
// if(uname.length > 1) {
// //resume and send user info to the server
// console.log(`Connecting as ${uname}`);
// user.name = uname;
// clientSocket.resume();
// clientSocket.write(JSON.stringify(user));
// } else {
// console.log(`User invalid. Please try again.`);
// auth();
// }
// });
// }
// auth();
let jsonVerify = function(jsonstr) {
return (/^[\],:{}\s]*$/.test(jsonstr));
}