XFEExtension.NetCore.ServerInteractive
[DLL] Server interaction extension, including user identity verification and querying in conjunction with AutoConfig
关注
0
Fork
0
Star
0
返回提交历史
Added
XFEExtension.NetCore.ServerInteractive/Attributes/EntryPointAttribute.cs
+27
-0
Modified
XFEExtension.NetCore.ServerInteractive/Interfaces/CoreService/IServerCoreStandardService.cs
+8
-8
Modified
XFEExtension.NetCore.ServerInteractive/Options/XFEServerCoreOptions.cs
+4
-0
Modified
XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/EntryPointVerifyService.cs
+3
-2
Modified
XFEExtension.NetCore.ServerInteractive/Utilities/Server/XFEServerCore.cs
+26
-6
Modified
XFEExtension.NetCore.ServerInteractive/Utilities/Server/XFEServerCoreBuilder.cs
+1
-0
XFEstudio/XFEExtension.NetCore.ServerInteractive
重构:实现主入口点和次级入口点分离验证系统
Agent-Logs-Url: https://github.com/XFEstudio/XFEExtension.NetCore.ServerInteractive/sessions/94801386-67f0-44c8-99b7-7dbcc8a45b8e Co-authored-by: XFEstudio <132526994+XFEstudio@users.noreply.github.com>
3772a40
代码差异
6 个文件
+69
-16
@@ -0,0 +1,27 @@
1
namespace XFEExtension.NetCore.ServerInteractive.Attributes;
2
3
/// <summary>
4
/// 次级入口点特性,用于标记IServerCoreStandardService中的处理方法
5
/// </summary>
6
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
7
public class EntryPointAttribute : Attribute
8
{
9
/// <summary>
10
/// 次级入口点路径
11
/// </summary>
12
public string Path { get; }
13
14
/// <summary>
15
/// 是否为异步方法
16
/// </summary>
17
public bool IsAsync { get; set; }
18
19
/// <summary>
20
/// 创建次级入口点特性
21
/// </summary>
22
/// <param name="path">入口点路径</param>
23
public EntryPointAttribute(string path)
24
{
25
Path = path;
26
}
27
}
@@ -6,17 +6,17 @@
6
6
public interface IServerCoreStandardService : IXFEStandardServerCoreServiceBase
7
7
{
8
8
/// <summary>
9
/// 服务器标准核心服务初始化事件(在请求初始化的时候执行)
9
/// 同步入口点字典,Key为入口点路径,Value为对应的处理方法
10
10
/// </summary>
11
void Initialize();
11
Dictionary<string, Action> SyncEntryPoints { get; }
12
12
13
/// <summary>
13
/// 服务器标准请求接收异步事件
14
/// 子类可直接通过属性访问 Execute / QueryableJsonNode / ReturnArgs
14
/// 异步入口点字典,Key为入口点路径,Value为对应的异步处理方法
15
15
/// </summary>
16
Task RequestReceiveAsync();
16
Dictionary<string, Func<Task>> AsyncEntryPoints { get; }
17
17
18
/// <summary>
18
/// 服务器标准请求接收事件
19
/// 子类可直接通过属性访问 Execute / QueryableJsonNode / ReturnArgs
19
/// 服务器标准核心服务初始化事件(在请求初始化的时候执行)
20
20
/// </summary>
21
void RequestReceive();
21
void Initialize();
22
22
}
@@ -27,6 +27,10 @@ public class XFEServerCoreOptions
27
27
/// 获取IP地址的函数,默认为从请求事件参数中获取客户端IP地址
28
28
/// </summary>
29
29
public Func<CyberCommRequestEventArgs, string> GetIPFunction { get; set; } = args => args.ClientIP;
30
/// <summary>
31
/// 主入口点路径(ServerCore主要入口点),默认为"api"
32
/// </summary>
33
public string MainEntryPoint { get; set; } = "api";
30
34
31
35
/// <summary>
32
36
/// 绑定IP地址
@@ -5,7 +5,7 @@ using XFEExtension.NetCore.ServerInteractive.Profiles;
5
5
namespace XFEExtension.NetCore.ServerInteractive.Utilities.Server.Services.CoreService;
6
6
7
7
/// <summary>
8
/// 服务器入口点校验
8
/// 服务器主入口点校验
9
9
/// </summary>
10
10
public class EntryPointVerifyService : ServerCoreVerifyServiceBase
11
11
{
@@ -18,7 +18,8 @@ public class EntryPointVerifyService : ServerCoreVerifyServiceBase
18
18
Console.WriteLine("-校验失败");
19
19
throw Error("您的IP已被封禁", HttpStatusCode.Forbidden);
20
20
}
21
if (Request.Url?.Segments.Length < 2 || $"{Request.Url?.Segments[0]}{Request.Url?.Segments[1]}" != $"/{ServerBaseProfile.EntryPoint}" || Request.HttpMethod != "POST")
21
// 使用XFEServerCore的MainEntryPoint属性进行主入口点校验
22
if (Request.Url?.Segments.Length < 2 || $"{Request.Url?.Segments[0]}{Request.Url?.Segments[1]}" != $"/{XFEServerCore.MainEntryPoint}/" || Request.HttpMethod != "POST")
22
23
{
23
24
Console.WriteLine("-校验失败");
24
25
throw Error("请求的API接口不正确", HttpStatusCode.BadGateway);
@@ -36,6 +36,10 @@ public abstract class XFEServerCore : ServerCoreServiceBase
36
36
/// </summary>
37
37
public Func<CyberCommRequestEventArgs, string> GetIPFunction { get; set; } = args => args.ClientIP;
38
38
/// <summary>
39
/// 主入口点路径(ServerCore主要入口点),默认为"api"
40
/// </summary>
41
public string MainEntryPoint { get; set; } = "api";
42
/// <summary>
39
43
/// 核心服务列表
40
44
/// </summary>
41
45
internal readonly List<IServerCoreOriginalService> ServerCoreServiceList = [];
@@ -111,7 +115,7 @@ public abstract class XFEServerCore : ServerCoreServiceBase
111
115
if (queryableJsonNode is null && !AcceptNonStandardJson)
112
116
throw new ProcessStandardRequestException("QueryableJsonNode为空");
113
117
if (execute.IsNullOrEmpty()) return;
114
Console.Write($"({ServerCoreName})【{clientIP}】请求方法-{execute}:");
118
Console.Write($"({ServerCoreName})【{clientIP}】请求次级入口点-{execute}:");
115
119
var stopWatch = Stopwatch.StartNew();
116
120
if (StandardCoreServiceDictionary.TryGetValue(execute, out var serviceFactory))
117
121
{
@@ -124,8 +128,16 @@ public abstract class XFEServerCore : ServerCoreServiceBase
124
128
serviceInstance.Request = e.Request;
125
129
serviceInstance.ReturnArgs = r;
126
130
serviceInstance.Initialize();
127
serviceInstance.RequestReceive();
128
await serviceInstance.RequestReceiveAsync();
131
132
// 根据次级入口点调用对应的处理方法
133
if (serviceInstance.SyncEntryPoints.TryGetValue(execute, out var syncHandler))
134
{
135
syncHandler();
136
}
137
if (serviceInstance.AsyncEntryPoints.TryGetValue(execute, out var asyncHandler))
138
{
139
await asyncHandler();
140
}
129
141
}
130
142
catch (Exception ex)
131
143
{
@@ -151,8 +163,16 @@ public abstract class XFEServerCore : ServerCoreServiceBase
151
163
instance.Request = e.Request;
152
164
instance.ReturnArgs = r;
153
165
instance.Initialize();
154
instance.RequestReceive();
155
await instance.RequestReceiveAsync();
166
167
// 根据次级入口点调用对应的处理方法
168
if (instance.SyncEntryPoints.TryGetValue(execute, out var syncHandler))
169
{
170
syncHandler();
171
}
172
if (instance.AsyncEntryPoints.TryGetValue(execute, out var asyncHandler))
173
{
174
await asyncHandler();
175
}
156
176
}
157
177
catch (Exception ex)
158
178
{
@@ -172,7 +192,7 @@ public abstract class XFEServerCore : ServerCoreServiceBase
172
192
{
173
193
StatusCode = HttpStatusCode.BadRequest,
174
194
ReturnArgs = r,
175
ServerException = new ExecutionUnregisteredException($"请求的方法未注册-{execute}")
195
ServerException = new ExecutionUnregisteredException($"请求的次级入口点未注册-{execute}")
176
196
});
177
197
}
178
198
catch (Exception ex)
@@ -129,6 +129,7 @@ public abstract class XFEServerCoreBuilder : XFEBuilderBase<XFEServerCoreBuilder
129
129
_xFEServerCore.AutoUnescapeJson = xFEServerCoreOptions.AutoUnescapeJson;
130
130
_xFEServerCore.AcceptNonStandardJson = xFEServerCoreOptions.AcceptNonStandardJson;
131
131
_xFEServerCore.GetIPFunction = xFEServerCoreOptions.GetIPFunction;
132
_xFEServerCore.MainEntryPoint = xFEServerCoreOptions.MainEntryPoint;
132
133
if (xFEServerCoreOptions.BindingIPAddress.Count > 0)
133
134
_xFEServerCore.BindingIPAddressList = xFEServerCoreOptions.BindingIPAddress;
134
135
}