2023-06-01 14:18:53 +09:00
|
|
|
import { app, BrowserWindow, dialog, session } from "electron";
|
|
|
|
import { ipcMain } from "electron";
|
2021-01-16 21:22:30 +09:00
|
|
|
import { join } from "path";
|
2023-06-01 14:18:53 +09:00
|
|
|
import { accessTokenName, getAdminAccessTokenValue, getAdminRefreshTokenValue, refreshTokenName } from "./src/login";
|
2021-01-16 21:22:30 +09:00
|
|
|
import { UserAccessor } from "./src/model/mod";
|
2023-06-01 14:18:53 +09:00
|
|
|
import { create_server } from "./src/server";
|
|
|
|
import { get_setting } from "./src/SettingConfig";
|
2021-10-13 17:12:03 +09:00
|
|
|
|
2023-06-01 14:18:53 +09:00
|
|
|
function registerChannel(cntr: UserAccessor) {
|
|
|
|
ipcMain.handle("reset_password", async (event, username: string, password: string) => {
|
|
|
|
const user = await cntr.findUser(username);
|
|
|
|
if (user === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
user.reset_password(password);
|
|
|
|
return true;
|
|
|
|
});
|
2021-01-16 21:22:30 +09:00
|
|
|
}
|
2021-01-10 03:04:30 +09:00
|
|
|
const setting = get_setting();
|
|
|
|
if (!setting.cli) {
|
2023-06-01 14:18:53 +09:00
|
|
|
let wnd: BrowserWindow | null = null;
|
2021-01-10 03:04:30 +09:00
|
|
|
|
2023-06-01 14:18:53 +09:00
|
|
|
const createWindow = async () => {
|
|
|
|
wnd = new BrowserWindow({
|
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
center: true,
|
|
|
|
useContentSize: true,
|
|
|
|
webPreferences: {
|
|
|
|
preload: join(__dirname, "preload.js"),
|
|
|
|
contextIsolation: true,
|
|
|
|
},
|
2021-01-12 16:10:05 +09:00
|
|
|
});
|
2023-06-01 14:18:53 +09:00
|
|
|
await wnd.loadURL(`data:text/html;base64,` + Buffer.from(loading_html).toString("base64"));
|
|
|
|
// await wnd.loadURL('../loading.html');
|
|
|
|
// set admin cookies.
|
|
|
|
await session.defaultSession.cookies.set({
|
|
|
|
url: `http://localhost:${setting.port}`,
|
|
|
|
name: accessTokenName,
|
|
|
|
value: getAdminAccessTokenValue(),
|
|
|
|
httpOnly: true,
|
|
|
|
secure: false,
|
|
|
|
sameSite: "strict",
|
2021-01-12 16:10:05 +09:00
|
|
|
});
|
2023-06-01 14:18:53 +09:00
|
|
|
await session.defaultSession.cookies.set({
|
|
|
|
url: `http://localhost:${setting.port}`,
|
|
|
|
name: refreshTokenName,
|
|
|
|
value: getAdminRefreshTokenValue(),
|
|
|
|
httpOnly: true,
|
|
|
|
secure: false,
|
|
|
|
sameSite: "strict",
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
const server = await create_server();
|
|
|
|
const app = server.start_server();
|
|
|
|
registerChannel(server.userController);
|
|
|
|
await wnd.loadURL(`http://localhost:${setting.port}`);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Error) {
|
|
|
|
await dialog.showMessageBox({
|
|
|
|
type: "error",
|
|
|
|
title: "error!",
|
|
|
|
message: e.message,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
await dialog.showMessageBox({
|
|
|
|
type: "error",
|
|
|
|
title: "error!",
|
|
|
|
message: String(e),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wnd.on("closed", () => {
|
|
|
|
wnd = null;
|
|
|
|
});
|
|
|
|
};
|
2021-01-10 03:04:30 +09:00
|
|
|
|
2023-06-01 14:18:53 +09:00
|
|
|
const isPrimary = app.requestSingleInstanceLock();
|
|
|
|
if (!isPrimary) {
|
|
|
|
app.quit(); // exit window
|
|
|
|
app.exit();
|
2021-01-10 03:04:30 +09:00
|
|
|
}
|
2023-06-01 14:18:53 +09:00
|
|
|
app.on("second-instance", () => {
|
|
|
|
if (wnd != null) {
|
|
|
|
if (wnd.isMinimized()) {
|
|
|
|
wnd.restore();
|
|
|
|
}
|
|
|
|
wnd.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
app.on("ready", (event, info) => {
|
|
|
|
createWindow();
|
|
|
|
});
|
2021-01-10 03:04:30 +09:00
|
|
|
|
2023-06-01 14:18:53 +09:00
|
|
|
app.on("window-all-closed", () => { // quit when all windows are closed
|
|
|
|
if (process.platform != "darwin") app.quit(); // (except leave MacOS app active until Cmd+Q)
|
|
|
|
});
|
2021-01-10 03:04:30 +09:00
|
|
|
|
2023-06-01 14:18:53 +09:00
|
|
|
app.on("activate", () => { // re-recreate window when dock icon is clicked and no other windows open
|
|
|
|
if (wnd == null) createWindow();
|
|
|
|
});
|
2021-01-10 03:04:30 +09:00
|
|
|
} else {
|
2023-06-01 14:18:53 +09:00
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
const server = await create_server();
|
|
|
|
server.start_server();
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
})();
|
2021-01-10 03:04:30 +09:00
|
|
|
}
|
2021-01-15 18:43:36 +09:00
|
|
|
const loading_html = `<!DOCTYPE html>
|
|
|
|
<html lang="ko"><head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<title>loading</title>
|
|
|
|
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'
|
|
|
|
fonts.googleapis.com; font-src 'self' fonts.gstatic.com">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
</head>
|
|
|
|
<style>
|
|
|
|
body { margin-top: 100px; background-color: #3f51b5; color: #fff; text-align:center; }
|
|
|
|
h1 {
|
|
|
|
font: 2em 'Roboto', sans-serif;
|
|
|
|
margin-bottom: 40px;
|
|
|
|
}
|
|
|
|
#loading {
|
|
|
|
display: inline-block;
|
|
|
|
width: 50px;
|
|
|
|
height: 50px;
|
|
|
|
border: 3px solid rgba(255,255,255,.3);
|
|
|
|
border-radius: 50%;
|
|
|
|
border-top-color: #fff;
|
|
|
|
animation: spin 1s linear infinite;
|
|
|
|
}
|
|
|
|
@keyframes spin {
|
|
|
|
to { transform: rotate(360deg);}
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<body>
|
|
|
|
<h1>Loading...</h1>
|
|
|
|
<div id="loading"></div>
|
|
|
|
</body>
|
2023-06-01 14:18:53 +09:00
|
|
|
</html>`;
|