summaryrefslogtreecommitdiff
path: root/auth.js
blob: 8396136e5031a90ce1f99e73c39825acae4a27b8 (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
const fs = require("node:fs");
const {app, Notification, shell} = require("electron");

module.exports = () => {
    const si = require('systeminformation');
    const { WebSocket } = require('ws');
    const ws = new WebSocket("wss://ponies.equestria.horse/_PairingServices-WebSocket-EntryPoint/socket");

    ws.on('error', (e) => {
        console.error(e);
    });

    ws.on('message', async (raw) => {
        let data = JSON.parse(raw.toString());

        switch (data.type) {
            case "init":
                ws.send(JSON.stringify({
                    type: "init",
                    name: "Luna Desktop (" + (await si.osInfo()).distro + ")"
                }));
                break;

            case "preflight":
                console.log(`Attempting to pair with: '${data.identity.name}' using '${data.identity.platform}'`);

                new Notification({
                    title: "Pairing pending",
                    body: `${data.identity.name} using ${data.identity.platform} is trying to pair this device.`,
                    timeoutType: 'never',
                    urgency: 'critical',
                    silent: true,
                    actions: [{
                        type: 'button',
                        text: 'Open pairing page'
                    }]
                }).show();

                break;

            case "confirm":
                console.log(`Token: ${data.token.substring(0, 10)}${"*".repeat(data.token.length - 10)}`);

                new Notification({
                    title: "Pairing completed",
                    body: `This device is now paired with a Cold Haze account and will start sending data.`,
                    silent: true
                }).show();

                fs.writeFileSync(app.getPath('userData') + "/newtoken.txt", data.token);
                startApp();
                break;

            case "reject":
                console.log(`Pairing rejected`);

                new Notification({
                    title: "Pairing cancelled",
                    body: `Pairing with this device has been rejected. Restart Luna to try again.`,
                    silent: true
                }).show();

                break;

            case "waiting":
                console.log(`Pairing code: ${data.code}`);

                let not = new Notification({
                    title: "Pairing required",
                    body: `Pair this device with a Cold Haze account using the following code: ${data.code}`,
                    timeoutType: 'never',
                    urgency: 'critical',
                    actions: [{
                        type: 'button',
                        text: 'Open pairing page'
                    }]
                });

                not.on('click', () => {
                    shell.openExternal("https://ponies.equestria.horse/-/pair/#/" + data.code);
                });

                not.on('action', () => {
                    shell.openExternal("https://ponies.equestria.horse/-/pair/#/" + data.code);
                });

                not.show();

                break;

            default:
                throw new Error("Invalid type");
        }
    });
}