Commit bcfae214 authored by Honfika's avatar Honfika

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

parent 6f0cb5f8
......@@ -127,6 +127,19 @@ namespace Titanium.Web.Proxy
{
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;
try
......@@ -140,15 +153,15 @@ namespace Titanium.Web.Proxy
//Successfully managed to authenticate the client using the fake certificate
var options = new SslServerAuthenticationOptions();
options.ApplicationProtocols = clientHelloInfo.GetAlpn();
if (options.ApplicationProtocols == null || options.ApplicationProtocols.Count == 0)
if (http2Supproted)
{
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.ClientCertificateRequired = false;
options.EnabledSslProtocols = SupportedSslProtocols;
......
......@@ -30,6 +30,8 @@ namespace Titanium.Web.Proxy.Network.Tcp
internal bool IsHttps { get; set; }
internal bool IsHttp2Supported { get; set; }
internal bool UseUpstreamProxy { get; set; }
/// <summary>
......
......@@ -55,6 +55,8 @@ namespace Titanium.Web.Proxy.Network.Tcp
TcpClient client = null;
CustomBufferedStream stream = null;
bool http2Supported = false;
try
{
client = new TcpClient(upStreamEndPoint);
......@@ -119,14 +121,14 @@ namespace Titanium.Web.Proxy.Network.Tcp
options.ApplicationProtocols = SslExtensions.Http11ProtocolAsList;
}
// server connection is always HTTP 1.x, todo
//options.ApplicationProtocols = SslExtensions.Http11ProtocolAsList;
options.TargetHost = remoteHostName;
options.ClientCertificates = null;
options.EnabledSslProtocols = proxyServer.SupportedSslProtocols;
options.CertificateRevocationCheckMode = proxyServer.CheckCertificateRevocation;
await sslStream.AuthenticateAsClientAsync(options, cancellationToken);
#if NETCOREAPP2_1
http2Supported = sslStream.NegotiatedApplicationProtocol == SslApplicationProtocol.Http2;
#endif
}
client.ReceiveTimeout = proxyServer.ConnectionTimeOutSeconds * 1000;
......@@ -146,6 +148,7 @@ namespace Titanium.Web.Proxy.Network.Tcp
HostName = remoteHostName,
Port = remotePort,
IsHttps = decryptSsl,
IsHttp2Supported = http2Supported,
UseUpstreamProxy = useUpstreamProxy,
TcpClient = client,
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