2017-04-15 08:45:10 +00:00
|
|
|
|
using Autofac;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Reflection;
|
2017-11-05 09:42:03 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2018-03-10 08:05:56 +00:00
|
|
|
|
using Jackett.Common.Indexers;
|
|
|
|
|
using Jackett.Common.Indexers.Meta;
|
2017-11-13 08:38:38 +00:00
|
|
|
|
using Jackett.Common.Models.Config;
|
2018-03-10 08:05:56 +00:00
|
|
|
|
using Jackett.Common.Services;
|
|
|
|
|
using Jackett.Common.Services.Interfaces;
|
|
|
|
|
using Jackett.Common.Utils;
|
|
|
|
|
using Jackett.Common.Utils.Clients;
|
2017-04-15 08:45:10 +00:00
|
|
|
|
|
2017-11-13 08:38:38 +00:00
|
|
|
|
namespace Jackett.Common.Plumbing
|
2017-04-15 08:45:10 +00:00
|
|
|
|
{
|
|
|
|
|
public class JackettModule : Autofac.Module
|
|
|
|
|
{
|
2017-11-13 08:38:38 +00:00
|
|
|
|
private RuntimeSettings _runtimeSettings;
|
|
|
|
|
|
|
|
|
|
public JackettModule (RuntimeSettings runtimeSettings)
|
|
|
|
|
{
|
|
|
|
|
_runtimeSettings = runtimeSettings;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-11 20:32:56 +00:00
|
|
|
|
protected override void Load(ContainerBuilder builder)
|
2017-04-15 08:45:10 +00:00
|
|
|
|
{
|
2017-11-05 09:42:03 +00:00
|
|
|
|
// Just register everything! TODO: Something better and more explicit than scanning everything.
|
2017-11-08 14:45:21 +00:00
|
|
|
|
builder.RegisterAssemblyTypes(typeof(JackettModule).Assembly)
|
2017-11-05 09:42:03 +00:00
|
|
|
|
.Except<IIndexer>()
|
|
|
|
|
.Except<IImdbResolver>()
|
|
|
|
|
.Except<OmdbResolver>()
|
|
|
|
|
.Except<IFallbackStrategyProvider>()
|
|
|
|
|
.Except<ImdbFallbackStrategyProvider>()
|
|
|
|
|
.Except<IFallbackStrategy>()
|
|
|
|
|
.Except<ImdbFallbackStrategy>()
|
|
|
|
|
.Except<IResultFilterProvider>()
|
|
|
|
|
.Except<ImdbTitleResultFilterProvider>()
|
|
|
|
|
.Except<IResultFilter>()
|
|
|
|
|
.Except<ImdbTitleResultFilterProvider>()
|
|
|
|
|
.Except<BaseMetaIndexer>()
|
|
|
|
|
.Except<AggregateIndexer>()
|
|
|
|
|
.Except<CardigannIndexer>()
|
|
|
|
|
.AsImplementedInterfaces().SingleInstance();
|
2017-11-13 08:38:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
builder.RegisterInstance(_runtimeSettings);
|
2017-11-05 09:42:03 +00:00
|
|
|
|
builder.Register(ctx =>
|
|
|
|
|
{
|
|
|
|
|
return BuildServerConfig(ctx);
|
|
|
|
|
}).As<ServerConfig>().SingleInstance();
|
2017-07-11 20:32:56 +00:00
|
|
|
|
builder.RegisterType<HttpWebClient>();
|
2017-11-13 08:38:38 +00:00
|
|
|
|
|
2017-04-15 08:45:10 +00:00
|
|
|
|
// Register the best web client for the platform or the override
|
2017-11-13 08:38:38 +00:00
|
|
|
|
switch (_runtimeSettings.ClientOverride)
|
2017-07-11 20:32:56 +00:00
|
|
|
|
{
|
|
|
|
|
case "httpclient":
|
2017-12-07 13:35:50 +00:00
|
|
|
|
RegisterWebClient<HttpWebClient>(builder);
|
2017-07-11 20:32:56 +00:00
|
|
|
|
break;
|
|
|
|
|
case "httpclient2":
|
2017-12-07 13:35:50 +00:00
|
|
|
|
RegisterWebClient<HttpWebClient2>(builder);
|
2017-07-11 20:32:56 +00:00
|
|
|
|
break;
|
|
|
|
|
case "safecurl":
|
2017-12-07 13:35:50 +00:00
|
|
|
|
RegisterWebClient<UnixSafeCurlWebClient>(builder);
|
2017-07-11 20:32:56 +00:00
|
|
|
|
break;
|
|
|
|
|
case "libcurl":
|
2017-12-07 13:35:50 +00:00
|
|
|
|
RegisterWebClient<UnixLibCurlWebClient>(builder);
|
2017-07-11 20:32:56 +00:00
|
|
|
|
break;
|
|
|
|
|
case "automatic":
|
|
|
|
|
default:
|
2017-11-05 09:42:03 +00:00
|
|
|
|
if (System.Environment.OSVersion.Platform != PlatformID.Unix)
|
2017-07-11 20:32:56 +00:00
|
|
|
|
{
|
2017-12-07 13:35:50 +00:00
|
|
|
|
RegisterWebClient<HttpWebClient>(builder);
|
2017-11-05 09:42:03 +00:00
|
|
|
|
break;
|
2017-07-11 20:32:56 +00:00
|
|
|
|
}
|
2017-11-05 09:42:03 +00:00
|
|
|
|
var usehttpclient = DetectMonoCompatabilityWithHttpClient();
|
|
|
|
|
if (usehttpclient)
|
2017-12-07 13:35:50 +00:00
|
|
|
|
RegisterWebClient<HttpWebClient>(builder);
|
2017-04-15 08:45:10 +00:00
|
|
|
|
else
|
2017-12-07 13:35:50 +00:00
|
|
|
|
RegisterWebClient<UnixLibCurlWebClient>(builder);
|
2017-07-11 20:32:56 +00:00
|
|
|
|
break;
|
2017-04-15 08:45:10 +00:00
|
|
|
|
}
|
2017-12-07 13:35:50 +00:00
|
|
|
|
}
|
2017-11-05 09:42:03 +00:00
|
|
|
|
|
2017-12-07 13:35:50 +00:00
|
|
|
|
private void RegisterWebClient<WebClientType>(ContainerBuilder builder)
|
|
|
|
|
{
|
2018-06-11 07:17:56 +00:00
|
|
|
|
//TODO: Remove once off Owin
|
|
|
|
|
if (EnvironmentUtil.IsRunningLegacyOwin)
|
|
|
|
|
{
|
|
|
|
|
Engine.WebClientType = typeof(WebClientType);
|
|
|
|
|
}
|
2017-12-07 13:35:50 +00:00
|
|
|
|
builder.RegisterType<WebClientType>().As<WebClient>();
|
2017-11-05 09:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-13 08:38:38 +00:00
|
|
|
|
private ServerConfig BuildServerConfig(IComponentContext ctx)
|
2017-11-05 09:42:03 +00:00
|
|
|
|
{
|
|
|
|
|
var configService = ctx.Resolve<IConfigurationService>();
|
|
|
|
|
// Load config
|
|
|
|
|
var config = configService.GetConfig<ServerConfig>();
|
|
|
|
|
if (config == null)
|
|
|
|
|
{
|
2017-11-13 08:38:38 +00:00
|
|
|
|
config = new ServerConfig(_runtimeSettings);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
//We don't load these out of the config files as it could get confusing to users who accidently save.
|
|
|
|
|
//In future we could flatten the serverconfig, and use command line parameters to override any configuration.
|
|
|
|
|
config.RuntimeSettings = _runtimeSettings;
|
2017-11-05 09:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(config.APIKey))
|
|
|
|
|
{
|
|
|
|
|
// Check for legacy key config
|
|
|
|
|
var apiKeyFile = Path.Combine(configService.GetAppDataFolder(), "api_key.txt");
|
|
|
|
|
if (File.Exists(apiKeyFile))
|
|
|
|
|
{
|
|
|
|
|
config.APIKey = File.ReadAllText(apiKeyFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for legacy settings
|
|
|
|
|
|
|
|
|
|
var path = Path.Combine(configService.GetAppDataFolder(), "config.json"); ;
|
|
|
|
|
var jsonReply = new JObject();
|
|
|
|
|
if (File.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
jsonReply = JObject.Parse(File.ReadAllText(path));
|
|
|
|
|
config.Port = (int)jsonReply["port"];
|
|
|
|
|
config.AllowExternal = (bool)jsonReply["public"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(config.APIKey))
|
|
|
|
|
config.APIKey = StringUtil.GenerateRandom(32);
|
|
|
|
|
|
|
|
|
|
configService.SaveConfig(config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(config.InstanceId))
|
|
|
|
|
{
|
|
|
|
|
config.InstanceId = StringUtil.GenerateRandom(64);
|
|
|
|
|
configService.SaveConfig(config);
|
|
|
|
|
}
|
2017-11-17 15:46:58 +00:00
|
|
|
|
config.ConfigChanged();
|
2017-11-05 09:42:03 +00:00
|
|
|
|
return config;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-13 08:38:38 +00:00
|
|
|
|
|
2017-11-05 09:42:03 +00:00
|
|
|
|
|
|
|
|
|
private static bool DetectMonoCompatabilityWithHttpClient()
|
|
|
|
|
{
|
|
|
|
|
bool usehttpclient = false;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Type monotype = Type.GetType("Mono.Runtime");
|
|
|
|
|
if (monotype != null)
|
|
|
|
|
{
|
|
|
|
|
MethodInfo displayName = monotype.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
|
|
|
|
|
if (displayName != null)
|
|
|
|
|
{
|
|
|
|
|
var monoVersion = displayName.Invoke(null, null).ToString();
|
|
|
|
|
var monoVersionO = new Version(monoVersion.Split(' ')[0]);
|
|
|
|
|
if ((monoVersionO.Major >= 4 && monoVersionO.Minor >= 8) || monoVersionO.Major >= 5)
|
|
|
|
|
{
|
|
|
|
|
// check if btls is supported
|
|
|
|
|
var monoSecurity = Assembly.Load("Mono.Security");
|
|
|
|
|
Type monoTlsProviderFactory = monoSecurity.GetType("Mono.Security.Interface.MonoTlsProviderFactory");
|
|
|
|
|
if (monoTlsProviderFactory != null)
|
|
|
|
|
{
|
|
|
|
|
MethodInfo isProviderSupported = monoTlsProviderFactory.GetMethod("IsProviderSupported");
|
|
|
|
|
if (isProviderSupported != null)
|
|
|
|
|
{
|
|
|
|
|
var btlsSupported = (bool)isProviderSupported.Invoke(null, new string[] { "btls" });
|
|
|
|
|
if (btlsSupported)
|
|
|
|
|
{
|
|
|
|
|
// initialize btls
|
|
|
|
|
MethodInfo initialize = monoTlsProviderFactory.GetMethod("Initialize", new[] { typeof(string) });
|
|
|
|
|
if (initialize != null)
|
|
|
|
|
{
|
|
|
|
|
initialize.Invoke(null, new string[] { "btls" });
|
|
|
|
|
usehttpclient = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Console.Out.WriteLine("Error while deciding which HttpWebClient to use: " + e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return usehttpclient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-04-15 08:45:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|