]> git.deb.at Git - rhonda/impostor.git/commitdiff
Fixes #81 - Adds hostname support to the config file
authorAeonLucid <aeonlucid@outlook.com>
Mon, 26 Oct 2020 18:43:20 +0000 (19:43 +0100)
committerAeonLucid <aeonlucid@outlook.com>
Mon, 26 Oct 2020 18:43:20 +0000 (19:43 +0100)
README.md
appveyor.yml
docs/Server-configuration.md
src/Impostor.Api/Exceptions/ImpostorConfigException.cs [new file with mode: 0644]
src/Impostor.Server/Config/ServerConfig.cs
src/Impostor.Server/Net/Manager/GameManager.cs
src/Impostor.Server/Net/MatchmakerService.cs
src/Impostor.Server/Utils/IpUtils.cs [new file with mode: 0644]

index 5312b9411c815ca23f9527067da07fee212ac737..351c1727f7def7615875b9f6ee2a8f7cef58ead9 100644 (file)
--- 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.
 
index c8ae638bea0f0db6b7ee82faf7b13e1900d6dfe4..e0c82f688f916bc9ba28efec1dd28363aca0ba7f 100644 (file)
@@ -1,7 +1,7 @@
 version: '{build}'
 
 environment:
-  IMPOSTOR_VERSION: '1.2.1'
+  IMPOSTOR_VERSION: '1.2.2'
   DOTNET_CLI_TELEMETRY_OPTOUT: 1
 
 branches:
index f8e37748933e4351dd7a1f1a1391cd8f5196759b..2e7d2912c5c84b4cd9aca43b7df4fd8d781baf7d 100644 (file)
@@ -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 (file)
index 0000000..1e59a9b
--- /dev/null
@@ -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)
+        {
+        }
+    }
+}
index d6afcb51ff8143843f4a0fd45fe7e304776d8179..1c584333f581225e9c3eb28bb4632b6d8a566013 100644 (file)
@@ -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";
         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
+}
index 405ebc0eade9eba2cf1ce034f8b3b5c820addb1c..a37829e585460dbceb9d05c420b74a9bc374fdce 100644 (file)
@@ -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<int, Game>();
         }
 
index 227d6e3a8ec6072f44d04dfefd494d5f84f3c3f4..bd9a855381e6ae7c51b5b6c5afd86db0bdd5d387 100644 (file)
@@ -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 (file)
index 0000000..649d45a
--- /dev/null
@@ -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();
+        }
+    }
+}