XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

SpaceNinjaServer

A simple server for a small space ninja game

公开
关注 0 Fork 1 Star 0
返回提交历史

XFEstudio/SpaceNinjaServer

feat: bindAddress (#2766)

so people can limit the server to only be reachable via 127.0.0.1 etc Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/2766 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>

5a9415ae
Sainan <63328889+Sainan@users.noreply.github.com>
提交于

代码差异

4 个文件 +43 -18
Modified config-vanilla.json +1 -0
@@ -5,6 +5,7 @@
5 5 "level": "trace"
6 6 },
7 7 "myAddress": "localhost",
8 "bindAddress": "0.0.0.0",
8 9 "httpPort": 80,
9 10 "httpsPort": 443,
10 11 "administratorNames": [],
Modified src/services/configService.ts +15 -0
@@ -12,6 +12,7 @@ export interface IConfig {
12 12 level: string; // "fatal" | "error" | "warn" | "info" | "http" | "debug" | "trace";
13 13 };
14 14 myAddress: string;
15 bindAddress?: string;
15 16 httpPort?: number;
16 17 httpsPort?: number;
17 18 ircAddress?: string;
@@ -188,3 +189,17 @@ export const getReflexiveAddress = (request: Request): { myAddress: string; myUr
188 189 }
189 190 return { myAddress, myUrlBase };
190 191 };
192
193 export interface IBindings {
194 address: string;
195 httpPort: number;
196 httpsPort: number;
197 }
198
199 export const configGetWebBindings = (): IBindings => {
200 return {
201 address: config.bindAddress || "0.0.0.0",
202 httpPort: config.httpPort || 80,
203 httpsPort: config.httpsPort || 443
204 };
205 };
Modified src/services/configWatcherService.ts +10 -4
@@ -2,6 +2,7 @@ import chokidar from "chokidar";
2 2 import { logger } from "../utils/logger.ts";
3 3 import {
4 4 config,
5 configGetWebBindings,
5 6 configPath,
6 7 configRemovedOptionsKeys,
7 8 loadConfig,
@@ -9,7 +10,7 @@ import {
9 10 type IConfig
10 11 } from "./configService.ts";
11 12 import { saveConfig, shouldReloadConfig } from "./configWriterService.ts";
12 import { getWebPorts, startWebServer, stopWebServer } from "./webService.ts";
13 import { getWebBindings, startWebServer, stopWebServer } from "./webService.ts";
13 14 import { sendWsBroadcast } from "./wsService.ts";
14 15 import varzia from "../../static/fixed_responses/worldState/varzia.json" with { type: "json" };
15 16
@@ -25,9 +26,14 @@ chokidar.watch(configPath).on("change", () => {
25 26 validateConfig();
26 27 syncConfigWithDatabase();
27 28
28 const webPorts = getWebPorts();
29 if (config.httpPort != webPorts.http || config.httpsPort != webPorts.https) {
30 logger.info(`Restarting web server to apply port changes.`);
29 const configBindings = configGetWebBindings();
30 const bindings = getWebBindings();
31 if (
32 configBindings.address != bindings.address ||
33 configBindings.httpPort != bindings.httpPort ||
34 configBindings.httpsPort != bindings.httpsPort
35 ) {
36 logger.info(`Restarting web server to apply binding changes.`);
31 37
32 38 // Tell webui clients to reload with new port
33 39 sendWsBroadcast({ ports: { http: config.httpPort, https: config.httpsPort } });
Modified src/services/webService.ts +17 -14
@@ -1,7 +1,7 @@
1 1 import http from "http";
2 2 import https from "https";
3 3 import fs from "node:fs";
4 import { config } from "./configService.ts";
4 import { configGetWebBindings, type IBindings } from "./configService.ts";
5 5 import { logger } from "../utils/logger.ts";
6 6 import { app } from "../app.ts";
7 7 import type { AddressInfo } from "node:net";
@@ -17,33 +17,35 @@ const tlsOptions = {
17 17 };
18 18
19 19 export const startWebServer = (): void => {
20 const httpPort = config.httpPort || 80;
21 const httpsPort = config.httpsPort || 443;
20 const bindings = configGetWebBindings();
22 21
23 22 // eslint-disable-next-line @typescript-eslint/no-misused-promises
24 23 httpServer = http.createServer(app);
25 httpServer.listen(httpPort, () => {
24 httpServer.listen(bindings.httpPort, bindings.address, () => {
26 25 startWsServer(httpServer!);
27 26
28 logger.info("HTTP server started on port " + httpPort);
27 logger.info(`HTTP server started on ${bindings.address}:${bindings.httpPort}`);
29 28
30 29 // eslint-disable-next-line @typescript-eslint/no-misused-promises
31 30 httpsServer = https.createServer(tlsOptions, app);
32 httpsServer.listen(httpsPort, () => {
31 httpsServer.listen(bindings.httpsPort, bindings.address, () => {
33 32 startWssServer(httpsServer!);
34 33
35 logger.info("HTTPS server started on port " + httpsPort);
34 logger.info(`HTTPS server started on ${bindings.address}:${bindings.httpsPort}`);
36 35
37 36 logger.info(
38 "Access the WebUI in your browser at http://localhost" + (httpPort == 80 ? "" : ":" + httpPort)
37 "Access the WebUI in your browser at http://localhost" +
38 (bindings.httpPort == 80 ? "" : ":" + bindings.httpPort)
39 39 );
40 40
41 void runWsSelfTest("wss", httpsPort).then(ok => {
41 void runWsSelfTest("wss", bindings.httpsPort).then(ok => {
42 42 if (!ok) {
43 logger.warn(`WSS self-test failed. The server may not actually be reachable at port ${httpsPort}.`);
43 logger.warn(
44 `WSS self-test failed. The server may not be reachable locally on port ${bindings.httpsPort}.`
45 );
44 46 if (process.platform == "win32") {
45 47 logger.warn(
46 `You can check who actually has that port via powershell: Get-Process -Id (Get-NetTCPConnection -LocalPort ${httpsPort}).OwningProcess`
48 `You can check who has that port via powershell: Get-Process -Id (Get-NetTCPConnection -LocalPort ${bindings.httpsPort}).OwningProcess`
47 49 );
48 50 }
49 51 }
@@ -80,10 +82,11 @@ const runWsSelfTest = (protocol: "ws" | "wss", port: number): Promise<boolean> =
80 82 });
81 83 };
82 84
83 export const getWebPorts = (): Record<"http" | "https", number | undefined> => {
85 export const getWebBindings = (): Partial<IBindings> => {
84 86 return {
85 http: (httpServer?.address() as AddressInfo | undefined)?.port,
86 https: (httpsServer?.address() as AddressInfo | undefined)?.port
87 address: (httpServer?.address() as AddressInfo | undefined)?.address,
88 httpPort: (httpServer?.address() as AddressInfo | undefined)?.port,
89 httpsPort: (httpsServer?.address() as AddressInfo | undefined)?.port
87 90 };
88 91 };
89 92