From 5c819938c8e74fc14e97d7f8818c66551a34bc2c Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Tue, 18 Oct 2022 01:46:04 +0200 Subject: [PATCH] login mechanics and launch is working now --- app.js | 59 +++++++++++++++++++++++++++++++----- config.js | 24 ++++++++++++++- ezppUtil.js | 10 +++++++ html/index.html | 20 ++++++------- osuUtil.js | 40 +++++++++++++------------ preload/preload.js | 74 +++++++++++++++++++++++++++++++++++++++------- 6 files changed, 180 insertions(+), 47 deletions(-) create mode 100644 ezppUtil.js diff --git a/app.js b/app.js index f752f8f..72b344c 100644 --- a/app.js +++ b/app.js @@ -2,6 +2,7 @@ const { app, BrowserWindow, ipcMain, dialog } = require('electron'); const { setupTitlebar, attachTitlebarToWindow } = require('custom-electron-titlebar/main'); const windowManager = require('./ui/windowManager'); const osuUtil = require('./osuUtil'); +const ezppUtil = require('./ezppUtil'); const config = require('./config'); const fs = require('fs'); @@ -23,6 +24,7 @@ const run = () => { mainWindow.on('show', async () => { await updateConfigVars(mainWindow); await tryLogin(mainWindow); + await doUpdateCheck(mainWindow); }) app.on('activate', function () { if (BrowserWindow.getAllWindows().length === 0) mainWindow = createWindow(); @@ -31,6 +33,19 @@ const run = () => { app.quit() }) ipcMain.handle('launch', async () => { + const osuConfig = await osuUtil.getLatestConfig(tempOsuPath); + const username = await config.get('username'); + const password = await config.get('password'); + if (password && username) { + await osuUtil.setConfigValue(osuConfig.path, "SaveUsername", "1"); + await osuUtil.setConfigValue(osuConfig.path, "SavePassword", "1"); + await osuUtil.setConfigValue(osuConfig.path, "Username", username); + await osuUtil.setConfigValue(osuConfig.path, "Password", await osuUtil.decryptString(password)); + await osuUtil.setConfigValue(osuConfig.path, "CredentialEndpoint", "ez-pp.farm"); + } else { + await osuUtil.setConfigValue(osuConfig.path, "Username", ""); + await osuUtil.setConfigValue(osuConfig.path, "Password", ""); + } const result = await osuUtil.startOsuWithDevServer(tempOsuPath, "ez-pp.farm", async () => { await doUpdateCheck(mainWindow); }); @@ -85,23 +100,47 @@ const run = () => { if (validOsuDir) await config.set("osuPath", folderPath); - return validOsuDir; + return { validOsuDir, folderPath }; + }) + ipcMain.handle('perform-login', async (event, data) => { + const { username, password } = data; + const loginData = await ezppUtil.performLogin(username, password); + if (loginData && loginData.code === 200) { + await config.set("username", username); + await config.set("password", await osuUtil.encryptString(password)); + } + return loginData; + }) + ipcMain.on('perform-logout', async (event) => { + await config.remove("username"); + await config.remove("password"); }) }) } async function updateConfigVars(window) { - const osuPath = JSON.stringify(config.get("osuPath", "")); + const osuPath = await config.get("osuPath", ""); window.webContents.send('config_update', { osuPath: osuPath }) } async function tryLogin(window) { - const username = config.get("username", ""); - const password = config.get("password", ""); + const username = await config.get("username", ""); + const password = await config.get("password", ""); if ((username && username.length > 0) && (password && password.length > 0)) { - + const passwordPlain = await osuUtil.decryptString(password); + const loginResponse = await ezppUtil.performLogin(username, passwordPlain); + if (loginResponse && loginResponse.code === 200) { + window.webContents.send('account_update', { + type: "loggedin", + user: loginResponse.user + }) + } else { + window.webContents.send('account_update', { + type: "login-failed" + }) + } } else { window.webContents.send('account_update', { type: "not-loggedin" @@ -110,9 +149,15 @@ async function tryLogin(window) { } async function doUpdateCheck(window) { - const osuPath = await config.get("osuPath", ""); + const osuPath = await config.get("osuPath"); + if (!osuPath || osuPath.trim == "") { + window.webContents.send('status_update', { + type: "missing-folder" + }) + return; + } const isValid = await osuUtil.isValidOsuFolder(osuPath); - if (osuPath.trim == "" || !isValid) { + if (!isValid) { window.webContents.send('status_update', { type: "missing-folder" }) diff --git a/config.js b/config.js index 216c503..b60e5ca 100644 --- a/config.js +++ b/config.js @@ -47,4 +47,26 @@ async function set(key, value) { await fs.promises.writeFile(configLocation, arr.join('\n')); } -module.exports = { get, set } \ No newline at end of file +async function remove(key) { + const configValues = new Map(); + const fileStream = await fs.promises.readFile(configLocation, "utf-8"); + const lines = fileStream.split(/\r?\n/) + for (const line of lines) { + if (line.includes('=')) { + const argsPair = line.split('=', 2); + const keyname = argsPair[0] + const value = argsPair[1]; + configValues.set(keyname, value); + } + } + + const arr = []; + for (var [storkey, storvalue] of configValues.entries()) { + if (storkey != key) + arr.push(`${storkey}=${storvalue}`); + } + + await fs.promises.writeFile(configLocation, arr.join('\n')); +} + +module.exports = { get, set, remove } \ No newline at end of file diff --git a/ezppUtil.js b/ezppUtil.js new file mode 100644 index 0000000..3e6e7e4 --- /dev/null +++ b/ezppUtil.js @@ -0,0 +1,10 @@ +const axios = require('axios').default; + +const loginCheckEndpoint = 'https://new.ez-pp.farm/login/check'; + +const performLogin = async (username, password) => { + const result = await axios.post(loginCheckEndpoint, { username, password }); + return result.data; +} + +module.exports = { performLogin }; \ No newline at end of file diff --git a/html/index.html b/html/index.html index 59da581..f28849c 100644 --- a/html/index.html +++ b/html/index.html @@ -25,7 +25,7 @@
-