aboutsummaryrefslogtreecommitdiff
path: root/_shakeapi/server.js
blob: 3ebde8df3d5f6ee3530716a3cc907c261ed8cd28 (plain)
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
const { WebSocketServer } = require('ws');
const devices = {};

const wss = new WebSocketServer({ port: 8080 });
const uuid = require('uuid-v4');

wss.on('connection', function connection(ws, req) {
    const ip = req.headers['x-forwarded-for'].split(',')[0].trim();
    const id = uuid();
    if (!devices[ip]) devices[ip] = null;

    ws.on('error', console.error);

    ws.on('message', function message(_data) {
        console.log(devices);

        try {
            const data = JSON.parse(_data);
            console.log(data);

            if (data.type === "init") {
                if (data.code) {
                    ws.watch = true;
                    ws.loaded = true;

                    if (!devices[ip]) devices[ip] = {
                        type: "watch",
                        socket: ws,
                        code: data.code,
                        clients: {}
                    }
                } else if (data.key) {
                    if (devices[ip] && devices[ip].code === data.key) {
                        ws.watch = false;
                        ws.loaded = true;

                        devices[ip].clients[id] = ws;
                        devices[ip].socket.send(JSON.stringify({
                            type: "status",
                            enabled: true,
                            count: devices[ip].clients.length
                        }));

                        ws.send(JSON.stringify({
                            type: "accepted"
                        }));
                    } else {
                        ws.send(JSON.stringify({
                            type: "rejected"
                        }));
                    }
                } else {
                    ws.send(JSON.stringify({
                        type: "invalid_init"
                    }));
                }
            } else if (data.type === "shake" && ws.loaded && ws.watch) {
                for (let socket of Object.values(devices[ip].clients)) {
                    try {
                        if (socket.loaded && !socket.watch) {
                            socket.send(JSON.stringify({
                                type: "shake"
                            }));
                        }
                    } catch (e) {}
                }
            } else {
                ws.send(JSON.stringify({
                    type: "invalid_global"
                }));
            }
        } catch (e) {
            console.error(e);
        }

        console.log("----------");
    });

    ws.on('close', () => {
        if (!ws.watch) {
            console.log("Remove " + id);
            if (devices[ip]) delete devices[ip].clients[id];

            if (Object.values(devices[ip].clients).filter(i => i).length <= 0) devices[ip].socket.send(JSON.stringify({
                type: "status",
                enabled: false
            }));
        } else {
            for (let socket of Object.values(devices[ip].clients)) {
                try {
                    if (socket.loaded && !socket.watch) {
                        socket.send(JSON.stringify({
                            type: "disconnect"
                        }));
                    }
                } catch (e) {}
            }

            delete devices[ip];
        }
    });

    ws.send(JSON.stringify({
        type: "init"
    }));
});