declare global { var debug: boolean; } import express from "express"; import https from "https"; import http from "http"; import nman from "nman"; import cors from "cors"; import { WebSocketServer } from "ws"; import { Command } from "commander"; import { generateTag, oConf } from "./sys/config.js"; import { error, log } from "./sys/log.js"; import { connectToDB } from "./sys/db.js"; import bruteforce from "./sys/bruteforce.js"; import { fullSetup, partiellSetup } from "./setup/config.js"; import { addGetMethods } from "./api/get.js"; import { activatePost, addPostMethods } from "./api/post.js"; import { activateWS, wsOnConnection } from "./api/ws.js"; import { startUpdateCert } from "./server/serverCerts.js"; import { wait } from "./sys/tools.js"; const config = { version: "0.0.1" }; const program = new Command(); program .name("Outbag Server") .description("The default way to host your own Outbag server.") .version(config.version); program .option("-c, --config ", "Start the Outbag server with a config file.") .option("-d, --debug", "Start the Outbag server with more log output.") .option("-s, --setup", "Start the Server Setup Tool") .action(({ config, debug, setup }) => { let dofullSetup = false; global.debug = debug != null; if (config) { log("System", "Starting with config:", config); if (!oConf.connect(config)) dofullSetup = true; } if (setup || dofullSetup) { if (dofullSetup) fullSetup(); else partiellSetup(); } else { startServer(); } }); program.parse(); const activeRequest = false; var serverclose = { close: (d: () => void) => d() }; nman.addShutdownTask(() => new Promise(async (res, rej) => { //await closeWebSocket(); serverclose.close(() => res()); }), 30000); async function startServer() { await connectToDB(); const server = express(); server.use(express.json({ limit: '1000mb' })); server.use(express.urlencoded({ limit: '1000mb', extended: false })); server.use(cors()); server.use(bruteforce); addGetMethods(server); addPostMethods(server); if (oConf.get("ssl", "enable")) { log("Server", "SSL Enabled"); oConf.readPathes(oConf.get("ssl", "privkey"), oConf.get("ssl", "cert"), oConf.get("ssl", "chain")) .then(([privkey, cert, chain]: any) => { const HTTPserver = https.createServer({ key: privkey, cert: cert, ca: chain }, server); const wssServer = new WebSocketServer({ server: HTTPserver }); wssServer.on('connection', wsOnConnection); serverclose = HTTPserver.listen(oConf.get("System", "PORT"), () => { complete_loaded(); }); }).catch(err => { error("Server", err); nman.shutdown(); }); } else { log("Server", "SSL Disabled"); const HTTPserver = http.createServer({}, server); const wssServer = new WebSocketServer({ server: HTTPserver }); wssServer.on('connection', wsOnConnection); serverclose = HTTPserver.listen(oConf.get("System", "PORT"), () => { complete_loaded(); }); } } async function complete_loaded() { startUpdateCert(); await wait(500); let succ = await generateTag(); if(!succ) error("Outbag", "Could not check own Server Tag. Remote-Auth will not work! Check if the Server is reachable and the config ist correct!"); activatePost(); activateWS(); log("Server", 'Listening...'); }