diff options
author | RaindropsSys <contact@minteck.org> | 2023-03-07 22:15:59 +0100 |
---|---|---|
committer | RaindropsSys <contact@minteck.org> | 2023-03-07 22:15:59 +0100 |
commit | 9b42afa8d706f3cdf1abea85e44dfb7f518dfe4f (patch) | |
tree | bb4960293535abe3b3c1d74dd4eb380e0cea537d /app.js | |
parent | 8ecde1d333997b8e5ce8d9c9cf22927db6f40559 (diff) | |
download | luna-9b42afa8d706f3cdf1abea85e44dfb7f518dfe4f.tar.gz luna-9b42afa8d706f3cdf1abea85e44dfb7f518dfe4f.tar.bz2 luna-9b42afa8d706f3cdf1abea85e44dfb7f518dfe4f.zip |
Updated 9 files, added 12 files and renamed .idea/ponieswatch.iml (automated)
Diffstat (limited to 'app.js')
-rw-r--r-- | app.js | 150 |
1 files changed, 126 insertions, 24 deletions
@@ -1,10 +1,92 @@ -const { app, desktopCapturer, dialog } = require('electron'); +const { app, desktopCapturer, dialog, Tray, Menu, shell } = require('electron'); const { platform, hostname } = require('node:os'); const { writeFileSync, existsSync, readFileSync } = require('node:fs'); const axios = require('axios'); const si = require('systeminformation'); -global.luna_version = "1.1.0"; +global.luna_version = "1.2.0"; +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, type: 'normal', enabled: false, icon: __dirname + "/icons/menu/16x16@2x.png" }, + { type: 'separator' }, + { label: global.proxyStatus === 0 ? "Disconnected" : (global.proxyStatus === 1 ? "Connecting..." : "Connected to proxy"), type: 'normal', enabled: false }, + { label: 'Last updated ' + timeAgo(global.lastDataUpdate), type: 'normal', enabled: false }, + { type: 'separator' }, + { label: 'Connected clients:', type: 'normal', enabled: false }, + ]; + + if (Object.values(global.clientsList).filter(i => i).length > 0) { + for (let client of Object.values(global.clientsList)) { + template.push({ label: ' ' + client.name + ' (' + client.location + ", " + client.ip + ")", type: 'normal', click: () => { + client.socket.close(); + } }); + } + } else { + template.push({ label: ' (no connections)', 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); + tray.setContextMenu(contextMenu); +} process.on('uncaughtException', (e) => { console.error(e); @@ -63,6 +145,8 @@ async function systemProfile() { }) } + global.lastSentData = data; + let sources = await desktopCapturer.getSources({ types: ['screen'], thumbnailSize: { width: 445, height: 256 } }); for (let source of sources) { @@ -75,11 +159,17 @@ async function systemProfile() { }); } - await axios("https://ponies.equestria.horse/api/computer?type=data", { - method: "post", - data, - headers: {'Cookie': 'PEH2_SESSION_TOKEN=' + token} - }); + 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); + } writeFileSync("./data.json", JSON.stringify(data)); } @@ -90,15 +180,19 @@ async function refresh() { for (let source of sources) { console.log(`Screen ${source.id} (${source.name})`); - console.log((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); + try { + console.log((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); + } } } @@ -122,16 +216,24 @@ app.whenReady().then(async () => { refresh(); systemProfile(); - setInterval(async () => { - await axios("https://ponies.equestria.horse/api/computer?type=heartbeat", { - method: "POST", - data: { - host: hostname() - }, - headers: {'Cookie': 'PEH2_SESSION_TOKEN=' + token} - }); + setInterval(() => { + updateTray(); }, 1000); + setInterval(async () => { + 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); + setInterval(() => { refresh(); }, 60000); |