forked from cjavad/lgsm-discord
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
42 lines (32 loc) · 1 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
// lgsmIO server
const fs = require('fs');
const ini = require('ini');
const runner = require('./lib/runner');
const notp = require('notp');
const crypto = require('crypto');
const config = ini.parse(fs.readFileSync('server.ini', 'utf-8'));
const server = require('http').createServer();
const io = require('socket.io')(server);
io.on('connection', socket => {
// New connection
let authed = false;
const token = notp.totp.gen(config.key, 30);
const hashed = crypto.createHash('sha256').update(token).digest('base64');
socket.on('auth', data => {
if (data === hashed) {
// Keys match
authed = true;
// Ready to execute lgsm commands
socket.emit('ready');
}
});
socket.on('lgsm', (user, path, command) => {
runner(user, path, command, output => {
socket.emit('output', output.toString());
});
})
socket.on('disconnect', () => {
// Connection Closed
});
});
server.listen(4545);