-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.js
48 lines (42 loc) · 1.74 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
#!/usr/bin/env node
var WebSocketClient = require('websocket').client;
const { exec } = require('child_process');
const fs = require('fs-extra');
const {createHmac} = require('crypto');
var client = new WebSocketClient();
client.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
});
client.on('connect', function(connection) {
console.log('✅');
connection.on('error', function(error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function() {
//console.log('echo-protocol Connection Closed');
});
function sendfile (filename, filecontent, hashdata){
const hmac = createHmac('sha256', "ZomEkEyz");
hmac.update(filecontent);
let hashcheck = hmac.digest('hex');
if (hashcheck == hashdata) {
fs.writeFileSync(filename, filecontent, 'utf8');
}
}
connection.on('message', function(message) {
if (message.type === 'utf8') {
if(message.utf8Data.includes("sendfile")){
let msgarray = message.utf8Data.split(',')
sendfile(msgarray[1], msgarray[2], msgarray[3])
} if(message.utf8Data.includes("execall")){
let msgarray = message.utf8Data.split(',')
exec(msgarray.slice(1).join(" "))
} else {
exec(message.utf8Data, (error, stdout, stderr) => {
connection.send(stdout);
});
} //include("sendfile")
} //type utf8
}); //event msg
}); //client connection
client.connect('ws://localhost:8080', 'echo-protocol');