From 968a7729c302f8de7fe476fa4c87d0af5c139e2f Mon Sep 17 00:00:00 2001 From: kaso17 Date: Thu, 16 Nov 2017 18:00:49 +0100 Subject: [PATCH] Fix SocksWebProxy race condition --- .../Utils/Clients/HttpWebClient.cs | 48 +++++++++------ .../Utils/Clients/HttpWebClient2.cs | 60 ++++++++++++------- 2 files changed, 67 insertions(+), 41 deletions(-) diff --git a/src/Jackett.Common/Utils/Clients/HttpWebClient.cs b/src/Jackett.Common/Utils/Clients/HttpWebClient.cs index 758e65f60..36e9efe0b 100644 --- a/src/Jackett.Common/Utils/Clients/HttpWebClient.cs +++ b/src/Jackett.Common/Utils/Clients/HttpWebClient.cs @@ -23,13 +23,41 @@ namespace Jackett.Utils.Clients static protected Dictionary> trustedCertificates = new Dictionary>(); static protected SocksWebProxy socksWebProxy; + static public void InitSocksWebProxy(ServerConfig serverConfig) + { + if (socksWebProxy != null) + { + socksWebProxy.Dispose(); + } + + 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; + } + socksWebProxy = new SocksWebProxy(socksConfig, false); + } + } + public HttpWebClient(IProcessService p, Logger l, IConfigurationService c, ServerConfig sc) : base(p: p, l: l, c: c, sc: sc) { - + if (socksWebProxy == null) + InitSocksWebProxy(sc); } override public void Init() @@ -95,24 +123,6 @@ namespace Jackett.Utils.Clients useProxy = true; if (serverConfig.ProxyType != ProxyType.Http) { - if (socksWebProxy == null) - { - var addresses = await Dns.GetHostAddressesAsync(serverConfig.ProxyUrl); - 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; - } - socksWebProxy = new SocksWebProxy(socksConfig, false); - } proxyServer = socksWebProxy; } else diff --git a/src/Jackett.Common/Utils/Clients/HttpWebClient2.cs b/src/Jackett.Common/Utils/Clients/HttpWebClient2.cs index 92f64bf81..b6e5e4a3f 100644 --- a/src/Jackett.Common/Utils/Clients/HttpWebClient2.cs +++ b/src/Jackett.Common/Utils/Clients/HttpWebClient2.cs @@ -33,6 +33,34 @@ namespace Jackett.Utils.Clients HttpClient client; static protected Dictionary> trustedCertificates = new Dictionary>(); + static protected SocksWebProxy socksWebProxy; + + static public void InitSocksWebProxy(ServerConfig serverConfig) + { + if (socksWebProxy != null) + { + socksWebProxy.Dispose(); + } + + 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; + } + socksWebProxy = new SocksWebProxy(socksConfig, false); + } + } public HttpWebClient2(IProcessService p, Logger l, IConfigurationService c, ServerConfig sc) : base(p: p, @@ -40,6 +68,8 @@ namespace Jackett.Utils.Clients c: c, sc: sc) { + if (socksWebProxy == null) + InitSocksWebProxy(sc); cookies = new CookieContainer(); var useProxy = false; @@ -48,33 +78,19 @@ namespace Jackett.Utils.Clients if (!string.IsNullOrWhiteSpace(proxyUrl)) { useProxy = true; - NetworkCredential creds = null; - if (!serverConfig.ProxyIsAnonymous) - { - var username = serverConfig.ProxyUsername; - var password = serverConfig.ProxyPassword; - creds = new NetworkCredential(username, password); - } 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; - } - proxyServer = new SocksWebProxy(socksConfig, false); + proxyServer = socksWebProxy; } else { + NetworkCredential creds = null; + if (!serverConfig.ProxyIsAnonymous) + { + var username = serverConfig.ProxyUsername; + var password = serverConfig.ProxyPassword; + creds = new NetworkCredential(username, password); + } proxyServer = new WebProxy(proxyUrl) { BypassProxyOnLocal = false,