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
|
|
|
import { RELAY_DOMAIN, RELAY_PORT } from "./constants.js";
|
|
|
|
// @ts-ignore
|
|
|
|
import GLE from "greenlock-express";
|
|
|
|
// @ts-ignore
|
|
|
|
import Greenlock from "@root/greenlock";
|
|
|
|
import path from "path";
|
|
|
|
import { fileURLToPath } from "url";
|
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-04 21:22:48 +00:00
|
|
|
const config = {
|
|
|
|
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() {
|
|
|
|
const greenlock = Greenlock.create(config);
|
|
|
|
await greenlock.add({
|
|
|
|
subject: RELAY_DOMAIN,
|
|
|
|
altnames: [RELAY_DOMAIN],
|
2022-06-27 17:53:00 +00:00
|
|
|
});
|
2022-07-04 21:22:48 +00:00
|
|
|
// @ts-ignore
|
|
|
|
config.greenlock = greenlock;
|
|
|
|
GLE.init(config).ready(async (GLEServer: any) => {
|
|
|
|
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) => {
|
|
|
|
httpsServer.listen(RELAY_PORT, "0.0.0.0", function () {
|
|
|
|
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({
|
|
|
|
servername: RELAY_DOMAIN,
|
|
|
|
});
|
|
|
|
});
|
2022-06-27 17:53:00 +00:00
|
|
|
}
|