2020-02-25 16:08:03 +00:00
|
|
|
using System;
|
2020-02-23 20:38:59 +00:00
|
|
|
using System.Diagnostics;
|
2020-02-09 18:08:34 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using Autofac;
|
2018-05-01 12:00:02 +00:00
|
|
|
using Autofac.Extensions.DependencyInjection;
|
|
|
|
using Jackett.Common.Models.Config;
|
|
|
|
using Jackett.Common.Plumbing;
|
2020-12-11 22:14:21 +00:00
|
|
|
using Jackett.Common.Services;
|
2018-05-01 12:00:02 +00:00
|
|
|
using Jackett.Common.Services.Interfaces;
|
2018-05-05 07:08:46 +00:00
|
|
|
using Jackett.Server.Middleware;
|
2018-05-01 12:00:02 +00:00
|
|
|
using Jackett.Server.Services;
|
2018-05-12 02:44:47 +00:00
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2018-05-01 11:13:20 +00:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
2018-05-30 11:43:58 +00:00
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
2018-05-01 11:13:20 +00:00
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2018-05-12 02:44:47 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2018-07-13 12:15:09 +00:00
|
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
2018-05-12 02:44:47 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc.Authorization;
|
2018-05-05 07:08:46 +00:00
|
|
|
using Microsoft.AspNetCore.Rewrite;
|
2018-05-01 11:13:20 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2018-05-01 12:00:02 +00:00
|
|
|
using Newtonsoft.Json.Serialization;
|
2023-01-07 19:38:52 +00:00
|
|
|
#if !NET462
|
2019-10-14 17:51:33 +00:00
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
#endif
|
2018-05-01 11:13:20 +00:00
|
|
|
|
|
|
|
namespace Jackett.Server
|
|
|
|
{
|
|
|
|
public class Startup
|
|
|
|
{
|
2022-04-24 22:17:35 +00:00
|
|
|
private const string AllowAllOrigins = "AllowAllOrigins";
|
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
public Startup(IConfiguration configuration) => Configuration = configuration;
|
2018-05-01 11:13:20 +00:00
|
|
|
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
2018-05-01 12:00:02 +00:00
|
|
|
public IServiceProvider ConfigureServices(IServiceCollection services)
|
2018-05-01 11:13:20 +00:00
|
|
|
{
|
2022-04-24 22:17:35 +00:00
|
|
|
services.AddResponseCompression()
|
|
|
|
.AddCors(
|
|
|
|
options =>
|
|
|
|
{
|
|
|
|
options.AddPolicy(name: AllowAllOrigins, corsPolicyBuilder => corsPolicyBuilder.AllowAnyOrigin());
|
2018-05-12 02:51:46 +00:00
|
|
|
|
2022-04-24 22:17:35 +00:00
|
|
|
})
|
|
|
|
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
2018-05-12 02:44:47 +00:00
|
|
|
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,
|
|
|
|
options =>
|
|
|
|
{
|
2019-12-10 22:49:51 +00:00
|
|
|
options.LoginPath = new PathString("/UI/Login");
|
|
|
|
options.AccessDeniedPath = new PathString("/UI/Login");
|
|
|
|
options.LogoutPath = new PathString("/UI/Logout");
|
2018-05-12 02:44:47 +00:00
|
|
|
options.Cookie.Name = "Jackett";
|
|
|
|
});
|
|
|
|
|
2023-01-07 19:38:52 +00:00
|
|
|
#if NET462
|
2020-02-25 16:08:03 +00:00
|
|
|
services.AddMvc(
|
|
|
|
config => config.Filters.Add(
|
|
|
|
new AuthorizeFilter(
|
|
|
|
new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build())))
|
|
|
|
.AddJsonOptions(options => options.SerializerSettings.ContractResolver =
|
|
|
|
new DefaultContractResolver()); //Web app uses Pascal Case JSON);
|
2019-10-14 17:51:33 +00:00
|
|
|
#else
|
2020-02-25 16:08:03 +00:00
|
|
|
|
|
|
|
services.AddControllers(
|
|
|
|
config => config.Filters.Add(
|
|
|
|
new AuthorizeFilter(
|
|
|
|
new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build())))
|
|
|
|
.AddNewtonsoftJson(
|
|
|
|
options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
|
2019-10-14 17:51:33 +00:00
|
|
|
#endif
|
2018-05-01 12:00:02 +00:00
|
|
|
|
2020-02-10 22:16:19 +00:00
|
|
|
var runtimeSettings = new RuntimeSettings();
|
2018-05-01 12:00:02 +00:00
|
|
|
Configuration.GetSection("RuntimeSettings").Bind(runtimeSettings);
|
|
|
|
|
2020-02-10 22:16:19 +00:00
|
|
|
var dataProtectionFolder = new DirectoryInfo(Path.Combine(runtimeSettings.DataFolder, "DataProtection"));
|
2018-05-30 11:43:58 +00:00
|
|
|
|
|
|
|
services.AddDataProtection()
|
|
|
|
.PersistKeysToFileSystem(dataProtectionFolder)
|
|
|
|
.SetApplicationName("Jackett");
|
|
|
|
|
2018-05-01 12:00:02 +00:00
|
|
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
|
|
|
|
|
|
|
|
var builder = new ContainerBuilder();
|
|
|
|
|
2018-06-18 11:50:09 +00:00
|
|
|
Helper.SetupLogging(builder);
|
2018-05-01 12:00:02 +00:00
|
|
|
|
|
|
|
builder.Populate(services);
|
|
|
|
builder.RegisterModule(new JackettModule(runtimeSettings));
|
2021-11-16 13:06:07 +00:00
|
|
|
builder.RegisterType<SecurityService>().As<ISecurityService>().SingleInstance();
|
2018-10-28 07:30:26 +00:00
|
|
|
builder.RegisterType<ServerService>().As<IServerService>().SingleInstance();
|
|
|
|
builder.RegisterType<ProtectionService>().As<IProtectionService>().SingleInstance();
|
2020-12-11 22:14:21 +00:00
|
|
|
builder.RegisterType<CacheService>().As<ICacheService>().SingleInstance();
|
2018-10-28 07:30:26 +00:00
|
|
|
builder.RegisterType<ServiceConfigService>().As<IServiceConfigService>().SingleInstance();
|
2019-03-06 10:16:20 +00:00
|
|
|
builder.RegisterType<FilePermissionService>().As<IFilePermissionService>().SingleInstance();
|
2018-05-01 12:00:02 +00:00
|
|
|
|
2020-02-10 22:16:19 +00:00
|
|
|
var container = builder.Build();
|
2018-06-03 11:11:18 +00:00
|
|
|
Helper.ApplicationContainer = container;
|
|
|
|
|
2018-07-10 12:22:02 +00:00
|
|
|
Helper.Logger.Debug("Autofac container built");
|
|
|
|
|
2018-06-03 11:11:18 +00:00
|
|
|
Helper.Initialize();
|
2018-05-01 12:00:02 +00:00
|
|
|
|
|
|
|
return new AutofacServiceProvider(container);
|
2018-05-01 11:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
2023-01-07 19:38:52 +00:00
|
|
|
#if NET462
|
2018-06-03 11:11:18 +00:00
|
|
|
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime applicationLifetime)
|
2018-05-01 11:13:20 +00:00
|
|
|
{
|
2020-02-23 20:38:59 +00:00
|
|
|
applicationLifetime.ApplicationStarted.Register(OnStarted);
|
|
|
|
applicationLifetime.ApplicationStopped.Register(OnStopped);
|
2018-06-10 02:33:16 +00:00
|
|
|
Helper.applicationLifetime = applicationLifetime;
|
2018-05-12 02:51:46 +00:00
|
|
|
app.UseResponseCompression();
|
|
|
|
|
2018-05-01 12:00:02 +00:00
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
2018-05-05 07:08:46 +00:00
|
|
|
app.UseCustomExceptionHandler();
|
|
|
|
|
2020-02-10 22:16:19 +00:00
|
|
|
var serverBasePath = Helper.ServerService.BasePath() ?? string.Empty;
|
2018-07-13 12:15:09 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(serverBasePath))
|
|
|
|
{
|
|
|
|
app.UsePathBase(serverBasePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
|
|
|
{
|
2018-08-15 07:00:54 +00:00
|
|
|
// When adjusting these pareamters make sure it's well tested with various environments
|
|
|
|
// See https://github.com/Jackett/Jackett/issues/3517
|
2018-08-12 10:18:14 +00:00
|
|
|
ForwardLimit = 10,
|
2018-08-15 07:00:54 +00:00
|
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost
|
2018-07-13 12:15:09 +00:00
|
|
|
});
|
|
|
|
|
2018-05-05 07:08:46 +00:00
|
|
|
var rewriteOptions = new RewriteOptions()
|
2018-06-18 11:48:45 +00:00
|
|
|
.AddRewrite(@"^torznab\/([\w-]*)", "api/v2.0/indexers/$1/results/torznab", skipRemainingRules: true) //legacy torznab route
|
|
|
|
.AddRewrite(@"^potato\/([\w-]*)", "api/v2.0/indexers/$1/results/potato", skipRemainingRules: true) //legacy potato route
|
2018-05-05 07:08:46 +00:00
|
|
|
.Add(RedirectRules.RedirectToDashboard);
|
|
|
|
|
|
|
|
app.UseRewriter(rewriteOptions);
|
|
|
|
|
2018-08-05 05:31:54 +00:00
|
|
|
app.UseStaticFiles();
|
2018-05-01 11:13:20 +00:00
|
|
|
|
2018-05-12 02:44:47 +00:00
|
|
|
app.UseAuthentication();
|
|
|
|
|
2022-04-24 22:17:35 +00:00
|
|
|
if (Helper.ServerConfiguration.AllowCORS)
|
|
|
|
app.UseCors(AllowAllOrigins);
|
|
|
|
|
2018-05-01 11:13:20 +00:00
|
|
|
app.UseMvc();
|
|
|
|
}
|
2019-10-14 17:51:33 +00:00
|
|
|
#else
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime applicationLifetime)
|
|
|
|
{
|
2020-02-23 20:38:59 +00:00
|
|
|
applicationLifetime.ApplicationStarted.Register(OnStarted);
|
|
|
|
applicationLifetime.ApplicationStopped.Register(OnStopped);
|
2019-10-14 17:51:33 +00:00
|
|
|
Helper.applicationLifetime = applicationLifetime;
|
|
|
|
app.UseResponseCompression();
|
|
|
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
|
|
|
app.UseCustomExceptionHandler();
|
|
|
|
|
2020-02-10 22:16:19 +00:00
|
|
|
var serverBasePath = Helper.ServerService.BasePath() ?? string.Empty;
|
2019-10-14 17:51:33 +00:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(serverBasePath))
|
|
|
|
{
|
|
|
|
app.UsePathBase(serverBasePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
app.UseForwardedHeaders(new ForwardedHeadersOptions
|
|
|
|
{
|
|
|
|
// When adjusting these pareamters make sure it's well tested with various environments
|
|
|
|
// See https://github.com/Jackett/Jackett/issues/3517
|
|
|
|
ForwardLimit = 10,
|
|
|
|
ForwardedHeaders = ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost
|
|
|
|
});
|
|
|
|
|
|
|
|
var rewriteOptions = new RewriteOptions()
|
|
|
|
.AddRewrite(@"^torznab\/([\w-]*)", "api/v2.0/indexers/$1/results/torznab", skipRemainingRules: true) //legacy torznab route
|
|
|
|
.AddRewrite(@"^potato\/([\w-]*)", "api/v2.0/indexers/$1/results/potato", skipRemainingRules: true) //legacy potato route
|
|
|
|
.Add(RedirectRules.RedirectToDashboard);
|
|
|
|
|
|
|
|
app.UseRewriter(rewriteOptions);
|
|
|
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
|
|
|
app.UseRouting();
|
|
|
|
|
2022-04-24 22:17:35 +00:00
|
|
|
if (Helper.ServerConfiguration.AllowCORS)
|
|
|
|
app.UseCors(AllowAllOrigins);
|
|
|
|
|
2020-02-25 16:08:03 +00:00
|
|
|
app.UseEndpoints(endpoints => endpoints.MapControllers());
|
2019-10-14 17:51:33 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-23 20:38:59 +00:00
|
|
|
private static void OnStarted()
|
2018-06-03 11:11:18 +00:00
|
|
|
{
|
2020-02-23 20:38:59 +00:00
|
|
|
var elapsed = (DateTime.Now - Process.GetCurrentProcess().StartTime).TotalSeconds;
|
|
|
|
Helper.Logger.Info($"Jackett startup finished in {elapsed:0.000} s");
|
2018-06-03 11:11:18 +00:00
|
|
|
}
|
2020-02-23 20:38:59 +00:00
|
|
|
|
|
|
|
private static void OnStopped() => Helper.Logger.Info($"Jackett stopped");
|
2018-05-01 11:13:20 +00:00
|
|
|
}
|
|
|
|
}
|