2018-05-01 12:55:09 +00:00
|
|
|
|
using Jackett.Common.Models;
|
2018-05-01 12:03:16 +00:00
|
|
|
|
using Jackett.Common.Models.Config;
|
|
|
|
|
using Jackett.Common.Services.Interfaces;
|
|
|
|
|
using Jackett.Common.Utils;
|
2018-05-01 12:55:09 +00:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2018-05-01 12:03:16 +00:00
|
|
|
|
using NLog;
|
2018-05-01 12:55:09 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2018-05-01 12:03:16 +00:00
|
|
|
|
|
2018-05-01 12:55:09 +00:00
|
|
|
|
namespace Jackett.Server.Controllers
|
2018-05-01 12:03:16 +00:00
|
|
|
|
{
|
2018-05-01 12:55:09 +00:00
|
|
|
|
[Route("api/v2.0/server/[action]")]
|
|
|
|
|
[ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
|
|
|
|
|
public class ServerConfigurationController : Controller
|
2018-05-01 12:03:16 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IConfigurationService configService;
|
|
|
|
|
private ServerConfig serverConfig;
|
|
|
|
|
private IServerService serverService;
|
|
|
|
|
private IProcessService processService;
|
|
|
|
|
private IIndexerManagerService indexerService;
|
|
|
|
|
private ISecuityService securityService;
|
|
|
|
|
private IUpdateService updater;
|
|
|
|
|
private ILogCacheService logCache;
|
|
|
|
|
private Logger logger;
|
|
|
|
|
|
2018-05-01 12:55:09 +00:00
|
|
|
|
public ServerConfigurationController(IConfigurationService c, IServerService s, IProcessService p, IIndexerManagerService i, ISecuityService ss, IUpdateService u, ILogCacheService lc, Logger l, ServerConfig sc)
|
2018-05-01 12:03:16 +00:00
|
|
|
|
{
|
|
|
|
|
configService = c;
|
|
|
|
|
serverConfig = sc;
|
|
|
|
|
serverService = s;
|
|
|
|
|
processService = p;
|
|
|
|
|
indexerService = i;
|
|
|
|
|
securityService = ss;
|
|
|
|
|
updater = u;
|
|
|
|
|
logCache = lc;
|
|
|
|
|
logger = l;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2018-05-05 07:10:36 +00:00
|
|
|
|
public IActionResult AdminPassword([FromBody]string password)
|
2018-05-01 12:03:16 +00:00
|
|
|
|
{
|
|
|
|
|
var oldPassword = serverConfig.AdminPassword;
|
|
|
|
|
if (string.IsNullOrEmpty(password))
|
|
|
|
|
password = null;
|
|
|
|
|
|
|
|
|
|
if (oldPassword != password)
|
|
|
|
|
{
|
|
|
|
|
serverConfig.AdminPassword = securityService.HashPassword(password);
|
|
|
|
|
configService.SaveConfig(serverConfig);
|
|
|
|
|
}
|
2018-05-05 07:10:36 +00:00
|
|
|
|
|
|
|
|
|
return new NoContentResult();
|
2018-05-01 12:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public void Update()
|
|
|
|
|
{
|
|
|
|
|
updater.CheckForUpdatesNow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public Common.Models.DTO.ServerConfig Config()
|
|
|
|
|
{
|
|
|
|
|
var dto = new Common.Models.DTO.ServerConfig(serverService.notices, serverConfig, configService.GetVersion());
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ActionName("Config")]
|
|
|
|
|
[HttpPost]
|
2018-05-01 12:55:09 +00:00
|
|
|
|
public IActionResult UpdateConfig([FromBody]Common.Models.DTO.ServerConfig config)
|
2018-05-01 12:03:16 +00:00
|
|
|
|
{
|
|
|
|
|
var originalPort = serverConfig.Port;
|
|
|
|
|
var originalAllowExternal = serverConfig.AllowExternal;
|
|
|
|
|
int port = config.port;
|
|
|
|
|
bool external = config.external;
|
|
|
|
|
string saveDir = config.blackholedir;
|
|
|
|
|
bool updateDisabled = config.updatedisabled;
|
|
|
|
|
bool preRelease = config.prerelease;
|
|
|
|
|
bool logging = config.logging;
|
|
|
|
|
string basePathOverride = config.basepathoverride;
|
|
|
|
|
if (basePathOverride != null)
|
|
|
|
|
{
|
|
|
|
|
basePathOverride = basePathOverride.TrimEnd('/');
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(basePathOverride) && !basePathOverride.StartsWith("/"))
|
|
|
|
|
throw new Exception("The Base Path Override must start with a /");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string omdbApiKey = config.omdbkey;
|
|
|
|
|
|
|
|
|
|
serverConfig.UpdateDisabled = updateDisabled;
|
|
|
|
|
serverConfig.UpdatePrerelease = preRelease;
|
|
|
|
|
serverConfig.BasePathOverride = basePathOverride;
|
2018-05-01 12:55:09 +00:00
|
|
|
|
serverConfig.RuntimeSettings.BasePath = serverService.BasePath();
|
2018-05-01 12:03:16 +00:00
|
|
|
|
configService.SaveConfig(serverConfig);
|
|
|
|
|
|
2018-06-03 11:11:18 +00:00
|
|
|
|
Helper.SetLogLevel(logging ? LogLevel.Debug : LogLevel.Info);
|
2018-05-01 12:03:16 +00:00
|
|
|
|
serverConfig.RuntimeSettings.TracingEnabled = logging;
|
|
|
|
|
|
|
|
|
|
if (omdbApiKey != serverConfig.OmdbApiKey)
|
|
|
|
|
{
|
|
|
|
|
serverConfig.OmdbApiKey = omdbApiKey;
|
2018-05-01 12:55:09 +00:00
|
|
|
|
configService.SaveConfig(serverConfig);
|
2018-05-01 12:03:16 +00:00
|
|
|
|
// HACK
|
|
|
|
|
indexerService.InitAggregateIndexer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (config.proxy_type != serverConfig.ProxyType ||
|
|
|
|
|
config.proxy_url != serverConfig.ProxyUrl ||
|
|
|
|
|
config.proxy_port != serverConfig.ProxyPort ||
|
|
|
|
|
config.proxy_username != serverConfig.ProxyUsername ||
|
|
|
|
|
config.proxy_password != serverConfig.ProxyPassword)
|
|
|
|
|
{
|
|
|
|
|
if (config.proxy_port < 1 || config.proxy_port > 65535)
|
|
|
|
|
throw new Exception("The port you have selected is invalid, it must be below 65535.");
|
|
|
|
|
|
|
|
|
|
serverConfig.ProxyUrl = config.proxy_url;
|
|
|
|
|
serverConfig.ProxyType = config.proxy_type;
|
|
|
|
|
serverConfig.ProxyPort = config.proxy_port;
|
|
|
|
|
serverConfig.ProxyUsername = config.proxy_username;
|
|
|
|
|
serverConfig.ProxyPassword = config.proxy_password;
|
|
|
|
|
configService.SaveConfig(serverConfig);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (port != serverConfig.Port || external != serverConfig.AllowExternal)
|
|
|
|
|
{
|
|
|
|
|
if (ServerUtil.RestrictedPorts.Contains(port))
|
|
|
|
|
throw new Exception("The port you have selected is restricted, try a different one.");
|
|
|
|
|
|
|
|
|
|
if (port < 1 || port > 65535)
|
|
|
|
|
throw new Exception("The port you have selected is invalid, it must be below 65535.");
|
|
|
|
|
|
|
|
|
|
// Save port to the config so it can be picked up by the if needed when running as admin below.
|
|
|
|
|
serverConfig.AllowExternal = external;
|
|
|
|
|
serverConfig.Port = port;
|
|
|
|
|
configService.SaveConfig(serverConfig);
|
|
|
|
|
|
|
|
|
|
// On Windows change the url reservations
|
|
|
|
|
if (System.Environment.OSVersion.Platform != PlatformID.Unix)
|
|
|
|
|
{
|
|
|
|
|
if (!ServerUtil.IsUserAdministrator())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-05-01 12:55:09 +00:00
|
|
|
|
//TODO
|
|
|
|
|
//processService.StartProcessAndLog(System.Windows.Forms.Application.ExecutablePath, "--ReserveUrls", true);
|
2018-05-01 12:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
serverConfig.Port = originalPort;
|
|
|
|
|
serverConfig.AllowExternal = originalAllowExternal;
|
|
|
|
|
configService.SaveConfig(serverConfig);
|
|
|
|
|
|
|
|
|
|
throw new Exception("Failed to acquire admin permissions to reserve the new port.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
serverService.ReserveUrls(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-01 12:55:09 +00:00
|
|
|
|
//TODO
|
|
|
|
|
//(new Thread(() =>
|
|
|
|
|
//{
|
|
|
|
|
// Thread.Sleep(500);
|
|
|
|
|
// serverService.Stop();
|
|
|
|
|
// Engine.BuildContainer(serverConfig.RuntimeSettings, new WebApi2Module());
|
|
|
|
|
// Engine.Server.Initalize();
|
|
|
|
|
// Engine.Server.Start();
|
|
|
|
|
//})).Start();
|
2018-05-01 12:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (saveDir != serverConfig.BlackholeDir)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(saveDir))
|
|
|
|
|
{
|
|
|
|
|
if (!Directory.Exists(saveDir))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Blackhole directory does not exist");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
serverConfig.BlackholeDir = saveDir;
|
|
|
|
|
configService.SaveConfig(serverConfig);
|
|
|
|
|
}
|
2018-05-01 12:55:09 +00:00
|
|
|
|
|
2018-05-01 12:03:16 +00:00
|
|
|
|
serverConfig.ConfigChanged();
|
2018-05-01 12:55:09 +00:00
|
|
|
|
|
|
|
|
|
return Json(serverConfig);
|
2018-05-01 12:03:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public List<CachedLog> Logs()
|
|
|
|
|
{
|
|
|
|
|
return logCache.Logs;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|