login mechanics and launch is working now

This commit is contained in:
HorizonCode 2022-10-18 01:46:04 +02:00
parent 4f84dd3248
commit 5c819938c8
6 changed files with 180 additions and 47 deletions

59
app.js
View File

@ -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"
})

View File

@ -47,4 +47,26 @@ async function set(key, value) {
await fs.promises.writeFile(configLocation, arr.join('\n'));
}
module.exports = { get, set }
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 }

10
ezppUtil.js Normal file
View File

@ -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 };

View File

@ -25,7 +25,7 @@
<div class="launcher-window position-relative overflow-hidden">
<div class="container px-1 py-2 w-100 mw-100 h-100">
<div class="row d-flex justify-content-center align-items-center h-100">
<div id="loading-page" class="sections col col-xl-10" style="display: none;">
<div id="loading-page" class="sections col col-xl-10" style="//display: none;">
<div class="launch-section flex-row">
<div class="server-logo">
<img src="../assets/logo.png" height="120">
@ -44,7 +44,7 @@
<div class="loading-indicator-text">Loading... Please wait</div>
</div>
</div>
<div id="launch-page" class="sections col col-xl-10" style="//display: none;">
<div id="launch-page" class="sections col col-xl-10" style="display: none;">
<div class="account-section">
<div class="user-image">
<img id="user-img" src="https://a.ez-pp.farm/0">
@ -63,14 +63,14 @@
<img src="../assets/logo.png" height="150">
</div>
<div class="launch-button">
<button class="btn btn-lg btn-launch btn-accent">Launch</button>
<button class="btn btn-lg btn-launch btn-accent" id="launch-btn">Launch</button>
</div>
</div>
<div class="folder-section">
<div class="folder-location">
Current osu! directory: C:\Users\Thomas\AppData\Local\osu!
Current osu! directory: <span id="currentOsuPath"></span>
</div>
<div class="folder-action">
<div class="folder-action" id="change-folder-btn">
Not right?
</div>
</div>
@ -84,18 +84,18 @@
</div>
<div class="login-section">
<div class="form-outline mb-3 w-50">
<input type="text" id="username" class="form-control form-control-lg" />
<label class="form-label" for="username">Username</label>
<input type="text" id="login-username" class="form-control form-control-lg" />
<label class="form-label" for="login-username">Username</label>
</div>
<div class="form-outline mb-3 w-50">
<input type="password" id="password" class="form-control form-control-lg" />
<label class="form-label" for="password">Password</label>
<input type="password" id="login-password" class="form-control form-control-lg" />
<label class="form-label" for="login-password">Password</label>
</div>
<div class="pt-1 mb-4">
<div class="btn-grouped">
<button id="action-cancel" class="btn btn-grey btn-lg"
type="button">Cancel</button>
<button id="login" class="btn btn-accent btn-lg"
<button id="action-login" class="btn btn-accent btn-lg"
type="button">Login</button>
</div>
</div>

View File

