Skip to content

Commit 69296f9

Browse files
committed
✨ IcuTestFail
1 parent 6577ed9 commit 69296f9

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

src/BD.WTTS.Client.Plugins.Accelerator.ReverseProxy/Services.Implementation/YarpReverseProxyServiceImpl.cs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,58 @@ private void StopCertificateTimer()
9898

9999
protected override Task<StartProxyResult> StartProxyImpl() => Task.FromResult(StartProxyCore());
100100

101+
readonly string[] allowedHosts = ["*",];
102+
103+
#if WINDOWS
104+
/// <summary>
105+
/// https://learn.microsoft.com/zh-cn/dotnet/core/extensions/globalization-icu#determine-if-your-app-is-using-icu
106+
/// </summary>
107+
/// <returns></returns>
108+
static bool ICUMode()
109+
{
110+
SortVersion sortVersion = CultureInfo.InvariantCulture.CompareInfo.Version;
111+
byte[] bytes = sortVersion.SortId.ToByteArray();
112+
int version = bytes[3] << 24 | bytes[2] << 16 | bytes[1] << 8 | bytes[0];
113+
return version != 0 && version == sortVersion.FullVersion;
114+
}
115+
116+
static bool IcuTest(IEnumerable<string> incoming)
117+
{
118+
if (!OperatingSystem.IsWindowsVersionAtLeast(10, 0, 18362) && ICUMode())
119+
{
120+
// https://learn.microsoft.com/zh-cn/dotnet/core/extensions/globalization-icu#icu-on-windows
121+
// 与 .NET 6 和 .NET 5 相比,.NET 7 及更高版本能够在早期 Windows 版本上加载 ICU
122+
// 通常问题出在 1703 ~ 1903 之间的 Windows 版本
123+
try
124+
{
125+
// https://github.com/dotnet/aspnetcore/blob/v9.0.2/src/Middleware/HostFiltering/src/MiddlewareConfigurationManager.cs#L67
126+
foreach (var entry in incoming)
127+
{
128+
// Punycode. Http.Sys requires you to register Unicode hosts, but the headers contain punycode.
129+
var host = new HostString(entry).ToUriComponent();
130+
}
131+
return true;
132+
}
133+
catch
134+
{
135+
return false;
136+
}
137+
}
138+
else
139+
{
140+
return true;
141+
}
142+
}
143+
#endif
144+
101145
StartProxyResult StartProxyCore()
102146
{
147+
#if WINDOWS
148+
if (!IcuTest(allowedHosts))
149+
{
150+
return StartProxyResultCode.IcuTestFail;
151+
}
152+
#endif
103153
// https://github.com/dotnetcore/FastGithub/blob/2.1.4/FastGithub/Program.cs#L29
104154
try
105155
{
@@ -113,13 +163,10 @@ StartProxyResult StartProxyCore()
113163

114164
builder.Logging.AddProvider(new LogConsoleService.Utf8StringLoggerProvider(AssemblyInfo.Accelerator));
115165

116-
builder.Services.Configure<HostFilteringOptions>(static o =>
166+
builder.Services.Configure<HostFilteringOptions>(o =>
117167
{
118168
o.AllowEmptyHosts = true;
119-
o.AllowedHosts = new List<string>
120-
{
121-
"*",
122-
};
169+
o.AllowedHosts = allowedHosts;
123170
});
124171

125172
builder.Host.UseNLog();

src/BD.WTTS.Client.Plugins.Accelerator.ReverseProxy/Services/IReverseProxyService.Constants.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,12 @@ public enum StartProxyResultCode : byte
341341
/// 绑定端口错误
342342
/// </summary>
343343
BindPortError,
344+
345+
/// <summary>
346+
/// 早期 Windows 版本且缺少 Windows 更新补丁,Icu 存在 Bug 导致,要么更新操作系统,要么更改配置使用 NLS 重启程序
347+
/// <para>https://learn.microsoft.com/zh-cn/dotnet/core/extensions/globalization-icu#use-nls-instead-of-icu</para>
348+
/// </summary>
349+
IcuTestFail = 127, // 值与 Mobius 保持一致
344350
}
345351

346352
/// <summary>

0 commit comments

Comments
 (0)