From 0cca2fd7b4295e6013b8700907f3296dfdc2fd24 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Mon, 12 Feb 2018 22:07:02 +0100 Subject: [PATCH] Implement IPCPrefixes This makes it possible to not only listen on multiple different host/port combinations, but also different protocols or base URLs (even though this will probably break things up, need to do more tests) --- ArchiSteamFarm/GlobalConfig.cs | 12 ++---------- ArchiSteamFarm/IPC.cs | 21 +++++++-------------- ArchiSteamFarm/Program.cs | 9 ++++++++- ArchiSteamFarm/config/ASF.json | 5 +++-- 4 files changed, 20 insertions(+), 27 deletions(-) diff --git a/ArchiSteamFarm/GlobalConfig.cs b/ArchiSteamFarm/GlobalConfig.cs index ae20deafd..d51ae3e15 100644 --- a/ArchiSteamFarm/GlobalConfig.cs +++ b/ArchiSteamFarm/GlobalConfig.cs @@ -75,8 +75,8 @@ namespace ArchiSteamFarm { [JsonProperty] internal readonly string IPCPassword; - [JsonProperty(Required = Required.DisallowNull)] - internal readonly ushort IPCPort = DefaultIPCPort; + [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace, Required = Required.DisallowNull)] + internal readonly HashSet IPCPrefixes = new HashSet { "http://127.0.0.1:" + DefaultIPCPort + "/" }; [JsonProperty(Required = Required.DisallowNull)] internal readonly byte LoginLimiterDelay = DefaultLoginLimiterDelay; @@ -99,9 +99,6 @@ namespace ArchiSteamFarm { [JsonProperty(Required = Required.DisallowNull)] internal readonly byte UpdatePeriod = 24; - [JsonProperty] - internal string IPCHost { get; set; } = "127.0.0.1"; - [JsonProperty(Required = Required.DisallowNull)] internal ulong SteamOwnerID { get; private set; } @@ -162,11 +159,6 @@ namespace ArchiSteamFarm { return null; } - if (globalConfig.IPCPort == 0) { - ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorConfigPropertyInvalid, nameof(globalConfig.IPCPort), globalConfig.IPCPort)); - return null; - } - if (globalConfig.SteamProtocols.HasFlag(ProtocolTypes.WebSocket) && !OS.SupportsWebSockets()) { globalConfig.SteamProtocols &= ~ProtocolTypes.WebSocket; if (globalConfig.SteamProtocols == 0) { diff --git a/ArchiSteamFarm/IPC.cs b/ArchiSteamFarm/IPC.cs index 6f71efabd..200b47a79 100644 --- a/ArchiSteamFarm/IPC.cs +++ b/ArchiSteamFarm/IPC.cs @@ -90,9 +90,9 @@ namespace ArchiSteamFarm { HistoryTarget = historyTarget; } - internal static void Start(string host, ushort port) { - if (string.IsNullOrEmpty(host) || (port == 0)) { - ASF.ArchiLogger.LogNullError(nameof(host) + " || " + nameof(port)); + internal static void Start(HashSet prefixes) { + if ((prefixes == null) || (prefixes.Count == 0)) { + ASF.ArchiLogger.LogNullError(nameof(prefixes)); return; } @@ -101,21 +101,14 @@ namespace ArchiSteamFarm { return; } - switch (host) { - case "0.0.0.0": - case "::": - // Silently map INADDR_ANY to match HttpListener expectations - host = "*"; - break; - } - - string url = "http://" + host + ":" + port + "/"; HttpListener = new HttpListener { IgnoreWriteExceptions = true }; try { - ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.IPCStarting, url)); + foreach (string prefix in prefixes) { + ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.IPCStarting, prefix)); + HttpListener.Prefixes.Add(prefix); + } - HttpListener.Prefixes.Add(url); HttpListener.Start(); } catch (Exception e) { // HttpListener can dispose itself on error, so don't keep it around diff --git a/ArchiSteamFarm/Program.cs b/ArchiSteamFarm/Program.cs index 67d1b0ef4..1ec6d28ed 100644 --- a/ArchiSteamFarm/Program.cs +++ b/ArchiSteamFarm/Program.cs @@ -449,7 +449,12 @@ namespace ArchiSteamFarm { goto default; } - IPC.Start(GlobalConfig.IPCHost, GlobalConfig.IPCPort); + if (GlobalConfig.IPCPrefixes.Count == 0) { + ASF.ArchiLogger.LogGenericError(string.Format(Strings.ErrorIsEmpty, nameof(GlobalConfig.IPCPrefixes))); + break; + } + + IPC.Start(GlobalConfig.IPCPrefixes); break; case "--service": if (cryptKeyNext) { @@ -471,6 +476,8 @@ namespace ArchiSteamFarm { break; } } + + IPC.Start(GlobalConfig.IPCPrefixes); } private static void ParsePreInitArgs(IReadOnlyCollection args) { diff --git a/ArchiSteamFarm/config/ASF.json b/ArchiSteamFarm/config/ASF.json index d2958024d..46a86c204 100644 --- a/ArchiSteamFarm/config/ASF.json +++ b/ArchiSteamFarm/config/ASF.json @@ -11,9 +11,10 @@ "Headless": false, "IdleFarmingPeriod": 8, "InventoryLimiterDelay": 3, - "IPCHost": "127.0.0.1", "IPCPassword": null, - "IPCPort": 1242, + "IPCPrefixes": [ + "http://127.0.0.1:1242/" + ], "LoginLimiterDelay": 10, "MaxFarmingTime": 10, "MaxTradeHoldDuration": 15,