2023-02-28 14:44:10 +01:00
|
|
|
declare global {
|
|
|
|
var debug: boolean;
|
|
|
|
}
|
|
|
|
import express from "express";
|
|
|
|
import https from "https";
|
|
|
|
import http from "http";
|
2023-03-01 16:14:46 +01:00
|
|
|
import nman from "nman";
|
|
|
|
import cors from "cors";
|
|
|
|
import { WebSocketServer } from "ws";
|
2023-02-28 14:44:10 +01:00
|
|
|
import { Command } from "commander";
|
2023-03-05 18:00:03 +01:00
|
|
|
import { generateTag, oConf } from "./sys/config.js";
|
2023-03-01 16:14:46 +01:00
|
|
|
import { error, log } from "./sys/log.js";
|
|
|
|
import { connectToDB } from "./sys/db.js";
|
2023-03-05 18:00:03 +01:00
|
|
|
import bruteforce from "./sys/bruteforce.js";
|
2023-03-01 16:14:46 +01:00
|
|
|
import { fullSetup, partiellSetup } from "./setup/config.js";
|
|
|
|
import { addGetMethods } from "./api/get.js";
|
2023-03-07 16:47:26 +01:00
|
|
|
import { activatePost, addPostMethods } from "./api/post.js";
|
|
|
|
import { activateWS, wsOnConnection } from "./api/ws.js";
|
2023-03-05 18:00:03 +01:00
|
|
|
import { startUpdateCert } from "./server/serverCerts.js";
|
2023-03-08 10:50:28 +01:00
|
|
|
import { wait } from "./sys/tools.js";
|
2023-02-28 14:44:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
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 <path>", "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);
|
2023-03-01 16:14:46 +01:00
|
|
|
if (!oConf.connect(config)) dofullSetup = true;
|
2023-02-28 14:44:10 +01:00
|
|
|
}
|
2023-03-01 16:14:46 +01:00
|
|
|
|
|
|
|
if (setup || dofullSetup) {
|
|
|
|
if (dofullSetup) fullSetup();
|
2023-02-28 14:44:10 +01:00
|
|
|
else partiellSetup();
|
2023-03-01 16:14:46 +01:00
|
|
|
} else {
|
2023-02-28 14:44:10 +01:00
|
|
|
startServer();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
program.parse();
|
|
|
|
|
2023-03-05 18:00:03 +01:00
|
|
|
const activeRequest = false;
|
|
|
|
|
2023-03-01 16:14:46 +01:00
|
|
|
var serverclose = { close: (d: () => void) => d() };
|
|
|
|
nman.addShutdownTask(() => new Promise(async (res, rej) => {
|
|
|
|
//await closeWebSocket();
|
|
|
|
serverclose.close(() => res());
|
|
|
|
}), 30000);
|
2023-02-28 14:44:10 +01:00
|
|
|
|
|
|
|
async function startServer() {
|
|
|
|
await connectToDB();
|
|
|
|
|
|
|
|
const server = express();
|
2023-03-07 16:47:26 +01:00
|
|
|
server.use(express.json({ limit: '1000mb' }));
|
|
|
|
server.use(express.urlencoded({ limit: '1000mb', extended: false }));
|
2023-03-01 16:14:46 +01:00
|
|
|
server.use(cors());
|
2023-02-28 14:44:10 +01:00
|
|
|
server.use(bruteforce);
|
2023-03-01 16:14:46 +01:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
2023-02-28 14:44:10 +01:00
|
|
|
}
|
2023-03-01 16:14:46 +01:00
|
|
|
|
|
|
|
|
2023-03-05 18:00:03 +01:00
|
|
|
async function complete_loaded() {
|
|
|
|
startUpdateCert();
|
2023-03-08 10:50:28 +01:00
|
|
|
await wait(500);
|
2023-03-05 18:00:03 +01:00
|
|
|
let succ = await generateTag();
|
2023-03-08 10:50:28 +01:00
|
|
|
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!");
|
2023-03-07 16:47:26 +01:00
|
|
|
activatePost();
|
|
|
|
activateWS();
|
|
|
|
log("Server", 'Listening...');
|
2023-03-05 18:00:03 +01:00
|
|
|
}
|