Jackett/src/Jackett.Server/Initialisation.cs

179 lines
7.0 KiB
C#
Raw Normal View History

2018-06-22 12:35:58 +00:00
using Jackett.Common.Models.Config;
using Jackett.Common.Services.Interfaces;
2018-06-17 04:39:49 +00:00
using Jackett.Common.Utils;
using Jackett.Server.Services;
2018-06-16 08:32:08 +00:00
using NLog;
using System;
using System.Linq;
2018-06-17 04:39:49 +00:00
using System.Runtime.InteropServices;
2018-06-16 08:32:08 +00:00
namespace Jackett.Server
{
public static class Initialisation
{
public static void ProcessSettings(RuntimeSettings runtimeSettings, Logger logger)
2018-06-16 08:32:08 +00:00
{
if (runtimeSettings.ClientOverride != "httpclient" && runtimeSettings.ClientOverride != "httpclient2" && runtimeSettings.ClientOverride != "httpclientnetcore")
{
logger.Error($"Client override ({runtimeSettings.ClientOverride}) has been deprecated, please remove it from your start arguments");
Environment.Exit(1);
}
2018-06-16 08:32:08 +00:00
if (runtimeSettings.DoSSLFix != null)
{
logger.Error("SSLFix has been deprecated, please remove it from your start arguments");
Environment.Exit(1);
}
2018-06-16 08:32:08 +00:00
if (runtimeSettings.LogRequests)
{
logger.Info("Logging enabled.");
}
2018-06-16 08:32:08 +00:00
if (runtimeSettings.TracingEnabled)
{
logger.Info("Tracing enabled.");
}
2018-06-16 08:32:08 +00:00
if (runtimeSettings.IgnoreSslErrors == true)
{
logger.Error($"The IgnoreSslErrors option has been deprecated, please remove it from your start arguments");
}
2018-06-16 08:32:08 +00:00
if (!string.IsNullOrWhiteSpace(runtimeSettings.CustomDataFolder))
2018-06-16 08:32:08 +00:00
{
logger.Info("Jackett Data will be stored in: " + runtimeSettings.CustomDataFolder);
2018-06-16 08:32:08 +00:00
}
if (runtimeSettings.ProxyConnection != null)
{
logger.Info("Proxy enabled. " + runtimeSettings.ProxyConnection);
}
2018-06-16 08:32:08 +00:00
}
public static void ProcessWindowsSpecificArgs(ConsoleOptions consoleOptions, IProcessService processService, ServerConfig serverConfig, Logger logger)
{
IServiceConfigService serviceConfigService = new ServiceConfigService();
/* ====== Actions ===== */
// Install service
if (consoleOptions.Install)
{
logger.Info("Initiating Jackett service install");
serviceConfigService.Install();
Environment.Exit(1);
}
// Uninstall service
if (consoleOptions.Uninstall)
{
logger.Info("Initiating Jackett service uninstall");
ReserveUrls(processService, serverConfig, logger, doInstall: false);
serviceConfigService.Uninstall();
Environment.Exit(1);
}
// Start Service
if (consoleOptions.StartService)
{
if (!serviceConfigService.ServiceRunning())
{
logger.Info("Initiating Jackett service start");
serviceConfigService.Start();
}
Environment.Exit(1);
}
// Stop Service
if (consoleOptions.StopService)
{
if (serviceConfigService.ServiceRunning())
{
logger.Info("Initiating Jackett service stop");
serviceConfigService.Stop();
}
Environment.Exit(1);
}
// Reserve urls
if (consoleOptions.ReserveUrls)
{
logger.Info("Initiating ReserveUrls");
ReserveUrls(processService, serverConfig, logger, doInstall: true);
Environment.Exit(1);
}
}
2018-06-17 04:39:49 +00:00
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");
serverConfig.GetListenAddresses(false).ToList().ForEach(u => RunNetSh(processService, string.Format("http delete urlacl {0}", u)));
serverConfig.GetListenAddresses(true).ToList().ForEach(u => RunNetSh(processService, string.Format("http delete urlacl {0}", u)));
if (doInstall)
{
logger.Debug("Reserving Urls");
serverConfig.GetListenAddresses(serverConfig.AllowExternal).ToList().ForEach(u => RunNetSh(processService, string.Format("http add urlacl {0} sddl=D:(A;;GX;;;S-1-1-0)", u)));
logger.Debug("Urls reserved");
}
}
private static void RunNetSh(IProcessService processService, string args)
{
processService.StartProcessAndLog("netsh.exe", args);
}
2018-06-16 08:32:08 +00:00
}
}