renaming, spawning forms if it does not exist, simplifying
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
node_modules
|
node_modules
|
||||||
package-lock.json
|
package-lock.json
|
||||||
master.forms
|
master.forms
|
||||||
|
forms/app.log
|
||||||
99
app.js
99
app.js
@@ -1,17 +1,20 @@
|
|||||||
import { app, BrowserWindow, globalShortcut } from 'electron';
|
import { app, BrowserWindow, globalShortcut } from 'electron';
|
||||||
import paths from 'path'
|
import paths from 'path'
|
||||||
import Server from "./server/index.js"
|
import { spawn } from 'child_process'
|
||||||
|
import fs from 'fs'
|
||||||
|
|
||||||
import { fileURLToPath } from 'url';
|
import util from './flame/util.js'
|
||||||
const __filename = fileURLToPath(import.meta.url);
|
import Server from "./flame/index.js"
|
||||||
const __dirname = paths.dirname(__filename);
|
|
||||||
|
|
||||||
const WINDOW_SHORTCUTS = [
|
const WINDOW_SHORTCUTS = [
|
||||||
process.platform === "darwin" ? "Command+R" : "Control+R",
|
process.platform === "darwin" ? "Command+R" : "Control+R",
|
||||||
"\\"
|
"\\"
|
||||||
];
|
];
|
||||||
|
|
||||||
function createWindow({onTop = false}) {
|
class App {
|
||||||
|
devToolsOpened = false;
|
||||||
|
|
||||||
|
createWindow({onTop = false}) {
|
||||||
const win = new BrowserWindow({
|
const win = new BrowserWindow({
|
||||||
width: 1200,
|
width: 1200,
|
||||||
height: 800,
|
height: 800,
|
||||||
@@ -30,7 +33,7 @@ function createWindow({onTop = false}) {
|
|||||||
win.setAlwaysOnTop(true, 'screen-saver', 1); // necessary so it doesn't bring you back out of full screen when spawned
|
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.loadFile('flame/index.html');
|
||||||
|
|
||||||
win.on("focus", () => {
|
win.on("focus", () => {
|
||||||
win.setVibrancy("appearance-based"); // full colors
|
win.setVibrancy("appearance-based"); // full colors
|
||||||
@@ -53,33 +56,58 @@ function createWindow({onTop = false}) {
|
|||||||
globalShortcut.unregister(key);
|
globalShortcut.unregister(key);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let devToolsOpened = false;
|
spawnFlame(spawnForms) {
|
||||||
|
new Server(spawnForms)
|
||||||
|
}
|
||||||
|
|
||||||
function toggleDevTools(win) {
|
async spawnForms() {
|
||||||
if (devToolsOpened) {
|
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();
|
win.closeDevTools();
|
||||||
} else {
|
} else {
|
||||||
win.openDevTools();
|
win.openDevTools();
|
||||||
}
|
}
|
||||||
devToolsOpened = !devToolsOpened;
|
this.devToolsOpened = !this.devToolsOpened;
|
||||||
}
|
}
|
||||||
|
|
||||||
app.on("ready", async () => {
|
registerShortcuts() {
|
||||||
// await Forms.init();
|
globalShortcut.register('CommandOrControl+Shift+Space', function CreateWindow() {
|
||||||
// createWindow({onTop: false});
|
this.createWindow({onTop: true});
|
||||||
new Server()
|
|
||||||
|
|
||||||
globalShortcut.register('CommandOrControl+Shift+Space', () => {
|
|
||||||
createWindow({onTop: true});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Register global shortcuts
|
app.on("browser-window-focus", function RegisterWindowOnFocus() {
|
||||||
app.on("browser-window-focus", () => {
|
|
||||||
const focused = BrowserWindow.getFocusedWindow();
|
const focused = BrowserWindow.getFocusedWindow();
|
||||||
if (!focused) return;
|
if (!focused) return;
|
||||||
|
|
||||||
|
let WINDOW_SHORTCUTS = [
|
||||||
|
process.platform === "darwin" ? "Command+R" : "Control+R",
|
||||||
|
"\\"
|
||||||
|
];
|
||||||
|
|
||||||
// Reload
|
// Reload
|
||||||
globalShortcut.register(WINDOW_SHORTCUTS[0], () => {
|
globalShortcut.register(WINDOW_SHORTCUTS[0], () => {
|
||||||
focused.reload();
|
focused.reload();
|
||||||
@@ -91,19 +119,30 @@ app.on("ready", async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.on("browser-window-blur", () => {
|
app.on("browser-window-blur", function UnregisterWindowOnUnfocus() {
|
||||||
for (const key of WINDOW_SHORTCUTS) {
|
for (const key of WINDOW_SHORTCUTS) {
|
||||||
globalShortcut.unregister(key);
|
globalShortcut.unregister(key);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
app.on("activate", () => {
|
|
||||||
if (BrowserWindow.getAllWindows().length === 0) {
|
|
||||||
createWindow({onTop: false});
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
app.on('window-all-closed', () => {
|
constructor() {
|
||||||
|
app.on("activate", () => {
|
||||||
|
if (BrowserWindow.getAllWindows().length === 0) {
|
||||||
|
this.createWindow({onTop: false});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.on('window-all-closed', () => {
|
||||||
if (process.platform !== 'darwin') app.quit();
|
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