From c008bfffc3ad721b08c73f7d401d9f29ca5a97b5 Mon Sep 17 00:00:00 2001 From: Gil Mirmovitch Date: Thu, 21 Oct 2021 17:28:46 +0300 Subject: [PATCH 1/3] #884 Allow explicitly setting PAC file for determining upstream proxy --- .../Helpers/WinHttp/WinHttpWebProxyFinder.cs | 10 ++++++++++ src/Titanium.Web.Proxy/ProxyServer.cs | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs b/src/Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs index e20a7c67..86dc0a4d 100644 --- a/src/Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs +++ b/src/Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs @@ -144,6 +144,16 @@ public void LoadFromIE() proxy = new WebProxy(new Uri("http://localhost"), BypassOnLocal, pi.BypassList); } + internal void UsePacFile(Uri upstreamProxyConfigurationScript) + { + AutomaticallyDetectSettings = true; + AutomaticConfigurationScript = upstreamProxyConfigurationScript; + BypassLoopback = true; + BypassOnLocal = false; + proxy = new WebProxy(new Uri("http://localhost"), BypassOnLocal); + } + + private ProxyInfo getProxyInfo() { var proxyConfig = new NativeMethods.WinHttp.WINHTTP_CURRENT_USER_IE_PROXY_CONFIG(); diff --git a/src/Titanium.Web.Proxy/ProxyServer.cs b/src/Titanium.Web.Proxy/ProxyServer.cs index bd0f0872..bf4f625b 100644 --- a/src/Titanium.Web.Proxy/ProxyServer.cs +++ b/src/Titanium.Web.Proxy/ProxyServer.cs @@ -136,6 +136,11 @@ public ProxyServer(string? rootCertificateName, string? rootCertificateIssuerNam /// public bool ForwardToUpstreamGateway { get; set; } + /// + /// If set, the upstream proxy will be detected by a script that will be loaded from the provided Uri + /// + public Uri UpstreamProxyConfigurationScript { get; set; } + /// /// Enable disable Windows Authentication (NTLM/Kerberos). /// Note: NTLM/Kerberos will always send local credentials of current user @@ -628,9 +633,17 @@ public void Start(bool changeSystemProxySettings = true) if (ForwardToUpstreamGateway && GetCustomUpStreamProxyFunc == null && systemProxySettingsManager != null) { - // Use WinHttp to handle PAC/WAPD scripts. systemProxyResolver = new WinHttpWebProxyFinder(); - systemProxyResolver.LoadFromIE(); + if (UpstreamProxyConfigurationScript != null) + { + //Use the provided proxy configuration script + systemProxyResolver.UsePacFile(UpstreamProxyConfigurationScript); + } + else + { + // Use WinHttp to handle PAC/WAPD scripts. + systemProxyResolver.LoadFromIE(); + } GetCustomUpStreamProxyFunc = getSystemUpStreamProxy; } From a8930c309909af671dda2b2fc7543d52d9459e10 Mon Sep 17 00:00:00 2001 From: Gil Mirmovitch Date: Sun, 7 Nov 2021 11:30:50 +0200 Subject: [PATCH 2/3] Update WinHttpWebProxyFinder.cs --- src/Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs b/src/Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs index 86dc0a4d..56d21a35 100644 --- a/src/Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs +++ b/src/Titanium.Web.Proxy/Helpers/WinHttp/WinHttpWebProxyFinder.cs @@ -153,7 +153,6 @@ internal void UsePacFile(Uri upstreamProxyConfigurationScript) proxy = new WebProxy(new Uri("http://localhost"), BypassOnLocal); } - private ProxyInfo getProxyInfo() { var proxyConfig = new NativeMethods.WinHttp.WINHTTP_CURRENT_USER_IE_PROXY_CONFIG(); From 400ca56ce3d0413c1798e3b0b0ab4bf728e0c10e Mon Sep 17 00:00:00 2001 From: Jehonathan Thomas Date: Wed, 10 Nov 2021 14:59:34 -0700 Subject: [PATCH 3/3] organize; clean-up; NET45 developer pack is not available anymore --- src/Titanium.Web.Proxy.Mono.sln | 61 ------------------- .../EventArguments/SessionEventArgs.cs | 2 +- .../{ => Handlers}/AsyncEventHandler.cs | 0 .../{ => Handlers}/CertificateHandler.cs | 0 .../{ => Handlers}/ExceptionHandler.cs | 0 .../ProxyAuthorizationHandler.cs | 0 .../{ => Handlers}/WebSocketHandler.cs | 0 .../{ => Handlers}/WinAuthHandler.cs | 0 .../Helpers/Net45Compatibility.cs | 2 +- src/Titanium.Web.Proxy/Helpers/RunTime.cs | 2 +- src/Titanium.Web.Proxy/Http/HeaderBuilder.cs | 4 +- src/Titanium.Web.Proxy/Http2/Hpack/Decoder.cs | 4 +- .../Http2/Hpack/DynamicTable.cs | 2 +- .../TcpConnection/TcpConnectionFactory.cs | 4 +- .../Network/Writers/NullWriter.cs | 2 +- .../Titanium.Web.Proxy.Mono.csproj | 47 -------------- .../Titanium.Web.Proxy.NetCore.csproj | 52 ---------------- .../Titanium.Web.Proxy.csproj | 4 +- .../Titanium.Web.Proxy.nuspec | 4 +- 19 files changed, 15 insertions(+), 175 deletions(-) delete mode 100644 src/Titanium.Web.Proxy.Mono.sln rename src/Titanium.Web.Proxy/{ => Handlers}/AsyncEventHandler.cs (100%) rename src/Titanium.Web.Proxy/{ => Handlers}/CertificateHandler.cs (100%) rename src/Titanium.Web.Proxy/{ => Handlers}/ExceptionHandler.cs (100%) rename src/Titanium.Web.Proxy/{ => Handlers}/ProxyAuthorizationHandler.cs (100%) rename src/Titanium.Web.Proxy/{ => Handlers}/WebSocketHandler.cs (100%) rename src/Titanium.Web.Proxy/{ => Handlers}/WinAuthHandler.cs (100%) delete mode 100644 src/Titanium.Web.Proxy/Titanium.Web.Proxy.Mono.csproj delete mode 100644 src/Titanium.Web.Proxy/Titanium.Web.Proxy.NetCore.csproj diff --git a/src/Titanium.Web.Proxy.Mono.sln b/src/Titanium.Web.Proxy.Mono.sln deleted file mode 100644 index 4914496d..00000000 --- a/src/Titanium.Web.Proxy.Mono.sln +++ /dev/null @@ -1,61 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.26906.1 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{B6DBABDC-C985-4872-9C38-B4E5079CBC4B}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documentation", "Documentation", "{38EA62D0-D2CB-465D-AF4F-407C5B4D4A1E}" - ProjectSection(SolutionItems) = preProject - ..\LICENSE = ..\LICENSE - ..\PULL_REQUEST_TEMPLATE.md = ..\PULL_REQUEST_TEMPLATE.md - ..\README.md = ..\README.md - EndProjectSection -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{AC9AE37A-3059-4FDB-9A5C-363AD86F2EEF}" - ProjectSection(SolutionItems) = preProject - ..\.build\build.ps1 = ..\.build\build.ps1 - ..\.build\docfx.json = ..\.build\docfx.json - ..\.build\setup.ps1 = ..\.build\setup.ps1 - EndProjectSection -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Titanium.Web.Proxy.Mono", "Titanium.Web.Proxy\Titanium.Web.Proxy.Mono.csproj", "{5985EBC2-75E8-4555-B715-B2302D879F9B}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Titanium.Web.Proxy.Examples.Basic.Mono", "..\examples\Titanium.Web.Proxy.Examples.Basic\Titanium.Web.Proxy.Examples.Basic.Mono.csproj", "{9B5FA6A0-8D7C-46AD-B4F5-3AF6E2720C09}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Debug|x64 = Debug|x64 - Release|Any CPU = Release|Any CPU - Release|x64 = Release|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {5985EBC2-75E8-4555-B715-B2302D879F9B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {5985EBC2-75E8-4555-B715-B2302D879F9B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {5985EBC2-75E8-4555-B715-B2302D879F9B}.Debug|x64.ActiveCfg = Debug|Any CPU - {5985EBC2-75E8-4555-B715-B2302D879F9B}.Debug|x64.Build.0 = Debug|Any CPU - {5985EBC2-75E8-4555-B715-B2302D879F9B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {5985EBC2-75E8-4555-B715-B2302D879F9B}.Release|Any CPU.Build.0 = Release|Any CPU - {5985EBC2-75E8-4555-B715-B2302D879F9B}.Release|x64.ActiveCfg = Release|Any CPU - {5985EBC2-75E8-4555-B715-B2302D879F9B}.Release|x64.Build.0 = Release|Any CPU - {9B5FA6A0-8D7C-46AD-B4F5-3AF6E2720C09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9B5FA6A0-8D7C-46AD-B4F5-3AF6E2720C09}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9B5FA6A0-8D7C-46AD-B4F5-3AF6E2720C09}.Debug|x64.ActiveCfg = Debug|Any CPU - {9B5FA6A0-8D7C-46AD-B4F5-3AF6E2720C09}.Debug|x64.Build.0 = Debug|Any CPU - {9B5FA6A0-8D7C-46AD-B4F5-3AF6E2720C09}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9B5FA6A0-8D7C-46AD-B4F5-3AF6E2720C09}.Release|Any CPU.Build.0 = Release|Any CPU - {9B5FA6A0-8D7C-46AD-B4F5-3AF6E2720C09}.Release|x64.ActiveCfg = Release|Any CPU - {9B5FA6A0-8D7C-46AD-B4F5-3AF6E2720C09}.Release|x64.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(NestedProjects) = preSolution - {9B5FA6A0-8D7C-46AD-B4F5-3AF6E2720C09} = {B6DBABDC-C985-4872-9C38-B4E5079CBC4B} - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - EnterpriseLibraryConfigurationToolBinariesPath = .1.505.2\lib\NET35 - SolutionGuid = {625C1EB5-44CF-47DE-A85A-B4C8C40ED90A} - EndGlobalSection -EndGlobal diff --git a/src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs b/src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs index f13ba045..4e6630b1 100644 --- a/src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs +++ b/src/Titanium.Web.Proxy/EventArguments/SessionEventArgs.cs @@ -606,7 +606,7 @@ public void Redirect(string url, bool closeServerConnection = false) var response = new RedirectResponse(); response.HttpVersion = HttpClient.Request.HttpVersion; response.Headers.AddHeader(KnownHeaders.Location, url); -#if NET45 +#if NET451 response.Body = Net45Compatibility.EmptyArray; #else response.Body = Array.Empty(); diff --git a/src/Titanium.Web.Proxy/AsyncEventHandler.cs b/src/Titanium.Web.Proxy/Handlers/AsyncEventHandler.cs similarity index 100% rename from src/Titanium.Web.Proxy/AsyncEventHandler.cs rename to src/Titanium.Web.Proxy/Handlers/AsyncEventHandler.cs diff --git a/src/Titanium.Web.Proxy/CertificateHandler.cs b/src/Titanium.Web.Proxy/Handlers/CertificateHandler.cs similarity index 100% rename from src/Titanium.Web.Proxy/CertificateHandler.cs rename to src/Titanium.Web.Proxy/Handlers/CertificateHandler.cs diff --git a/src/Titanium.Web.Proxy/ExceptionHandler.cs b/src/Titanium.Web.Proxy/Handlers/ExceptionHandler.cs similarity index 100% rename from src/Titanium.Web.Proxy/ExceptionHandler.cs rename to src/Titanium.Web.Proxy/Handlers/ExceptionHandler.cs diff --git a/src/Titanium.Web.Proxy/ProxyAuthorizationHandler.cs b/src/Titanium.Web.Proxy/Handlers/ProxyAuthorizationHandler.cs similarity index 100% rename from src/Titanium.Web.Proxy/ProxyAuthorizationHandler.cs rename to src/Titanium.Web.Proxy/Handlers/ProxyAuthorizationHandler.cs diff --git a/src/Titanium.Web.Proxy/WebSocketHandler.cs b/src/Titanium.Web.Proxy/Handlers/WebSocketHandler.cs similarity index 100% rename from src/Titanium.Web.Proxy/WebSocketHandler.cs rename to src/Titanium.Web.Proxy/Handlers/WebSocketHandler.cs diff --git a/src/Titanium.Web.Proxy/WinAuthHandler.cs b/src/Titanium.Web.Proxy/Handlers/WinAuthHandler.cs similarity index 100% rename from src/Titanium.Web.Proxy/WinAuthHandler.cs rename to src/Titanium.Web.Proxy/Handlers/WinAuthHandler.cs diff --git a/src/Titanium.Web.Proxy/Helpers/Net45Compatibility.cs b/src/Titanium.Web.Proxy/Helpers/Net45Compatibility.cs index e183d8fd..8fe9ca67 100644 --- a/src/Titanium.Web.Proxy/Helpers/Net45Compatibility.cs +++ b/src/Titanium.Web.Proxy/Helpers/Net45Compatibility.cs @@ -1,4 +1,4 @@ -#if NET45 +#if NET451 using System; using System.Threading.Tasks; diff --git a/src/Titanium.Web.Proxy/Helpers/RunTime.cs b/src/Titanium.Web.Proxy/Helpers/RunTime.cs index 97a45f49..c3a2ea9a 100644 --- a/src/Titanium.Web.Proxy/Helpers/RunTime.cs +++ b/src/Titanium.Web.Proxy/Helpers/RunTime.cs @@ -13,7 +13,7 @@ public static class RunTime { private static readonly Lazy isRunningOnMono = new Lazy(() => Type.GetType("Mono.Runtime") != null); -#if NET45 || NET461 +#if NET451 || NET461 /// /// cache for Windows platform check /// diff --git a/src/Titanium.Web.Proxy/Http/HeaderBuilder.cs b/src/Titanium.Web.Proxy/Http/HeaderBuilder.cs index db957395..2206425b 100644 --- a/src/Titanium.Web.Proxy/Http/HeaderBuilder.cs +++ b/src/Titanium.Web.Proxy/Http/HeaderBuilder.cs @@ -94,7 +94,7 @@ public void Write(string str) public ArraySegment GetBuffer() { -#if NET45 +#if NET451 return new ArraySegment(stream.ToArray()); #else stream.TryGetBuffer(out var buffer); @@ -104,7 +104,7 @@ public ArraySegment GetBuffer() public string GetString(Encoding encoding) { -#if NET45 +#if NET451 return encoding.GetString(stream.ToArray()); #else stream.TryGetBuffer(out var buffer); diff --git a/src/Titanium.Web.Proxy/Http2/Hpack/Decoder.cs b/src/Titanium.Web.Proxy/Http2/Hpack/Decoder.cs index 89b62440..4448f496 100644 --- a/src/Titanium.Web.Proxy/Http2/Hpack/Decoder.cs +++ b/src/Titanium.Web.Proxy/Http2/Hpack/Decoder.cs @@ -257,7 +257,7 @@ public void Decode(BinaryReader input, IHeaderListener headerListener) if (nameLength + HttpHeader.HttpHeaderOverhead > dynamicTable.Capacity) { dynamicTable.Clear(); -#if NET45 +#if NET451 name = Net45Compatibility.EmptyArray; #else name = Array.Empty(); @@ -378,7 +378,7 @@ public void Decode(BinaryReader input, IHeaderListener headerListener) if (valueLength == 0) { -#if NET45 +#if NET451 InsertHeader(headerListener, name, Net45Compatibility.EmptyArray, indexType); #else name = Array.Empty(); diff --git a/src/Titanium.Web.Proxy/Http2/Hpack/DynamicTable.cs b/src/Titanium.Web.Proxy/Http2/Hpack/DynamicTable.cs index 44ee27d1..907eac83 100644 --- a/src/Titanium.Web.Proxy/Http2/Hpack/DynamicTable.cs +++ b/src/Titanium.Web.Proxy/Http2/Hpack/DynamicTable.cs @@ -23,7 +23,7 @@ namespace Titanium.Web.Proxy.Http2.Hpack public class DynamicTable { // a circular queue of header fields -#if NET45 +#if NET451 HttpHeader[] headerFields = new HttpHeader[0]; #else HttpHeader[] headerFields = Array.Empty(); diff --git a/src/Titanium.Web.Proxy/Network/TcpConnection/TcpConnectionFactory.cs b/src/Titanium.Web.Proxy/Network/TcpConnection/TcpConnectionFactory.cs index 5d1c433f..3194682f 100644 --- a/src/Titanium.Web.Proxy/Network/TcpConnection/TcpConnectionFactory.cs +++ b/src/Titanium.Web.Proxy/Network/TcpConnection/TcpConnectionFactory.cs @@ -458,7 +458,7 @@ internal Task GetServerConnection(ProxyServer proxyServer, try { -#if NET45 +#if NET451 tcpServerSocket?.Close(); #else tcpServerSocket?.Dispose(); @@ -479,7 +479,7 @@ internal Task GetServerConnection(ProxyServer proxyServer, { // dispose the current TcpClient and try the next address lastException = e; -#if NET45 +#if NET451 tcpServerSocket?.Close(); #else tcpServerSocket?.Dispose(); diff --git a/src/Titanium.Web.Proxy/Network/Writers/NullWriter.cs b/src/Titanium.Web.Proxy/Network/Writers/NullWriter.cs index c3ef9dc8..fb917a8f 100644 --- a/src/Titanium.Web.Proxy/Network/Writers/NullWriter.cs +++ b/src/Titanium.Web.Proxy/Network/Writers/NullWriter.cs @@ -20,7 +20,7 @@ public void Write(byte[] buffer, int offset, int count) public Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { -#if NET45 +#if NET451 return Net45Compatibility.CompletedTask; #else return Task.CompletedTask; diff --git a/src/Titanium.Web.Proxy/Titanium.Web.Proxy.Mono.csproj b/src/Titanium.Web.Proxy/Titanium.Web.Proxy.Mono.csproj deleted file mode 100644 index 510b66f6..00000000 --- a/src/Titanium.Web.Proxy/Titanium.Web.Proxy.Mono.csproj +++ /dev/null @@ -1,47 +0,0 @@ - - - - net461 - Titanium.Web.Proxy - false - True - StrongNameKey.snk - True - 8.0 - AnyCPU;x64 - - - - - - - - - - - - - - - True - True - - - True - True - - - True - True - - - True - True - - - True - True - - - - \ No newline at end of file diff --git a/src/Titanium.Web.Proxy/Titanium.Web.Proxy.NetCore.csproj b/src/Titanium.Web.Proxy/Titanium.Web.Proxy.NetCore.csproj deleted file mode 100644 index 9bc3f43e..00000000 --- a/src/Titanium.Web.Proxy/Titanium.Web.Proxy.NetCore.csproj +++ /dev/null @@ -1,52 +0,0 @@ - - - - netstandard2.1 - Titanium.Web.Proxy - false - True - StrongNameKey.snk - False - True - enable - 8.0 - - - - - - - - - - 5.0.0 - - - 5.0.0 - - - - - - True - True - - - True - True - - - True - True - - - True - True - - - True - True - - - - \ No newline at end of file diff --git a/src/Titanium.Web.Proxy/Titanium.Web.Proxy.csproj b/src/Titanium.Web.Proxy/Titanium.Web.Proxy.csproj index c038ae86..64890593 100644 --- a/src/Titanium.Web.Proxy/Titanium.Web.Proxy.csproj +++ b/src/Titanium.Web.Proxy/Titanium.Web.Proxy.csproj @@ -1,7 +1,7 @@  - net45;net461;netstandard2.0;netstandard2.1 + net451;net461;netstandard2.0;netstandard2.1 Titanium.Web.Proxy false True @@ -23,7 +23,7 @@ - + 5.0.0 diff --git a/src/Titanium.Web.Proxy/Titanium.Web.Proxy.nuspec b/src/Titanium.Web.Proxy/Titanium.Web.Proxy.nuspec index 585a2088..908afe70 100644 --- a/src/Titanium.Web.Proxy/Titanium.Web.Proxy.nuspec +++ b/src/Titanium.Web.Proxy/Titanium.Web.Proxy.nuspec @@ -14,7 +14,7 @@ Copyright © Titanium. All rights reserved. - + @@ -48,7 +48,7 @@ - +