Skip to content

Commit

Permalink
Hack to return external or local ip from host in getEndPointsResponse
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
trippyone committed Jan 31, 2024
1 parent 12feda7 commit 24752ed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
28 changes: 19 additions & 9 deletions src/NatHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ class EndPoints {

export class NatHelper {

public webSockets: Record<string, WebSocket.WebSocket> = {};
public serverEndpoints: Record<string, EndPoints> = {};

logger: ILogger;
public static Instance: NatHelper;
logger: ILogger;
public webSockets: Record<string, WebSocket.WebSocket> = {};

constructor(
webSocketPort: number
, logger: ILogger
webSocketPort: number,
logger: ILogger
)
{
NatHelper.Instance = this;
Expand Down Expand Up @@ -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) =>
Expand All @@ -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());

Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/StayInTarkovMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 24752ed

Please sign in to comment.