From b72ade7b27b14dd0ca50285c09d12813aea4d098 Mon Sep 17 00:00:00 2001 From: flightlevel Date: Sun, 17 Jun 2018 14:39:49 +1000 Subject: [PATCH] Add ListenPublic/Private overrides --- src/Jackett.Server/Initialisation.cs | 54 ++++++++++++++++++++++++++++ src/Jackett.Server/Program.cs | 30 +++------------- 2 files changed, 59 insertions(+), 25 deletions(-) diff --git a/src/Jackett.Server/Initialisation.cs b/src/Jackett.Server/Initialisation.cs index e285e8af0..8a9759539 100644 --- a/src/Jackett.Server/Initialisation.cs +++ b/src/Jackett.Server/Initialisation.cs @@ -2,6 +2,7 @@ using Jackett.Common.Models.Config; using Jackett.Common.Services; using Jackett.Common.Services.Interfaces; +using Jackett.Common.Utils; using Jackett.Server.Services; using NLog; using NLog.Config; @@ -9,6 +10,7 @@ using NLog.Targets; using System; using System.IO; using System.Linq; +using System.Runtime.InteropServices; namespace Jackett.Server { @@ -109,6 +111,58 @@ namespace Jackett.Server } } + public static void ProcessConsoleOverrides(ConsoleOptions consoleOptions, IProcessService processService, ServerConfig serverConfig, IConfigurationService configurationService, Logger logger) + { + // Override port + if (consoleOptions.Port != 0) + { + Int32.TryParse(serverConfig.Port.ToString(), out Int32 configPort); + + if (configPort != consoleOptions.Port) + { + logger.Info("Overriding port to " + consoleOptions.Port); + serverConfig.Port = consoleOptions.Port; + bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); + if (isWindows) + { + if (ServerUtil.IsUserAdministrator()) + { + ReserveUrls(processService, serverConfig, logger, doInstall: true); + } + else + { + logger.Error("Unable to switch ports when not running as administrator"); + Environment.Exit(1); + } + } + configurationService.SaveConfig(serverConfig); + } + } + + // Override listen public + if (consoleOptions.ListenPublic || consoleOptions.ListenPrivate) + { + if (serverConfig.AllowExternal != consoleOptions.ListenPublic) + { + logger.Info("Overriding external access to " + consoleOptions.ListenPublic); + serverConfig.AllowExternal = consoleOptions.ListenPublic; + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + if (ServerUtil.IsUserAdministrator()) + { + ReserveUrls(processService, serverConfig, logger, doInstall: true); + } + else + { + logger.Error("Unable to switch to public listening without admin rights."); + Environment.Exit(1); + } + } + configurationService.SaveConfig(serverConfig); + } + } + } + public static void ReserveUrls(IProcessService processService, ServerConfig serverConfig, Logger logger, bool doInstall = true) { logger.Debug("Unreserving Urls"); diff --git a/src/Jackett.Server/Program.cs b/src/Jackett.Server/Program.cs index c58949b23..40a17fcd4 100644 --- a/src/Jackett.Server/Program.cs +++ b/src/Jackett.Server/Program.cs @@ -102,37 +102,17 @@ namespace Jackett.Server do { - ServerConfig serverConfig = configurationService.BuildServerConfig(Settings); - - Int32.TryParse(serverConfig.Port.ToString(), out Int32 configPort); - if (!isWebHostRestart) { - // Override port - if (consoleOptions.Port != 0) + if (consoleOptions.Port != 0 || consoleOptions.ListenPublic || consoleOptions.ListenPrivate) { - if (configPort != consoleOptions.Port) - { - logger.Info("Overriding port to " + consoleOptions.Port); - serverConfig.Port = consoleOptions.Port; - bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); - if (isWindows) - { - if (ServerUtil.IsUserAdministrator()) - { - Initialisation.ReserveUrls(processService, serverConfig, logger, doInstall: true); - } - else - { - logger.Error("Unable to switch ports when not running as administrator"); - Environment.Exit(1); - } - } - configurationService.SaveConfig(serverConfig); - } + ServerConfig serverConfiguration = configurationService.BuildServerConfig(Settings); + Initialisation.ProcessConsoleOverrides(consoleOptions, processService, serverConfiguration, configurationService, logger); } } + ServerConfig serverConfig = configurationService.BuildServerConfig(Settings); + Int32.TryParse(serverConfig.Port.ToString(), out Int32 configPort); string[] url = serverConfig.GetListenAddresses(serverConfig.AllowExternal).Take(1).ToArray(); //Kestrel doesn't need 127.0.0.1 and localhost to be registered, remove once off OWIN isWebHostRestart = false;