4 Commits
Author SHA1 Message Date
Tau 4298810728 aimedb: Defensively close connection after a Goodbye
We should probably also add request timeouts and such.
2019-11-06 17:28:30 -05:00
Tau 3fea2dae56 allnet.ts: Restrict request filter to PowerOn
PowerOn uses HTTP slightly incorrectly, but other endpoints use
HTTP incorrectly in different ways (or maybe even correctly, who
knows). Scope the current request interceptor to just PowerOn.
2019-11-06 17:28:30 -05:00
Tau 4a2b3f51a4 allnet.ts: Rename from startup.ts 2019-11-06 17:28:30 -05:00
Tau 355f20c50b Add host/port "switchboard" module
Centrally list all hostnames and port numbers here. Need to push
this into IDZ further but I'm a bit short on time (so what else is
new).
2019-11-06 17:28:30 -05:00
7 changed files with 118 additions and 66 deletions
+6 -2
View File
@@ -19,9 +19,13 @@ export default async function aimedb(socket: Socket) {
const req = obj as AimeRequest;
const res = await dispatch(txn, req, now);
if (res !== undefined) {
output.write(res);
if (res === undefined) {
console.log("Aimedb: Closing connection");
break;
}
output.write(res);
}
await txn.commit();
+4 -14
View File
@@ -5,26 +5,16 @@ 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<string, string>();
uris.set("SDBT", `http://${myHost}:9000/`); // Chunithm
uris.set("SBZV", `http://${myHost}:9001/`); // Project Diva Future Tone
const hosts = new Map<string, string>();
hosts.set("SDDF", `${myHost}:10000`); // Initial D Zero
const app = express();
// Startup request is url-encoded-ish... except it's also zlibed and base64ed.
// So in the absence of any exotic Transfer-Encoding headers this Content-Type
// is incorrect and we have to override Express' built-in handling.
app.use(async function(req, res, next) {
app.use("/sys/servlet/PowerOn", async function(req, res, next) {
if (req.method !== "POST") {
return res.status(405).end();
}
@@ -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,
-25
View File
@@ -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;
}
+10 -12
View File
@@ -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`,
};
}
+2 -2
View File
@@ -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);
+12 -11
View File
@@ -6,29 +6,30 @@ import http = require("http");
import net = require("net");
import aimedb from "./aimedb";
import allnet from "./allnet";
import billing from "./billing";
import chunithm from "./chunithm";
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(allnet).listen(Swb.PORT_ALLNET, 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");
+84
View File
@@ -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_ALLNET = 80;
export const PORT_BILLING = 8443;
//
// 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<string, string>();
startupHosts.set("SDDF", `${HOST_EXT}:${PORT_IDZ.USERDB.TCP}`);
const startupUris = new Map<string, string>();
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."
);
}