summaryrefslogtreecommitdiff
path: root/app.js
blob: 8b4d38f88a7709b99c328e1985f804c3cd1e534a (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const { app, desktopCapturer, dialog, Tray, Menu, shell } = require('electron');
const { platform, hostname } = require('node:os');
const { existsSync, readFileSync } = require('node:fs');
const axios = require('axios');
const si = require('systeminformation');

global.luna_version = "1.4.0";
global.dsb_version = 7;
global.lastSentData = {};
global.clientsList = {};
global.lastDataUpdate = 0;
global.proxyStatus = 0;
global.computerId = "";

function timeAgo(time) {
    if (!isNaN(parseInt(time))) {
        time = new Date(time).getTime();
    }

    if (time === 0) return "never";

    let periods = ["sec", "min", "hr", "d", "wk", "mo", "y", "ages"];

    let lengths = ["60", "60", "24", "7", "4.35", "12", "100"];

    let now = new Date().getTime();

    let difference = Math.round((now - time) / 1000);
    let tense;
    let period;

    if (difference <= 10 && difference >= 0) {
        return "now";
    } else if (difference > 0) {
        tense = "ago";
    } else {
        tense = "later";
    }

    let j;

    for (j = 0; difference >= lengths[j] && j < lengths.length - 1; j++) {
        difference /= lengths[j];
    }

    difference = Math.round(difference);

    period = periods[j];

    return `${difference} ${period} ${tense}`;
}

app.whenReady().then(() => {
    global.tray = new Tray(__dirname + '/icons/tray/16x16Template@2x.png');
    updateTray();
})

function updateTray() {
    let template = [
        { label: 'Luna ' + luna_version + '-' + dsb_version, type: 'normal', enabled: false, icon: __dirname + "/icons/menu/16x16@2x.png" },
        { label: 'Last updated ' + timeAgo(global.lastDataUpdate), type: 'normal', enabled: false },
    ];

    template.push(...[
        { type: 'separator' },
        { label: "View on Cold Haze", accelerator: "CmdOrCtrl+O", click: () => {
            shell.openExternal("https://ponies.equestria.horse/-/computers/" + global.computerId)
        } },
        { label: 'Quit', type: 'normal', role: 'quit', accelerator: "CmdOrCtrl+Q" }
    ]);

    const contextMenu = Menu.buildFromTemplate(template);

    tray.setToolTip('Luna ' + luna_version + '-' + dsb_version);
    tray.setContextMenu(contextMenu);
}

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

global.token = "";

async function systemProfile() {
    let osInfo = await si.osInfo();
    let baseboard = await si.baseboard();
    let processes = (await si.processes()).list;

    let data = {
        luna_version,
        dsb: {
            platform: "desktop",
            version: dsb_version
        },
        top_sites: null,
        remote_control: false,
        host: hostname(),
        os: osInfo.distro + " " + osInfo.release,
        kernel: osInfo.platform.substring(0, 1).toUpperCase() + osInfo.platform.substring(1) + " " + osInfo.kernel + " (" + osInfo.arch + ")",
        serial: baseboard.serial ?? osInfo.serial,
        serial_source: baseboard.serial ? "hardware" : "software",
        date: new Date().toISOString(),
        screens: [],
        windows: [],
        cpu: (await si.cpu()),
        temperature: (await si.cpuTemperature()),
        ram: (await si.mem()),
        ram_chips: (await si.memLayout()),
        battery: (await si.battery()),
        os_info: (await si.osInfo()),
        gpu: (await si.graphics()),
        uuid: (await si.uuid()),
        versions: (await si.versions()),
        users: (await si.users()),
        filesystems: (await si.fsSize()),
        fs_stats: (await si.fsStats()),
        usb: (await si.usb()),
        audio: (await si.audio()),
        network: (await si.networkInterfaces()),
        connections: (await si.networkConnections()),
        processes: processes.map((i) => {
            return {
                pid: i.pid,
                name: i.name,
                cpu: i.cpu,
                ram: i.mem,
                date: new Date(i.started).toISOString(),
                user: i.user,
                path: i.path
            }
        }).sort((a, b) => {
            return b.cpu - a.cpu;
        })
    }

    global.lastSentData = data;

    let sources = await desktopCapturer.getSources({ types: ['screen'], thumbnailSize: { width: 445, height: 256 } });

    for (let source of sources) {
        data.screens.push({
            id: source.display_id,
            gid: source.id,
            name: source.name,
        });
    }

    try {
        global.computerId = (await axios("https://ponies.equestria.horse/api/computer?type=data", {
            method: "post",
            data,
            headers: {'Cookie': 'PEH2_SESSION_TOKEN=' + token}
        })).data;

        global.lastDataUpdate = new Date().getTime();
    } catch (e) {
        console.error(e);
    }
}

async function refresh() {
    let sources = await desktopCapturer.getSources({ types: ['screen'], thumbnailSize: { width: 445, height: 256 } });

    for (let source of sources) {
        try {
            (await axios("https://ponies.equestria.horse/api/computer?type=screenshot", {
                method: "post",
                data: {
                    host: hostname(),
                    id: source.display_id,
                    data: source.thumbnail.toJPEG(80).toString("base64")
                },
                headers: {'Cookie': 'PEH2_SESSION_TOKEN=' + token}
            })).data;
        } catch (e) {
            console.error(e);
        }
    }
}

global.i1 = global.i2 = global.i3 = global.i4 = null;

global.startApp = async () => {
    let data = app.getPath('userData');

    if (platform() === "darwin") app.dock.hide();

    if (!existsSync(data + "/newtoken.txt")) {
        require('./auth')();
        return;
    } else {
        global.token = readFileSync(data + "/newtoken.txt").toString().trim();
    }

    try {
        if (!(await axios("https://ponies.equestria.horse/api/session", {
            headers: {'Cookie': 'PEH2_SESSION_TOKEN=' + token}
        })).data['created']) {
            clearInterval(global.i1); clearInterval(global.i2); clearInterval(global.i3); clearInterval(global.i4);
            require('./auth')();
            return;
        }
    } catch (e) {
        console.error(e);
    }

    try {
        await axios("https://ponies.equestria.horse/api/rename?name=" + encodeURIComponent("Luna " + luna_version + " (Desktop DSB " + dsb_version + ", " + hostname() + ", " + ((await si.osInfo()).distro) + ")"), {
            headers: {'Cookie': 'PEH2_SESSION_TOKEN=' + token}
        });
    } catch (e) {}

    refresh();
    systemProfile();

    global.i1 = setInterval(() => {
        updateTray();
    }, 1000);

    global.i2 = setInterval(async () => {
        try {
            if (!(await axios("https://ponies.equestria.horse/api/session", {
                headers: {'Cookie': 'PEH2_SESSION_TOKEN=' + token}
            })).data['created']) {
                clearInterval(global.i1); clearInterval(global.i2); clearInterval(global.i3); clearInterval(global.i4);
                require('./auth')();
                return;
            }
        } catch (e) {
            console.error(e);
        }

        try {
            await axios("https://ponies.equestria.horse/api/rename?name=" + encodeURIComponent("Luna " + luna_version + " (Desktop DSB " + dsb_version + ", " + hostname() + ", " + ((await si.osInfo()).distro) + ")"), {
                headers: {'Cookie': 'PEH2_SESSION_TOKEN=' + token}
            });
        } catch (e) {
            console.error(e);
        }

        try {
            await axios("https://ponies.equestria.horse/api/computer?type=heartbeat", {
                method: "POST",
                data: {
                    host: hostname()
                },
                headers: {'Cookie': 'PEH2_SESSION_TOKEN=' + token}
            });
        } catch (e) {
            console.error(e);
        }
    }, 5000);

    global.i3 = setInterval(() => {
        refresh();
    }, 60000);

    global.i4 = setInterval(() => {
        systemProfile();
    }, 600000);
}

app.whenReady().then(startApp)