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";
|
2022-07-04 21:22:48 +00:00
|
|
|
// @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
|
|
|
|
2022-07-04 21:22:48 +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 = {
|
2022-07-04 21:22:48 +00:00
|
|
|
packageRoot: path.dirname(__dirname),
|
|
|
|
configDir: path.resolve(__dirname, "../", "./data/greenlock.d/"),
|
|
|
|
cluster: false,
|
|
|
|
agreeToTerms: true,
|
|
|
|
staging: true,
|
|
|
|
};
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-04 21:22:48 +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);
|
2022-07-04 21:22:48 +00:00
|
|
|
await greenlock.add({
|
2022-07-05 19:02:07 +00:00
|
|
|
subject: relayDomain,
|
|
|
|
altnames: [relayDomain],
|
2022-06-27 17:53:00 +00:00
|
|
|
});
|
2022-07-04 21:22:48 +00:00
|
|
|
// @ts-ignore
|
|
|
|
config.greenlock = greenlock;
|
2022-07-05 19:02:07 +00:00
|
|
|
GLE.init(sslConfig).ready(async (GLEServer: any) => {
|
2022-07-04 21:22:48 +00:00
|
|
|
let httpsServer = GLEServer.httpsServer();
|
|
|
|
var httpServer = GLEServer.httpServer();
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-04 21:22:48 +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
|
|
|
|
2022-07-04 21:22:48 +00:00
|
|
|
const dht = await getDHT();
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-04 21:22:48 +00:00
|
|
|
let wsServer = new WS.Server({ server: httpServer });
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-04 21:22:48 +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 () {
|
2022-07-04 21:22:48 +00:00
|
|
|
console.info("Relay started on ", httpsServer.address());
|
|
|
|
resolve(null);
|
|
|
|
});
|
|
|
|
});
|
2022-06-27 17:53:00 +00:00
|
|
|
|
2022-07-04 21:22:48 +00:00
|
|
|
await greenlock.get({
|
2022-07-05 19:02:07 +00:00
|
|
|
servername: relayDomain,
|
2022-07-04 21:22:48 +00:00
|
|
|
});
|
|
|
|
});
|
2022-06-27 17:53:00 +00:00
|
|
|
}
|