summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaindropsSys <contact@minteck.org>2023-05-13 19:26:08 +0200
committerRaindropsSys <contact@minteck.org>2023-05-13 19:26:08 +0200
commita621f164c5c56d721d531b38d72312634e2e78a8 (patch)
tree9235734da889ceab49ccf4409b6e56ee88a6c8cd
parentc5fff288d2213c7b4ed3c8d26647c82f84c3e2c2 (diff)
downloadluna-a621f164c5c56d721d531b38d72312634e2e78a8.tar.gz
luna-a621f164c5c56d721d531b38d72312634e2e78a8.tar.bz2
luna-a621f164c5c56d721d531b38d72312634e2e78a8.zip
Updated app.js and added auth.js (automated)
-rw-r--r--app.js65
-rw-r--r--auth.js95
2 files changed, 145 insertions, 15 deletions
diff --git a/app.js b/app.js
index be4f584..11e6f6f 100644
--- a/app.js
+++ b/app.js
@@ -4,7 +4,8 @@ const { existsSync, readFileSync } = require('node:fs');
const axios = require('axios');
const si = require('systeminformation');
-global.luna_version = "1.3.0";
+global.luna_version = "1.4.0";
+global.dsb_version = 6;
global.lastSentData = {};
global.clientsList = {};
global.lastDataUpdate = 0;
@@ -89,7 +90,7 @@ async function systemProfile() {
luna_version,
dsb: {
platform: "desktop",
- version: 5
+ version: dsb_version
},
top_sites: null,
remote_control: false,
@@ -177,28 +178,60 @@ async function refresh() {
}
}
-app.whenReady().then(async () => {
+global.i1 = global.i2 = global.i3 = global.i4 = null;
+
+global.startApp = async () => {
let data = app.getPath('userData');
- if (!existsSync(data + "/token.txt")) {
- dialog.showMessageBoxSync({
- message: "Please create a token.txt file containing a valid Cold Haze administrator token in " + data + "."
- });
- process.exit();
+ if (platform() === "darwin") app.dock.hide();
+
+ if (!existsSync(data + "/newtoken.txt")) {
+ require('./auth')();
+ return;
} else {
- global.token = readFileSync(data + "/token.txt").toString().trim();
+ global.token = readFileSync(data + "/newtoken.txt").toString().trim();
}
- if (platform() === "darwin") app.dock.hide();
+ try {
+ if (!(await axios("https://ponies.equestria.horse/api/session", {
+ headers: {'Cookie': 'PEH2_SESSION_TOKEN=' + token}
+ })).data['created']) {
+ throw new Error();
+ }
+ } catch (e) {
+ clearInterval(global.i1); clearInterval(global.i2); clearInterval(global.i3); clearInterval(global.i4);
+ require('./auth')();
+ return;
+ }
+
+ 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}
+ });
refresh();
systemProfile();
- setInterval(() => {
+ global.i1 = setInterval(() => {
updateTray();
}, 1000);
- setInterval(async () => {
+ global.i2 = setInterval(async () => {
+ try {
+ if (!(await axios("https://ponies.equestria.horse/api/session", {
+ headers: {'Cookie': 'PEH2_SESSION_TOKEN=' + token}
+ })).data['created']) {
+ throw new Error();
+ }
+ } catch (e) {
+ clearInterval(global.i1); clearInterval(global.i2); clearInterval(global.i3); clearInterval(global.i4);
+ require('./auth')();
+ return;
+ }
+
+ 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}
+ });
+
try {
await axios("https://ponies.equestria.horse/api/computer?type=heartbeat", {
method: "POST",
@@ -212,11 +245,13 @@ app.whenReady().then(async () => {
}
}, 5000);
- setInterval(() => {
+ global.i3 = setInterval(() => {
refresh();
}, 60000);
- setInterval(() => {
+ global.i4 = setInterval(() => {
systemProfile();
}, 600000);
-}) \ No newline at end of file
+}
+
+app.whenReady().then(startApp) \ No newline at end of file
diff --git a/auth.js b/auth.js
new file mode 100644
index 0000000..8396136
--- /dev/null
+++ b/auth.js
@@ -0,0 +1,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");
+ }
+ });
+} \ No newline at end of file