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-01 16:14:46 +01:00
|
|
|
import { oConf } from "./sys/config.js";
|
|
|
|
import { error, log } from "./sys/log.js";
|
|
|
|
import { connectToDB } from "./sys/db.js";
|
|
|
|
import bruteforce, { suspectRequest } from "./sys/bruteforce.js";
|
|
|
|
import { fullSetup, partiellSetup } from "./setup/config.js";
|
|
|
|
import { addGetMethods } from "./api/get.js";
|
|
|
|
import { addPostMethods } from "./api/post.js";
|
|
|
|
import { wsOnConnection } from "./api/ws.js";
|
|
|
|
import { startUpdateCerts } from "./server/serverCerts.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-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-01 16:14:46 +01:00
|
|
|
server.use(express.json({ limit: '100mb' }));
|
|
|
|
server.use(express.urlencoded({ limit: '100mb', extended: false }));
|
|
|
|
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"), () => {
|
|
|
|
log("Server", 'Listening...');
|
|
|
|
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"), () => {
|
|
|
|
log("Server", 'Listening...');
|
|
|
|
complete_loaded();
|
|
|
|
});
|
|
|
|
}
|
2023-02-28 14:44:10 +01:00
|
|
|
}
|
2023-03-01 16:14:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
function complete_loaded() {
|
|
|
|
startUpdateCerts();
|
|
|
|
}
|