2023-02-28 14:44:10 +01:00
|
|
|
declare global {
|
|
|
|
var debug: boolean;
|
2023-03-17 23:48:01 +01:00
|
|
|
var provideLog: number;
|
2023-02-28 14:44:10 +01:00
|
|
|
}
|
|
|
|
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-10 16:28:24 +01:00
|
|
|
import { oConf } from "./sys/config.js";
|
|
|
|
import { generateTag } from "./sys/selfTag.js"
|
2023-03-20 21:48:44 +01:00
|
|
|
import { error, log, warn } from "./sys/log.js";
|
2023-03-01 16:14:46 +01:00
|
|
|
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";
|
2023-03-18 11:47:44 +01:00
|
|
|
import { activateWS, closeWebSocket, 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
|
|
|
|
2023-03-17 23:48:01 +01:00
|
|
|
global.provideLog = 10;
|
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.")
|
2023-03-17 23:48:01 +01:00
|
|
|
.option("-l, --log <number>", "Amount of log entires provided in get api! -1 = inifinity")
|
2023-02-28 14:44:10 +01:00
|
|
|
.option("-s, --setup", "Start the Server Setup Tool")
|
2023-03-17 23:48:01 +01:00
|
|
|
.action(({ config, debug, log: provideLog, setup }) => {
|
2023-02-28 14:44:10 +01:00
|
|
|
let dofullSetup = false;
|
|
|
|
global.debug = debug != null;
|
2023-03-17 23:48:01 +01:00
|
|
|
global.provideLog = provideLog != null ? provideLog : 0;
|
2023-03-10 16:28:24 +01:00
|
|
|
if (global.debug) {
|
2023-03-20 21:48:44 +01:00
|
|
|
warn("System", "Starting in debug mode: Server should not be used in production!");
|
2023-03-10 12:44:23 +01:00
|
|
|
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = "0";
|
|
|
|
}
|
2023-02-28 14:44:10 +01:00
|
|
|
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) => {
|
2023-03-18 11:47:44 +01:00
|
|
|
await closeWebSocket();
|
2023-03-01 16:14:46 +01:00
|
|
|
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) => {
|
2023-03-10 12:44:23 +01:00
|
|
|
const HTTPserver = chain != "" ?
|
|
|
|
https.createServer({ key: privkey, cert: cert, ca: chain }, server) :
|
|
|
|
https.createServer({ key: privkey, cert: cert }, server);
|
2023-03-01 16:14:46 +01:00
|
|
|
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() {
|
2023-03-10 16:28:24 +01:00
|
|
|
await 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-20 21:48:44 +01:00
|
|
|
if (!succ) {
|
|
|
|
error("Outbag", "Could not check own Server Tag. Remote-Auth may not work! Check if the Server is reachable and the config ist correct!");
|
|
|
|
if (!global.debug) {
|
|
|
|
await nman.shutdown();
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
warn("Outbag", "Keep running tue to debug Mode");
|
|
|
|
}
|
2023-03-07 16:47:26 +01:00
|
|
|
activatePost();
|
|
|
|
activateWS();
|
|
|
|
log("Server", 'Listening...');
|
2023-03-05 18:00:03 +01:00
|
|
|
}
|