diff --git a/src/hostname.ts b/src/hostname.ts deleted file mode 100644 index 5bbf108..0000000 --- a/src/hostname.ts +++ /dev/null @@ -1,25 +0,0 @@ -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 aa315b6..3fd3300 100644 --- a/src/idz/handler/loadServerList.ts +++ b/src/idz/handler/loadServerList.ts @@ -1,23 +1,21 @@ import { LoadServerListRequest } from "../request/loadServerList"; import { LoadServerListResponse } from "../response/loadServerList"; import { Repositories } from "../repo"; -import { hostname } from "../../hostname"; +import { HOST_EXT, PORT_IDZ } from "../../switchboard"; export function loadServerList( w: Repositories, req: LoadServerListRequest ): LoadServerListResponse { - const myHost = hostname(); - return { type: "load_server_list_res", status: 1, userDb: { - addr: myHost, + addr: HOST_EXT, tcp: 10000, http: 10001, }, - matchAddr: myHost, + matchAddr: HOST_EXT, matchPort: { tcp: 10002, udpSend: 10003, @@ -29,23 +27,23 @@ export function loadServerList( udpRecv: 10007, }, event: { - addr: myHost, + addr: HOST_EXT, tcp: 10008, }, screenshot: { - addr: myHost, + addr: HOST_EXT, tcp: 10009, }, - pingReturn: myHost, + pingReturn: HOST_EXT, echo1: { - addr: myHost, + addr: HOST_EXT, udp: 10010, }, echo2: { - addr: myHost, + addr: HOST_EXT, udp: 10011, }, - newsUrl: `http://${myHost}:10012/news`, - reportErrorUrl: `http://${myHost}:10013/error`, + newsUrl: `http://${HOST_EXT}:10012/news`, + reportErrorUrl: `http://${HOST_EXT}:10013/error`, }; } diff --git a/src/idz/ping.ts b/src/idz/ping.ts index d220331..921c8c9 100644 --- a/src/idz/ping.ts +++ b/src/idz/ping.ts @@ -1,9 +1,9 @@ import { createSocket } from "dgram"; -export default function createPing(port: number) { +export default function createPing(port: number, host: string) { const socket = createSocket("udp4"); - socket.bind(port); + socket.bind(port, host); socket.on("message", (msg, rinfo) => { console.log(`Idz Ping: Ping from ${rinfo.address}:${rinfo.port}`); socket.send(msg, rinfo.port, rinfo.address); diff --git a/src/index.ts b/src/index.ts index 3bb08cb..12288a7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,23 +12,24 @@ import diva from "./diva"; import idz from "./idz"; import idzPing from "./idz/ping"; import startup from "./startup"; +import * as Swb from "./switchboard"; const tls = { cert: fs.readFileSync("pki/server.pem"), key: fs.readFileSync("pki/server.key"), }; -net.createServer(aimedb).listen(22345); -http.createServer(startup).listen(80); -https.createServer(tls, billing).listen(8443); +net.createServer(aimedb).listen(Swb.PORT_AIMEDB, Swb.HOST_INT); +http.createServer(startup).listen(Swb.PORT_STARTUP, Swb.HOST_INT); +https.createServer(tls, billing).listen(Swb.PORT_BILLING, Swb.HOST_INT); -http.createServer(chunithm).listen(9000); -http.createServer(diva).listen(9001); +http.createServer(chunithm).listen(Swb.PORT_CHUNITHM, Swb.HOST_INT); +http.createServer(diva).listen(Swb.PORT_DIVA, Swb.HOST_INT); -net.createServer(idz).listen(10000); -idzPing(10001); -idzPing(10003); -idzPing(10010); -idzPing(10011); +net.createServer(idz).listen(Swb.PORT_IDZ.USERDB.TCP, Swb.HOST_INT); +idzPing(10001, Swb.HOST_INT); // ?? tbd +idzPing(Swb.PORT_IDZ.MATCH.UDP_SEND, Swb.HOST_INT); +idzPing(Swb.PORT_IDZ.ECHO1, Swb.HOST_INT); +idzPing(Swb.PORT_IDZ.ECHO2, Swb.HOST_INT); console.log("Startup OK"); diff --git a/src/startup.ts b/src/startup.ts index 114625b..baf494e 100644 --- a/src/startup.ts +++ b/src/startup.ts @@ -5,19 +5,9 @@ import read = require("raw-body"); import { unzipSync } from "zlib"; -import { hostname } from "./hostname"; +import { startupHost, startupUri } from "./switchboard"; const hourDelta = parseInt(process.env.HOUR_DELTA || "0"); -const myHost = hostname(); -const uris = new Map(); - -uris.set("SDBT", `http://${myHost}:9000/`); // Chunithm -uris.set("SBZV", `http://${myHost}:9001/`); // Project Diva Future Tone - -const hosts = new Map(); - -hosts.set("SDDF", `${myHost}:10000`); // Initial D Zero - const app = express(); // Startup request is url-encoded-ish... except it's also zlibed and base64ed. @@ -80,8 +70,8 @@ app.post("/sys/servlet/PowerOn", function(req, resp) { const resParams = { stat: 1, - uri: uris.get(req.body.game_id) || "", - host: hosts.get(req.body.game_id) || "", + uri: startupUri(req.body.game_id), + host: startupHost(req.body.game_id), place_id: "123", name: process.env.SHOP_NAME, nickname: process.env.SHOP_NAME, diff --git a/src/switchboard.ts b/src/switchboard.ts new file mode 100644 index 0000000..9e178c5 --- /dev/null +++ b/src/switchboard.ts @@ -0,0 +1,84 @@ +import * as os from "os"; + +const cfgHostExt: string | undefined = process.env.HOST_EXT; +const cfgHostInt: string | undefined = process.env.HOST_INT; + +export const HOST_EXT = cfgHostExt !== undefined ? cfgHostExt : os.hostname(); +export const HOST_INT = cfgHostInt !== undefined ? cfgHostInt : "127.0.0.1"; + +// +// Core services. These ports cannot be changed +// + +export const PORT_AIMEDB = 22345; +export const PORT_BILLING = 8443; +export const PORT_STARTUP = 80; + +// +// Title services. These can be freely chosen. +// + +export const PORT_CHUNITHM = 9001; +export const PORT_DIVA = 9000; +export const PORT_IDZ = { + // Most of these seem to be unused relics + + USERDB: { + TCP: 10000, + HTTP: 10001, + }, + MATCH: { + TCP: 10002, + UDP_SEND: 10003, + UDP_RECV: 10004, + }, + TAG_MATCH: { + TCP: 10005, + UDP_SEND: 10006, + UDP_RECV: 10007, + }, + EVENT: 10008, + SCREENSHOT: 10009, + ECHO1: 10010, + ECHO2: 10011, +}; + +// +// Startup responses +// + +const startupHosts = new Map(); + +startupHosts.set("SDDF", `${HOST_EXT}:${PORT_IDZ.USERDB.TCP}`); + +const startupUris = new Map(); + +startupUris.set("SDBT", `http://${HOST_EXT}:${PORT_CHUNITHM}/`); +startupUris.set("SBZV", `http://${HOST_EXT}:${PORT_DIVA}/`); + +export function startupHost(model: string): string { + const val = startupHosts.get(model); + + return val !== undefined ? val : ""; +} + +export function startupUri(model: string): string { + const val = startupUris.get(model); + + return val !== undefined ? val : ""; +} + +// +// Diagnostic dump +// + +console.log( + `Switchboard: HOST_EXT: ${HOST_EXT} (Service host name sent to clients)` +); +console.log(`Switchboard: HOST_INT: ${HOST_INT} (Bind address)`); + +if (cfgHostExt === undefined || cfgHostInt === undefined) { + console.log( + "Switchboard: Warning: Check .env and env vars! Using unreliable fallback." + ); +}