2020-02-25 16:08:03 +00:00
|
|
|
using System;
|
2018-06-10 02:29:07 +00:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2018-06-17 02:39:03 +00:00
|
|
|
using System.Reflection;
|
2020-02-09 18:08:34 +00:00
|
|
|
using System.ServiceProcess;
|
2018-06-17 02:39:03 +00:00
|
|
|
using Jackett.Common.Services;
|
2020-02-09 18:08:34 +00:00
|
|
|
using Jackett.Common.Services.Interfaces;
|
2020-12-12 20:38:33 +00:00
|
|
|
using Jackett.Common.Utils;
|
2020-02-09 18:08:34 +00:00
|
|
|
using NLog;
|
2018-06-10 02:29:07 +00:00
|
|
|
|
2018-06-10 02:33:16 +00:00
|
|
|
namespace Jackett.Server.Services
|
2018-06-10 02:29:07 +00:00
|
|
|
{
|
2021-05-16 18:13:54 +00:00
|
|
|
// This code is Windows specific but we are already doing the checks our way
|
2020-12-12 00:19:51 +00:00
|
|
|
#pragma warning disable CA1416
|
2018-06-10 02:29:07 +00:00
|
|
|
public class ServiceConfigService : IServiceConfigService
|
|
|
|
{
|
|
|
|
private const string NAME = "Jackett";
|
2018-06-10 02:33:16 +00:00
|
|
|
private const string DESCRIPTION = "API Support for your favorite torrent trackers";
|
2018-06-10 02:29:07 +00:00
|
|
|
private const string SERVICEEXE = "JackettService.exe";
|
|
|
|
|
2020-02-10 22:16:19 +00:00
|
|
|
private readonly IProcessService processService;
|
|
|
|
private readonly Logger logger;
|
2018-06-10 02:29:07 +00:00
|
|
|
|
2018-06-17 02:39:03 +00:00
|
|
|
public ServiceConfigService()
|
2018-06-10 02:29:07 +00:00
|
|
|
{
|
2018-06-17 02:39:03 +00:00
|
|
|
logger = LogManager.GetCurrentClassLogger();
|
|
|
|
processService = new ProcessService(logger);
|
2018-06-10 02:29:07 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public bool ServiceExists() => GetService(NAME) != null;
|
2018-06-10 02:29:07 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public bool ServiceRunning() =>
|
|
|
|
GetService(NAME)?.Status == ServiceControllerStatus.Running;
|
2018-06-10 02:29:07 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public void Start() => GetService(NAME).Start();
|
2018-06-10 02:29:07 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public void Stop() => GetService(NAME).Stop();
|
2018-06-10 02:29:07 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public ServiceController GetService(string serviceName) =>
|
|
|
|
ServiceController
|
|
|
|
.GetServices()
|
|
|
|
.FirstOrDefault(c => string.Equals(c.ServiceName, serviceName, StringComparison.InvariantCultureIgnoreCase));
|
2018-06-10 02:29:07 +00:00
|
|
|
|
|
|
|
public void Install()
|
|
|
|
{
|
|
|
|
if (ServiceExists())
|
|
|
|
{
|
|
|
|
logger.Warn("The service is already installed!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-12-12 20:38:33 +00:00
|
|
|
var applicationFolder = EnvironmentUtil.JackettInstallationPath();
|
2018-06-17 02:39:03 +00:00
|
|
|
var exePath = Path.Combine(applicationFolder, SERVICEEXE);
|
2018-06-10 02:29:07 +00:00
|
|
|
if (!File.Exists(exePath) && Debugger.IsAttached)
|
|
|
|
{
|
2018-06-17 02:39:03 +00:00
|
|
|
exePath = Path.Combine(applicationFolder, "..\\..\\..\\Jackett.Service\\bin\\Debug", SERVICEEXE);
|
2018-06-10 02:29:07 +00:00
|
|
|
}
|
|
|
|
|
2020-02-10 22:16:19 +00:00
|
|
|
var arg = $"create {NAME} start= auto binpath= \"{exePath}\" DisplayName= {NAME}";
|
2018-06-10 02:29:07 +00:00
|
|
|
|
2018-06-10 02:33:16 +00:00
|
|
|
processService.StartProcessAndLog("sc.exe", arg, true);
|
2018-06-10 02:29:07 +00:00
|
|
|
|
2018-06-10 02:33:16 +00:00
|
|
|
processService.StartProcessAndLog("sc.exe", $"description {NAME} \"{DESCRIPTION}\"", true);
|
2018-06-10 02:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Uninstall()
|
|
|
|
{
|
|
|
|
RemoveService();
|
|
|
|
|
2018-06-10 02:33:16 +00:00
|
|
|
processService.StartProcessAndLog("sc.exe", $"delete {NAME}", true);
|
2018-06-10 02:29:07 +00:00
|
|
|
|
|
|
|
logger.Info("The service was uninstalled.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveService()
|
|
|
|
{
|
|
|
|
var service = GetService(NAME);
|
2020-02-09 02:35:16 +00:00
|
|
|
if (service == null)
|
2018-06-10 02:29:07 +00:00
|
|
|
{
|
|
|
|
logger.Warn("The service is already uninstalled");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (service.Status != ServiceControllerStatus.Stopped)
|
|
|
|
{
|
|
|
|
service.Stop();
|
|
|
|
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(60));
|
|
|
|
|
|
|
|
service.Refresh();
|
|
|
|
if (service.Status == ServiceControllerStatus.Stopped)
|
2018-06-10 02:33:16 +00:00
|
|
|
{
|
2018-06-10 02:29:07 +00:00
|
|
|
logger.Info("Service stopped.");
|
2018-06-10 02:33:16 +00:00
|
|
|
}
|
2018-06-10 02:29:07 +00:00
|
|
|
else
|
2018-06-10 02:33:16 +00:00
|
|
|
{
|
2018-06-10 02:29:07 +00:00
|
|
|
logger.Error("Failed to stop the service");
|
2018-06-10 02:33:16 +00:00
|
|
|
}
|
2018-06-10 02:29:07 +00:00
|
|
|
}
|
|
|
|
else
|
2018-06-10 02:33:16 +00:00
|
|
|
{
|
2018-06-10 02:29:07 +00:00
|
|
|
logger.Warn("The service was already stopped");
|
2018-06-10 02:33:16 +00:00
|
|
|
}
|
2018-06-10 02:29:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-12 00:19:51 +00:00
|
|
|
#pragma warning restore CA1416
|
2018-06-10 02:29:07 +00:00
|
|
|
}
|