mirror of
https://github.com/Jackett/Jackett.git
synced 2025-09-17 17:34:09 +02:00
Get startup configuration without using DI
This commit is contained in:
@@ -144,8 +144,8 @@ namespace Jackett.Server.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//TODO
|
var consoleExePath = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace(".dll", ".exe");
|
||||||
//processService.StartProcessAndLog(System.Windows.Forms.Application.ExecutablePath, "--ReserveUrls", true);
|
processService.StartProcessAndLog(consoleExePath, "--ReserveUrls", true);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
|
@@ -84,66 +84,6 @@ namespace Jackett.Server
|
|||||||
{
|
{
|
||||||
Logger.Info("Proxy enabled. " + runtimeSettings.ProxyConnection);
|
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()
|
public static void RestartWebHost()
|
||||||
|
@@ -1,10 +1,14 @@
|
|||||||
using Jackett.Common;
|
using Jackett.Common;
|
||||||
using Jackett.Common.Models.Config;
|
using Jackett.Common.Models.Config;
|
||||||
using Jackett.Common.Services;
|
using Jackett.Common.Services;
|
||||||
|
using Jackett.Common.Services.Interfaces;
|
||||||
|
using Jackett.Server.Services;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NLog.Config;
|
using NLog.Config;
|
||||||
using NLog.Targets;
|
using NLog.Targets;
|
||||||
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Jackett.Server
|
namespace Jackett.Server
|
||||||
{
|
{
|
||||||
@@ -67,5 +71,77 @@ namespace Jackett.Server
|
|||||||
|
|
||||||
LogManager.ReconfigExistingLoggers();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,17 +1,16 @@
|
|||||||
using Autofac;
|
using CommandLine;
|
||||||
using CommandLine;
|
|
||||||
using CommandLine.Text;
|
using CommandLine.Text;
|
||||||
using Jackett.Common.Models.Config;
|
using Jackett.Common.Models.Config;
|
||||||
using Jackett.Common.Plumbing;
|
using Jackett.Common.Services;
|
||||||
using Jackett.Common.Services.Interfaces;
|
using Jackett.Common.Services.Interfaces;
|
||||||
using Jackett.Common.Utils;
|
using Jackett.Common.Utils;
|
||||||
using Jackett.Server.Services;
|
|
||||||
using Microsoft.AspNetCore;
|
using Microsoft.AspNetCore;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using NLog;
|
using NLog;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
@@ -62,6 +61,40 @@ namespace Jackett.Server
|
|||||||
Logger logger = LogManager.GetCurrentClassLogger();
|
Logger logger = LogManager.GetCurrentClassLogger();
|
||||||
logger.Info("Starting Jackett v" + EnvironmentUtil.JackettVersion);
|
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();
|
var builder = new ConfigurationBuilder();
|
||||||
builder.AddInMemoryCollection(runtimeDictionary);
|
builder.AddInMemoryCollection(runtimeDictionary);
|
||||||
|
|
||||||
@@ -69,18 +102,8 @@ namespace Jackett.Server
|
|||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
//hack TODO: Get the configuration without any DI
|
ServerConfig serverConfig = configurationService.BuildServerConfig(Settings);
|
||||||
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 = tempContainer.Resolve<ServerConfig>();
|
|
||||||
IConfigurationService configurationService = tempContainer.Resolve<IConfigurationService>();
|
|
||||||
IServerService serverService = tempContainer.Resolve<IServerService>();
|
|
||||||
Int32.TryParse(serverConfig.Port.ToString(), out Int32 configPort);
|
Int32.TryParse(serverConfig.Port.ToString(), out Int32 configPort);
|
||||||
|
|
||||||
if (!isWebHostRestart)
|
if (!isWebHostRestart)
|
||||||
@@ -97,7 +120,7 @@ namespace Jackett.Server
|
|||||||
{
|
{
|
||||||
if (ServerUtil.IsUserAdministrator())
|
if (ServerUtil.IsUserAdministrator())
|
||||||
{
|
{
|
||||||
serverService.ReserveUrls(doInstall: true);
|
Initialisation.ReserveUrls(processService, serverConfig, logger, doInstall: true);
|
||||||
}
|
}
|
||||||
else
|
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
|
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;
|
isWebHostRestart = false;
|
||||||
tempContainer.Dispose();
|
|
||||||
tempContainer = null;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -124,14 +145,13 @@ namespace Jackett.Server
|
|||||||
{
|
{
|
||||||
if (ex.InnerException is Microsoft.AspNetCore.Connections.AddressInUseException)
|
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);
|
Environment.Exit(1);
|
||||||
}
|
}
|
||||||
|
logger.Error(ex);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
} while (isWebHostRestart);
|
} while (isWebHostRestart);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Dictionary<string, string> GetValues(object obj)
|
public static Dictionary<string, string> GetValues(object obj)
|
||||||
@@ -154,6 +174,7 @@ namespace Jackett.Server
|
|||||||
Console.WriteLine("Deleting PID file " + PIDFile);
|
Console.WriteLine("Deleting PID file " + PIDFile);
|
||||||
File.Delete(PIDFile);
|
File.Delete(PIDFile);
|
||||||
}
|
}
|
||||||
|
LogManager.Shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
@@ -84,7 +84,6 @@ namespace Jackett.Server.Services
|
|||||||
|
|
||||||
public void Initalize()
|
public void Initalize()
|
||||||
{
|
{
|
||||||
logger.Info("Starting Jackett " + configService.GetVersion());
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var x = Environment.OSVersion;
|
var x = Environment.OSVersion;
|
||||||
@@ -102,6 +101,8 @@ namespace Jackett.Server.Services
|
|||||||
logger.Error("Error while getting MaxThreads details: " + e);
|
logger.Error("Error while getting MaxThreads details: " + e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.Info("App config/log directory: " + configService.GetAppDataFolder());
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var issuefile = "/etc/issue";
|
var issuefile = "/etc/issue";
|
||||||
|
@@ -5,6 +5,8 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.ServiceProcess;
|
using System.ServiceProcess;
|
||||||
using Jackett.Common.Services.Interfaces;
|
using Jackett.Common.Services.Interfaces;
|
||||||
|
using System.Reflection;
|
||||||
|
using Jackett.Common.Services;
|
||||||
|
|
||||||
namespace Jackett.Server.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 DESCRIPTION = "API Support for your favorite torrent trackers";
|
||||||
private const string SERVICEEXE = "JackettService.exe";
|
private const string SERVICEEXE = "JackettService.exe";
|
||||||
|
|
||||||
private IConfigurationService configService;
|
|
||||||
private IProcessService processService;
|
private IProcessService processService;
|
||||||
private Logger logger;
|
private Logger logger;
|
||||||
|
|
||||||
public ServiceConfigService(IConfigurationService c, IProcessService p, Logger l)
|
public ServiceConfigService()
|
||||||
{
|
{
|
||||||
configService = c;
|
logger = LogManager.GetCurrentClassLogger();
|
||||||
processService = p;
|
processService = new ProcessService(logger);
|
||||||
logger = l;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ServiceExists()
|
public bool ServiceExists()
|
||||||
@@ -64,10 +64,12 @@ namespace Jackett.Server.Services
|
|||||||
}
|
}
|
||||||
else
|
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)
|
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}";
|
string arg = $"create {NAME} start= auto binpath= \"{exePath}\" DisplayName= {NAME}";
|
||||||
|
Reference in New Issue
Block a user