XFEExtension.NetCore.ServerInteractive
[DLL] Server interaction extension, including user identity verification and querying in conjunction with AutoConfig
关注
0
Fork
0
Star
0
返回提交历史
Modified
XFEExtension.NetCore.ServerInteractive/Utilities/Helpers/RouteMatchHelper.cs
+1
-1
Modified
XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/EntryPointVerifyService.cs
+8
-8
Modified
XFEExtension.NetCore.ServerInteractive/Utilities/Server/XFEServerCore.cs
+30
-6
XFEstudio/XFEExtension.NetCore.ServerInteractive
fix: apply review feedback - wildcard route detection, HTTP method allowlist, and exception handling
Agent-Logs-Url: https://github.com/XFEstudio/XFEExtension.NetCore.ServerInteractive/sessions/77a7da83-b8fd-4f29-8e8a-4036a394ef18 Co-authored-by: XFEstudio <132526994+XFEstudio@users.noreply.github.com>
1713246
代码差异
3 个文件
+39
-15
@@ -10,7 +10,7 @@ internal static class RouteMatchHelper
10
10
/// </summary>
11
11
/// <param name="pattern">路由模式</param>
12
12
/// <returns>是否包含通配符</returns>
13
public static bool IsWildcardRoute(string pattern) => pattern.Contains('*');
13
public static bool IsWildcardRoute(string pattern) => pattern.Split('/').Any(segment => segment == "*");
14
14
15
15
/// <summary>
16
16
/// 将路由模式与实际路由进行通配符匹配
@@ -19,16 +19,16 @@ public class EntryPointVerifyService : ServerCoreVerifyServiceBase
19
19
throw Error("您的IP已被封禁", HttpStatusCode.Forbidden);
20
20
}
21
21
22
if (!XFEServerCore.AcceptPost && Request.HttpMethod == "POST")
22
var method = Request.HttpMethod;
23
var isAllowed = (XFEServerCore.AcceptGet && method == "GET") || (XFEServerCore.AcceptPost && method == "POST");
24
if (!isAllowed)
23
25
{
24
26
Console.WriteLine("-校验失败");
25
throw Error("不接受为POST的请求方法", HttpStatusCode.MethodNotAllowed);
26
}
27
28
if (!XFEServerCore.AcceptGet && Request.HttpMethod == "GET")
29
{
30
Console.WriteLine("-校验失败");
31
throw Error("不接受为GET的请求方法", HttpStatusCode.MethodNotAllowed);
27
var allowedMethods = new List<string>();
28
if (XFEServerCore.AcceptGet) allowedMethods.Add("GET");
29
if (XFEServerCore.AcceptPost) allowedMethods.Add("POST");
30
var allowedStr = allowedMethods.Count > 0 ? string.Join(", ", allowedMethods) : "无";
31
throw Error($"不接受的请求方法 {method},当前允许的方法:{allowedStr}", HttpStatusCode.MethodNotAllowed);
32
32
}
33
33
34
34
Console.WriteLine("-校验通过");
@@ -70,12 +70,12 @@ public abstract class XFEServerCore : ServerCoreServiceBase
70
70
71
71
private async void CyberCommServer_RequestReceived(object? sender, CyberCommRequestEventArgs e)
72
72
{
73
var r = new ServerCoreReturnArgs
74
{
75
Args = e
76
};
73
77
try
74
78
{
75
var r = new ServerCoreReturnArgs
76
{
77
Args = e
78
};
79
79
var clientIP = e.ClientIP;
80
80
try { clientIP = GetIPFunction(e); } catch (Exception ex) { Console.WriteLine($"[WARN]获取IP地址失败:{ex.Message}"); }
81
81
r.ClientIP = clientIP;
@@ -188,13 +188,31 @@ public abstract class XFEServerCore : ServerCoreServiceBase
188
188
}
189
189
else
190
190
{
191
// 尝试通配符匹配
191
// 尝试通配符匹配:在所有命中的候选中选择最具体的模式(字面量段越多越优先),避免结果依赖注册顺序
192
static int GetWildcardPatternPriority(string pattern)
193
{
194
var segments = pattern.Split('/', StringSplitOptions.RemoveEmptyEntries);
195
var literalSegmentCount = 0;
196
var wildcardSegmentCount = 0;
197
foreach (var segment in segments)
198
{
199
if (segment == "*")
200
wildcardSegmentCount++;
201
else
202
literalSegmentCount++;
203
}
204
return (literalSegmentCount * 1000) - (wildcardSegmentCount * 10) + pattern.Length;
205
}
206
207
var bestPriority = int.MinValue;
192
208
foreach (var (pattern, factory) in WildcardCoreServiceList)
193
209
{
194
210
if (!RouteMatchHelper.MatchWildcardRoute(pattern, route)) continue;
211
var currentPriority = GetWildcardPatternPriority(pattern);
212
if (currentPriority <= bestPriority) continue;
213
bestPriority = currentPriority;
195
214
matchedPattern = pattern;
196
215
serviceFactory = factory;
197
break;
198
216
}
199
217
}
200
218
@@ -261,6 +279,12 @@ public abstract class XFEServerCore : ServerCoreServiceBase
261
279
{
262
280
Console.WriteLine($"[ERROR]处理请求时发生未捕获的异常:{ex.Message}");
263
281
Console.WriteLine($"[TRACE] {ex.StackTrace}");
282
ServerCoreError?.Invoke(this, new()
283
{
284
StatusCode = HttpStatusCode.InternalServerError,
285
ReturnArgs = r,
286
ServerException = new ProcessStandardRequestException("处理请求时发生未捕获的异常", ex)
287
});
264
288
}
265
289
}
266
290