From: AeonLucid Date: Mon, 26 Oct 2020 18:43:20 +0000 (+0100) Subject: Fixes #81 - Adds hostname support to the config file X-Git-Tag: v1.2.2~67 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=9e55a9738beed4dd18c35842541eef2abf0f620f;p=rhonda%2Fimpostor.git Fixes #81 - Adds hostname support to the config file --- diff --git a/README.md b/README.md index 5312b94..351c172 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ We support steam, itch, android and iOS. The latest version supported is `2020.9 | Impostor version | Among Us version | Experimental | Download | |-|-|-|-| | 1.1.0 | 2020.09.07 - 2020.09.22 | No | [![Download](https://img.shields.io/badge/Download-v1.1.0-blue?style=flat-square)](https://github.com/Impostor/Impostor/releases/tag/v1.1.0) | -| 1.2.1 | 2020.09.22 - 2020.10.22 | Yes | [![Download](https://img.shields.io/badge/Download-v1.2.1-blue?style=flat-square)](https://ci.appveyor.com/project/Impostor/Impostor/branch/dev/artifacts) | +| 1.2.2 | 2020.09.22 - 2020.10.22 | Yes | [![Download](https://img.shields.io/badge/Download-v1.2.2-blue?style=flat-square)](https://ci.appveyor.com/project/Impostor/Impostor/branch/dev/artifacts) | There are no special features at this moment, the goal is aiming to be as close as possible to the real server, for now. In a later stage, making modifications to game logic by modifying `GameData` packets can be looked at. diff --git a/appveyor.yml b/appveyor.yml index c8ae638..e0c82f6 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,7 +1,7 @@ version: '{build}' environment: - IMPOSTOR_VERSION: '1.2.1' + IMPOSTOR_VERSION: '1.2.2' DOTNET_CLI_TELEMETRY_OPTOUT: 1 branches: diff --git a/docs/Server-configuration.md b/docs/Server-configuration.md index f8e3774..2e7d291 100644 --- a/docs/Server-configuration.md +++ b/docs/Server-configuration.md @@ -8,9 +8,9 @@ Some information about all the possible configurations. Click [here](https://git | Key | Default | Description | |-|-|-| -| **PublicIp** | `127.0.0.1` | This needs to the IPv4 address of the server, what you give to others to connect. You can find the your IPv4 address [here](http://whatismyip.host/). | +| **PublicIp** | `127.0.0.1` | This needs to the IPv4 address of the server, what you give to others to connect. You can find the your IPv4 address [here](http://whatismyip.host/). Since 1.2.2 it is also possible to use hostnames instead of IPv4 addresses, these must resolve to a valid IPv4 address. | | **PublicPort** | `22023` | The public port of the server, what you give to others to connect. Usually `22023`. | -| **ListenIp** | `0.0.0.0` | The network interface to listen on. If you do not know what to put here, use `0.0.0.0`. | +| **ListenIp** | `0.0.0.0` | The network interface to listen on. If you do not know what to put here, use `0.0.0.0`. Since 1.2.2 it is also possible to use hostnames instead of IPv4 addresses, these must resolve to a valid IPv4 address. | | **ListenPort** | `22023` | The listen port of the server, usually `22023`. | ### AntiCheat diff --git a/src/Impostor.Api/Exceptions/ImpostorConfigException.cs b/src/Impostor.Api/Exceptions/ImpostorConfigException.cs new file mode 100644 index 0000000..1e59a9b --- /dev/null +++ b/src/Impostor.Api/Exceptions/ImpostorConfigException.cs @@ -0,0 +1,24 @@ +using System; +using System.Runtime.Serialization; + +namespace Impostor.Api +{ + public class ImpostorConfigException : ImpostorException + { + public ImpostorConfigException() + { + } + + protected ImpostorConfigException(SerializationInfo info, StreamingContext context) : base(info, context) + { + } + + public ImpostorConfigException(string? message) : base(message) + { + } + + public ImpostorConfigException(string? message, Exception? innerException) : base(message, innerException) + { + } + } +} diff --git a/src/Impostor.Server/Config/ServerConfig.cs b/src/Impostor.Server/Config/ServerConfig.cs index d6afcb5..1c58433 100644 --- a/src/Impostor.Server/Config/ServerConfig.cs +++ b/src/Impostor.Server/Config/ServerConfig.cs @@ -1,7 +1,12 @@ -namespace Impostor.Server.Config +using Impostor.Server.Utils; + +namespace Impostor.Server.Config { internal class ServerConfig { + private string? _resolvedPublicIp; + private string? _resolvedListenIp; + public const string Section = "Server"; public string PublicIp { get; set; } = "127.0.0.1"; @@ -11,5 +16,15 @@ public string ListenIp { get; set; } = "127.0.0.1"; public ushort ListenPort { get; set; } = 22023; + + public string ResolvePublicIp() + { + return _resolvedPublicIp ??= IpUtils.ResolveIp(PublicIp); + } + + public string ResolveListenIp() + { + return _resolvedListenIp ??= IpUtils.ResolveIp(ListenIp); + } } -} \ No newline at end of file +} diff --git a/src/Impostor.Server/Net/Manager/GameManager.cs b/src/Impostor.Server/Net/Manager/GameManager.cs index 405ebc0..a37829e 100644 --- a/src/Impostor.Server/Net/Manager/GameManager.cs +++ b/src/Impostor.Server/Net/Manager/GameManager.cs @@ -36,7 +36,7 @@ namespace Impostor.Server.Net.Manager _serviceProvider = serviceProvider; _eventManager = eventManager; _gameCodeFactory = gameCodeFactory; - _publicIp = new IPEndPoint(IPAddress.Parse(config.Value.PublicIp), config.Value.PublicPort); + _publicIp = new IPEndPoint(IPAddress.Parse(config.Value.ResolvePublicIp()), config.Value.PublicPort); _games = new ConcurrentDictionary(); } diff --git a/src/Impostor.Server/Net/MatchmakerService.cs b/src/Impostor.Server/Net/MatchmakerService.cs index 227d6e3..bd9a855 100644 --- a/src/Impostor.Server/Net/MatchmakerService.cs +++ b/src/Impostor.Server/Net/MatchmakerService.cs @@ -29,7 +29,7 @@ namespace Impostor.Server.Net public async Task StartAsync(CancellationToken cancellationToken) { - var endpoint = new IPEndPoint(IPAddress.Parse(_serverConfig.ListenIp), _serverConfig.ListenPort); + var endpoint = new IPEndPoint(IPAddress.Parse(_serverConfig.ResolveListenIp()), _serverConfig.ListenPort); await _matchmaker.StartAsync(endpoint); @@ -37,7 +37,7 @@ namespace Impostor.Server.Net "Matchmaker is listening on {0}:{1}, the public server ip is {2}:{3}.", endpoint.Address, endpoint.Port, - _serverConfig.PublicIp, + _serverConfig.ResolvePublicIp(), _serverConfig.PublicPort); if (_redirectorConfig.Enabled) @@ -54,4 +54,4 @@ namespace Impostor.Server.Net await _matchmaker.StopAsync(); } } -} \ No newline at end of file +} diff --git a/src/Impostor.Server/Utils/IpUtils.cs b/src/Impostor.Server/Utils/IpUtils.cs new file mode 100644 index 0000000..649d45a --- /dev/null +++ b/src/Impostor.Server/Utils/IpUtils.cs @@ -0,0 +1,42 @@ +using System.Linq; +using System.Net; +using System.Net.Sockets; +using Impostor.Api; + +namespace Impostor.Server.Utils +{ + internal static class IpUtils + { + public static string ResolveIp(string ip) + { + // Check if valid ip was entered. + if (!IPAddress.TryParse(ip, out var ipAddress)) + { + // Attempt to resolve DNS. + try + { + var hostAddresses = Dns.GetHostAddresses(ip); + if (hostAddresses.Length == 0) + { + throw new ImpostorConfigException($"Invalid IP Address entered '{ip}'."); + } + + // Use first IPv4 result. + ipAddress = hostAddresses.First(x => x.AddressFamily == AddressFamily.InterNetwork); + } + catch (SocketException) + { + throw new ImpostorConfigException($"Failed to resolve hostname '{ip}'."); + } + } + + // Only IPv4. + if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6) + { + throw new ImpostorConfigException($"Invalid IP Address entered '{ipAddress}', only IPv4 is supported by Among Us."); + } + + return ipAddress.ToString(); + } + } +}