diff --git a/.gitignore b/.gitignore index 6566c9e..428e6b4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ bin/ node_modules/ pki/ state/ +hostname.txt diff --git a/src/hostname.ts b/src/hostname.ts new file mode 100644 index 0000000..5bbf108 --- /dev/null +++ b/src/hostname.ts @@ -0,0 +1,25 @@ +import { existsSync, readFileSync } from "fs"; +import * as os from "os"; + +const path = "./hostname.txt"; +let myHostname: string; + +if (!existsSync(path)) { + const osHostname = os.hostname(); + + console.log( + `Hostname: ${path} does not exist, sending OS hostname "${osHostname}" to clients. This may be unreliable!` + ); + + myHostname = osHostname; +} else { + myHostname = readFileSync(path, { encoding: "ascii" }).trim(); + + console.log( + `Hostname: Sending configured hostname "${myHostname}" to clients` + ); +} + +export function hostname(): string { + return myHostname; +} diff --git a/src/idz/handler/loadServerList.ts b/src/idz/handler/loadServerList.ts index 2ff78b5..72f2232 100644 --- a/src/idz/handler/loadServerList.ts +++ b/src/idz/handler/loadServerList.ts @@ -1,8 +1,7 @@ -import { hostname } from "os"; - import { LoadServerListRequest } from "../request/loadServerList"; import { LoadServerListResponse } from "../response/loadServerList"; import { World } from "../world"; +import { hostname } from "../../hostname"; export function loadServerList( w: World, diff --git a/src/startup.ts b/src/startup.ts index 56324c3..1ae8cd9 100644 --- a/src/startup.ts +++ b/src/startup.ts @@ -2,7 +2,8 @@ import express = require("express"); import read = require("raw-body"); import { unzipSync } from "zlib"; -import { hostname } from "os"; + +import { hostname } from "./hostname"; const myHost = hostname(); const uris = new Map();