From 24752ed71b357870db0f527ff19c98b3ac67a2b6 Mon Sep 17 00:00:00 2001 From: trippyone <137233897+trippyone@users.noreply.github.com> Date: Wed, 31 Jan 2024 10:53:54 -0500 Subject: [PATCH] Hack to return external or local ip from host in getEndPointsResponse This should return the host's local IP (if you connected to SPT using a local IP) or the public IP (if you connected to SPT using a public IP). --- src/NatHelper.ts | 28 +++++++++++++++++++--------- src/StayInTarkovMod.ts | 4 ++-- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/NatHelper.ts b/src/NatHelper.ts index e2cfba87..1cf60dae 100644 --- a/src/NatHelper.ts +++ b/src/NatHelper.ts @@ -32,15 +32,13 @@ class EndPoints { export class NatHelper { - public webSockets: Record = {}; - public serverEndpoints: Record = {}; - - logger: ILogger; public static Instance: NatHelper; + logger: ILogger; + public webSockets: Record = {}; constructor( - webSocketPort: number - , logger: ILogger + webSocketPort: number, + logger: ILogger ) { NatHelper.Instance = this; @@ -81,7 +79,7 @@ export class NatHelper { ws.on("message", async function message(msg) { - wsh.processMessage(msg); + wsh.processMessage(msg, req); }); ws.on("close", async (code: number, reason: Buffer) => @@ -93,7 +91,10 @@ export class NatHelper { console.log(`${sessionID} has connected to Nat Helper!`); } - private async processMessage(msg: RawData) { + // Requests are sent to the serverId (host) + // Responses are sent back to the requester's profileId (client) + + private async processMessage(msg: RawData, req: IncomingMessage) { const msgObj = JSON.parse(msg.toString()); @@ -113,7 +114,16 @@ export class NatHelper { if(msgObj.requestType == "getEndPointsResponse") { - this.webSockets[msgObj.profileId].send(msg.toString()); + // This is a hack to provide the host's external or local IP address + if(msgObj.publicEndPoints["remote"] !== undefined) + { + const udpPort = msgObj.publicEndPoints["remote"].split(":")[1]; + + msgObj.publicEndPoints["remote"] = `${req.socket.remoteAddress.split(":")[3]}:${udpPort}`; + } + + console.log(JSON.stringify(msgObj)); + this.webSockets[msgObj.profileId].send(JSON.stringify(msgObj)); } if(msgObj.requestType == "natPunchResponse") diff --git a/src/StayInTarkovMod.ts b/src/StayInTarkovMod.ts index a92cb21e..9d35b2dd 100644 --- a/src/StayInTarkovMod.ts +++ b/src/StayInTarkovMod.ts @@ -14,6 +14,7 @@ import { IGetLocationRequestData } from "@spt-aki/models/eft/location/IGetLocati import { CoopConfig } from "./CoopConfig"; import { CoopMatch, CoopMatchEndSessionMessages, CoopMatchStatus } from "./CoopMatch"; import { WebSocketHandler } from "./WebSocketHandler"; +import { NatHelper } from "./NatHelper"; import { RouteAction } from "@spt-aki/di/Router"; import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes"; @@ -43,7 +44,6 @@ import { LocationController } from "@spt-aki/controllers/LocationController"; // Callbacks --------------------------------------------------------------- import { BundleCallbacks } from "@spt-aki/callbacks/BundleCallbacks"; import { InraidCallbacks } from "@spt-aki/callbacks/InraidCallbacks"; -import { NatHelper } from "./NatHelper"; // ------------------------------------------------------------------------- @tsyringe.injectable() @@ -109,7 +109,7 @@ export class StayInTarkovMod implements IPreAkiLoadMod, IPostDBLoadMod // Relay server this.webSocketHandler = new WebSocketHandler(this.coopConfig.webSocketPort, logger); - // Nat punch helper + // Nat Helper (for P2P connection) this.natHelper = new NatHelper(this.coopConfig.natHelperPort, logger); // this.traders.push(new SITCustomTraders(), new CoopGroupTrader(), new UsecTrader(), new BearTrader());