2018-06-22 12:21:10 +00:00
|
|
|
using System;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.ServiceProcess;
|
2020-02-09 18:08:34 +00:00
|
|
|
using Jackett.Common.Services.Interfaces;
|
|
|
|
using NLog;
|
2018-06-22 12:21:10 +00:00
|
|
|
|
|
|
|
namespace Jackett.Common.Services
|
|
|
|
{
|
|
|
|
public class WindowsServiceConfigService : IServiceConfigService
|
|
|
|
{
|
|
|
|
private const string NAME = "Jackett";
|
|
|
|
private const string DESCRIPTION = "API Support for your favorite torrent trackers";
|
|
|
|
private const string SERVICEEXE = "JackettService.exe";
|
|
|
|
|
2020-02-10 22:16:19 +00:00
|
|
|
private readonly IProcessService processService;
|
|
|
|
private readonly Logger logger;
|
2018-06-22 12:21:10 +00:00
|
|
|
|
|
|
|
public WindowsServiceConfigService(IProcessService p, Logger l)
|
|
|
|
{
|
|
|
|
processService = p;
|
|
|
|
logger = l;
|
|
|
|
}
|
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public bool ServiceExists() => GetService(NAME) != null;
|
2018-06-22 12:21:10 +00:00
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public bool ServiceRunning() =>
|
|
|
|
GetService(NAME)?.Status == ServiceControllerStatus.Running;
|
2018-06-22 12:21:10 +00:00
|
|
|
|
|
|
|
public void Start()
|
|
|
|
{
|
|
|
|
var service = GetService(NAME);
|
|
|
|
service.Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Stop()
|
|
|
|
{
|
|
|
|
var service = GetService(NAME);
|
|
|
|
service.Stop();
|
|
|
|
}
|
|
|
|
|
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-22 12:21:10 +00:00
|
|
|
|
|
|
|
public void Install()
|
|
|
|
{
|
|
|
|
if (ServiceExists())
|
|
|
|
{
|
|
|
|
logger.Warn("The service is already installed!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-13 07:34:45 +00:00
|
|
|
// Use EscapedCodeBase to avoid Uri reserved characters from causing bugs
|
|
|
|
// https://stackoverflow.com/questions/896572
|
|
|
|
var applicationFolder = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath);
|
2018-06-22 12:21:10 +00:00
|
|
|
|
|
|
|
var exePath = Path.Combine(applicationFolder, SERVICEEXE);
|
|
|
|
if (!File.Exists(exePath) && Debugger.IsAttached)
|
|
|
|
{
|
|
|
|
exePath = Path.Combine(applicationFolder, "..\\..\\..\\Jackett.Service\\bin\\Debug", SERVICEEXE);
|
|
|
|
}
|
|
|
|
|
2020-02-10 22:16:19 +00:00
|
|
|
var arg = $"create {NAME} start= auto binpath= \"{exePath}\" DisplayName= {NAME}";
|
2018-06-22 12:21:10 +00:00
|
|
|
|
|
|
|
processService.StartProcessAndLog("sc.exe", arg, true);
|
|
|
|
|
|
|
|
processService.StartProcessAndLog("sc.exe", $"description {NAME} \"{DESCRIPTION}\"", true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Uninstall()
|
|
|
|
{
|
|
|
|
RemoveService();
|
|
|
|
|
|
|
|
processService.StartProcessAndLog("sc.exe", $"delete {NAME}", true);
|
|
|
|
|
|
|
|
logger.Info("The service was uninstalled.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveService()
|
|
|
|
{
|
|
|
|
var service = GetService(NAME);
|
|
|
|
if (service == null)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
logger.Info("Service stopped.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logger.Error("Failed to stop the service");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logger.Warn("The service was already stopped");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|