Commit c63e280b authored by Honfika's avatar Honfika

Detect server http/2 support (fallback proxy to 1.x when server does not supprot http/2)

parent 0fe5a400
...@@ -127,6 +127,19 @@ namespace Titanium.Web.Proxy ...@@ -127,6 +127,19 @@ namespace Titanium.Web.Proxy
{ {
connectRequest.RequestUri = new Uri("https://" + httpUrl); connectRequest.RequestUri = new Uri("https://" + httpUrl);
bool http2Supproted = false;
var alpn = clientHelloInfo.GetAlpn();
if (alpn != null && alpn.Contains(SslApplicationProtocol.Http2))
{
// test server HTTP/2 support
// todo: this is a hack, because Titanium does not support HTTP protocol changing currently
using (var connection = await GetServerConnection(connectArgs, true, cancellationToken))
{
http2Supproted = connection.IsHttp2Supported;
}
}
SslStream sslStream = null; SslStream sslStream = null;
try try
...@@ -140,15 +153,15 @@ namespace Titanium.Web.Proxy ...@@ -140,15 +153,15 @@ namespace Titanium.Web.Proxy
//Successfully managed to authenticate the client using the fake certificate //Successfully managed to authenticate the client using the fake certificate
var options = new SslServerAuthenticationOptions(); var options = new SslServerAuthenticationOptions();
options.ApplicationProtocols = clientHelloInfo.GetAlpn(); if (http2Supproted)
if (options.ApplicationProtocols == null || options.ApplicationProtocols.Count == 0)
{ {
options.ApplicationProtocols = SslExtensions.Http11ProtocolAsList; options.ApplicationProtocols = clientHelloInfo.GetAlpn();
if (options.ApplicationProtocols == null || options.ApplicationProtocols.Count == 0)
{
options.ApplicationProtocols = SslExtensions.Http11ProtocolAsList;
}
} }
// client connection is always HTTP 1.x, todo
//options.ApplicationProtocols = SslExtensions.Http11ProtocolAsList;
options.ServerCertificate = certificate; options.ServerCertificate = certificate;
options.ClientCertificateRequired = false; options.ClientCertificateRequired = false;
options.EnabledSslProtocols = SupportedSslProtocols; options.EnabledSslProtocols = SupportedSslProtocols;
......
...@@ -30,6 +30,8 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -30,6 +30,8 @@ namespace Titanium.Web.Proxy.Network.Tcp
internal bool IsHttps { get; set; } internal bool IsHttps { get; set; }
internal bool IsHttp2Supported { get; set; }
internal bool UseUpstreamProxy { get; set; } internal bool UseUpstreamProxy { get; set; }
/// <summary> /// <summary>
......
...@@ -55,6 +55,8 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -55,6 +55,8 @@ namespace Titanium.Web.Proxy.Network.Tcp
TcpClient client = null; TcpClient client = null;
CustomBufferedStream stream = null; CustomBufferedStream stream = null;
bool http2Supported = false;
try try
{ {
client = new TcpClient(upStreamEndPoint); client = new TcpClient(upStreamEndPoint);
...@@ -119,14 +121,14 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -119,14 +121,14 @@ namespace Titanium.Web.Proxy.Network.Tcp
options.ApplicationProtocols = SslExtensions.Http11ProtocolAsList; options.ApplicationProtocols = SslExtensions.Http11ProtocolAsList;
} }
// server connection is always HTTP 1.x, todo
//options.ApplicationProtocols = SslExtensions.Http11ProtocolAsList;
options.TargetHost = remoteHostName; options.TargetHost = remoteHostName;
options.ClientCertificates = null; options.ClientCertificates = null;
options.EnabledSslProtocols = proxyServer.SupportedSslProtocols; options.EnabledSslProtocols = proxyServer.SupportedSslProtocols;
options.CertificateRevocationCheckMode = proxyServer.CheckCertificateRevocation; options.CertificateRevocationCheckMode = proxyServer.CheckCertificateRevocation;
await sslStream.AuthenticateAsClientAsync(options, cancellationToken); await sslStream.AuthenticateAsClientAsync(options, cancellationToken);
#if NETCOREAPP2_1
http2Supported = sslStream.NegotiatedApplicationProtocol == SslApplicationProtocol.Http2;
#endif
} }
client.ReceiveTimeout = proxyServer.ConnectionTimeOutSeconds * 1000; client.ReceiveTimeout = proxyServer.ConnectionTimeOutSeconds * 1000;
...@@ -146,6 +148,7 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -146,6 +148,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
HostName = remoteHostName, HostName = remoteHostName,
Port = remotePort, Port = remotePort,
IsHttps = decryptSsl, IsHttps = decryptSsl,
IsHttp2Supported = http2Supported,
UseUpstreamProxy = useUpstreamProxy, UseUpstreamProxy = useUpstreamProxy,
TcpClient = client, TcpClient = client,
StreamReader = new CustomBinaryReader(stream, proxyServer.BufferSize), StreamReader = new CustomBinaryReader(stream, proxyServer.BufferSize),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment