using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Primitives; using SPTarkov.Common.Extensions; using SPTarkov.DI.Annotations; using SPTarkov.Server.Core.Models.Common; using SPTarkov.Server.Core.Models.Spt.Config; using SPTarkov.Server.Core.Models.Utils; using SPTarkov.Server.Core.Servers.Http; using SPTarkov.Server.Core.Services; namespace SPTarkov.Server.Core.Servers; [Injectable(InjectionType.Singleton)] public class HttpServer( ISptLogger _logger, ServerLocalisationService _serverLocalisationService, ConfigServer _configServer, WebSocketServer _webSocketServer, ProfileActivityService _profileActivityService, IEnumerable _httpListeners ) { private readonly HttpConfig _httpConfig = _configServer.GetConfig(); public async Task HandleRequest(HttpContext context) { if (context.WebSockets.IsWebSocketRequest) { await _webSocketServer.OnConnection(context); return; } // Use default empty mongoId if not found in cookie var sessionId = context.Request.Cookies.TryGetValue("PHPSESSID", out var sessionIdString) ? new MongoId(sessionIdString) : MongoId.Empty(); if (!string.IsNullOrEmpty(sessionIdString)) { _profileActivityService.SetActivityTimestamp(sessionId); } // Extract header for original IP detection var realIp = context.GetHeaderIfExists("x-real-ip"); var clientIp = GetClientIp(context, realIp); if (_httpConfig.LogRequests) { LogRequest(context, clientIp, IsLocalRequest(clientIp)); } try { var listener = _httpListeners.FirstOrDefault(l => l.CanHandle(sessionId, context.Request)); if (listener != null) { await listener.Handle(sessionId, context.Request, context.Response); } } catch (Exception ex) { _logger.Critical("Error handling request: " + context.Request.Path); _logger.Critical(ex.Message); _logger.Critical(ex.StackTrace); #if DEBUG throw; // added this so we can debug something. #endif } // This http request would be passed through the SPT Router and handled by an ICallback } /// /// Log request - handle differently if request is local /// /// HttpContext of request /// Ip of requester /// Is this local request protected void LogRequest(HttpContext context, string clientIp, bool isLocalRequest) { if (isLocalRequest) { _logger.Info(_serverLocalisationService.GetText("client_request", context.Request.Path.Value)); } else { _logger.Info(_serverLocalisationService.GetText("client_request_ip", new { ip = clientIp, url = context.Request.Path.Value })); } } protected static string GetClientIp(HttpContext context, StringValues? realIp) { if (realIp.HasValue) { return realIp.Value.First(); } var forwardedFor = context.GetHeaderIfExists("x-forwarded-for"); return forwardedFor.HasValue ? forwardedFor.Value.First()!.Split(",")[0].Trim() : context.Connection.RemoteIpAddress!.ToString().Split(":").Last(); } /// /// Check against hardcoded values that determine it's from a local address /// /// Address to check /// True if its local protected bool IsLocalRequest(string? remoteAddress) { if (remoteAddress == null) { return false; } return remoteAddress.StartsWith("127.0.0") || remoteAddress.StartsWith("192.168.") || remoteAddress.StartsWith("localhost"); } protected Dictionary GetCookies(HttpRequest req) { var found = new Dictionary(); foreach (var keyValuePair in req.Cookies) { found.Add(keyValuePair.Key, keyValuePair.Value); } return found; } public string ListeningUrl() { return $"https://{_httpConfig.Ip}:{_httpConfig.Port}"; } }