Get startup configuration without using DI

This commit is contained in:
flightlevel
2018-06-17 12:39:03 +10:00
parent 6be64bbe36
commit 72a18e9b73
6 changed files with 131 additions and 91 deletions

View File

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

View File

@@ -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()

View File

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

View File

@@ -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<ServerService>().As<IServerService>();
containerBuilder.RegisterType<SecuityService>().As<ISecuityService>();
containerBuilder.RegisterType<ProtectionService>().As<IProtectionService>();
var tempContainer = containerBuilder.Build();
ServerConfig serverConfig = configurationService.BuildServerConfig(Settings);
ServerConfig serverConfig = tempContainer.Resolve<ServerConfig>();
IConfigurationService configurationService = tempContainer.Resolve<IConfigurationService>();
IServerService serverService = tempContainer.Resolve<IServerService>();
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<string, string> GetValues(object obj)
@@ -154,6 +174,7 @@ namespace Jackett.Server
Console.WriteLine("Deleting PID file " + PIDFile);
File.Delete(PIDFile);
}
LogManager.Shutdown();
}
}
catch (Exception ex)

View File

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

View File

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