-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.mjs
More file actions
113 lines (93 loc) · 2.76 KB
/
Copy pathserver.mjs
File metadata and controls
113 lines (93 loc) · 2.76 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
import { createServer } from 'node:http';
import { WebSocketServer } from 'ws';
import { randomBytes } from 'crypto'
const hostname = '0.0.0.0';
const port = 5115;
var state = {};
const server = createServer((req, res) => {
const { headers, method, url } = req;
let body = [];
req
.on('error', err => {
console.error(err);
})
.on('data', chunk => {
body.push(chunk);
})
.on('end', () => {
body = Buffer.concat(body).toString();
res.setHeader('Content-Type', 'text/plain');
if (req.method === 'POST') {
if (req.url === '/auto') {
res.end('Set AUTO');
SendToClients(Buffer.from("10", 'hex'));
state["state"] = "auto";
} else if (req.url === '/on') {
res.end('Set ON');
SendToClients(Buffer.from("0601", 'hex'));
state["state"] = "on";
} else if (req.url === '/off') {
res.end('Set OFF');
SendToClients(Buffer.from("0600", 'hex'));
state["state"] = "off";
} else {
res.statusCode = 404;
res.end('Unsupported Command');
}
} else if (req.method === 'GET') {
res.setHeader('Content-Type', 'text/json');
res.end(JSON.stringify(state));
} else {
res.statusCode = 404;
res.end('Not Found');
}
});
});
server.on('upgrade', (req, socket, head) => {
if (req.headers['sec-websocket-key'] == "123456") {
console.log("Fixing invalid sec-websocket-key");
req.headers['sec-websocket-key'] = randomBytes(16).toString('base64');
}
});
function heartbeat() {
this.isAlive = true;
}
function SendToClients(data) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data, { binary: true });
}
});
}
const wss = new WebSocketServer({ server });
wss.on('connection', function connection(ws,req) {
ws.isAlive = true;
ws.on('error', console.error);
ws.on('pong', heartbeat);
ws.on('message', function message(data) {
if (data.toString()[0] == '{') {
console.log('received: %s', data);
state = Object.assign(state, JSON.parse(data));
}
else
console.log('received: %s', data.toString('base64'));
});
// Init packet?
ws.send(Buffer.from("04", 'hex'), { binary: true });
// Start Auto Mode
ws.send(Buffer.from("10", 'hex'), { binary: true });
state["state"] = "auto";
});
const interval = setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) return ws.terminate();
ws.isAlive = false;
ws.ping();
});
}, 30000);
wss.on('close', function close() {
clearInterval(interval);
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});