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>();
|
2018-06-17 01:48:59 +00:00
|
|
|
|
return configService.BuildServerConfig(_runtimeSettings);
|
2017-11-05 09:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool DetectMonoCompatabilityWithHttpClient()
|
|
|
|
|
{
|
|
|
|
|
bool usehttpclient = false;
|
2018-06-17 01:48:59 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-11-05 09:42:03 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|