Skip to content

Commit

Permalink
- Fix HTTPS proxies some issues (issue #44)
Browse files Browse the repository at this point in the history
- Changed target NET Framework to 4.5 now supported (4.5+ now supported)
- ProxyClient.AbsoluteUriInStartingLine by default is false now
  • Loading branch information
grandsilence committed Mar 26, 2020
1 parent fa9d032 commit 653a925
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Leaf.xNet.Tests/Leaf.xNet.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net472;netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>net45;netcoreapp3.0</TargetFrameworks>

<IsPackable>false</IsPackable>

Expand Down
4 changes: 2 additions & 2 deletions Leaf.xNet/Leaf.xNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Leaf.xNet</RootNamespace>
<AssemblyName>Leaf.xNet</AssemblyName>
<TargetFrameworks>net452;net462;net472;net48;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<FileAlignment>512</FileAlignment>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Grand Silence</Authors>
Expand Down Expand Up @@ -49,7 +49,7 @@
<PackageTags>net,http,socks,proxy,cloudflare,xnet,https,stormwall,useragent,parsing,bot,web,crowling</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<NoWarn>1591,1573</NoWarn>
<Description>Improved xNet for .NET Framework 4.5.2+ &amp; .NET Core 2+</Description> <!-- Ignore warinings abound undocumented code -->
<Description>Improved xNet for .NET Framework 4.5+ / .NET Standard 2.0</Description> <!-- Ignore warinings abound undocumented code -->
</PropertyGroup>

<ItemGroup>
Expand Down
19 changes: 18 additions & 1 deletion Leaf.xNet/~Http/HttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,18 @@ public HttpRequest()
{
Init();
}

static HttpRequest()
{
// It's a fix of HTTPs Proxies SSL issue
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback += ServerCertificateValidationCallback;
}

private static bool ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslpolicyerrors)
{
return true;
}

/// <summary>
/// Инициализирует новый экземпляр класса <see cref="HttpRequest"/>.
Expand Down Expand Up @@ -3528,7 +3540,12 @@ private void CreateConnection(Uri address)
private string GenerateStartingLine(HttpMethod method)
{
// Fix by Igor Vacil'ev: sometimes proxies returns 404 when used full path.
string query = _currentProxy != null && _currentProxy.Type == ProxyType.HTTP && _currentProxy.AbsoluteUriInStartingLine
bool hasHttpProxyWithAbsoluteUriInStartingLine =
_currentProxy != null &&
_currentProxy.Type == ProxyType.HTTP &&
_currentProxy.AbsoluteUriInStartingLine;

string query = hasHttpProxyWithAbsoluteUriInStartingLine
? Address.AbsoluteUri
: Address.PathAndQuery;

Expand Down
16 changes: 8 additions & 8 deletions Leaf.xNet/~Proxy/HttpProxyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,14 @@ private string GenerateAuthorizationHeader()

private void SendConnectionCommand(Stream nStream, string destinationHost, int destinationPort)
{
var commandBuilder = new StringBuilder();

commandBuilder.AppendFormat("CONNECT {0}:{1} HTTP/{2}\r\n", destinationHost, destinationPort, ProtocolVersion);
commandBuilder.AppendFormat(GenerateAuthorizationHeader());
commandBuilder.AppendLine();

var buffer = Encoding.ASCII.GetBytes(commandBuilder.ToString());

var sb = new StringBuilder();
sb.AppendFormat("CONNECT {0}:{1} HTTP/{2}\r\n", destinationHost, destinationPort, ProtocolVersion);
sb.AppendFormat(GenerateAuthorizationHeader());
sb.Append("Host: "); sb.AppendLine(destinationHost);
sb.AppendLine("Proxy-Connection: Keep-Alive");
sb.AppendLine();

var buffer = Encoding.ASCII.GetBytes(sb.ToString());
nStream.Write(buffer, 0, buffer.Length);
}

Expand Down
4 changes: 1 addition & 3 deletions Leaf.xNet/~Proxy/ProxyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public int ReadWriteTimeout
/// Если задано <see langword="false"/> - всегда будет использован относительный адрес в заголовке запроса.
/// </summary>
// ReSharper disable once MemberCanBePrivate.Global
public bool AbsoluteUriInStartingLine { get; set; } = true;
public bool AbsoluteUriInStartingLine { get; set; }

#endregion

Expand Down Expand Up @@ -215,8 +215,6 @@ public static HttpProxyClient DebugHttpProxy {
return _debugHttpProxy;

_debugHttpProxy = HttpProxyClient.Parse("127.0.0.1:8888");
_debugHttpProxy.AbsoluteUriInStartingLine = false; // Fix tree Charles bug

return _debugHttpProxy;
}
}
Expand Down
12 changes: 3 additions & 9 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@ build:
publish_nuget: true
verbosity: minimal
artifacts:
- path: Leaf.xNet\bin\Release\net452
name: Leaf.xNet_v$(appveyor_build_version)__NET_Framework_4.5.2
- path: Leaf.xNet\bin\Release\net462
name: Leaf.xNet_v$(appveyor_build_version)__NET_Framework_4.6.2
- path: Leaf.xNet\bin\Release\net472
name: Leaf.xNet_v$(appveyor_build_version)__NET_Framework_4.7.2
- path: Leaf.xNet\bin\Release\net48
name: Leaf.xNet_v$(appveyor_build_version)__NET_Framework_4.8
- path: Leaf.xNet\bin\Release\net45
name: Leaf.xNet_v$(appveyor_build_version)__NET_Framework_4.5
- path: Leaf.xNet\bin\Release\netstandard2.0
name: Leaf.xNet_v$(appveyor_build_version)__NET_Standard_2.0
deploy:
Expand All @@ -50,7 +44,7 @@ deploy:
release: Leaf.xNet v$(appveyor_build_version)
auth_token:
secure: NQtMOO3yB309cDK/pFWRiQ==
artifact: Leaf.xNet_v$(appveyor_build_version)__NET_Framework_4.5.2;Leaf.xNet_v$(appveyor_build_version)__NET_Framework_4.6.2;Leaf.xNet_v$(appveyor_build_version)__NET_Framework_4.7.2;Leaf.xNet_v$(appveyor_build_version)__NET_Framework_4.8;Leaf.xNet_v$(appveyor_build_version)__NET_Standard_2.0
artifact: Leaf.xNet_v$(appveyor_build_version)__NET_Framework_4.5;Leaf.xNet_v$(appveyor_build_version)__NET_Standard_2.0
on:
branch: master
only_commits:
Expand Down

0 comments on commit 653a925

Please sign in to comment.