diff --git a/src/controllers/api/hubController.ts b/src/controllers/api/hubController.ts index 41e17a84..bc0129e5 100644 --- a/src/controllers/api/hubController.ts +++ b/src/controllers/api/hubController.ts @@ -1,7 +1,7 @@ import type { RequestHandler } from "express"; +import { getReflexiveAddress } from "../../services/configService.ts"; -const hubController: RequestHandler = (_req, res) => { - res.json("hub 127.0.0.1:6952"); +export const hubController: RequestHandler = (req, res) => { + const { myAddress } = getReflexiveAddress(req); + res.json(`hub ${myAddress}:6952`); }; - -export { hubController }; diff --git a/src/controllers/api/loginController.ts b/src/controllers/api/loginController.ts index 9d05d532..bff89eb7 100644 --- a/src/controllers/api/loginController.ts +++ b/src/controllers/api/loginController.ts @@ -1,6 +1,6 @@ import type { RequestHandler } from "express"; -import { config } from "../../services/configService.ts"; +import { config, getReflexiveAddress } from "../../services/configService.ts"; import { buildConfig } from "../../services/buildConfigService.ts"; import { Account } from "../../models/loginModel.ts"; @@ -20,21 +20,7 @@ export const loginController: RequestHandler = async (request, response) => { ? request.query.buildLabel.split(" ").join("+") : buildConfig.buildLabel; - let myAddress: string; - let myUrlBase: string = request.protocol + "://"; - if (request.host.indexOf("warframe.com") == -1) { - // Client request was redirected cleanly, so we know it can reach us how it's reaching us now. - myAddress = request.hostname; - myUrlBase += request.host; - } else { - // Don't know how the client reached us, hoping the config does. - myAddress = config.myAddress; - myUrlBase += myAddress; - const port: number = request.protocol == "http" ? config.httpPort || 80 : config.httpsPort || 443; - if (port != (request.protocol == "http" ? 80 : 443)) { - myUrlBase += ":" + port; - } - } + const { myAddress, myUrlBase } = getReflexiveAddress(request); if ( !account && diff --git a/src/services/configService.ts b/src/services/configService.ts index 87c97d4f..c79bc808 100644 --- a/src/services/configService.ts +++ b/src/services/configService.ts @@ -3,6 +3,7 @@ import path from "path"; import { repoDir } from "../helpers/pathHelper.ts"; import { args } from "../helpers/commandLineArguments.ts"; import { Inbox } from "../models/inboxModel.ts"; +import type { Request } from "express"; export interface IConfig { mongodbUrl: string; @@ -165,3 +166,22 @@ export const syncConfigWithDatabase = (): void => { void Inbox.deleteMany({ goalTag: "GalleonRobbery" }).then(() => {}); } }; + +export const getReflexiveAddress = (request: Request): { myAddress: string; myUrlBase: string } => { + let myAddress: string; + let myUrlBase: string = request.protocol + "://"; + if (request.host.indexOf("warframe.com") == -1) { + // Client request was redirected cleanly, so we know it can reach us how it's reaching us now. + myAddress = request.hostname; + myUrlBase += request.host; + } else { + // Don't know how the client reached us, hoping the config does. + myAddress = config.myAddress; + myUrlBase += myAddress; + const port: number = request.protocol == "http" ? config.httpPort || 80 : config.httpsPort || 443; + if (port != (request.protocol == "http" ? 80 : 443)) { + myUrlBase += ":" + port; + } + } + return { myAddress, myUrlBase }; +};