*reorder logic to boot up the relay server only after the SSL is ready

This commit is contained in:
Derrick Hammer 2022-08-24 11:10:50 -04:00
parent 573ebec0a4
commit f89a8e93a4
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 26 additions and 26 deletions

View File

@ -52,16 +52,8 @@ export async function start() {
router(req, res, next);
});
let httpsServer = https.createServer({
SNICallback(servername, cb) {
cb(null, sslCtx);
},
});
let httpServer = http.createServer(app);
cron.schedule("0 * * * *", setupSSl.bind(null, false));
await new Promise((resolve) => {
httpServer.listen(80, "0.0.0.0", function () {
const address = httpServer.address() as AddressInfo;
@ -71,23 +63,6 @@ export async function start() {
});
const dht = await getDHT();
let wsServer = new WS.Server({ server: httpsServer });
wsServer.on("connection", (socket: any) => {
relay(dht, new Stream(false, socket));
});
await new Promise((resolve) => {
httpsServer.listen(relayPort, "0.0.0.0", function () {
const address = httpsServer.address() as AddressInfo;
log.info(
"DHT Relay Server started on ",
`${address.address}:${address.port}`
);
resolve(null);
});
});
const statusCodeServer = http.createServer(function (req, res) {
// @ts-ignore
res.writeHead(req.headers["x-status"] ?? 200, {
@ -108,6 +83,31 @@ export async function start() {
});
await setupSSl(true);
let httpsServer = https.createServer({
SNICallback(servername, cb) {
cb(null, sslCtx);
},
});
let wsServer = new WS.Server({ server: httpsServer });
wsServer.on("connection", (socket: any) => {
relay(dht, new Stream(false, socket));
});
await new Promise((resolve) => {
httpsServer.listen(relayPort, "0.0.0.0", function () {
const address = httpsServer.address() as AddressInfo;
log.info(
"DHT Relay Server started on ",
`${address.address}:${address.port}`
);
resolve(null);
});
});
cron.schedule("0 * * * *", setupSSl.bind(null, false));
}
async function setupSSl(bootup: boolean) {
@ -130,7 +130,7 @@ async function setupSSl(bootup: boolean) {
}, retryOptions);
await promiseRetry(async (retry: any) => {
sslKey = await getSslCert();
sslKey = await getSslKey();
if (!sslKey) {
retry();
}