Repository navigation
Expand file tree
/
Copy pathclient.js
More file actions
114 lines (99 loc) · 3.5 KB
/
Copy pathclient.js
File metadata and controls
114 lines (99 loc) · 3.5 KB
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
const net = require('net');
const crypto = require('crypto');
const config = require('./config');
class EncryptionHandler {
constructor(key, iv) {
this.key = Buffer.from(key, 'utf8');
this.iv = Buffer.from(iv, 'utf8');
this.blockSize = 16;
}
encrypt(data) {
const padded = this.pad(Buffer.from(data, 'utf8'));
const cipher = crypto.createCipheriv('aes-256-cbc', this.key, this.iv);
return Buffer.concat([cipher.update(padded), cipher.final()]).toString('base64');
}
decrypt(data) {
const decipher = crypto.createDecipheriv('aes-256-cbc', this.key, this.iv);
const decrypted = Buffer.concat([decipher.update(Buffer.from(data, 'base64')), decipher.final()]);
return this.unpad(decrypted).toString('utf8');
}
pad(data) {
const padLength = this.blockSize - (data.length % this.blockSize);
const padding = Buffer.alloc(padLength, padLength);
return Buffer.concat([data, padding]);
}
unpad(data) {
if (!data.length) return Buffer.alloc(0);
const padLength = data[data.length - 1];
if (padLength < 1 || padLength > this.blockSize) return data;
return data.subarray(0, data.length - padLength);
}
}
class D3vSkymanClient {
constructor(host, port, password, iv, key) {
this.host = host;
this.port = port;
this.password = password;
this.encryption = new EncryptionHandler(key, iv);
this.buffer = Buffer.alloc(0);
this.socket = null;
this.phase = 'welcome';
this.result = '';
this.streamChunks = [];
}
sendCommand(command) {
return new Promise((resolve, reject) => {
const socket = net.createConnection(this.port, this.host);
this.socket = socket;
socket.on('connect', () => {
socket.on('data', (chunk) => {
this.buffer = Buffer.concat([this.buffer, chunk]);
let newlineIndex;
while ((newlineIndex = this.buffer.indexOf(Buffer.from('\n'))) !== -1) {
const frame = this.buffer.subarray(0, newlineIndex).toString('utf8').replace(/\r$/, '');
this.buffer = this.buffer.subarray(newlineIndex + 1);
if (this.phase === 'welcome') {
this.phase = 'auth';
this.writeFrame(`AUTH ${this.password};`);
continue;
}
if (this.phase === 'auth') {
this.phase = 'command';
this.writeFrame(command);
continue;
}
const payload = this.encryption.decrypt(frame);
if (payload === 'STREAM_END') {
resolve({ status: 'success', result: this.result, stream: this.streamChunks });
socket.end();
return;
}
if (payload.startsWith('STREAM_CHUNK:')) {
const chunkText = payload.slice('STREAM_CHUNK:'.length);
this.result += chunkText;
this.streamChunks.push(chunkText);
} else {
this.result += payload;
this.streamChunks.push(payload);
}
}
});
});
socket.on('error', reject);
});
}
writeFrame(message) {
const payload = this.encryption.encrypt(message) + '\n';
this.socket.write(payload);
}
}
const client = new D3vSkymanClient(config.host, config.port, config.password, config.iv, config.key);
client.result = '';
client.streamChunks = [];
client.sendCommand("CLI echo 'hello from JavaScript' && echo 'streaming works';")
.then((response) => {
console.log(response.result);
})
.catch((error) => {
console.error(error);
});