renaming, spawning forms if it does not exist, simplifying
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
node_modules
|
||||
package-lock.json
|
||||
master.forms
|
||||
master.forms
|
||||
forms/app.log
|
||||
223
app.js
223
app.js
@@ -1,109 +1,148 @@
|
||||
import { app, BrowserWindow, globalShortcut } from 'electron';
|
||||
import paths from 'path'
|
||||
import Server from "./server/index.js"
|
||||
import { spawn } from 'child_process'
|
||||
import fs from 'fs'
|
||||
|
||||
import { fileURLToPath } from 'url';
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = paths.dirname(__filename);
|
||||
import util from './flame/util.js'
|
||||
import Server from "./flame/index.js"
|
||||
|
||||
const WINDOW_SHORTCUTS = [
|
||||
process.platform === "darwin" ? "Command+R" : "Control+R",
|
||||
"\\"
|
||||
];
|
||||
|
||||
function createWindow({onTop = false}) {
|
||||
const win = new BrowserWindow({
|
||||
width: 1200,
|
||||
height: 800,
|
||||
frame: false,
|
||||
show: false,
|
||||
titleBarStyle: "customButtonsOnHover",
|
||||
webPreferences: {
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false,
|
||||
webviewTag: true,
|
||||
}
|
||||
});
|
||||
class App {
|
||||
devToolsOpened = false;
|
||||
|
||||
if(onTop) {
|
||||
win.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true }); // necessary for full screen
|
||||
win.setAlwaysOnTop(true, 'screen-saver', 1); // necessary so it doesn't bring you back out of full screen when spawned
|
||||
}
|
||||
|
||||
win.loadFile('server/index.html');
|
||||
|
||||
win.on("focus", () => {
|
||||
win.setVibrancy("appearance-based"); // full colors
|
||||
});
|
||||
|
||||
win.on("blur", () => {
|
||||
win.setVibrancy("selection"); // dims colors
|
||||
});
|
||||
|
||||
win.webContents.on('did-finish-load', () => {
|
||||
win.show();
|
||||
});
|
||||
|
||||
win.webContents.on('did-fail-load', (e, code, desc) => {
|
||||
console.log('Webview failed:', desc);
|
||||
});
|
||||
|
||||
win.on('closed', () => {
|
||||
for (const key of WINDOW_SHORTCUTS) {
|
||||
globalShortcut.unregister(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let devToolsOpened = false;
|
||||
|
||||
function toggleDevTools(win) {
|
||||
if (devToolsOpened) {
|
||||
win.closeDevTools();
|
||||
} else {
|
||||
win.openDevTools();
|
||||
}
|
||||
devToolsOpened = !devToolsOpened;
|
||||
}
|
||||
|
||||
app.on("ready", async () => {
|
||||
// await Forms.init();
|
||||
// createWindow({onTop: false});
|
||||
new Server()
|
||||
|
||||
globalShortcut.register('CommandOrControl+Shift+Space', () => {
|
||||
createWindow({onTop: true});
|
||||
});
|
||||
|
||||
// Register global shortcuts
|
||||
app.on("browser-window-focus", () => {
|
||||
const focused = BrowserWindow.getFocusedWindow();
|
||||
if (!focused) return;
|
||||
|
||||
// Reload
|
||||
globalShortcut.register(WINDOW_SHORTCUTS[0], () => {
|
||||
focused.reload();
|
||||
createWindow({onTop = false}) {
|
||||
const win = new BrowserWindow({
|
||||
width: 1200,
|
||||
height: 800,
|
||||
frame: false,
|
||||
show: false,
|
||||
titleBarStyle: "customButtonsOnHover",
|
||||
webPreferences: {
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false,
|
||||
webviewTag: true,
|
||||
}
|
||||
});
|
||||
|
||||
// Devtools
|
||||
globalShortcut.register(WINDOW_SHORTCUTS[1], () => {
|
||||
toggleDevTools(focused);
|
||||
if(onTop) {
|
||||
win.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true }); // necessary for full screen
|
||||
win.setAlwaysOnTop(true, 'screen-saver', 1); // necessary so it doesn't bring you back out of full screen when spawned
|
||||
}
|
||||
|
||||
win.loadFile('flame/index.html');
|
||||
|
||||
win.on("focus", () => {
|
||||
win.setVibrancy("appearance-based"); // full colors
|
||||
});
|
||||
});
|
||||
|
||||
app.on("browser-window-blur", () => {
|
||||
for (const key of WINDOW_SHORTCUTS) {
|
||||
globalShortcut.unregister(key);
|
||||
}
|
||||
});
|
||||
});
|
||||
win.on("blur", () => {
|
||||
win.setVibrancy("selection"); // dims colors
|
||||
});
|
||||
|
||||
app.on("activate", () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
createWindow({onTop: false});
|
||||
win.webContents.on('did-finish-load', () => {
|
||||
win.show();
|
||||
});
|
||||
|
||||
win.webContents.on('did-fail-load', (e, code, desc) => {
|
||||
console.log('Webview failed:', desc);
|
||||
});
|
||||
|
||||
win.on('closed', () => {
|
||||
for (const key of WINDOW_SHORTCUTS) {
|
||||
globalShortcut.unregister(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') app.quit();
|
||||
});
|
||||
spawnFlame(spawnForms) {
|
||||
new Server(spawnForms)
|
||||
}
|
||||
|
||||
async spawnForms() {
|
||||
const logPath = paths.join(util.FORMS_PATH, "app.log");
|
||||
const out = fs.openSync(logPath, "a");
|
||||
const err = fs.openSync(logPath, "a");
|
||||
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
[paths.join(util.FORMS_PATH, "kernel/kernel.js")],
|
||||
{
|
||||
detached: true,
|
||||
stdio: [
|
||||
"ignore",
|
||||
out,
|
||||
err
|
||||
],
|
||||
cwd: process.cwd(),
|
||||
env: process.env
|
||||
}
|
||||
);
|
||||
|
||||
child.unref();
|
||||
}
|
||||
|
||||
toggleDevTools(win) {
|
||||
if (this.devToolsOpened) {
|
||||
win.closeDevTools();
|
||||
} else {
|
||||
win.openDevTools();
|
||||
}
|
||||
this.devToolsOpened = !this.devToolsOpened;
|
||||
}
|
||||
|
||||
registerShortcuts() {
|
||||
globalShortcut.register('CommandOrControl+Shift+Space', function CreateWindow() {
|
||||
this.createWindow({onTop: true});
|
||||
});
|
||||
|
||||
app.on("browser-window-focus", function RegisterWindowOnFocus() {
|
||||
const focused = BrowserWindow.getFocusedWindow();
|
||||
if (!focused) return;
|
||||
|
||||
let WINDOW_SHORTCUTS = [
|
||||
process.platform === "darwin" ? "Command+R" : "Control+R",
|
||||
"\\"
|
||||
];
|
||||
|
||||
// Reload
|
||||
globalShortcut.register(WINDOW_SHORTCUTS[0], () => {
|
||||
focused.reload();
|
||||
});
|
||||
|
||||
// Devtools
|
||||
globalShortcut.register(WINDOW_SHORTCUTS[1], () => {
|
||||
toggleDevTools(focused);
|
||||
});
|
||||
});
|
||||
|
||||
app.on("browser-window-blur", function UnregisterWindowOnUnfocus() {
|
||||
for (const key of WINDOW_SHORTCUTS) {
|
||||
globalShortcut.unregister(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
constructor() {
|
||||
app.on("activate", () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
this.createWindow({onTop: false});
|
||||
}
|
||||
});
|
||||
|
||||
app.on('window-all-closed', () => {
|
||||
if (process.platform !== 'darwin') app.quit();
|
||||
});
|
||||
|
||||
app.on("ready", async () => {
|
||||
this.createWindow({onTop: false});
|
||||
this.spawnFlame(this.spawnForms)
|
||||
this.registerShortcuts()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
new App()
|
||||
|
Before Width: | Height: | Size: 117 KiB After Width: | Height: | Size: 117 KiB |
120
flame/index.js
Normal file
120
flame/index.js
Normal file
@@ -0,0 +1,120 @@
|
||||
import path from 'path';
|
||||
import express from 'express';
|
||||
import cors from 'cors'
|
||||
import http from 'http'
|
||||
import chalk from 'chalk'
|
||||
import * as z from 'zod';
|
||||
import forms from 'forms'
|
||||
|
||||
import { initWebSocket } from './ws.js'
|
||||
import util from './util.js'
|
||||
|
||||
export default class Server {
|
||||
store;
|
||||
|
||||
registerRoutes(router) {
|
||||
router.get('/*', this.get)
|
||||
return router
|
||||
}
|
||||
|
||||
index = async (req, res) => {
|
||||
let filePath = path.join(util.CAVE_PATH, "index.html");
|
||||
res.sendFile(filePath)
|
||||
}
|
||||
|
||||
get = async (req, res) => {
|
||||
let url = req.url
|
||||
let filePath;
|
||||
if(url.startsWith("/_")) {
|
||||
filePath = path.join(util.CAVE_PATH, url);
|
||||
} else if(url.includes("75820185")) {
|
||||
filePath = path.join(util.CAVE_PATH, url.split("75820185")[1]);
|
||||
} else {
|
||||
filePath = path.join(util.CAVE_PATH, "index.html");
|
||||
}
|
||||
|
||||
res.sendFile(filePath);
|
||||
}
|
||||
|
||||
async connectToForms(spawnForms) {
|
||||
try {
|
||||
await this.store.connect()
|
||||
}
|
||||
catch(e) {
|
||||
try {
|
||||
await spawnForms()
|
||||
await this.store.connect()
|
||||
}
|
||||
catch(e) {
|
||||
try {
|
||||
let tryReconnect = async (attempts = 5, interval = 100) => {
|
||||
for (let i = 0; i < attempts; i++) {
|
||||
try {
|
||||
return await this.store.connect();
|
||||
} catch (e) {
|
||||
if (i === attempts - 1) {throw e;}
|
||||
await new Promise(resolve => setTimeout(resolve, interval));
|
||||
}
|
||||
}
|
||||
}
|
||||
await tryReconnect(5, 100)
|
||||
}
|
||||
catch(e) {
|
||||
console.error("Could not spawn forms: ", e)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.store.register("log", z.toJSONSchema(z.object({
|
||||
time: z.string(),
|
||||
host: z.string(),
|
||||
ip: z.string(),
|
||||
path: z.string(),
|
||||
method: z.string()
|
||||
})))
|
||||
this.store.add("log", {
|
||||
time: "0100",
|
||||
host: "parchment.page",
|
||||
ip: "0.0.0.1",
|
||||
path: "/asd",
|
||||
method: "GET"
|
||||
})
|
||||
}
|
||||
|
||||
constructor(spawnForms) {
|
||||
const app = express();
|
||||
app.use(cors({ origin: '*' }));
|
||||
app.use(express.json());
|
||||
|
||||
app.use(util.logRequest);
|
||||
app.use(util.logResponse);
|
||||
|
||||
let router = express.Router();
|
||||
this.registerRoutes(router)
|
||||
app.use('/', router);
|
||||
|
||||
this.store = new forms.client()
|
||||
this.connectToForms(spawnForms)
|
||||
|
||||
const server = http.createServer(app);
|
||||
initWebSocket(server);
|
||||
|
||||
const PORT = 80;
|
||||
server.listen(PORT, () => {
|
||||
console.log("\n")
|
||||
console.log(chalk.yellow("**************Parchment****************"))
|
||||
console.log(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
|
||||
console.log(chalk.yellow("***************************************"))
|
||||
console.log("\n")
|
||||
});
|
||||
|
||||
process.on('SIGINT', async () => {
|
||||
console.log(chalk.red('Closing server...'));
|
||||
console.log(chalk.green('Database connection closed.'));
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
Object.preventExtensions(this);
|
||||
}
|
||||
}
|
||||
40
flame/util.js
Normal file
40
flame/util.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import moment from 'moment'
|
||||
import chalk from 'chalk'
|
||||
import paths from 'path'
|
||||
|
||||
import { fileURLToPath } from 'url';
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = paths.dirname(__filename);
|
||||
|
||||
export default class util {
|
||||
|
||||
static APP_PATH = paths.join(__dirname, "..")
|
||||
static CAVE_PATH = paths.join(__dirname, "../cave")
|
||||
static FLAME_PATH = __dirname
|
||||
static FORMS_PATH = paths.join(__dirname, "../forms")
|
||||
|
||||
static logRequest(req, res, next) {
|
||||
const formattedDate = moment().format('M.D');
|
||||
const formattedTime = moment().format('h:mma');
|
||||
if(req.url.includes("/api/")) {
|
||||
console.log(chalk.blue(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
||||
} else {
|
||||
if(req.url === "/")
|
||||
console.log(chalk.gray(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
static logResponse(req, res, next) {
|
||||
const originalSend = res.send;
|
||||
res.send = function (body) {
|
||||
if(res.statusCode >= 400) {
|
||||
console.log(chalk.blue( `<-${chalk.red(res.statusCode)}- ${req.method} ${req.url} | ${chalk.red(body)}`));
|
||||
} else {
|
||||
console.log(chalk.blue(`<-${res.statusCode}- ${req.method} ${req.url}`));
|
||||
}
|
||||
originalSend.call(this, body);
|
||||
};
|
||||
next();
|
||||
}
|
||||
}
|
||||
137
server/index.js
137
server/index.js
@@ -1,137 +0,0 @@
|
||||
import path from 'path';
|
||||
import fs from 'fs/promises'
|
||||
import { fileURLToPath } from 'url';
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = path.dirname(__filename);
|
||||
|
||||
import express from 'express';
|
||||
import cors from 'cors'
|
||||
import http from 'http'
|
||||
import chalk from 'chalk'
|
||||
import moment from 'moment'
|
||||
import * as z from "zod";
|
||||
|
||||
import { initWebSocket } from './ws.js'
|
||||
import getAllDownloadsFiles from "./getDownloads.js"
|
||||
import forms from "forms"
|
||||
|
||||
export default class Server {
|
||||
store;
|
||||
UIPath = path.join(__dirname, '../ui')
|
||||
|
||||
registerRoutes(router) {
|
||||
router.get('/*', this.get)
|
||||
return router
|
||||
}
|
||||
|
||||
index = async (req, res) => {
|
||||
let filePath = path.join(this.UIPath, "index.html");
|
||||
res.sendFile(filePath)
|
||||
return
|
||||
|
||||
let html = await fs.readFile(filePath, 'utf-8');
|
||||
|
||||
let downloads = await getAllDownloadsFiles()
|
||||
const jsonString = JSON.stringify(downloads, null, 2); // pretty-print optional
|
||||
const scriptTag = `<script type="application/json" id="jsonData">\n${jsonString}\n</script>`;
|
||||
|
||||
const headEndRegex = /<\/head\s*>/i;
|
||||
|
||||
const match = html.match(headEndRegex);
|
||||
const insertPos = match.index;
|
||||
let result = html.slice(0, insertPos) + scriptTag + '\n' + html.slice(insertPos);
|
||||
res.type('html').send(result);
|
||||
}
|
||||
|
||||
get = async (req, res) => {
|
||||
let url = req.url
|
||||
let filePath;
|
||||
if(url.startsWith("/_")) {
|
||||
filePath = path.join(this.UIPath, url);
|
||||
} else if(url.includes("75820185")) {
|
||||
filePath = path.join(this.UIPath, url.split("75820185")[1]);
|
||||
} else {
|
||||
filePath = path.join(this.UIPath, "index.html");
|
||||
}
|
||||
|
||||
res.sendFile(filePath);
|
||||
}
|
||||
|
||||
logRequest(req, res, next) {
|
||||
const formattedDate = moment().format('M.D');
|
||||
const formattedTime = moment().format('h:mma');
|
||||
if(req.url.includes("/api/")) {
|
||||
console.log(chalk.blue(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
||||
} else {
|
||||
if(req.url === "/")
|
||||
console.log(chalk.gray(` ${req.method} ${req.url} | ${formattedDate} ${formattedTime}`));
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
logResponse(req, res, next) {
|
||||
const originalSend = res.send;
|
||||
res.send = function (body) {
|
||||
if(res.statusCode >= 400) {
|
||||
console.log(chalk.blue( `<-${chalk.red(res.statusCode)}- ${req.method} ${req.url} | ${chalk.red(body)}`));
|
||||
} else {
|
||||
console.log(chalk.blue(`<-${res.statusCode}- ${req.method} ${req.url}`));
|
||||
}
|
||||
originalSend.call(this, body);
|
||||
};
|
||||
next();
|
||||
}
|
||||
|
||||
async connectToForms() {
|
||||
await this.store.connect()
|
||||
this.store.register("log", z.toJSONSchema(z.object({
|
||||
time: z.string(),
|
||||
host: z.string(),
|
||||
ip: z.string(),
|
||||
path: z.string(),
|
||||
method: z.string()
|
||||
})))
|
||||
this.store.add("log", {
|
||||
time: "0100",
|
||||
host: "parchment.page",
|
||||
ip: "0.0.0.1",
|
||||
path: "/asd",
|
||||
method: "GET"
|
||||
})
|
||||
}
|
||||
|
||||
constructor() {
|
||||
const app = express();
|
||||
app.use(cors({ origin: '*' }));
|
||||
app.use(express.json());
|
||||
|
||||
app.use(this.logRequest);
|
||||
app.use(this.logResponse);
|
||||
|
||||
let router = express.Router();
|
||||
this.registerRoutes(router)
|
||||
app.use('/', router);
|
||||
|
||||
this.store = new forms.client()
|
||||
this.connectToForms()
|
||||
|
||||
const server = http.createServer(app);
|
||||
initWebSocket(server);
|
||||
const PORT = 80;
|
||||
server.listen(PORT, () => {
|
||||
console.log("\n")
|
||||
console.log(chalk.yellow("**************Parchment****************"))
|
||||
console.log(chalk.yellowBright(`Server is running on port ${PORT}: http://localhost`));
|
||||
console.log(chalk.yellow("***************************************"))
|
||||
console.log("\n")
|
||||
});
|
||||
|
||||
process.on('SIGINT', async () => {
|
||||
console.log(chalk.red('Closing server...'));
|
||||
console.log(chalk.green('Database connection closed.'));
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
Object.preventExtensions(this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user