Updated dependencies + A few other things (!150)
Initially this was going to be an update to dependencies but it seems i got a little carried away!
Anyways this PR removes 2 unused dependencies (`jshint` and `utf-8-validate`), and 2 other, `del` and `fs-extra` that were replaced by the built-in `fs/promises`.
It also renames all `tsconfig` and `Dockerfile` files, in a way that when viewed in a file tree sorted alphabetically they will be next to each other.
It also updates the typescript target to `ES2022`, and changes moduleResolution from `Node` to `Node10` (this isn't an update, they are the same thing but `Node` is now deprecated).
It also adds the `node:` discriminator to every import from built-in modules.
It also has major changes to the build script, `del` and `fs-extra` were only being used in the build script, it's now using `fs/promises` instead, cleaned up the code from some functions, adds better documentation to a few functions, and renames some gulp tasks and npm scripts to better represent what they actually do.
And finally it updates dependencies, except for `atomically` which can't be updated unless the project switches to ESM.
Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/150
Co-authored-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
Co-committed-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
2023-10-14 09:01:41 +00:00
|
|
|
import http, { IncomingMessage, ServerResponse } from "node:http";
|
2023-10-19 17:21:17 +00:00
|
|
|
import { inject, injectAll, injectable } from "tsyringe";
|
2023-03-03 15:23:46 +00:00
|
|
|
|
2023-10-19 17:21:17 +00:00
|
|
|
import { ApplicationContext } from "@spt-aki/context/ApplicationContext";
|
|
|
|
import { ContextVariableType } from "@spt-aki/context/ContextVariableType";
|
|
|
|
import { HttpServerHelper } from "@spt-aki/helpers/HttpServerHelper";
|
|
|
|
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
|
|
|
|
import { IHttpConfig } from "@spt-aki/models/spt/config/IHttpConfig";
|
|
|
|
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
|
|
|
|
import { ConfigServer } from "@spt-aki/servers/ConfigServer";
|
|
|
|
import { DatabaseServer } from "@spt-aki/servers/DatabaseServer";
|
|
|
|
import { WebSocketServer } from "@spt-aki/servers/WebSocketServer";
|
|
|
|
import { IHttpListener } from "@spt-aki/servers/http/IHttpListener";
|
|
|
|
import { LocalisationService } from "@spt-aki/services/LocalisationService";
|
2023-03-03 15:23:46 +00:00
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class HttpServer
|
|
|
|
{
|
2023-03-25 15:03:20 +00:00
|
|
|
protected httpConfig: IHttpConfig;
|
|
|
|
|
2023-03-03 15:23:46 +00:00
|
|
|
constructor(
|
|
|
|
@inject("WinstonLogger") protected logger: ILogger,
|
|
|
|
@inject("DatabaseServer") protected databaseServer: DatabaseServer,
|
|
|
|
@inject("HttpServerHelper") protected httpServerHelper: HttpServerHelper,
|
|
|
|
@inject("LocalisationService") protected localisationService: LocalisationService,
|
|
|
|
@injectAll("HttpListener") protected httpListeners: IHttpListener[],
|
|
|
|
@inject("ConfigServer") protected configServer: ConfigServer,
|
|
|
|
@inject("ApplicationContext") protected applicationContext: ApplicationContext,
|
|
|
|
@inject("WebSocketServer") protected webSocketServer: WebSocketServer
|
|
|
|
)
|
|
|
|
{
|
|
|
|
this.httpConfig = this.configServer.getConfig(ConfigTypes.HTTP);
|
|
|
|
}
|
|
|
|
|
2023-03-25 15:03:20 +00:00
|
|
|
/**
|
|
|
|
* Handle server loading event
|
|
|
|
*/
|
2023-03-03 15:23:46 +00:00
|
|
|
public load(): void
|
|
|
|
{
|
|
|
|
/* create server */
|
|
|
|
const httpServer: http.Server = http.createServer((req, res) =>
|
|
|
|
{
|
|
|
|
this.handleRequest(req, res);
|
|
|
|
});
|
|
|
|
|
2023-03-25 15:03:20 +00:00
|
|
|
this.databaseServer.getTables().server.ip = this.httpConfig.ip;
|
|
|
|
this.databaseServer.getTables().server.port = this.httpConfig.port;
|
2023-03-03 15:23:46 +00:00
|
|
|
|
2023-03-25 15:03:20 +00:00
|
|
|
/* Config server to listen on a port */
|
2023-03-03 15:23:46 +00:00
|
|
|
httpServer.listen(this.httpConfig.port, this.httpConfig.ip, () =>
|
|
|
|
{
|
|
|
|
this.logger.success(this.localisationService.getText("started_webserver_success", this.httpServerHelper.getBackendUrl()));
|
|
|
|
});
|
|
|
|
|
|
|
|
httpServer.on("error", (e: any) =>
|
|
|
|
{
|
|
|
|
/* server is already running or program using privileged port without root */
|
|
|
|
if (process.platform === "linux" && !(process.getuid && process.getuid() === 0) && e.port < 1024)
|
|
|
|
{
|
|
|
|
this.logger.error(this.localisationService.getText("linux_use_priviledged_port_non_root"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.logger.error(this.localisationService.getText("port_already_in_use", e.port));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Setting up websocket
|
|
|
|
this.webSocketServer.setupWebSocket(httpServer);
|
|
|
|
}
|
|
|
|
|
2023-03-25 15:03:20 +00:00
|
|
|
protected handleRequest(req: IncomingMessage, resp: ServerResponse): void
|
|
|
|
{
|
|
|
|
// Pull sessionId out of cookies and store inside app context
|
|
|
|
const sessionId = this.getCookies(req)["PHPSESSID"];
|
|
|
|
this.applicationContext.addValue(ContextVariableType.SESSION_ID, sessionId);
|
|
|
|
|
|
|
|
// http.json logRequests boolean option to allow the user/server to choose to not log requests
|
|
|
|
if (this.httpConfig.logRequests)
|
|
|
|
{
|
|
|
|
this.logger.info(this.localisationService.getText("client_request", req.url));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const listener of this.httpListeners)
|
|
|
|
{
|
|
|
|
if (listener.canHandle(sessionId, req))
|
|
|
|
{
|
|
|
|
listener.handle(sessionId, req, resp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getCookies(req: http.IncomingMessage): Record<string, string>
|
|
|
|
{
|
|
|
|
const found: Record<string, string> = {};
|
|
|
|
const cookies = req.headers.cookie;
|
|
|
|
|
|
|
|
if (cookies)
|
|
|
|
{
|
|
|
|
for (const cookie of cookies.split(";"))
|
|
|
|
{
|
|
|
|
const parts = cookie.split("=");
|
|
|
|
|
|
|
|
found[parts.shift().trim()] = decodeURI(parts.join("="));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|