diff --git a/src/Jackett.Common/Content/custom.js b/src/Jackett.Common/Content/custom.js index b4290a2b2..6cd1449fe 100644 --- a/src/Jackett.Common/Content/custom.js +++ b/src/Jackett.Common/Content/custom.js @@ -85,6 +85,7 @@ function loadJackettSettings() { $("#jackett-proxy-port").val(data.proxy_port); $("#jackett-proxy-username").val(data.proxy_username); $("#jackett-proxy-password").val(data.proxy_password); + proxyWarning(data.proxy_type); $("#jackett-basepathoverride").val(data.basepathoverride); basePath = data.basepathoverride; @@ -114,10 +115,9 @@ function loadJackettSettings() { $.each(data.notices, function (index, value) { console.log(value); doNotify(value, "danger", "glyphicon glyphicon-alert", false); - }) + }); reloadIndexers(); - proxyWarning(data.proxy_url); }); } @@ -1241,7 +1241,7 @@ function bindUIButtons() { doNotify("Redirecting you to complete configuration update..", "success", "glyphicon glyphicon-ok"); window.setTimeout(function () { window.location.reload(true); - }, 3000); + }, 5000); }).fail(function (data) { if (data.responseJSON !== undefined && data.responseJSON.result == "error") { doNotify("Error: " + data.responseJSON.error, "danger", "glyphicon glyphicon-alert"); @@ -1284,13 +1284,13 @@ function bindUIButtons() { }); }); - $('#jackett-proxy-url').on('input', function () { + $('#jackett-proxy-type').on('input', function () { proxyWarning($(this).val()); }); } function proxyWarning(input) { - if (input != null && input.trim() !== "") { + if (input != null && input.toString().trim() !== "-1") { // disabled = -1 $('#proxy-warning').show(); } else diff --git a/src/Jackett.Common/Content/index.html b/src/Jackett.Common/Content/index.html index b2ed5dab1..e92475e94 100644 --- a/src/Jackett.Common/Content/index.html +++ b/src/Jackett.Common/Content/index.html @@ -143,9 +143,10 @@
Proxy type:
- Proxy url: - + Proxy URL: +
Proxy port: @@ -691,6 +692,6 @@ - + diff --git a/src/Jackett.Common/Models/Config/ProxyType.cs b/src/Jackett.Common/Models/Config/ProxyType.cs index 73aec3d7c..71c0197ae 100644 --- a/src/Jackett.Common/Models/Config/ProxyType.cs +++ b/src/Jackett.Common/Models/Config/ProxyType.cs @@ -2,8 +2,9 @@ namespace Jackett.Common.Models.Config { public enum ProxyType { + Disabled = -1, Http, Socks4, - Socks5, + Socks5 } } diff --git a/src/Jackett.Common/Models/Config/ServerConfig.cs b/src/Jackett.Common/Models/Config/ServerConfig.cs index 31c71f145..6faccdbdc 100644 --- a/src/Jackett.Common/Models/Config/ServerConfig.cs +++ b/src/Jackett.Common/Models/Config/ServerConfig.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Runtime.Serialization; using Newtonsoft.Json; namespace Jackett.Common.Models.Config @@ -17,6 +18,13 @@ namespace Jackett.Common.Models.Config RuntimeSettings = runtimeSettings; } + [OnDeserialized] + internal void OnDeserializedMethod(StreamingContext context) + { + if (string.IsNullOrWhiteSpace(ProxyUrl)) + ProxyType = ProxyType.Disabled; + } + public int Port { get; set; } public bool AllowExternal { get; set; } public string APIKey { get; set; } @@ -30,14 +38,14 @@ namespace Jackett.Common.Models.Config public string OmdbApiUrl { get; set; } /// - /// Ignore as we don't really want to be saving settings specified in the command line. + /// Ignore as we don't really want to be saving settings specified in the command line. /// This is a bit of a hack, but in future it might not be all that bad to be able to override config values using settings that were provided at runtime. (and save them if required) /// [JsonIgnore] public RuntimeSettings RuntimeSettings { get; set; } - public string ProxyUrl { get; set; } public ProxyType ProxyType { get; set; } + public string ProxyUrl { get; set; } public int? ProxyPort { get; set; } public string ProxyUsername { get; set; } public string ProxyPassword { get; set; } @@ -49,34 +57,33 @@ namespace Jackett.Common.Models.Config ? $"{ProxyUsername}:{ProxyPassword}" : null; - public string GetProxyUrl(bool withCreds = false) + public string GetProxyUrl(bool withCreds = true) { var url = ProxyUrl; - if (string.IsNullOrWhiteSpace(url)) - { + + // if disabled + if (ProxyType == ProxyType.Disabled || string.IsNullOrWhiteSpace(url)) return null; - } - //remove protocol from url - var index = url.IndexOf("://"); + + // remove protocol from url + var index = url.IndexOf("://", StringComparison.Ordinal); if (index > -1) - { url = url.Substring(index + 3); - } + + // add port url = ProxyPort.HasValue ? $"{url}:{ProxyPort}" : url; + // add credentials var authString = GetProxyAuthString(); if (withCreds && authString != null) - { url = $"{authString}@{url}"; - } - if (ProxyType != ProxyType.Http) + // add protocol + if (ProxyType == ProxyType.Socks4 || ProxyType == ProxyType.Socks5) { var protocol = (Enum.GetName(typeof(ProxyType), ProxyType) ?? "").ToLower(); if (!string.IsNullOrEmpty(protocol)) - { url = $"{protocol}://{url}"; - } } return url; } diff --git a/src/Jackett.Common/Utils/Clients/WebClient.cs b/src/Jackett.Common/Utils/Clients/WebClient.cs index d5c152a5e..0e83acd0e 100644 --- a/src/Jackett.Common/Utils/Clients/WebClient.cs +++ b/src/Jackett.Common/Utils/Clients/WebClient.cs @@ -37,45 +37,44 @@ namespace Jackett.Common.Utils.Clients if (webProxy is SocksWebProxy proxy) proxy.Dispose(); webProxy = null; - webProxyUrl = serverConfig.GetProxyUrl(); - if (!string.IsNullOrWhiteSpace(webProxyUrl)) - { - if (serverConfig.ProxyType != ProxyType.Http) - { - var addresses = Dns.GetHostAddressesAsync(serverConfig.ProxyUrl).Result; - var socksConfig = new ProxyConfig - { - SocksAddress = addresses.FirstOrDefault(), - Username = serverConfig.ProxyUsername, - Password = serverConfig.ProxyPassword, - Version = serverConfig.ProxyType == ProxyType.Socks4 ? - ProxyConfig.SocksVersion.Four : - ProxyConfig.SocksVersion.Five - }; - if (serverConfig.ProxyPort.HasValue) - { - socksConfig.SocksPort = serverConfig.ProxyPort.Value; - } - webProxy = new SocksWebProxy(socksConfig, false); - } - else - { - NetworkCredential creds = null; - if (!serverConfig.ProxyIsAnonymous) - { - var username = serverConfig.ProxyUsername; - var password = serverConfig.ProxyPassword; - creds = new NetworkCredential(username, password); - } - webProxy = new WebProxy(webProxyUrl) - { - BypassProxyOnLocal = false, - Credentials = creds - }; - } - } - } + webProxyUrl = serverConfig.GetProxyUrl(); + if (serverConfig.ProxyType == ProxyType.Disabled || string.IsNullOrWhiteSpace(webProxyUrl)) + return; + if (serverConfig.ProxyType == ProxyType.Http) + { + NetworkCredential creds = null; + if (!serverConfig.ProxyIsAnonymous) + { + var username = serverConfig.ProxyUsername; + var password = serverConfig.ProxyPassword; + creds = new NetworkCredential(username, password); + } + webProxy = new WebProxy(serverConfig.GetProxyUrl(false)) // proxy URL without credentials + { + BypassProxyOnLocal = false, + Credentials = creds + }; + } + else if (serverConfig.ProxyType == ProxyType.Socks4 || serverConfig.ProxyType == ProxyType.Socks5) + { + var addresses = Dns.GetHostAddressesAsync(serverConfig.ProxyUrl).Result; + var socksConfig = new ProxyConfig + { + SocksAddress = addresses.FirstOrDefault(), + Username = serverConfig.ProxyUsername, + Password = serverConfig.ProxyPassword, + Version = serverConfig.ProxyType == ProxyType.Socks4 ? + ProxyConfig.SocksVersion.Four : + ProxyConfig.SocksVersion.Five + }; + if (serverConfig.ProxyPort.HasValue) + socksConfig.SocksPort = serverConfig.ProxyPort.Value; + webProxy = new SocksWebProxy(socksConfig, false); + } + else + throw new Exception($"Proxy type '{serverConfig.ProxyType}' is not implemented!"); + } public double requestDelay { diff --git a/src/Jackett.Server/Controllers/ServerConfigurationController.cs b/src/Jackett.Server/Controllers/ServerConfigurationController.cs index afd1b6829..f86350642 100644 --- a/src/Jackett.Server/Controllers/ServerConfigurationController.cs +++ b/src/Jackett.Server/Controllers/ServerConfigurationController.cs @@ -122,8 +122,8 @@ namespace Jackett.Server.Controllers 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.ProxyType = string.IsNullOrWhiteSpace(config.proxy_url) ? ProxyType.Disabled : config.proxy_type; 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; diff --git a/src/Jackett.Server/Services/ServerService.cs b/src/Jackett.Server/Services/ServerService.cs index 8095e4bb0..49bbd7521 100644 --- a/src/Jackett.Server/Services/ServerService.cs +++ b/src/Jackett.Server/Services/ServerService.cs @@ -137,7 +137,7 @@ namespace Jackett.Server.Services logger.Info("App config/log directory: " + configService.GetAppDataFolder()); - logger.Info("Using Proxy: " + (string.IsNullOrEmpty(config.ProxyUrl) ? "No" : config.ProxyType.ToString())); + logger.Info($"Using proxy: {config.ProxyType}"); var monotype = Type.GetType("Mono.Runtime"); if (monotype != null && !DotNetCoreUtil.IsRunningOnDotNetCore)