Add ListenPublic/Private overrides

This commit is contained in:
flightlevel 2018-06-17 14:39:49 +10:00
parent e7098d01c5
commit b72ade7b27
2 changed files with 59 additions and 25 deletions

View File

@ -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");

View File

@ -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;