From 79b42c9e2e2683da41339576170fca59b7e6146e Mon Sep 17 00:00:00 2001 From: ThomasAmpen Date: Tue, 14 Jul 2015 21:07:55 +0200 Subject: [PATCH] Change port --- src/Jackett/ExceptionWithConfigData.cs | 8 ++ src/Jackett/Jackett.csproj | 1 + src/Jackett/Program.cs | 13 ++++ src/Jackett/Server.cs | 52 +++++++++++++ src/Jackett/ServerUtil.cs | 100 +++++++++++++++++++++++++ src/Jackett/WebApi.cs | 69 ++++++++++++++++- src/Jackett/WebContent/custom.js | 41 ++++++++++ src/Jackett/WebContent/index.html | 6 ++ 8 files changed, 288 insertions(+), 2 deletions(-) create mode 100644 src/Jackett/ServerUtil.cs diff --git a/src/Jackett/ExceptionWithConfigData.cs b/src/Jackett/ExceptionWithConfigData.cs index 0585b0db4..60cf8deae 100644 --- a/src/Jackett/ExceptionWithConfigData.cs +++ b/src/Jackett/ExceptionWithConfigData.cs @@ -15,5 +15,13 @@ namespace Jackett { ConfigData = data; } + + } + + public class CustomException : Exception + { + public CustomException(string message) + : base(message) + { } } } diff --git a/src/Jackett/Jackett.csproj b/src/Jackett/Jackett.csproj index e53951a72..94932db25 100644 --- a/src/Jackett/Jackett.csproj +++ b/src/Jackett/Jackett.csproj @@ -131,6 +131,7 @@ + diff --git a/src/Jackett/Program.cs b/src/Jackett/Program.cs index cc16b2bc1..4d7134dbd 100644 --- a/src/Jackett/Program.cs +++ b/src/Jackett/Program.cs @@ -120,6 +120,19 @@ namespace Jackett Console.WriteLine("Server thread exit"); } + public static void RestartServer() + { + + ServerInstance.Stop(); + ServerInstance = null; + var serverTask = Task.Run(async () => + { + ServerInstance = new Server(); + await ServerInstance.Start(); + }); + Task.WaitAll(serverTask); + } + static void MigrateSettingsDirectory() { try diff --git a/src/Jackett/Server.cs b/src/Jackett/Server.cs index 30e060746..ed4b26111 100644 --- a/src/Jackett/Server.cs +++ b/src/Jackett/Server.cs @@ -7,6 +7,7 @@ using System.IO; using System.Linq; using System.Net; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web; using System.Windows.Forms; @@ -30,8 +31,10 @@ namespace Jackett // Allow all SSL.. sucks I know but mono on linux is having problems without it.. ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; + ReadServerSettingsFile(); LoadApiKey(); + indexerManager = new IndexerManager(); sonarrApi = new SonarrApi(); webApi = new WebApi(indexerManager, sonarrApi); @@ -68,6 +71,7 @@ namespace Jackett } listener.Start(); + webApi.server = this; } catch (HttpListenerException ex) { @@ -249,5 +253,53 @@ namespace Jackett + + public JObject ReadServerSettingsFile() + { + var path = ServerConfigFile; + JObject jsonReply = new JObject(); + if (File.Exists(path)) + { + jsonReply = JObject.Parse(File.ReadAllText(path)); + Port = (int)jsonReply["port"]; + ListenPublic = (bool)jsonReply["public"]; + } + else + { + jsonReply["port"] = Port; + jsonReply["public"] = ListenPublic; + } + return jsonReply; + } + + public Task ApplyPortConfiguration(JToken json) + { + JObject jsonObject = (JObject)json; + JToken jJackettPort = jsonObject.GetValue("port"); + int jackettPort; + if (!ServerUtil.IsPort(jJackettPort.ToString())) + throw new CustomException("The value entered is not a valid port"); + else + jackettPort = int.Parse(jJackettPort.ToString()); + + if (jackettPort == Port) + throw new CustomException("The current port is the same as the one being used now."); + else if (ServerUtil.RestrictedPorts.Contains(jackettPort)) + throw new CustomException("This port is not allowed due to it not being safe."); + SaveSettings(jackettPort); + + return Task.FromResult(jackettPort); + } + + private static string ServerConfigFile = Path.Combine(Program.AppConfigDirectory, "config.json"); + + private void SaveSettings(int jacketPort) + { + JObject json = new JObject(); + json["port"] = jacketPort; + json["public"] = ListenPublic; + File.WriteAllText(ServerConfigFile, json.ToString()); + } + } } diff --git a/src/Jackett/ServerUtil.cs b/src/Jackett/ServerUtil.cs new file mode 100644 index 000000000..8f3e6c02f --- /dev/null +++ b/src/Jackett/ServerUtil.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace Jackett +{ + public static class ServerUtil + { + public static int[] RestrictedPorts = new int[] { + 1, // tcpmux + 7, // echo + 9, // discard + 11, // systat + 13, // daytime + 15, // netstat + 17, // qotd + 19, // chargen + 20, // ftp data + 21, // ftp access + 22, // ssh + 23, // telnet + 25, // smtp + 37, // time + 42, // name + 43, // nicname + 53, // domain + 77, // priv-rjs + 79, // finger + 87, // ttylink + 95, // supdup + 101, // hostriame + 102, // iso-tsap + 103, // gppitnp + 104, // acr-nema + 109, // pop2 + 110, // pop3 + 111, // sunrpc + 113, // auth + 115, // sftp + 117, // uucp-path + 119, // nntp + 123, // NTP + 135, // loc-srv /epmap + 139, // netbios + 143, // imap2 + 179, // BGP + 389, // ldap + 465, // smtp+ssl + 512, // print / exec + 513, // login + 514, // shell + 515, // printer + 526, // tempo + 530, // courier + 531, // chat + 532, // netnews + 540, // uucp + 556, // remotefs + 563, // nntp+ssl + 587, // stmp? + 601, // ?? + 636, // ldap+ssl + 993, // ldap+ssl + 995, // pop3+ssl + 2049, // nfs + 3659, // apple-sasl / PasswordServer + 4045, // lockd + 6000, // X11 + 6665, // Alternate IRC [Apple addition] + 6666, // Alternate IRC [Apple addition] + 6667, // Standard IRC [Apple addition] + 6668, // Alternate IRC [Apple addition] + 6669, // Alternate IRC [Apple addition]}; + }; + public static bool IsPort(string value) + { + if (string.IsNullOrEmpty(value)) + return false; + + Regex numeric = new Regex(@"^[0-9]+$", RegexOptions.Compiled | RegexOptions.IgnoreCase); + + if (numeric.IsMatch(value)) + { + try + { + if (Convert.ToInt32(value) < 65536) + return true; + } + catch (OverflowException) + { + } + } + + return false; + } + } +} diff --git a/src/Jackett/WebApi.cs b/src/Jackett/WebApi.cs index 8c46f3d6c..bb1702cbe 100644 --- a/src/Jackett/WebApi.cs +++ b/src/Jackett/WebApi.cs @@ -16,6 +16,7 @@ namespace Jackett { static string WebContentFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "WebContent"); static string[] StaticFiles = Directory.EnumerateFiles(WebContentFolder, "*", SearchOption.AllDirectories).ToArray(); + public Server server; public enum WebApiMethod { @@ -26,7 +27,10 @@ namespace Jackett DeleteIndexer, GetSonarrConfig, ApplySonarrConfig, - TestSonarr + TestSonarr, + GetJackettConfig, + ApplyJackettConfig, + JackettRestart, } static Dictionary WebApiMethods = new Dictionary { @@ -37,7 +41,10 @@ namespace Jackett { "delete_indexer", WebApiMethod.DeleteIndexer }, { "get_sonarr_config", WebApiMethod.GetSonarrConfig }, { "apply_sonarr_config", WebApiMethod.ApplySonarrConfig }, - { "test_sonarr", WebApiMethod.TestSonarr } + { "test_sonarr", WebApiMethod.TestSonarr }, + { "get_jackett_config",WebApiMethod.GetJackettConfig}, + { "apply_jackett_config",WebApiMethod.ApplyJackettConfig}, + { "jackett_restart", WebApiMethod.JackettRestart }, }; IndexerManager indexerManager; @@ -127,6 +134,15 @@ namespace Jackett case WebApiMethod.TestSonarr: handlerTask = HandleTestSonarr; break; + case WebApiMethod.ApplyJackettConfig: + handlerTask = HandleApplyJackettConfig; + break; + case WebApiMethod.GetJackettConfig: + handlerTask = HandleJackettConfig; + break; + case WebApiMethod.JackettRestart: + handlerTask = HandleJackettRestart; + break; default: handlerTask = HandleInvalidApiMethod; break; @@ -318,5 +334,54 @@ namespace Jackett return jsonReply; } + + //Jacket port functions + Task HandleJackettConfig(HttpListenerContext context) + { + JObject jsonReply = new JObject(); + try + { + jsonReply["config"] = server.ReadServerSettingsFile(); + jsonReply["result"] = "success"; + } + catch (CustomException ex) + { + jsonReply["result"] = "error"; + jsonReply["error"] = ex.Message; + } + catch (Exception ex) + { + jsonReply["result"] = "error"; + jsonReply["error"] = ex.Message; + } + return Task.FromResult(jsonReply); + } + + async Task HandleApplyJackettConfig(HttpListenerContext context) + { + JToken jsonReply = new JObject(); + + try + { + var postData = await ReadPostDataJson(context.Request.InputStream); + int port = await server.ApplyPortConfiguration(postData); + jsonReply["result"] = "success"; + jsonReply["port"] = port; + } + catch (Exception ex) + { + jsonReply["result"] = "error"; + jsonReply["error"] = ex.Message; + } + return jsonReply; + } + + async Task HandleJackettRestart(HttpListenerContext context) + { + Program.RestartServer(); + return null; + } + + } } diff --git a/src/Jackett/WebContent/custom.js b/src/Jackett/WebContent/custom.js index 38c4871de..35dd07e04 100644 --- a/src/Jackett/WebContent/custom.js +++ b/src/Jackett/WebContent/custom.js @@ -1,8 +1,49 @@  reloadIndexers(); +loadJackettSettings(); loadSonarrInfo(); +function loadJackettSettings() { + getJackettConfig(function (data) { + $("#jackett-port").val(data.config.port); + }); +} + +$("#change-jackett-port").click(function () { + var jackett_port = $("#jackett-port").val(); + var jsonObject = JSON.parse('{"port":"' + jackett_port + '"}'); + + var jqxhr = $.post("apply_jackett_config", JSON.stringify(jsonObject), function (data) { + + if (data.result == "error") { + doNotify("Error: " + data.error, "danger", "glyphicon glyphicon-alert"); + return; + } else { + doNotify("The port has been changed. Jackett will now restart...", "success", "glyphicon glyphicon-ok"); + var jqxhr0 = $.post("jackett_restart", null, function (data_restart) { }); + + window.setTimeout(function () { + url = window.location.href; + window.location.href = url.substr(0, url.lastIndexOf(":") + 1) + data.port; + + }, 3000); + + } + }).fail(function () { + doNotify("Request to Jackett server failed", "danger", "glyphicon glyphicon-alert"); + }); +}); + +function getJackettConfig(callback) { + var jqxhr = $.get("get_jackett_config", function (data) { + + callback(data); + }).fail(function () { + doNotify("Error loading Jackett settings, request to Jackett server failed", "danger", "glyphicon glyphicon-alert"); + }); +} + function loadSonarrInfo() { getSonarrConfig(function (data) { $("#sonarr-host").val(""); diff --git a/src/Jackett/WebContent/index.html b/src/Jackett/WebContent/index.html index ad6ccb22b..b31e95b70 100644 --- a/src/Jackett/WebContent/index.html +++ b/src/Jackett/WebContent/index.html @@ -45,6 +45,12 @@ Jackett API Key:

Use this key when adding indexers to Sonarr. This key works for all indexers.

+ Jackett port: + + +