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

chore: respond to hub request with reflexive address (#2736)

Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/2736 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>

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

代码差异

3 个文件 +26 -20
Modified src/controllers/api/hubController.ts +4 -4
@@ -1,7 +1,7 @@
1 1 import type { RequestHandler } from "express";
2 import { getReflexiveAddress } from "../../services/configService.ts";
2 3
3 const hubController: RequestHandler = (_req, res) => {
4 res.json("hub 127.0.0.1:6952");
4 export const hubController: RequestHandler = (req, res) => {
5 const { myAddress } = getReflexiveAddress(req);
6 res.json(`hub ${myAddress}:6952`);
5 7 };
6
7 export { hubController };
Modified src/controllers/api/loginController.ts +2 -16
@@ -1,6 +1,6 @@
1 1 import type { RequestHandler } from "express";
2 2
3 import { config } from "../../services/configService.ts";
3 import { config, getReflexiveAddress } from "../../services/configService.ts";
4 4 import { buildConfig } from "../../services/buildConfigService.ts";
5 5
6 6 import { Account } from "../../models/loginModel.ts";
@@ -20,21 +20,7 @@ export const loginController: RequestHandler = async (request, response) => {
20 20 ? request.query.buildLabel.split(" ").join("+")
21 21 : buildConfig.buildLabel;
22 22
23 let myAddress: string;
24 let myUrlBase: string = request.protocol + "://";
25 if (request.host.indexOf("warframe.com") == -1) {
26 // Client request was redirected cleanly, so we know it can reach us how it's reaching us now.
27 myAddress = request.hostname;
28 myUrlBase += request.host;
29 } else {
30 // Don't know how the client reached us, hoping the config does.
31 myAddress = config.myAddress;
32 myUrlBase += myAddress;
33 const port: number = request.protocol == "http" ? config.httpPort || 80 : config.httpsPort || 443;
34 if (port != (request.protocol == "http" ? 80 : 443)) {
35 myUrlBase += ":" + port;
36 }
37 }
23 const { myAddress, myUrlBase } = getReflexiveAddress(request);
38 24
39 25 if (
40 26 !account &&
Modified src/services/configService.ts +20 -0
@@ -3,6 +3,7 @@ import path from "path";
3 3 import { repoDir } from "../helpers/pathHelper.ts";
4 4 import { args } from "../helpers/commandLineArguments.ts";
5 5 import { Inbox } from "../models/inboxModel.ts";
6 import type { Request } from "express";
6 7
7 8 export interface IConfig {
8 9 mongodbUrl: string;
@@ -165,3 +166,22 @@ export const syncConfigWithDatabase = (): void => {
165 166 void Inbox.deleteMany({ goalTag: "GalleonRobbery" }).then(() => {});
166 167 }
167 168 };
169
170 export const getReflexiveAddress = (request: Request): { myAddress: string; myUrlBase: string } => {
171 let myAddress: string;
172 let myUrlBase: string = request.protocol + "://";
173 if (request.host.indexOf("warframe.com") == -1) {
174 // Client request was redirected cleanly, so we know it can reach us how it's reaching us now.
175 myAddress = request.hostname;
176 myUrlBase += request.host;
177 } else {
178 // Don't know how the client reached us, hoping the config does.
179 myAddress = config.myAddress;
180 myUrlBase += myAddress;
181 const port: number = request.protocol == "http" ? config.httpPort || 80 : config.httpsPort || 443;
182 if (port != (request.protocol == "http" ? 80 : 443)) {
183 myUrlBase += ":" + port;
184 }
185 }
186 return { myAddress, myUrlBase };
187 };