relay/src/relay.ts

70 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-06-27 17:53:00 +00:00
import WS from "ws";
// @ts-ignore
import DHT from "@hyperswarm/dht";
// @ts-ignore
import { relay } from "@hyperswarm/dht-relay";
// @ts-ignore
import Stream from "@hyperswarm/dht-relay/ws";
import { get as getDHT } from "./dht.js";
// @ts-ignore
import GLE from "greenlock-express";
// @ts-ignore
import Greenlock from "@root/greenlock";
import path from "path";
import { fileURLToPath } from "url";
2022-07-05 19:02:07 +00:00
import config from "./config.js";
2022-06-27 17:53:00 +00:00
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
2022-06-27 21:52:20 +00:00
2022-07-05 19:02:07 +00:00
const sslConfig = {
packageRoot: path.dirname(__dirname),
2022-07-05 20:32:26 +00:00
maintainerEmail: "contact@lumeweb.com",
2022-07-05 19:05:31 +00:00
configDir: path.resolve(config.prefix, "ssl"),
cluster: false,
agreeToTerms: true,
staging: true,
};
2022-06-27 17:53:00 +00:00
export async function start() {
2022-07-05 19:02:07 +00:00
const relayDomain = config.str("relay-domain");
const relayPort = config.str("relay-port");
const greenlock = Greenlock.create(sslConfig);
await greenlock.add({
2022-07-05 19:02:07 +00:00
subject: relayDomain,
altnames: [relayDomain],
2022-06-27 17:53:00 +00:00
});
// @ts-ignore
config.greenlock = greenlock;
2022-07-05 19:02:07 +00:00
GLE.init(sslConfig).ready(async (GLEServer: any) => {
let httpsServer = GLEServer.httpsServer();
var httpServer = GLEServer.httpServer();
2022-06-27 17:53:00 +00:00
await new Promise((resolve) => {
httpServer.listen(80, "0.0.0.0", function () {
console.info("HTTP Listening on ", httpServer.address());
resolve(null);
});
});
2022-06-27 17:53:00 +00:00
const dht = await getDHT();
2022-06-27 17:53:00 +00:00
let wsServer = new WS.Server({ server: httpServer });
2022-06-27 17:53:00 +00:00
wsServer.on("connection", (socket: any) => {
relay(dht, new Stream(false, socket));
});
await new Promise((resolve) => {
2022-07-05 19:02:07 +00:00
httpsServer.listen(relayPort, "0.0.0.0", function () {
console.info("Relay started on ", httpsServer.address());
resolve(null);
});
});
2022-06-27 17:53:00 +00:00
await greenlock.get({
2022-07-05 19:02:07 +00:00
servername: relayDomain,
});
});
2022-06-27 17:53:00 +00:00
}