返回提交历史
Modified
config-vanilla.json
+2
-0
Modified
src/services/configService.ts
+9
-3
Modified
src/services/configWatcherService.ts
+12
-11
Modified
src/services/webService.ts
+15
-26
XFEstudio/XFESpaceNinjaServer
feat: httpsCertFile & httpsKeyFile configs (#3317)
Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/3317 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>
29b092ff
代码差异
4 个文件
+38
-40
@@ -8,6 +8,8 @@
8
8
"bindAddress": "0.0.0.0",
9
9
"httpPort": 80,
10
10
"httpsPort": 443,
11
"httpsCertFile": "static/cert/cert.pem",
12
"httpsKeyFile": "static/cert/key.pem",
11
13
"ircExecutable": null,
12
14
"ircAddress": null,
13
15
"hubExecutable": null,
@@ -16,6 +16,8 @@ export interface IConfig {
16
16
bindAddress?: string;
17
17
httpPort?: number;
18
18
httpsPort?: number;
19
httpsCertFile?: string;
20
httpsKeyFile?: string;
19
21
ircExecutable?: string;
20
22
ircAddress?: string;
21
23
hubExecutable?: string;
@@ -244,16 +246,20 @@ export const getReflexiveAddress = (request: Request): { myAddress: string; myUr
244
246
return { myAddress, myUrlBase };
245
247
};
246
248
247
export interface IBindings {
249
export interface IWebServerParams {
248
250
address: string;
249
251
httpPort: number;
250
252
httpsPort: number;
253
certFile: string;
254
keyFile: string;
251
255
}
252
256
253
export const configGetWebBindings = (): IBindings => {
257
export const getWebServerParams = (): IWebServerParams => {
254
258
return {
255
259
address: config.bindAddress || "0.0.0.0",
256
260
httpPort: config.httpPort || 80,
257
httpsPort: config.httpsPort || 443
261
httpsPort: config.httpsPort || 443,
262
certFile: config.httpsCertFile || "static/cert/cert.pem",
263
keyFile: config.httpsKeyFile || "static/cert/key.pem"
258
264
};
259
265
};
@@ -2,15 +2,15 @@ import chokidar from "chokidar";
2
2
import { logger } from "../utils/logger.ts";
3
3
import {
4
4
config,
5
configGetWebBindings,
6
5
configPath,
7
6
configRemovedOptionsKeys,
7
getWebServerParams,
8
8
loadConfig,
9
9
syncConfigWithDatabase,
10
10
type IConfig
11
11
} from "./configService.ts";
12
12
import { saveConfig, shouldReloadConfig } from "./configWriterService.ts";
13
import { getWebBindings, startWebServer, stopWebServer } from "./webService.ts";
13
import { startWebServer, stopWebServer } from "./webService.ts";
14
14
import { forEachWsClient, sendWsBroadcast, type IWsMsgToClient } from "./wsService.ts";
15
15
import varzia from "../constants/varzia.ts";
16
16
import { getTunablesForClient } from "./tunablesService.ts";
@@ -18,6 +18,7 @@ import { getTunablesForClient } from "./tunablesService.ts";
18
18
chokidar.watch(configPath).on("change", () => {
19
19
if (shouldReloadConfig()) {
20
20
const prevTunables = JSON.stringify(config.tunables);
21
const prevWebParams = JSON.stringify(getWebServerParams());
21
22
22
23
logger.info("Detected a change to config file, reloading its contents.");
23
24
try {
@@ -42,19 +43,19 @@ chokidar.watch(configPath).on("change", () => {
42
43
});
43
44
}
44
45
45
const configBindings = configGetWebBindings();
46
const bindings = getWebBindings();
47
if (
48
configBindings.address != bindings.address ||
49
configBindings.httpPort != bindings.httpPort ||
50
configBindings.httpsPort != bindings.httpsPort
51
) {
52
logger.info(`Restarting web server to apply binding changes.`);
46
if (JSON.stringify(getWebServerParams()) != prevWebParams) {
47
logger.info(`Restarting web server to apply changes.`);
53
48
54
49
// Tell webui clients to reload with new port
55
50
sendWsBroadcast({ ports: { http: config.httpPort, https: config.httpsPort } });
56
51
57
void stopWebServer().then(startWebServer);
52
void stopWebServer().then(() => {
53
try {
54
startWebServer();
55
} catch (e) {
56
logger.error(`Could not start up web server again: ${(e as Error).message}`);
57
}
58
});
58
59
} else {
59
60
sendWsBroadcast({ config_reloaded: true });
60
61
}
@@ -1,51 +1,48 @@
1
1
import http from "http";
2
2
import https from "https";
3
3
import fs from "node:fs";
4
import { configGetWebBindings, type IBindings } from "./configService.ts";
4
import { getWebServerParams } from "./configService.ts";
5
5
import { logger } from "../utils/logger.ts";
6
6
import { app } from "../app.ts";
7
import type { AddressInfo } from "node:net";
8
7
import { Agent, WebSocket as UnidiciWebSocket } from "undici";
9
8
import { startWsServer, startWssServer, stopWsServers } from "./wsService.ts";
10
9
11
10
let httpServer: http.Server | undefined;
12
11
let httpsServer: https.Server | undefined;
13
12
14
const tlsOptions = {
15
key: fs.readFileSync("static/cert/key.pem"),
16
cert: fs.readFileSync("static/cert/cert.pem")
17
};
18
19
13
export const startWebServer = (): void => {
20
const bindings = configGetWebBindings();
14
const params = getWebServerParams();
15
16
const tlsOptions = {
17
cert: fs.readFileSync(params.certFile),
18
key: fs.readFileSync(params.keyFile)
19
};
21
20
22
// eslint-disable-next-line @typescript-eslint/no-misused-promises
23
21
httpServer = http.createServer(app);
24
httpServer.listen(bindings.httpPort, bindings.address, () => {
22
httpServer.listen(params.httpPort, params.address, () => {
25
23
startWsServer(httpServer!);
26
24
27
logger.info(`HTTP server started on ${bindings.address}:${bindings.httpPort}`);
25
logger.info(`HTTP server started on ${params.address}:${params.httpPort}`);
28
26
29
// eslint-disable-next-line @typescript-eslint/no-misused-promises
30
27
httpsServer = https.createServer(tlsOptions, app);
31
httpsServer.listen(bindings.httpsPort, bindings.address, () => {
28
httpsServer.listen(params.httpsPort, params.address, () => {
32
29
startWssServer(httpsServer!);
33
30
34
logger.info(`HTTPS server started on ${bindings.address}:${bindings.httpsPort}`);
31
logger.info(`HTTPS server started on ${params.address}:${params.httpsPort}`);
35
32
36
33
logger.info(
37
34
"Access the WebUI in your browser at http://localhost" +
38
(bindings.httpPort == 80 ? "" : ":" + bindings.httpPort)
35
(params.httpPort == 80 ? "" : ":" + params.httpPort)
39
36
);
40
37
41
void runWsSelfTest("wss", bindings.httpsPort).then(ok => {
38
void runWsSelfTest("wss", params.httpsPort).then(ok => {
42
39
if (!ok) {
43
40
logger.warn(
44
`WSS self-test failed. The server may not be reachable locally on port ${bindings.httpsPort}.`
41
`WSS self-test failed. The server may not be reachable locally on port ${params.httpsPort}.`
45
42
);
46
43
if (process.platform == "win32") {
47
44
logger.warn(
48
`You can check who has that port via powershell: Get-Process -Id (Get-NetTCPConnection -LocalPort ${bindings.httpsPort}).OwningProcess`
45
`You can check who has that port via powershell: Get-Process -Id (Get-NetTCPConnection -LocalPort ${params.httpsPort}).OwningProcess`
49
46
);
50
47
}
51
48
}
@@ -82,14 +79,6 @@ const runWsSelfTest = (protocol: "ws" | "wss", port: number): Promise<boolean> =
82
79
});
83
80
};
84
81
85
export const getWebBindings = (): Partial<IBindings> => {
86
return {
87
address: (httpServer?.address() as AddressInfo | undefined)?.address,
88
httpPort: (httpServer?.address() as AddressInfo | undefined)?.port,
89
httpsPort: (httpsServer?.address() as AddressInfo | undefined)?.port
90
};
91
};
92
93
82
export const stopWebServer = async (): Promise<void> => {
94
83
const promises: Promise<void>[] = [];
95
84
if (httpServer) {