From 2e726ea9edbaa23fccffd7b28151f88f1f1ce23f Mon Sep 17 00:00:00 2001 From: RaidMax Date: Tue, 4 Apr 2023 22:21:18 -0500 Subject: [PATCH] update references from IP to ListenAddress --- Application/IW4MServer.cs | 18 +-- Application/Misc/MasterCommunication.cs | 12 +- SharedLibraryCore/Server.cs | 15 ++- SharedLibraryCore/Utilities.cs | 2 +- WebfrontCore/Controllers/API/APIController.cs | 110 +++++++----------- WebfrontCore/Controllers/API/Server.cs | 12 +- WebfrontCore/Controllers/AboutController.cs | 8 +- .../Controllers/Client/ClientController.cs | 4 +- .../ViewComponents/ServerListViewComponent.cs | 6 +- 9 files changed, 86 insertions(+), 101 deletions(-) diff --git a/Application/IW4MServer.cs b/Application/IW4MServer.cs index b10f27544..9b3fb5c16 100644 --- a/Application/IW4MServer.cs +++ b/Application/IW4MServer.cs @@ -307,12 +307,12 @@ namespace IW4MAdmin if (!Manager.GetApplicationSettings().Configuration().IgnoreServerConnectionLost) { - Console.WriteLine(loc["SERVER_ERROR_COMMUNICATION"].FormatExt($"{IP}:{Port}")); + Console.WriteLine(loc["SERVER_ERROR_COMMUNICATION"].FormatExt($"{ListenAddress}:{ListenPort}")); var alert = Alert.AlertState.Build().OfType(E.Type.ToString()) .WithCategory(Alert.AlertCategory.Error) .FromSource("System") - .WithMessage(loc["SERVER_ERROR_COMMUNICATION"].FormatExt($"{IP}:{Port}")) + .WithMessage(loc["SERVER_ERROR_COMMUNICATION"].FormatExt($"{ListenAddress}:{ListenPort}")) .ExpiresIn(TimeSpan.FromDays(1)); Manager.AlertManager.AddAlert(alert); @@ -328,12 +328,12 @@ namespace IW4MAdmin if (!Manager.GetApplicationSettings().Configuration().IgnoreServerConnectionLost) { - Console.WriteLine(loc["MANAGER_CONNECTION_REST"].FormatExt($"{IP}:{Port}")); + Console.WriteLine(loc["MANAGER_CONNECTION_REST"].FormatExt($"{ListenAddress}:{ListenPort}")); var alert = Alert.AlertState.Build().OfType(E.Type.ToString()) .WithCategory(Alert.AlertCategory.Information) .FromSource("System") - .WithMessage(loc["MANAGER_CONNECTION_REST"].FormatExt($"{IP}:{Port}")) + .WithMessage(loc["MANAGER_CONNECTION_REST"].FormatExt($"{ListenAddress}:{ListenPort}")) .ExpiresIn(TimeSpan.FromDays(1)); Manager.AlertManager.AddAlert(alert); @@ -1159,7 +1159,7 @@ namespace IW4MAdmin ServerLogger.LogError(e, "Unexpected exception occured during processing updates"); } - Console.WriteLine(loc["SERVER_ERROR_EXCEPTION"].FormatExt($"[{IP}:{Port}]")); + Console.WriteLine(loc["SERVER_ERROR_EXCEPTION"].FormatExt($"[{ListenAddress}:{ListenPort}]")); return false; } } @@ -1198,13 +1198,13 @@ namespace IW4MAdmin { ResolvedIpEndPoint = new IPEndPoint( - (await Dns.GetHostAddressesAsync(IP)).First(address => - address.AddressFamily == AddressFamily.InterNetwork), Port); + (await Dns.GetHostAddressesAsync(ListenAddress)).First(address => + address.AddressFamily == AddressFamily.InterNetwork), ListenPort); } catch (Exception ex) { - ServerLogger.LogWarning(ex, "Could not resolve hostname or IP for RCon connection {IP}:{Port}", IP, Port); - ResolvedIpEndPoint = new IPEndPoint(IPAddress.Parse(IP), Port); + ServerLogger.LogWarning(ex, "Could not resolve hostname or IP for RCon connection {Address}:{Port}", ListenAddress, ListenPort); + ResolvedIpEndPoint = new IPEndPoint(IPAddress.Parse(ListenAddress), ListenPort); } RconParser = Manager.AdditionalRConParsers diff --git a/Application/Misc/MasterCommunication.cs b/Application/Misc/MasterCommunication.cs index 6b356b982..76fc09049 100644 --- a/Application/Misc/MasterCommunication.cs +++ b/Application/Misc/MasterCommunication.cs @@ -103,17 +103,15 @@ namespace IW4MAdmin.Application.Misc await UploadStatus(); } } - catch (Exception ex) { - _logger.LogWarning(ex, "Could not send heartbeat"); + _logger.LogWarning("Could not send heartbeat - {Message}", ex.Message); } try { await Task.Delay(Interval, token); } - catch { break; @@ -149,13 +147,13 @@ namespace IW4MAdmin.Application.Misc Map = s.CurrentMap.Name, MaxClientNum = s.MaxClients, Id = s.EndPoint, - Port = (short)s.Port, - IPAddress = s.IP + Port = (short)s.ListenPort, + IPAddress = s.ListenAddress }).ToList(), WebfrontUrl = _appConfig.WebfrontUrl }; - Response response = null; + Response response; if (_firstHeartBeat) { @@ -170,7 +168,7 @@ namespace IW4MAdmin.Application.Misc if (response.ResponseMessage.StatusCode != System.Net.HttpStatusCode.OK) { - _logger.LogWarning("Non success response code from master is {statusCode}, message is {message}", response.ResponseMessage.StatusCode, response.StringContent); + _logger.LogWarning("Non success response code from master is {StatusCode}, message is {Message}", response.ResponseMessage.StatusCode, response.StringContent); } } } diff --git a/SharedLibraryCore/Server.cs b/SharedLibraryCore/Server.cs index 67b9ba69e..fd5f704e2 100644 --- a/SharedLibraryCore/Server.cs +++ b/SharedLibraryCore/Server.cs @@ -85,9 +85,14 @@ namespace SharedLibraryCore InitializeAutoMessages(); } - public long EndPoint => IPAddress.TryParse(IP, out _) - ? Convert.ToInt64($"{IP.Replace(".", "")}{Port}") - : $"{IP.Replace(".", "")}{Port}".GetStableHashCode(); + public long EndPoint => IPAddress.TryParse(ListenAddress, out _) + ? Convert.ToInt64($"{ListenAddress!.Replace(".", "")}{ListenPort}") + : $"{ListenAddress!.Replace(".", "")}{ListenPort}".GetStableHashCode(); + + public long LegacyEndpoint => EndPoint; + + public abstract long LegacyDatabaseId { get; } + public string Id => $"{ListenAddress}:{ListenPort}"; // Objects public IManager Manager { get; protected set; } @@ -142,6 +147,8 @@ namespace SharedLibraryCore /// public string IP { get; protected set; } + public string ListenAddress => IP; + public IPEndPoint ResolvedIpEndPoint { get; protected set; } public string Version { get; protected set; } public bool IsInitialized { get; set; } @@ -391,7 +398,7 @@ namespace SharedLibraryCore public override string ToString() { - return $"{IP}:{Port}"; + return $"{ListenAddress}:{ListenPort}"; } protected async Task ScriptLoaded() diff --git a/SharedLibraryCore/Utilities.cs b/SharedLibraryCore/Utilities.cs index b7d8a11be..fc5ae1b1e 100644 --- a/SharedLibraryCore/Utilities.cs +++ b/SharedLibraryCore/Utilities.cs @@ -1242,7 +1242,7 @@ namespace SharedLibraryCore var serverConfig = appConfig.Servers? .FirstOrDefault(configServer => - configServer.IPAddress == server.IP && configServer.Port == server.Port); + configServer.IPAddress == server.ListenAddress && configServer.Port == server.ListenPort); var allRules = appConfig.GlobalRules?.ToList() ?? new List(); if (serverConfig?.Rules != null) diff --git a/WebfrontCore/Controllers/API/APIController.cs b/WebfrontCore/Controllers/API/APIController.cs index 505496422..da6187697 100644 --- a/WebfrontCore/Controllers/API/APIController.cs +++ b/WebfrontCore/Controllers/API/APIController.cs @@ -1,82 +1,62 @@ using Microsoft.AspNetCore.Mvc; using SharedLibraryCore; -using SharedLibraryCore.Events; using SharedLibraryCore.Interfaces; using System.Linq; -namespace WebfrontCore.Controllers.API +namespace WebfrontCore.Controllers.API; + +public class ApiController : BaseController { - public class ApiController : BaseController + public ApiController(IManager manager) : base(manager) { - public ApiController(IManager manager) : base(manager) - { - } + } - public IActionResult Index() - { - return Ok($"IW4MAdmin API"); - } + public IActionResult Index() + { + return Ok("IW4MAdmin API"); + } - [HttpGet] - public IActionResult Event(bool shouldConsume = true) - { - var events = EventApi.GetEvents(shouldConsume); - return Json(events); - } + [HttpGet] + public IActionResult Event() + { + return NotFound("This API endpoint is no longer supported"); + } - [HttpGet] - public IActionResult Status(long? id) - { - var serverInfo = Manager.GetServers() - .Select(server => new - { - Id = server.EndPoint, - IsOnline = !server.Throttled, - Name = server.Hostname, - MaxPlayers = server.MaxClients, - CurrentPlayers = server.GetClientsAsList().Count, - Map = server.CurrentMap, - GameMode = server.Gametype, - server.Port, - Game = server.GameName.ToString(), - Players = server.GetClientsAsList() - .Select(player => new - { - player.Name, - player.Score, - player.Ping, - State = player.State.ToString(), - player.ClientNumber, - ConnectionTime = player.ConnectionLength, - Level = player.Level.ToLocalizedLevelName(), - }) - }); - - if (id != null) + [HttpGet] + public IActionResult Status(long? id) + { + var serverInfo = Manager.GetServers() + .Select(server => new { - serverInfo = serverInfo.Where(server => server.Id == id); - } + Id = server.EndPoint, + IsOnline = !server.Throttled, + Name = server.ServerName, + MaxPlayers = server.MaxClients, + CurrentPlayers = server.GetClientsAsList().Count, + Map = server.CurrentMap, + GameMode = server.Gametype, + server.ListenAddress, + server.ListenPort, + Game = server.GameName.ToString(), + Players = server.GetClientsAsList() + .Select(player => new + { + player.Name, + player.Score, + player.Ping, + State = player.State.ToString(), + player.ClientNumber, + ConnectionTime = player.ConnectionLength, + Level = player.Level.ToLocalizedLevelName(), + }) + }); - return Json(serverInfo); - } - - [HttpGet] - public IActionResult RestartApproved() + if (id != null) { - var serverToRestart = Manager.GetServers().FirstOrDefault(_server => _server.RestartRequested); - - if (serverToRestart != null) - { - serverToRestart.RestartRequested = false; - } - - return serverToRestart != null ? - (IActionResult)Json(new - { - port = serverToRestart.Port - }) : - Unauthorized(); + serverInfo = serverInfo.Where(server => server.Id == id); } + + return Json(serverInfo); } } diff --git a/WebfrontCore/Controllers/API/Server.cs b/WebfrontCore/Controllers/API/Server.cs index 6fdf8fd0c..ab1324bcc 100644 --- a/WebfrontCore/Controllers/API/Server.cs +++ b/WebfrontCore/Controllers/API/Server.cs @@ -23,9 +23,9 @@ namespace WebfrontCore.Controllers.API return new JsonResult(Manager.GetServers().Select(server => new { Id = server.EndPoint, - server.Hostname, - server.IP, - server.Port, + server.ServerName, + server.ListenAddress, + server.ListenPort, Game = server.GameName.ToString(), server.ClientNum, server.MaxClients, @@ -52,9 +52,9 @@ namespace WebfrontCore.Controllers.API return new JsonResult(new { Id = foundServer.EndPoint, - foundServer.Hostname, - foundServer.IP, - foundServer.Port, + foundServer.ServerName, + foundServer.ListenAddress, + foundServer.ListenPort, Game = foundServer.GameName.ToString(), foundServer.ClientNum, foundServer.MaxClients, diff --git a/WebfrontCore/Controllers/AboutController.cs b/WebfrontCore/Controllers/AboutController.cs index ab2d8c5b9..2928a2aa5 100644 --- a/WebfrontCore/Controllers/AboutController.cs +++ b/WebfrontCore/Controllers/AboutController.cs @@ -28,7 +28,7 @@ namespace WebfrontCore.Controllers : _appConfig.CommunityInformation.Name; var activeServers = _appConfig.Servers.Where(server => - Manager.GetServers().FirstOrDefault(s => s.IP == server.IPAddress && s.Port == server.Port) != null); + Manager.GetServers().FirstOrDefault(s => s.ListenAddress == server.IPAddress && s.ListenPort == server.Port) != null); var info = new CommunityInfo { @@ -36,9 +36,9 @@ namespace WebfrontCore.Controllers ServerRules = activeServers.ToDictionary( config => { - var server = Manager.GetServers().FirstOrDefault(server => - server.IP == config.IPAddress && server.Port == config.Port); - return (server.Hostname, server.EndPoint); + var server = Manager.GetServers().First(server => + server.ListenAddress == config.IPAddress && server.ListenPort == config.Port); + return (server.ServerName, server.EndPoint); }, config => config.Rules), CommunityInformation = _appConfig.CommunityInformation diff --git a/WebfrontCore/Controllers/Client/ClientController.cs b/WebfrontCore/Controllers/Client/ClientController.cs index 9ec2c49ff..27e2d33e3 100644 --- a/WebfrontCore/Controllers/Client/ClientController.cs +++ b/WebfrontCore/Controllers/Client/ClientController.cs @@ -143,8 +143,8 @@ namespace WebfrontCore.Controllers ConnectProtocolUrl = ingameClient?.CurrentServer.EventParser.URLProtocolFormat.FormatExt( ingameClient.CurrentServer.ResolvedIpEndPoint.Address.IsInternal() ? Program.Manager.ExternalIPAddress - : ingameClient.CurrentServer.IP, - ingameClient.CurrentServer.Port), + : ingameClient.CurrentServer.ListenAddress, + ingameClient.CurrentServer.ListenPort), CurrentServerName = ingameClient?.CurrentServer?.Hostname, GeoLocationInfo = await _geoLocationService.Locate(client.IPAddressString), NoteMeta = string.IsNullOrWhiteSpace(note?.Note) ? null: note, diff --git a/WebfrontCore/ViewComponents/ServerListViewComponent.cs b/WebfrontCore/ViewComponents/ServerListViewComponent.cs index e9800feff..e574dd1e7 100644 --- a/WebfrontCore/ViewComponents/ServerListViewComponent.cs +++ b/WebfrontCore/ViewComponents/ServerListViewComponent.cs @@ -93,10 +93,10 @@ namespace WebfrontCore.ViewComponents }).ToList(), ChatHistory = server.ChatHistory.ToList(), Online = !server.Throttled, - IPAddress = server.IP, - ExternalIPAddress = server.ResolvedIpEndPoint.Address.IsInternal() ? Program.Manager.ExternalIPAddress : server.IP, + IPAddress = server.ListenAddress, + ExternalIPAddress = server.ResolvedIpEndPoint.Address.IsInternal() ? Program.Manager.ExternalIPAddress : server.ListenAddress, ConnectProtocolUrl = server.EventParser.URLProtocolFormat.FormatExt( - server.ResolvedIpEndPoint.Address.IsInternal() ? Program.Manager.ExternalIPAddress : server.IP, + server.ResolvedIpEndPoint.Address.IsInternal() ? Program.Manager.ExternalIPAddress : server.ListenAddress, server.ListenPort) }); }