Skip to content

Commit

Permalink
Merge pull request #1 from trippyone/backendUrl
Browse files Browse the repository at this point in the history
net loopback fix
  • Loading branch information
paulov-t authored Nov 18, 2023
2 parents c1094ac + db6cb0e commit 4d7595c
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 35 deletions.
14 changes: 6 additions & 8 deletions src/BundleLoaderFixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { BundleLoader } from "@spt-aki/loaders/BundleLoader";
import { JsonUtil } from "@spt-aki/utils/JsonUtil";
import { VFS } from "@spt-aki/utils/VFS";
import { DependencyContainer } from "tsyringe";
import { ExternalIPFinder } from "./ExternalIPFinder";

class BundleInfo
{
Expand All @@ -27,18 +26,16 @@ export class BundleLoaderFixed
protected bundles: Record<string, BundleInfo> = {};
jsonUtil: JsonUtil;
vfs: VFS;
externalIPFinder: ExternalIPFinder;
backendUrl: string;

constructor(
vfs: VFS,
jsonUtil: JsonUtil,
externalIPFinder: ExternalIPFinder
)
{

this.vfs = vfs;
this.jsonUtil = jsonUtil;
this.externalIPFinder = externalIPFinder
}

public getBundles(local: boolean): BundleInfo[]
Expand All @@ -49,7 +46,7 @@ export class BundleLoaderFixed
{
result.push(this.getBundle(bundle, local));
}

return result;
}

Expand All @@ -71,8 +68,9 @@ export class BundleLoaderFixed
const manifest = this.jsonUtil.deserialize<BundleManifest>(this.vfs.readFile(`${modpath}bundles.json`)).manifest;

