summaryrefslogtreecommitdiff
path: root/proxy.js
blob: 034c709ec8d068ebd49ad9cb6d34ff431b2000fc (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
const WebSocket = require('ws');
const axios = require("axios");
const fs = require('fs');
let username;

async function main() {
    const clients = {};

    username = (await axios("https://ponies.equestria.horse/api/me", {
        method: "get",
        headers: {'Cookie': 'PEH2_SESSION_TOKEN=' + token}
    })).data.id;

    console.log(username);

    try {
        global.pws = new WebSocket("wss://ponies.equestria.horse/_Computers-RemoteControl-EntryPoint/socket");
        pws.closed = false;
    } catch (e) {
        console.error(e);
        console.log("Failed");

        setTimeout(() => {
            main();
        }, 5000);
    }

    pws.on("open", () => {
        console.log("Connected");
        pws.send(JSON.stringify({
            type: "server",
            id: username + "-" + require('node:crypto').createHash("md5").update(require('node:os').hostname()).digest("hex")
        }));
    })

    pws.on('message', (_data) => {
        try {
            let data = JSON.parse(_data);

            if (data.type === "client") {
                clients[data.id] = new WebSocket("ws://127.0.0.1:38071");

                clients[data.id].on('message', (_d2) => {
                    let d2 = JSON.parse(_d2);
                    d2["_id"] = data.id;
                    pws.send(JSON.stringify(d2));
                })
            } else if (data.type === "close") {
                clients[data.id].close();
            } else if (data._id) {
                clients[data._id].send(_data);
            }
        } catch (e) {
            console.error(e);
        }
    })

    pws.on('close', () => {
        pws.closed = true;
        console.log("Disconnected");

        setTimeout(() => {
            main();
        }, 5000);
    })
}

main();

module.exports = function mainIfNotWorking() {
    if (!pws['closed']) {
        console.log("Closing proxy");
        pws.close();
        pws.closed = true;
        console.log("Disconnected");

        setTimeout(() => {
            main();
        }, 5000);
    } else {
        console.log("Restart in 5 sec");
        setTimeout(() => {
            main();
        }, 5000);
    }
}