2022-06-27 17:53:00 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import DHT from "@hyperswarm/dht";
|
|
|
|
// @ts-ignore
|
2022-07-19 22:31:15 +00:00
|
|
|
import { relay } from "@hyperswarm/dht-relay";
|
2022-06-27 17:53:00 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import Stream from "@hyperswarm/dht-relay/ws";
|
2022-07-19 22:31:15 +00:00
|
|
|
import express, { Express } from "express";
|
2022-09-21 12:59:22 +00:00
|
|
|
import config from "../config.js";
|
2022-07-19 22:24:53 +00:00
|
|
|
import * as http from "http";
|
|
|
|
import * as https from "https";
|
2022-07-19 22:31:15 +00:00
|
|
|
import { get as getDHT } from "./dht.js";
|
2022-07-19 22:24:53 +00:00
|
|
|
import WS from "ws";
|
|
|
|
// @ts-ignore
|
2022-07-24 00:24:19 +00:00
|
|
|
import log from "loglevel";
|
|
|
|
import { AddressInfo } from "net";
|
2022-08-22 16:22:54 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import promiseRetry from "promise-retry";
|
2022-09-09 09:17:25 +00:00
|
|
|
import { getSslContext } from "./ssl.js";
|
2022-06-27 21:52:20 +00:00
|
|
|
|
2022-09-09 09:17:25 +00:00
|
|
|
export async function start() {
|
2022-07-26 00:35:21 +00:00
|
|
|
const relayPort = config.uint("port");
|
2022-07-19 22:31:15 +00:00
|
|
|
|
|
|
|
const dht = await getDHT();
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-08-14 10:37:00 +00:00
|
|
|
const statusCodeServer = http.createServer(function (req, res) {
|
|
|
|
// @ts-ignore
|
|
|
|
res.writeHead(req.headers["x-status"] ?? 200, {
|
|
|
|
"Content-Type": "text/plain",
|
|
|
|
});
|
|
|
|
res.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
statusCodeServer.listen(25252, "0.0.0.0", function () {
|
|
|
|
const address = statusCodeServer.address() as AddressInfo;
|
|
|
|
log.info(
|
|
|
|
"Status Code Server started on ",
|
|
|
|
`${address.address}:${address.port}`
|
|
|
|
);
|
|
|
|
resolve(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-09 09:17:25 +00:00
|
|
|
let relayServer: https.Server | http.Server;
|
2022-08-24 15:10:50 +00:00
|
|
|
|
2022-09-09 09:17:25 +00:00
|
|
|
if (config.bool("ssl")) {
|
|
|
|
relayServer = https.createServer({
|
|
|
|
SNICallback(servername, cb) {
|
|
|
|
cb(null, getSslContext());
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
relayServer = http.createServer();
|
|
|
|
}
|
2022-08-24 15:10:50 +00:00
|
|
|
|
2022-09-09 09:17:25 +00:00
|
|
|
let wsServer = new WS.Server({ server: relayServer });
|
2022-08-24 15:10:50 +00:00
|
|
|
|
|
|
|
wsServer.on("connection", (socket: any) => {
|
|
|
|
relay(dht, new Stream(false, socket));
|
|
|
|
});
|
|
|
|
|
|
|
|
await new Promise((resolve) => {
|
2022-09-09 09:17:25 +00:00
|
|
|
relayServer.listen(relayPort, "0.0.0.0", function () {
|
|
|
|
const address = relayServer.address() as AddressInfo;
|
2022-08-24 15:10:50 +00:00
|
|
|
log.info(
|
|
|
|
"DHT Relay Server started on ",
|
|
|
|
`${address.address}:${address.port}`
|
|
|
|
);
|
|
|
|
resolve(null);
|
|
|
|
});
|
|
|
|
});
|
2022-07-23 01:09:06 +00:00
|
|
|
}
|