@ -49,12 +49,12 @@ async function isValidOsuFolder(path) {
}
async function getLatestConfig(osuPath) {
const allFiles = await fs.promises.readdir(osuPath);
const configFileInfo = {
name: "",
path: "",
lastModified: 0,
get: async (key) => {
if (!configFileInfo.path)
return "";
const fileStream = await fs.promises.readFile(configFileInfo.path, "utf-8");
const lines = fileStream.split(/\r?\n/)
for (const line of lines) {
@ -69,17 +69,10 @@ async function getLatestConfig(osuPath) {
}
}
}
for (const file of allFiles) {
if (file.startsWith('osu!.') && file.endsWith('.cfg') && file !== "osu!.cfg") {
const fullFilePath = path.join(osuPath, file);
const fileStats = await fs.promises.stat(fullFilePath);
const lastModified = fileStats.mtimeMs;
if (lastModified > configFileInfo.lastModified) {
configFileInfo.name = file;
configFileInfo.path = fullFilePath;
configFileInfo.lastModified = lastModified;
}
}
const userOsuConfig = path.join(osuPath, `osu!.${process.env['USERNAME']}.cfg`)
if (fs.existsSync(userOsuConfig)) {
configFileInfo.name = `osu!.${process.env['USERNAME']}.cfg`;
configFileInfo.path = userOsuConfig;
}
return configFileInfo;
}
@ -149,8 +142,13 @@ async function startWithDevServer(osuPath, serverDomain, onExit) {
return true;
}
function encryptString(value) {
return dpapi.protect(Buffer.from(value, 'utf-8'), osuEncryptBuffer, 'CurrentUser');
async function encryptString(value) {
return Buffer.from(await dpapi.protect(Buffer.from(value, 'utf-8'), osuEncryptBuffer, 'CurrentUser'), 'utf-8').toString('base64');
}
async function decryptString(value) {
const decrypted = await dpapi.unprotect(Buffer.from(value, 'base64'), osuEncryptBuffer, 'CurrentUser');
return decrypted.toString();
}
async function setConfigValue(configPath, key, value) {
@ -160,8 +158,7 @@ async function setConfigValue(configPath, key, value) {
for (const line of lines) {
if (line.includes(' = ')) {
const argsPair = line.split(' = ', 2);
const keyname = argsPair[0]
const value = argsPair[1];
const keyname = argsPair[0].trim();
if (key == keyname) {
configLines.push(`${keyname} = ${value}`);
} else {
@ -171,6 +168,11 @@ async function setConfigValue(configPath, key, value) {
configLines.push(line);
}
}
await fs.promises.writeFile(configPath, configLines.join("\n"), 'utf-8');
}
module.exports = { isValidOsuFolder, getLatestConfig, getUpdateFiles, filesThatNeedUpdate, downloadUpdateFiles, startOsuWithDevServer: startWithDevServer, setConfigValue }
module.exports = {
isValidOsuFolder, getLatestConfig, getUpdateFiles, filesThatNeedUpdate,
downloadUpdateFiles, startOsuWithDevServer: startWithDevServer, setConfigValue,
encryptString, decryptString
}

View File

@ -2,8 +2,8 @@ const { ipcRenderer } = require('electron');
const { Titlebar, Color } = require('custom-electron-titlebar');
const appInfo = require('../appInfo');
let titlebar;
const currentPage = "loading-page";
let currentPage = "loading";
let loggedIn = false;
window.addEventListener('DOMContentLoaded', () => {
titlebar = new Titlebar({
@ -19,11 +19,21 @@ window.addEventListener('DOMContentLoaded', () => {
const axios = require('axios').default;
const Swal = require('sweetalert2');
const loggedIn = false;
$('#account-action').on('click', () => {
if (!loggedIn) {
changePage('login');
} else {
$('#welcome-text').text(`Nice to see you!`);
$('#account-action').text('Click to login');
$('#user-img').prop('src', `https://a.ez-pp.farm/0`)
loggedIn = false;
ipcRenderer.send("perform-logout");
Swal.fire({
title: 'See ya soon!',
text: "Successfully logged out!",
icon: 'success',
confirmButtonText: 'Okay'
});
}
});
@ -62,17 +72,46 @@ window.addEventListener('DOMContentLoaded', () => {
}
});
$("#folder-btn").on('click', async () => {
const success = await ipcRenderer.invoke('set-osu-dir');
if (success == undefined)
$("#action-login").on('click', async () => {
const username = $('#login-username').val();
const password = $('#login-password').val();
$("#action-login").attr('disabled', true);
$("#action-cancel").attr('disabled', true);
const responseData = await ipcRenderer.invoke('perform-login', { username, password });
$("#action-login").attr('disabled', false);
$("#action-cancel").attr('disabled', false);
if (!responseData)
return;
if (success) {
if (responseData.code != 200) {
Swal.fire({
title: 'Uh oh!',
text: responseData.message,
icon: 'error',
confirmButtonText: 'Oops.. my bad!'
})
return;
}
$('#login-username').val("");
$('#login-password').val("");
$('#welcome-text').text(`Welcome back, ${responseData.user.name}!`);
$('#account-action').text('Not you?');
$('#user-img').prop('src', `https://a.ez-pp.farm/${responseData.user.id}`);
loggedIn = true;
changePage('launch');
})
$("#change-folder-btn").on('click', async () => {
const responseData = await ipcRenderer.invoke('set-osu-dir');
if (!responseData)
return;
if (responseData.validOsuDir) {
Swal.fire({
title: 'Success!',
text: 'osu! folder set.',
icon: 'success',
confirmButtonText: 'Cool'
})
$('#currentOsuPath').text(responseData.folderPath);
ipcRenderer.send("do-update-check");
} else {
Swal.fire({
@ -84,11 +123,24 @@ window.addEventListener('DOMContentLoaded', () => {
}
});
ipcRenderer.on('config_update', (event, data) => {
if (data.osuPath) {
$('#currentOsuPath').text(data.osuPath);
}
})
ipcRenderer.on('account_update', (event, data) => {
switch (data.type) {
case "not-loggedin":
changePage("launch");
break;
case "loggedin":
changePage("launch");
$('#welcome-text').text(`Welcome back, ${data.user.name}!`);
$('#account-action').text('Not you?');
$('#user-img').prop('src', `https://a.ez-pp.farm/${data.user.id}`)
loggedIn = true;
break;
}
})
@ -104,7 +156,8 @@ window.addEventListener('DOMContentLoaded', () => {
$('#launch-btn').html('Update');
break;
case "missing-folder":
$('#launch-btn').html('Please set your osu! folder');
$("#launch-btn").attr('disabled', true);
$('#launch-btn').html('set your osu! folder');
break;
case "error":
Swal.fire({
@ -128,9 +181,10 @@ window.addEventListener('DOMContentLoaded', () => {
})
function changePage(page) {
$(`#${currentPage}`).fadeOut(50, "swing", () => {
$(`#${currentPage}-page`).fadeOut(50, "swing", () => {
$(`#${page}-page`).fadeIn(350);
});
currentPage = page;
}
// workaround for the dark theme