for (const bundle of manifest)
{
const bundlePath = `${this.externalIPFinder.resolveExternalIP()}/files/bundle/${bundle.key}`;
{
// return a partial url. the complete url will be build on client side.
const bundlePath = `/files/bundle/${bundle.key}`;
const bundleFilepath = bundle.path || `${modpath}bundles/${bundle.key}`.replace(/\\/g, "/");
this.addBundle(bundle.key, new BundleInfo(modpath, bundle, bundlePath, bundleFilepath));
}
Expand All @@ -84,7 +82,6 @@ export class BundleLoaderFixed

public resolveAndOverride(container: DependencyContainer): void {


const thisObj = this;

container.afterResolution("BundleLoader", (_t, result: BundleLoader) => {
Expand All @@ -98,6 +95,7 @@ export class BundleLoaderFixed
return thisObj.getBundle(key, local);
}
result.getBundles = (local: boolean) => {

return thisObj.getBundles(local);
}
});
Expand Down
8 changes: 5 additions & 3 deletions src/CoopConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class CoopConfig {
public protocol: string;
public externalIP: string;
public webSocketPort: number;
public useExternalIPFinder: boolean;
//public useExternalIPFinder: boolean;
public webSocketTimeoutSeconds: number;
public webSocketTimeoutCheckStartSeconds: number;
public static Instance: CoopConfig;
Expand All @@ -15,7 +15,7 @@ export class CoopConfig {
this.protocol = "http";
this.externalIP = "127.0.0.1";
this.webSocketPort = 6970;
this.useExternalIPFinder = true;
//this.useExternalIPFinder = true;
this.webSocketTimeoutSeconds = 5;
this.webSocketTimeoutCheckStartSeconds = 120;

Expand All @@ -28,16 +28,18 @@ export class CoopConfig {
// console.log(coopConfigFilePath);
if(!fs.existsSync(coopConfigFilePath)) {
console.warn(`Coop Config doesn't exist, creating default config.`);
console.warn(`BE AWARE! ExternalIPFinder is ACTIVE! The externalIP config value is ignored!`);
//console.warn(`BE AWARE! ExternalIPFinder is ACTIVE! The externalIP config value is ignored!`);
const coopcfgString = JSON.stringify(this, null, 4);
fs.writeFileSync(coopConfigFilePath, coopcfgString);
}
else {
Object.assign(this, JSON.parse(fs.readFileSync(coopConfigFilePath).toString()))
console.log(`COOP MOD: Coop Config loaded.`);
/*
if(this.useExternalIPFinder) {
console.log(`COOP MOD: BE AWARE! ExternalIPFinder is ACTIVE!`);
}
*/
}
// console.log(this);

Expand Down
59 changes: 44 additions & 15 deletions src/StayInTarkovMod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { IncomingMessage, ServerResponse } from "http";
import { CoopMatchResponse } from "./CoopMatchResponse";
import { friendlyAI } from "./FriendlyAI";
import { SITCustomTraders } from "./Traders/SITCustomTraders";
import { HttpServerHelper } from "@spt-aki/helpers/HttpServerHelper";
// -------------------------------------------------------------------------


Expand All @@ -68,9 +69,12 @@ export class StayInTarkovMod implements IPreAkiLoadMod, IPostDBLoadMod
bundleLoader: BundleLoader;
resolvedExternalIP: string;
profileHelper: ProfileHelper;
httpServerHelper: HttpServerHelper;

public traders: any[] = [];

public sessionBackendUrl: Record<string, string> = {};

public getCoopMatch(serverId: string) : CoopMatch {

if(serverId === undefined) {
Expand Down Expand Up @@ -109,6 +113,7 @@ export class StayInTarkovMod implements IPreAkiLoadMod, IPostDBLoadMod
this.webSocketHandler = new WebSocketHandler(this.coopConfig.webSocketPort, logger);
this.bundleLoader = container.resolve<BundleLoader>("BundleLoader");
this.profileHelper = container.resolve<ProfileHelper>("ProfileHelper");
this.httpServerHelper = container.resolve<HttpServerHelper>("HttpServerHelper");

// this.traders.push(new SITCustomTraders(), new CoopGroupTrader(), new UsecTrader(), new BearTrader());
this.traders.push(new SITCustomTraders());
Expand All @@ -127,10 +132,10 @@ export class StayInTarkovMod implements IPreAkiLoadMod, IPostDBLoadMod
t.preAkiLoad(container);
}

this.externalIPFinder = new ExternalIPFinder(this.coopConfig, this.httpConfig);
//this.externalIPFinder = new ExternalIPFinder(this.coopConfig, this.httpConfig);

// ----------------------- Bundle Loader Fixes ------------------------------------------------
const bundleLoaderFixed = new BundleLoaderFixed(container.resolve<VFS>("VFS"), container.resolve<JsonUtil>("JsonUtil"), this.externalIPFinder);
const bundleLoaderFixed = new BundleLoaderFixed(container.resolve<VFS>("VFS"), container.resolve<JsonUtil>("JsonUtil"));
bundleLoaderFixed.resolveAndOverride(container);

// ----------------------- TODO: Azure WebApp Helper (trying to fix this ASAP) ------------------------------------------------
Expand Down Expand Up @@ -392,13 +397,15 @@ export class StayInTarkovMod implements IPreAkiLoadMod, IPostDBLoadMod
{
if(WebSocketHandler.Instance.webSockets[info.profileId].readyState == WebSocket.OPEN)
{
logger.info(`JoinMatch failed: ${info.profileId} is already connected!`);

output = JSON.stringify(
{
alreadyConnected: true
}
)

logger.info(`JoinMatch failed: ${info.profileId} is already connected!`);
return output;
}
}
}
Expand Down Expand Up @@ -538,6 +545,26 @@ export class StayInTarkovMod implements IPreAkiLoadMod, IPostDBLoadMod
staticRouterModService.registerStaticRouter(
"MatchStaticRouter-SIT",
[
{
url: "/launcher/profile/login",
action: (url: string, info: any, sessionID: string, output: string): any =>
{
let sessionId: string = output;
let backendUrl: string;

if(info.backendUrl !== undefined && info.backendUrl !== null) {
backendUrl = info.backendUrl;
}
else{
backendUrl = `${this.coopConfig.protocol}://${this.coopConfig.externalIP}:${this.httpConfig.port}`;
}

// store backendUrl per session ID for GameController injection
this.sessionBackendUrl[sessionId] = backendUrl;

return output;
}
},
{
url: "/client/match/group/status",
action: (url: string, info: any, sessionID: string, output: string): any =>
Expand Down Expand Up @@ -753,12 +780,15 @@ export class StayInTarkovMod implements IPreAkiLoadMod, IPostDBLoadMod
// We want to replace the original method logic with something different
result.getGameConfig = (sessionID: string) =>
{
return this.getGameConfig(sessionID);
// get the requestUrl for the sessionID
let backendUrl = this.sessionBackendUrl[sessionID];

delete this.sessionBackendUrl[sessionID];

return this.getGameConfig(sessionID, backendUrl);
}
// The modifier Always makes sure this replacement method is ALWAYS replaced
}, {frequency: "Always"});


}

public generateNewLootForLocation(sessionID: string, request: IGetLocationRequestData) {
Expand Down Expand Up @@ -820,11 +850,10 @@ export class StayInTarkovMod implements IPreAkiLoadMod, IPostDBLoadMod
// return friendList;
// }

public getGameConfig(sessionID: string): IGameConfigResponse
public getGameConfig(sessionID: string, backendUrl: string): IGameConfigResponse
{
const profile = this.profileHelper.getPmcProfile(sessionID);

let externalIp = this.externalIPFinder.resolveExternalIP();
//let externalIp = this.externalIPFinder.resolveExternalIP();

const config: IGameConfigResponse = {
languages: this.databaseServer.getTables().locales.languages,
Expand All @@ -836,11 +865,11 @@ export class StayInTarkovMod implements IPreAkiLoadMod, IPostDBLoadMod
taxonomy: 6,
activeProfileId: `pmc${sessionID}`,
backend: {
Lobby: externalIp,
Trading: externalIp,
Messaging: externalIp,
Main: externalIp,
RagFair: externalIp,
Lobby: backendUrl,
Trading: backendUrl,
Messaging: backendUrl,
Main: backendUrl,
RagFair: backendUrl,
},
useProtobuf: false,
utc_time: new Date().getTime() / 1000,
Expand Down
9 changes: 0 additions & 9 deletions src/WebSocketHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ export class WebSocketHandler {
const webSocketServer = new WebSocket.Server({
"port": webSocketPort,
perMessageDeflate: {
zlibDeflateOptions: {
// See zlib defaults.
chunkSize: 1024,
memLevel: 7,
level: 3
},
zlibInflateOptions: {
chunkSize: 10 * 1024
},
// Other options settable:
clientNoContextTakeover: true, // Defaults to negotiated value.
serverNoContextTakeover: true, // Defaults to negotiated value.
Expand Down

0 comments on commit 4d7595c

Please sign in to comment.