From 72a18e9b7346c3a189b2a2135f3436bb35ecfbc7 Mon Sep 17 00:00:00 2001 From: flightlevel Date: Sun, 17 Jun 2018 12:39:03 +1000 Subject: [PATCH] Get startup configuration without using DI --- .../ServerConfigurationController.cs | 4 +- src/Jackett.Server/Helper.cs | 60 --------------- src/Jackett.Server/Initialisation.cs | 76 +++++++++++++++++++ src/Jackett.Server/Program.cs | 63 ++++++++++----- src/Jackett.Server/Services/ServerService.cs | 3 +- .../Services/ServiceConfigService.cs | 16 ++-- 6 files changed, 131 insertions(+), 91 deletions(-) diff --git a/src/Jackett.Server/Controllers/ServerConfigurationController.cs b/src/Jackett.Server/Controllers/ServerConfigurationController.cs index 6924d188d..cc9a2fb57 100644 --- a/src/Jackett.Server/Controllers/ServerConfigurationController.cs +++ b/src/Jackett.Server/Controllers/ServerConfigurationController.cs @@ -144,8 +144,8 @@ namespace Jackett.Server.Controllers { try { - //TODO - //processService.StartProcessAndLog(System.Windows.Forms.Application.ExecutablePath, "--ReserveUrls", true); + var consoleExePath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace(".dll", ".exe"); + processService.StartProcessAndLog(consoleExePath, "--ReserveUrls", true); } catch { diff --git a/src/Jackett.Server/Helper.cs b/src/Jackett.Server/Helper.cs index 3403bec8c..dbe1ae974 100644 --- a/src/Jackett.Server/Helper.cs +++ b/src/Jackett.Server/Helper.cs @@ -84,66 +84,6 @@ namespace Jackett.Server { Logger.Info("Proxy enabled. " + runtimeSettings.ProxyConnection); } - - if (ConsoleOptions.Install || ConsoleOptions.Uninstall || ConsoleOptions.StartService || ConsoleOptions.StopService || ConsoleOptions.ReserveUrls) - { - bool isWindows = Environment.OSVersion.Platform == PlatformID.Win32NT; - - if (!isWindows) - { - Logger.Error($"ReserveUrls and service arguments only apply to Windows, please remove them from your start arguments"); - Environment.Exit(1); - } - } - - - /* ====== 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"); - 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"); - ServerService.ReserveUrls(doInstall: true); - Environment.Exit(1); - } } public static void RestartWebHost() diff --git a/src/Jackett.Server/Initialisation.cs b/src/Jackett.Server/Initialisation.cs index beccea856..46e3bb94f 100644 --- a/src/Jackett.Server/Initialisation.cs +++ b/src/Jackett.Server/Initialisation.cs @@ -1,10 +1,14 @@ using Jackett.Common; using Jackett.Common.Models.Config; using Jackett.Common.Services; +using Jackett.Common.Services.Interfaces; +using Jackett.Server.Services; using NLog; using NLog.Config; using NLog.Targets; +using System; using System.IO; +using System.Linq; namespace Jackett.Server { @@ -67,5 +71,77 @@ namespace Jackett.Server LogManager.ReconfigExistingLoggers(); } + + 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); + } + } + + 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); + } } } diff --git a/src/Jackett.Server/Program.cs b/src/Jackett.Server/Program.cs index 3c4acb3c6..0a524b691 100644 --- a/src/Jackett.Server/Program.cs +++ b/src/Jackett.Server/Program.cs @@ -1,17 +1,16 @@ -using Autofac; -using CommandLine; +using CommandLine; using CommandLine.Text; using Jackett.Common.Models.Config; -using Jackett.Common.Plumbing; +using Jackett.Common.Services; using Jackett.Common.Services.Interfaces; using Jackett.Common.Utils; -using Jackett.Server.Services; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using NLog; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; @@ -62,6 +61,40 @@ namespace Jackett.Server Logger logger = LogManager.GetCurrentClassLogger(); logger.Info("Starting Jackett v" + EnvironmentUtil.JackettVersion); + // create PID file early + if (!string.IsNullOrWhiteSpace(Settings.PIDFile)) + { + try + { + var proc = Process.GetCurrentProcess(); + File.WriteAllText(Settings.PIDFile, proc.Id.ToString()); + } + catch (Exception e) + { + logger.Error(e, "Error while creating the PID file"); + } + } + + ISerializeService serializeService = new SerializeService(); + IProcessService processService = new ProcessService(logger); + IConfigurationService configurationService = new ConfigurationService(serializeService, processService, logger, Settings); + + if (consoleOptions.Install || consoleOptions.Uninstall || consoleOptions.StartService || consoleOptions.StopService || consoleOptions.ReserveUrls) + { + bool isWindows = Environment.OSVersion.Platform == PlatformID.Win32NT; + + if (isWindows) + { + ServerConfig serverConfig = configurationService.BuildServerConfig(Settings); + Initialisation.ProcessWindowsSpecificArgs(consoleOptions, processService, serverConfig, logger); + } + else + { + logger.Error($"ReserveUrls and service arguments only apply to Windows, please remove them from your start arguments"); + Environment.Exit(1); + } + } + var builder = new ConfigurationBuilder(); builder.AddInMemoryCollection(runtimeDictionary); @@ -69,18 +102,8 @@ namespace Jackett.Server do { - //hack TODO: Get the configuration without any DI - var containerBuilder = new ContainerBuilder(); - Helper.SetupLogging(Settings, containerBuilder); - containerBuilder.RegisterModule(new JackettModule(Settings)); - containerBuilder.RegisterType().As(); - containerBuilder.RegisterType().As(); - containerBuilder.RegisterType().As(); - var tempContainer = containerBuilder.Build(); + ServerConfig serverConfig = configurationService.BuildServerConfig(Settings); - ServerConfig serverConfig = tempContainer.Resolve(); - IConfigurationService configurationService = tempContainer.Resolve(); - IServerService serverService = tempContainer.Resolve(); Int32.TryParse(serverConfig.Port.ToString(), out Int32 configPort); if (!isWebHostRestart) @@ -97,7 +120,7 @@ namespace Jackett.Server { if (ServerUtil.IsUserAdministrator()) { - serverService.ReserveUrls(doInstall: true); + Initialisation.ReserveUrls(processService, serverConfig, logger, doInstall: true); } else { @@ -113,8 +136,6 @@ namespace Jackett.Server 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; - tempContainer.Dispose(); - tempContainer = null; try { @@ -124,14 +145,13 @@ namespace Jackett.Server { if (ex.InnerException is Microsoft.AspNetCore.Connections.AddressInUseException) { - Console.WriteLine("Address already in use: Most likely Jackett is already running. " + ex.Message); + logger.Error("Address already in use: Most likely Jackett is already running. " + ex.Message); Environment.Exit(1); } + logger.Error(ex); throw; } - } while (isWebHostRestart); - } public static Dictionary GetValues(object obj) @@ -154,6 +174,7 @@ namespace Jackett.Server Console.WriteLine("Deleting PID file " + PIDFile); File.Delete(PIDFile); } + LogManager.Shutdown(); } } catch (Exception ex) diff --git a/src/Jackett.Server/Services/ServerService.cs b/src/Jackett.Server/Services/ServerService.cs index 81b01cc73..20b56ca08 100644 --- a/src/Jackett.Server/Services/ServerService.cs +++ b/src/Jackett.Server/Services/ServerService.cs @@ -84,7 +84,6 @@ namespace Jackett.Server.Services public void Initalize() { - logger.Info("Starting Jackett " + configService.GetVersion()); try { var x = Environment.OSVersion; @@ -102,6 +101,8 @@ namespace Jackett.Server.Services logger.Error("Error while getting MaxThreads details: " + e); } + logger.Info("App config/log directory: " + configService.GetAppDataFolder()); + try { var issuefile = "/etc/issue"; diff --git a/src/Jackett.Server/Services/ServiceConfigService.cs b/src/Jackett.Server/Services/ServiceConfigService.cs index 5fb71a0c8..a0ff5ac4d 100644 --- a/src/Jackett.Server/Services/ServiceConfigService.cs +++ b/src/Jackett.Server/Services/ServiceConfigService.cs @@ -5,6 +5,8 @@ using System.IO; using System.Linq; using System.ServiceProcess; using Jackett.Common.Services.Interfaces; +using System.Reflection; +using Jackett.Common.Services; namespace Jackett.Server.Services { @@ -14,15 +16,13 @@ namespace Jackett.Server.Services private const string DESCRIPTION = "API Support for your favorite torrent trackers"; private const string SERVICEEXE = "JackettService.exe"; - private IConfigurationService configService; private IProcessService processService; private Logger logger; - public ServiceConfigService(IConfigurationService c, IProcessService p, Logger l) + public ServiceConfigService() { - configService = c; - processService = p; - logger = l; + logger = LogManager.GetCurrentClassLogger(); + processService = new ProcessService(logger); } public bool ServiceExists() @@ -64,10 +64,12 @@ namespace Jackett.Server.Services } else { - var exePath = Path.Combine(configService.ApplicationFolder(), SERVICEEXE); + string applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath); + + var exePath = Path.Combine(applicationFolder, SERVICEEXE); if (!File.Exists(exePath) && Debugger.IsAttached) { - exePath = Path.Combine(configService.ApplicationFolder(), "..\\..\\..\\Jackett.Service\\bin\\Debug", SERVICEEXE); + exePath = Path.Combine(applicationFolder, "..\\..\\..\\Jackett.Service\\bin\\Debug", SERVICEEXE); } string arg = $"create {NAME} start= auto binpath= \"{exePath}\" DisplayName= {NAME}";