Files
UDN/index.ts
T

136 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-07-05 19:53:19 +02:00
#!/usr/bin/env bun
2024-07-05 12:48:42 +02:00
import {
2024-07-05 16:04:12 +02:00
connectServers,
2024-07-05 12:48:42 +02:00
forgetConnection,
2024-07-07 18:05:31 +02:00
getWebSocketStats,
2024-07-05 12:48:42 +02:00
processMessage,
2024-07-07 18:16:28 +02:00
trackConnection,
2024-07-05 12:48:42 +02:00
} from "./websocket-handler";
2024-07-08 02:00:39 +02:00
import {
getVersionNumber,
writeStatNumber,
writeStatString,
writeSuccess,
} from "./utility";
2024-07-04 17:27:46 +02:00
2024-07-05 12:48:42 +02:00
import Colors from "colors";
2024-07-05 13:17:19 +02:00
import IP from "ip";
2024-07-04 16:11:02 +02:00
import Path from "path";
2024-07-05 19:58:42 +02:00
import { ServerWebSocket } from "bun";
2024-07-05 16:04:12 +02:00
import { getConfig } from "./config-handler";
2024-07-04 16:11:02 +02:00
// CONFIG
2024-07-07 18:20:37 +02:00
export const CODE_DIR = Path.dirname(import.meta.path);
export const STATIC_DIR = Path.join(CODE_DIR, "static/dist");
export const PACKAGE_FILE = Path.join(CODE_DIR, "package.json");
export const DEFAULT_PAGE = "index.html";
2024-07-04 16:11:02 +02:00
2024-07-04 17:27:46 +02:00
// TYPES
2024-07-05 16:04:12 +02:00
export type WS = ServerWebSocket<unknown> | WebSocket;
// MAIN
async function main() {
// CONFIG
const config = await getConfig();
2024-07-07 18:20:37 +02:00
const versionNumber = await getVersionNumber();
2024-07-05 16:04:12 +02:00
// SERVER
const server = Bun.serve({
port: config.port,
fetch(request, server) {
// allow websockets
server.upgrade(request);
// respond
const requestPath = new URL(request.url).pathname;
switch (requestPath) {
case "/":
2024-07-07 18:16:28 +02:00
return createFileResponse(DEFAULT_PAGE);
2024-07-05 16:49:08 +02:00
case "/stats":
2024-07-07 18:05:31 +02:00
return new Response(JSON.stringify(getAllStats()));
2024-07-05 16:04:12 +02:00
default:
return createFileResponse(requestPath);
}
2024-07-04 17:27:46 +02:00
},
2024-07-05 16:04:12 +02:00
websocket: {
2024-07-07 18:05:31 +02:00
open(ws) {
2024-07-07 18:16:28 +02:00
trackConnection(ws);
2024-07-05 16:04:12 +02:00
},
message(ws, message) {
processMessage(ws, message.toString(), config);
},
close(ws) {
forgetConnection(ws);
},
2024-07-04 16:18:40 +02:00
},
2024-07-08 02:00:39 +02:00
tls: await (async () => {
const keyFile = Bun.file("./key.pem");
const certFile = Bun.file("./cert.pem");
if (
(await keyFile.exists()) == false ||
(await certFile.exists()) == false
)
return undefined;
return {
key: keyFile,
cert: certFile,
passphrase: process.env.TLS_PASSPHRASE,
};
})(),
2024-07-05 16:04:12 +02:00
});
2024-07-04 16:11:02 +02:00
2024-07-05 16:04:12 +02:00
function createFileResponse(requestPath: string): Response {
2024-07-07 18:16:28 +02:00
const fullPath = Path.join(STATIC_DIR, requestPath);
2024-07-05 16:04:12 +02:00
return new Response(Bun.file(fullPath));
}
// OTHER SERVERS
connectServers(config);
2024-07-07 18:05:31 +02:00
// AUDIT
function getServerStats(): [string, string][] {
return [
["server url", server.url.toString()],
["server ip address", IP.address()],
2024-07-08 02:00:39 +02:00
["server version", versionNumber],
2024-07-07 18:05:31 +02:00
];
}
function getAllStats(): [string, string | number][] {
return [...getServerStats(), ...getWebSocketStats()];
}
2024-07-05 16:04:12 +02:00
// CLI
writeSuccess(`###\nstarted up ${new Date().toLocaleString()}`);
function updateCLI() {
console.clear();
2024-07-07 18:20:37 +02:00
2024-07-05 16:04:12 +02:00
console.log(Colors.bold.bgWhite("UNIVERSAL DECENTRALIZED NETWORK"));
2024-07-07 18:20:37 +02:00
console.log(
Colors.yellow("relevant output is available in the 'logs' file")
);
2024-07-05 16:04:12 +02:00
console.log();
2024-07-04 16:11:02 +02:00
2024-07-05 16:04:12 +02:00
console.log(new Date().toLocaleString());
console.log();
2024-07-05 13:17:19 +02:00
2024-07-07 18:16:28 +02:00
getServerStats().forEach((entry) => writeStatString(...entry));
console.log();
2024-07-07 18:20:37 +02:00
2024-07-07 18:16:28 +02:00
getWebSocketStats().forEach((entry) => writeStatNumber(...entry));
2024-07-05 16:04:12 +02:00
console.log();
}
2024-07-05 13:17:19 +02:00
2024-07-07 18:16:28 +02:00
setTimeout(updateCLI, 1000);
2024-07-05 16:04:12 +02:00
setInterval(updateCLI, 5000);
2024-07-05 12:48:42 +02:00
}
2024-07-05 16:04:12 +02:00
main();