Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c8810f7

Browse files
committed
⬆️ 反代 http3 支持
1 parent 073b588 commit c8810f7

1 file changed

Lines changed: 36 additions & 15 deletions

File tree

src/BD.WTTS.Client.Plugins.Accelerator.ReverseProxy/Services.Implementation/HttpServer/Middleware/HttpReverseProxyMiddleware.cs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using Microsoft.AspNetCore.Http;
44
using Yarp.ReverseProxy.Forwarder;
5+
using HttpVersion = System.Net.HttpVersion;
56

67
// ReSharper disable once CheckNamespace
78
namespace BD.WTTS.Services.Implementation;
@@ -30,6 +31,27 @@ public HttpReverseProxyMiddleware(
3031
this.logger = logger;
3132
}
3233

34+
static ArgumentOutOfRangeException GetUnknownHttpVersionException(string? actualValue, [CallerArgumentExpression(nameof(actualValue))] string? paramName = null) => new(
35+
$"""
36+
Version doesn't map to a known HTTP protocol. (Parameter '{paramName}')
37+
Actual value was {actualValue}.
38+
""");
39+
40+
static Version GetHttpVersion(string requestProtocol) =>
41+
(requestProtocol != null && requestProtocol.Length >= 6) ? requestProtocol[5] switch
42+
{
43+
// 参考 Microsoft.AspNetCore.Http.HttpProtocol.GetHttpProtocol
44+
'1' => requestProtocol.Length >= 8 ? requestProtocol[7] switch
45+
{
46+
'0' => HttpVersion.Version10,
47+
'1' => HttpVersion.Version11,
48+
_ => throw GetUnknownHttpVersionException(requestProtocol),
49+
} : throw GetUnknownHttpVersionException(requestProtocol),
50+
'2' => HttpVersion.Version20,
51+
'3' => HttpVersion.Version30,
52+
_ => throw GetUnknownHttpVersionException(requestProtocol),
53+
} : throw GetUnknownHttpVersionException(requestProtocol);
54+
3355
/// <summary>
3456
/// 处理请求
3557
/// </summary>
@@ -60,12 +82,7 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
6082
var destinationPrefix = GetDestinationPrefix(context.Request.Scheme, context.Request.Host, null);
6183
var forwarderRequestConfig = new ForwarderRequestConfig()
6284
{
63-
Version = context.Request.Protocol switch
64-
{
65-
var protocol when protocol.StartsWith("HTTP/2") => System.Net.HttpVersion.Version20,
66-
var protocol when protocol.StartsWith("HTTP/3") => System.Net.HttpVersion.Version30,
67-
_ => System.Net.HttpVersion.Version11,
68-
},
85+
Version = GetHttpVersion(context.Request.Protocol),
6986
};
7087
var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient, forwarderRequestConfig, HttpTransformer.Empty);
7188
if (error != ForwarderError.None)
@@ -119,19 +136,23 @@ var protocol when protocol.StartsWith("HTTP/3") => System.Net.HttpVersion.Versio
119136
context.Request.Headers.UserAgent = domainConfig.UserAgent.Replace("${origin}", context.Request.Headers.UserAgent, StringComparison.OrdinalIgnoreCase);
120137
}
121138

122-
var forwarderRequestConfig = new ForwarderRequestConfig()
123-
{
124-
Version = context.Request.Protocol switch
125-
{
126-
var protocol when protocol.StartsWith("HTTP/2") => System.Net.HttpVersion.Version20,
127-
var protocol when protocol.StartsWith("HTTP/3") => System.Net.HttpVersion.Version30,
128-
_ => System.Net.HttpVersion.Version11,
129-
},
130-
};
139+
ForwarderRequestConfig forwarderRequestConfig;
131140

132141
if (domainConfig.IsServerSideProxy)
133142
{
134143
SetWattHeaders(context, reverseProxyConfig.Service.ServerSideProxyToken);
144+
forwarderRequestConfig = new ForwarderRequestConfig()
145+
{
146+
Version = GetHttpVersion(context.Request.Protocol),
147+
VersionPolicy = HttpVersionPolicy.RequestVersionOrHigher
148+
};
149+
}
150+
else
151+
{
152+
forwarderRequestConfig = new ForwarderRequestConfig()
153+
{
154+
Version = GetHttpVersion(context.Request.Protocol),
155+
};
135156
}
136157

137158
var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient, forwarderRequestConfig, HttpTransformer.Empty);

0 commit comments

Comments
 (0)