Jackett/src/Jackett.Server/Program.cs

115 lines
4.4 KiB
C#
Raw Normal View History

2018-05-20 11:21:08 +00:00
using Autofac;
using CommandLine;
2018-05-01 12:00:02 +00:00
using CommandLine.Text;
using Jackett.Common.Models.Config;
2018-05-20 11:21:08 +00:00
using Jackett.Common.Plumbing;
using Jackett.Common.Services.Interfaces;
2018-05-01 12:00:02 +00:00
using Jackett.Common.Utils;
2018-05-20 11:21:08 +00:00
using Jackett.Server.Services;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
2018-05-20 11:21:08 +00:00
using NLog;
2018-05-01 12:00:02 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2018-05-20 11:21:08 +00:00
using System.Runtime.InteropServices;
namespace Jackett.Server
{
public class Program
{
2018-05-01 12:00:02 +00:00
public static IConfiguration Configuration { get; set; }
public static void Main(string[] args)
{
2018-05-01 12:00:02 +00:00
var optionsResult = Parser.Default.ParseArguments<ConsoleOptions>(args);
optionsResult.WithNotParsed(errors =>
{
var text = HelpText.AutoBuild(optionsResult);
text.Copyright = " ";
text.Heading = "Jackett v" + EnvironmentUtil.JackettVersion + " options:";
2018-05-20 11:21:08 +00:00
Environment.Exit(1);
2018-05-01 12:00:02 +00:00
return;
});
var runtimeDictionary = new Dictionary<string, string>();
2018-05-20 11:21:08 +00:00
RuntimeSettings r = new RuntimeSettings();
ConsoleOptions consoleOptions = new ConsoleOptions();
2018-05-01 12:00:02 +00:00
optionsResult.WithParsed(options =>
{
2018-05-20 11:21:08 +00:00
r = options.ToRunTimeSettings();
consoleOptions = options;
2018-05-01 12:00:02 +00:00
runtimeDictionary = GetValues(r);
});
var builder = new ConfigurationBuilder();
builder.AddInMemoryCollection(runtimeDictionary);
Configuration = builder.Build();
2018-05-20 11:21:08 +00:00
//hack TODO: Get the configuration without any DI
var containerBuilder = new ContainerBuilder();
Initialisation.SetupLogging(r, containerBuilder);
containerBuilder.RegisterModule(new JackettModule(r));
containerBuilder.RegisterType<ServerService>().As<IServerService>();
containerBuilder.RegisterType<SecuityService>().As<ISecuityService>();
containerBuilder.RegisterType<ProtectionService>().As<IProtectionService>();
var tempContainer = containerBuilder.Build();
Logger logger = tempContainer.Resolve<Logger>();
ServerConfig serverConfig = tempContainer.Resolve<ServerConfig>();
IConfigurationService configurationService = tempContainer.Resolve<IConfigurationService>();
IServerService serverService = tempContainer.Resolve<IServerService>();
Int32.TryParse(serverConfig.Port.ToString(), out Int32 configPort);
// Override port
if (consoleOptions.Port != 0)
{
if (configPort != consoleOptions.Port)
{
logger.Info("Overriding port to " + consoleOptions.Port);
serverConfig.Port = consoleOptions.Port;
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (isWindows)
{
if (ServerUtil.IsUserAdministrator())
{
serverService.ReserveUrls(doInstall: true);
}
else
{
logger.Error("Unable to switch ports when not running as administrator");
Environment.Exit(1);
}
}
configurationService.SaveConfig(serverConfig);
}
}
string[] url = serverConfig.GetListenAddresses(serverConfig.AllowExternal).Take(1).ToArray(); //Kestrel doesn't need 127.0.0.1 and localhost to be registered, remove once off OWIN
tempContainer.Dispose();
tempContainer = null;
CreateWebHostBuilder(args, url).Build().Run();
2018-05-01 12:00:02 +00:00
}
public static Dictionary<string, string> GetValues(object obj)
{
return obj
.GetType()
.GetProperties()
.ToDictionary(p => "RuntimeSettings:" + p.Name, p => p.GetValue(obj) == null ? null : p.GetValue(obj).ToString());
}
2018-05-20 11:21:08 +00:00
public static IWebHostBuilder CreateWebHostBuilder(string[] args, string[] urls) =>
WebHost.CreateDefaultBuilder(args)
.UseConfiguration(Configuration)
2018-05-20 11:21:08 +00:00
.UseUrls(urls)
.PreferHostingUrls(true)
.UseStartup<Startup>();
}
}