2018-05-01 12:00:02 +00:00
|
|
|
|
using Autofac;
|
|
|
|
|
using AutoMapper;
|
|
|
|
|
using Jackett.Common.Models;
|
|
|
|
|
using Jackett.Common.Models.Config;
|
|
|
|
|
using Jackett.Common.Services.Interfaces;
|
|
|
|
|
using Jackett.Common.Utils.Clients;
|
2018-06-10 02:33:16 +00:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2018-05-01 12:00:02 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Jackett.Server
|
|
|
|
|
{
|
2018-06-03 11:11:18 +00:00
|
|
|
|
public static class Helper
|
2018-05-01 12:00:02 +00:00
|
|
|
|
{
|
|
|
|
|
public static IContainer ApplicationContainer { get; set; }
|
2018-06-10 02:33:16 +00:00
|
|
|
|
public static IApplicationLifetime applicationLifetime;
|
2018-05-01 12:00:02 +00:00
|
|
|
|
private static bool _automapperInitialised = false;
|
|
|
|
|
|
|
|
|
|
public static void Initialize()
|
|
|
|
|
{
|
|
|
|
|
if (_automapperInitialised == false)
|
|
|
|
|
{
|
|
|
|
|
//Automapper only likes being initialized once per app domain.
|
|
|
|
|
//Since we can restart Jackett from the command line it's possible that we'll build the container more than once. (tests do this too)
|
|
|
|
|
InitAutomapper();
|
|
|
|
|
_automapperInitialised = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Load the indexers
|
|
|
|
|
ServerService.Initalize();
|
2018-06-14 09:21:31 +00:00
|
|
|
|
|
|
|
|
|
//Kicks off the update checker
|
|
|
|
|
ServerService.Start();
|
2018-07-10 12:22:02 +00:00
|
|
|
|
|
|
|
|
|
Logger.Debug("Helper initialization complete");
|
2018-05-01 12:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-10 02:33:16 +00:00
|
|
|
|
public static void RestartWebHost()
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Restart of the web application host (not process) initiated");
|
|
|
|
|
Program.isWebHostRestart = true;
|
|
|
|
|
applicationLifetime.StopApplication();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void StopWebHost()
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Jackett is being stopped");
|
|
|
|
|
applicationLifetime.StopApplication();
|
2018-06-03 11:11:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IConfigurationService ConfigService
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ApplicationContainer.Resolve<IConfigurationService>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IServerService ServerService
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ApplicationContainer.Resolve<IServerService>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-10 02:33:16 +00:00
|
|
|
|
public static IServiceConfigService ServiceConfigService
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ApplicationContainer.Resolve<IServiceConfigService>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-03 11:11:18 +00:00
|
|
|
|
public static ServerConfig ServerConfiguration
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ApplicationContainer.Resolve<ServerConfig>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Logger Logger
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return ApplicationContainer.Resolve<Logger>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-01 12:00:02 +00:00
|
|
|
|
private static void InitAutomapper()
|
|
|
|
|
{
|
|
|
|
|
Mapper.Initialize(cfg =>
|
|
|
|
|
{
|
|
|
|
|
cfg.CreateMap<WebClientByteResult, WebClientStringResult>().ForMember(x => x.Content, opt => opt.Ignore()).AfterMap((be, str) =>
|
|
|
|
|
{
|
|
|
|
|
var encoding = be.Request.Encoding ?? Encoding.UTF8;
|
|
|
|
|
str.Content = encoding.GetString(be.Content);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cfg.CreateMap<WebClientStringResult, WebClientByteResult>().ForMember(x => x.Content, opt => opt.Ignore()).AfterMap((str, be) =>
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(str.Content))
|
|
|
|
|
{
|
|
|
|
|
var encoding = str.Request.Encoding ?? Encoding.UTF8;
|
|
|
|
|
be.Content = encoding.GetBytes(str.Content);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cfg.CreateMap<WebClientStringResult, WebClientStringResult>();
|
|
|
|
|
cfg.CreateMap<WebClientByteResult, WebClientByteResult>();
|
|
|
|
|
cfg.CreateMap<ReleaseInfo, ReleaseInfo>();
|
|
|
|
|
|
|
|
|
|
cfg.CreateMap<ReleaseInfo, TrackerCacheResult>().AfterMap((r, t) =>
|
|
|
|
|
{
|
|
|
|
|
if (r.Category != null)
|
|
|
|
|
{
|
2018-06-03 11:11:18 +00:00
|
|
|
|
t.CategoryDesc = string.Join(", ", r.Category.Select(x => TorznabCatType.GetCatDesc(x)).Where(x => !string.IsNullOrEmpty(x)));
|
2018-05-01 12:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
t.CategoryDesc = "";
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-18 11:50:09 +00:00
|
|
|
|
public static void SetupLogging(ContainerBuilder builder)
|
2018-05-01 12:00:02 +00:00
|
|
|
|
{
|
2018-06-16 08:32:08 +00:00
|
|
|
|
Logger logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2018-05-01 12:00:02 +00:00
|
|
|
|
if (builder != null)
|
|
|
|
|
{
|
2018-06-16 08:32:08 +00:00
|
|
|
|
builder.RegisterInstance(logger).SingleInstance();
|
2018-05-01 12:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-06-22 12:35:58 +00:00
|
|
|
|
|
|
|
|
|
public static void SetLogLevel(LogLevel level)
|
|
|
|
|
{
|
|
|
|
|
foreach (var rule in LogManager.Configuration.LoggingRules)
|
|
|
|
|
{
|
2018-11-14 16:52:56 +00:00
|
|
|
|
if (rule.LoggerNamePattern == "Microsoft.*")
|
|
|
|
|
{
|
|
|
|
|
if (!rule.Levels.Contains(LogLevel.Debug))
|
|
|
|
|
{
|
|
|
|
|
//don't change the first microsoftRule
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var targets = LogManager.Configuration.ConfiguredNamedTargets;
|
|
|
|
|
if (level == LogLevel.Debug)
|
|
|
|
|
{
|
|
|
|
|
foreach (var target in targets)
|
|
|
|
|
{
|
|
|
|
|
rule.Targets.Add(target);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
foreach (var target in targets)
|
|
|
|
|
{
|
|
|
|
|
rule.Targets.Remove(target);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 12:35:58 +00:00
|
|
|
|
if (level == LogLevel.Debug)
|
|
|
|
|
{
|
|
|
|
|
if (!rule.Levels.Contains(LogLevel.Debug))
|
|
|
|
|
{
|
|
|
|
|
rule.EnableLoggingForLevel(LogLevel.Debug);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (rule.Levels.Contains(LogLevel.Debug))
|
|
|
|
|
{
|
|
|
|
|
rule.DisableLoggingForLevel(LogLevel.Debug);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LogManager.ReconfigExistingLoggers();
|
|
|
|
|
}
|
2018-05-01 12:00:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|