@@ -98,8 +98,58 @@ private void StopCertificateTimer()
98
98
99
99
protected override Task < StartProxyResult > StartProxyImpl ( ) => Task . FromResult ( StartProxyCore ( ) ) ;
100
100
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
+
101
145
StartProxyResult StartProxyCore ( )
102
146
{
147
+ #if WINDOWS
148
+ if ( ! IcuTest ( allowedHosts ) )
149
+ {
150
+ return StartProxyResultCode . IcuTestFail ;
151
+ }
152
+ #endif
103
153
// https://github.com/dotnetcore/FastGithub/blob/2.1.4/FastGithub/Program.cs#L29
104
154
try
105
155
{
@@ -113,13 +163,10 @@ StartProxyResult StartProxyCore()
113
163
114
164
builder . Logging . AddProvider ( new LogConsoleService . Utf8StringLoggerProvider ( AssemblyInfo . Accelerator ) ) ;
115
165
116
- builder . Services . Configure < HostFilteringOptions > ( static o =>
166
+ builder . Services . Configure < HostFilteringOptions > ( o =>
117
167
{
118
168
o . AllowEmptyHosts = true ;
119
- o . AllowedHosts = new List < string >
120
- {
121
- "*" ,
122
- } ;
169
+ o . AllowedHosts = allowedHosts ;
123
170
} ) ;
124
171
125
172
builder . Host . UseNLog ( ) ;
0 commit comments