XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension.NetCore.ServerInteractive

[DLL] Server interaction extension, including user identity verification and querying in conjunction with AutoConfig

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension.NetCore.ServerInteractive

Refactor entry point system from Execute/JSON-based to Route/URL-based validation

Agent-Logs-Url: https://github.com/XFEstudio/XFEExtension.NetCore.ServerInteractive/sessions/fbda264b-6bf2-4d1d-96b6-74ade9d42deb Co-authored-by: XFEstudio <132526994+XFEstudio@users.noreply.github.com>

68db6f9
anthropic-code-agent[bot] <242468646+Claude@users.noreply.github.com>
提交于

代码差异

16 个文件 +247 -208
Modified XFEExtension.NetCore.ServerInteractive.SourceGenerator/EntryPointGenerator.cs +17 -3
@@ -58,7 +58,7 @@ public class EntryPointGenerator : IIncrementalGenerator
58 58 if (string.IsNullOrEmpty(path))
59 59 return null;
60 60
61 // 检查是否为异步方法
61 // 自动检查是否为异步方法
62 62 var isAsync = methodSymbol.IsAsync ||
63 63 (methodSymbol.ReturnType is INamedTypeSymbol returnType &&
64 64 returnType.Name == "Task");
@@ -104,8 +104,22 @@ namespace {namespaceName}
104 104 /// </summary>
105 105 public partial class {className}
106 106 {{
107 /// <summary>
108 /// 静态构造函数,用于初始化EntryPointList
109 /// </summary>
110 static {className}()
111 {{");
112
113 // 添加所有入口点到静态列表
114 foreach (var method in methodInfos)
115 {
116 sourceBuilder.AppendLine($" EntryPointList.Add(\"{method!.Path}\");");
117 }
118
119 sourceBuilder.AppendLine($@" }}
120
107 121 /// <inheritdoc/>
108 public Dictionary<string, Action> SyncEntryPoints {{ get; }} = new()
122 public new Dictionary<string, Action> SyncEntryPoints {{ get; }} = new()
109 123 {{");
110 124
111 125 // 添加同步入口点
@@ -117,7 +131,7 @@ namespace {namespaceName}
117 131 sourceBuilder.AppendLine($@" }};
118 132
119 133 /// <inheritdoc/>
120 public Dictionary<string, Func<Task>> AsyncEntryPoints {{ get; }} = new()
134 public new Dictionary<string, Func<Task>> AsyncEntryPoints {{ get; }} = new()
121 135 {{");
122 136
123 137 // 添加异步入口点
Modified XFEExtension.NetCore.ServerInteractive/Attributes/EntryPointAttribute.cs +1 -6
@@ -11,15 +11,10 @@ public class EntryPointAttribute : Attribute
11 11 /// </summary>
12 12 public string Path { get; }
13 13
14 /// <summary>
15 /// 是否为异步方法
16 /// </summary>
17 public bool IsAsync { get; set; }
18
19 14 /// <summary>
20 15 /// 创建次级入口点特性
21 16 /// </summary>
22 /// <param name="path">入口点路径</param>
17 /// <param name="path">入口点路径(例如:user/login)</param>
23 18 public EntryPointAttribute(string path)
24 19 {
25 20 Path = path;
Modified XFEExtension.NetCore.ServerInteractive/Implements/CoreService/ServerCoreStandardServiceBase.cs +5 -12
@@ -7,6 +7,11 @@ namespace XFEExtension.NetCore.ServerInteractive.Implements.CoreService;
7 7 /// </summary>
8 8 public abstract partial class ServerCoreStandardServiceBase : XFEServerCoreServiceBase, IServerCoreStandardService
9 9 {
10 /// <summary>
11 /// 所有入口点路径列表(由增量生成器自动填充)
12 /// </summary>
13 public static List<string> EntryPointList { get; } = new();
14
10 15 /// <inheritdoc/>
11 16 public virtual Dictionary<string, Action> SyncEntryPoints { get; } = new();
12 17
@@ -15,16 +20,4 @@ public abstract partial class ServerCoreStandardServiceBase : XFEServerCoreServi
15 20
16 21 /// <inheritdoc/>
17 22 public virtual void Initialize() { }
18
19 /// <summary>
20 /// 服务器标准请求接收事件(已废弃,请使用EntryPointAttribute特性标记方法)
21 /// </summary>
22 [Obsolete("此方法已废弃,请使用EntryPointAttribute特性标记方法来定义次级入口点")]
23 public virtual void RequestReceive() { }
24
25 /// <summary>
26 /// 服务器标准请求接收异步事件(已废弃,请使用EntryPointAttribute特性标记方法)
27 /// </summary>
28 [Obsolete("此方法已废弃,请使用EntryPointAttribute特性标记方法来定义次级入口点")]
29 public virtual Task RequestReceiveAsync() => Task.CompletedTask;
30 23 }
Modified XFEExtension.NetCore.ServerInteractive/Implements/CoreService/XFEServerCoreServiceBase.cs +1 -1
@@ -18,7 +18,7 @@ public abstract class XFEServerCoreServiceBase : IXFEStandardServerCoreServiceBa
18 18 /// <inheritdoc/>
19 19 public bool IsStandardError { get => ReturnArgs.IsStandardError; set => ReturnArgs.IsStandardError = value; }
20 20 /// <inheritdoc/>
21 public string Execute { get; set; } = string.Empty;
21 public string Route { get; set; } = string.Empty;
22 22 /// <inheritdoc/>
23 23 public XFEServerCore XFEServerCore { get; set; }
24 24 /// <inheritdoc/>
Modified XFEExtension.NetCore.ServerInteractive/Interfaces/CoreService/IXFEStandardServerCoreServiceBase.cs +2 -2
@@ -10,9 +10,9 @@ namespace XFEExtension.NetCore.ServerInteractive.Interfaces.CoreService;
10 10 public interface IXFEStandardServerCoreServiceBase : IXFEServerCoreServiceBase
11 11 {
12 12 /// <summary>
13 /// 当前请求的执行语句
13 /// 当前请求的路由路径
14 14 /// </summary>
15 string Execute { get; set; }
15 string Route { get; set; }
16 16
17 17 /// <summary>
18 18 /// 客户端IP地址
Modified XFEExtension.NetCore.ServerInteractive/Options/XFEServerCoreOptions.cs +2 -2
@@ -28,9 +28,9 @@ public class XFEServerCoreOptions
28 28 /// </summary>
29 29 public Func<CyberCommRequestEventArgs, string> GetIPFunction { get; set; } = args => args.ClientIP;
30 30 /// <summary>
31 /// 主入口点路径(ServerCore主要入口点),默认为"api"
31 /// 主入口点路径(ServerCore主要入口点),默认为空字符串(为空时直接使用次级入口点)
32 32 /// </summary>
33 public string MainEntryPoint { get; set; } = "api";
33 public string MainEntryPoint { get; set; } = string.Empty;
34 34
35 35 /// <summary>
36 36 /// 绑定IP地址
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Extensions/XFEServerCoreBuilderExtensions.cs +18 -1
@@ -21,7 +21,24 @@ public static class XFEServerCoreBuilderExtensions
21 21 /// <param name="getUserFunction"></param>
22 22 /// <param name="getEncryptedUserLoginModelFunction"></param>
23 23 /// <returns></returns>
24 public XFEServerCoreBuilder AddDataTableManager(XFEDataTableManagerBuilder xFEDataTableManagerBuilder, Func<IEnumerable<User>> getUserFunction, Func<IEnumerable<EncryptedUserLoginModel>> getEncryptedUserLoginModelFunction) => xFEServerCoreBuilder.AddParameter("TableManager", xFEDataTableManagerBuilder.Build(getUserFunction, getEncryptedUserLoginModelFunction)).AddStandardService<XFEDataTableManagerService>(xFEDataTableManagerBuilder.ExecuteList);
24 public XFEServerCoreBuilder AddDataTableManager(XFEDataTableManagerBuilder xFEDataTableManagerBuilder, Func<IEnumerable<User>> getUserFunction, Func<IEnumerable<EncryptedUserLoginModel>> getEncryptedUserLoginModelFunction)
25 {
26 xFEServerCoreBuilder.AddParameter("TableManager", xFEDataTableManagerBuilder.Build(getUserFunction, getEncryptedUserLoginModelFunction));
27
28 // Register the service for each table operation route
29 // Convert old format "get_tableName" to new format "table/get/tableName"
30 foreach (var execute in xFEDataTableManagerBuilder.ExecuteList)
31 {
32 var parts = execute.Split('_', 2);
33 if (parts.Length == 2)
34 {
35 var route = $"table/{parts[0]}/{parts[1]}";
36 xFEServerCoreBuilder.AddStandardService<XFEDataTableManagerService>(route);
37 }
38 }
39
40 return xFEServerCoreBuilder;
41 }
25 42
26 43 /// <summary>
27 44 /// 添加用户基本参数
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/ConnectService.cs +7 -3
@@ -1,4 +1,5 @@
1 1 using System.Globalization;
2 using XFEExtension.NetCore.ServerInteractive.Attributes;
2 3 using XFEExtension.NetCore.ServerInteractive.Implements.CoreService;
3 4
4 5 namespace XFEExtension.NetCore.ServerInteractive.Utilities.Server.Services.CoreService;
@@ -6,10 +7,13 @@ namespace XFEExtension.NetCore.ServerInteractive.Utilities.Server.Services.CoreS
6 7 /// <summary>
7 8 /// 连接校验服务
8 9 /// </summary>
9 public class ConnectService : ServerCoreStandardServiceBase
10 public partial class ConnectService : ServerCoreStandardServiceBase
10 11 {
11 /// <inheritdoc/>
12 public override async Task RequestReceiveAsync()
12 /// <summary>
13 /// 连接检查入口点
14 /// </summary>
15 [EntryPoint("connect")]
16 public async Task Connect()
13 17 {
14 18 Console.Write("检查连接");
15 19 await Close(DateTime.Now.ToString(CultureInfo.CurrentCulture));
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/CoreLogService.cs +27 -22
@@ -1,11 +1,12 @@
1 using XFEExtension.NetCore.ServerInteractive.Utilities.Helpers;
1 using XFEExtension.NetCore.ServerInteractive.Attributes;
2 using XFEExtension.NetCore.ServerInteractive.Utilities.Helpers;
2 3
3 4 namespace XFEExtension.NetCore.ServerInteractive.Utilities.Server.Services.CoreService;
4 5
5 6 /// <summary>
6 7 /// 核心日志服务
7 8 /// </summary>
8 public class CoreLogService : ServerCoreUserServiceBase
9 public partial class CoreLogService : ServerCoreUserServiceBase
9 10 {
10 11 /// <summary>
11 12 /// 获取日志权限
@@ -16,26 +17,30 @@ public class CoreLogService : ServerCoreUserServiceBase
16 17 /// </summary>
17 18 public int ClearPermission { get; set; }
18 19
19 /// <inheritdoc/>
20 public override async Task RequestReceiveAsync()
20 /// <summary>
21 /// 获取服务器日志
22 /// </summary>
23 [EntryPoint("log/get")]
24 public async Task GetLog()
25 {
26 Console.Write("获取服务器日志请求");
27 UserHelper.ValidatePermission(Json?["session"], Json?["deviceInfo"], ReturnArgs.Args.ClientIP, GetPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
28 if (!DateTime.TryParse(Json?["startDateTime"], out var startDatetime)) throw Error("起始日期格式不正确");
29 if (!DateTime.TryParse(Json?["endDateTime"], out var endDatetime)) throw Error("结束日期格式不正确");
30 await Close(XFEConsole.XFEConsole.Log.Export(startDatetime, endDatetime));
31 }
32
33 /// <summary>
34 /// 清除服务器日志
35 /// </summary>
36 [EntryPoint("log/clear")]
37 public void ClearLog()
21 38 {
22 switch (Execute)
23 {
24 case "get_log":
25 Console.Write("获取服务器日志请求");
26 UserHelper.ValidatePermission(Json?["session"], Json?["deviceInfo"], ReturnArgs.Args.ClientIP, GetPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
27 if (!DateTime.TryParse(Json?["startDateTime"], out var startDatetime)) throw Error("起始日期格式不正确");
28 if (!DateTime.TryParse(Json?["endDateTime"], out var endDatetime)) throw Error("结束日期格式不正确");
29 await Close(XFEConsole.XFEConsole.Log.Export(startDatetime, endDatetime));
30 break;
31 case "clear_log":
32 Console.Write("清除服务器日志请求");
33 UserHelper.ValidatePermission(Json?["session"], Json?["deviceInfo"], ReturnArgs.Args.ClientIP, ClearPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
34 if (File.Exists("server.log"))
35 File.Delete("server.log");
36 XFEConsole.XFEConsole.Log.Clear();
37 OK();
38 break;
39 }
39 Console.Write("清除服务器日志请求");
40 UserHelper.ValidatePermission(Json?["session"], Json?["deviceInfo"], ReturnArgs.Args.ClientIP, ClearPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
41 if (File.Exists("server.log"))
42 File.Delete("server.log");
43 XFEConsole.XFEConsole.Log.Clear();
44 OK();
40 45 }
41 46 }
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/EntryPointVerifyService.cs +13 -3
@@ -18,12 +18,22 @@ public class EntryPointVerifyService : ServerCoreVerifyServiceBase
18 18 Console.WriteLine("-校验失败");
19 19 throw Error("您的IP已被封禁", HttpStatusCode.Forbidden);
20 20 }
21 // 使用XFEServerCore的MainEntryPoint属性进行主入口点校验
22 if (Request.Url?.Segments.Length < 2 || $"{Request.Url?.Segments[0]}{Request.Url?.Segments[1]}" != $"/{XFEServerCore.MainEntryPoint}/" || Request.HttpMethod != "POST")
21
22 // 使用XFEServerCore的MainEntryPoint属性进行主入口点校验(如果配置了)
23 if (!string.IsNullOrEmpty(XFEServerCore.MainEntryPoint))
24 {
25 if (Request.Url?.Segments.Length < 2 || $"{Request.Url?.Segments[0]}{Request.Url?.Segments[1]}".TrimEnd('/') != $"/{XFEServerCore.MainEntryPoint}" || Request.HttpMethod != "POST")
26 {
27 Console.WriteLine("-校验失败");
28 throw Error("请求的API接口不正确", HttpStatusCode.BadGateway);
29 }
30 }
31 else if (Request.HttpMethod != "POST")
23 32 {
24 33 Console.WriteLine("-校验失败");
25 throw Error("请求的API接口不正确", HttpStatusCode.BadGateway);
34 throw Error("请求方法必须为POST", HttpStatusCode.MethodNotAllowed);
26 35 }
36
27 37 Console.WriteLine("-校验通过");
28 38 return true;
29 39 }
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/IpBannerService.cs +40 -28
@@ -1,4 +1,5 @@
1 using XFEExtension.NetCore.ServerInteractive.Profiles;
1 using XFEExtension.NetCore.ServerInteractive.Attributes;
2 using XFEExtension.NetCore.ServerInteractive.Profiles;
2 3 using XFEExtension.NetCore.ServerInteractive.Utilities.Helpers;
3 4 using XFEExtension.NetCore.StringExtension.Json;
4 5
@@ -7,7 +8,7 @@ namespace XFEExtension.NetCore.ServerInteractive.Utilities.Server.Services.CoreS
7 8 /// <summary>
8 9 /// IP封禁服务
9 10 /// </summary>
10 public class IPBannerService : ServerCoreUserServiceBase
11 public partial class IPBannerService : ServerCoreUserServiceBase
11 12 {
12 13 /// <summary>
13 14 /// 获取权限
@@ -21,33 +22,44 @@ public class IPBannerService : ServerCoreUserServiceBase
21 22 /// 移除权限
22 23 /// </summary>
23 24 public int RemovePermission { get; set; } = 0;
24 /// <inheritdoc/>
25 public override async Task RequestReceiveAsync()
25
26 /// <summary>
27 /// 获取禁止的IP地址列表
28 /// </summary>
29 [EntryPoint("ip/banned/get")]
30 public async Task GetBannedIPList()
26 31 {
27 switch (Execute)
32 Console.Write("获取禁止的IP地址列表请求");
33 UserHelper.ValidatePermission(Json?["session"]?.ToString(), Json?["deviceInfo"]?.ToString(), ReturnArgs.Args.ClientIP, GetPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
34 await Close(ServerBaseProfile.BannedIPAddressList.ToJson());
35 }
36
37 /// <summary>
38 /// 添加禁止的IP地址
39 /// </summary>
40 [EntryPoint("ip/banned/add")]
41 public void AddBannedIP()
42 {
43 Console.Write($"添加禁止的IP地址请求 添加:{Json?["bannedIP"]}");
44 UserHelper.ValidatePermission(Json?["session"]?.ToString(), Json?["deviceInfo"]?.ToString(), ReturnArgs.Args.ClientIP, AddPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
45 if (Json?["bannedIP"] is null) throw Error("无IP地址传入");
46 ServerBaseProfile.BannedIPAddressList.Add(new()
28 47 {
29 case "get_bannedIPList":
30 Console.Write("获取禁止的IP地址列表请求");
31 UserHelper.ValidatePermission(Json?["session"]?.ToString(), Json?["deviceInfo"]?.ToString(), ReturnArgs.Args.ClientIP, GetPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
32 await Close(ServerBaseProfile.BannedIPAddressList.ToJson());
33 break;
34 case "add_bannedIP":
35 Console.Write($"添加禁止的IP地址请求 添加:{Json?["bannedIP"]}");
36 UserHelper.ValidatePermission(Json?["session"]?.ToString(), Json?["deviceInfo"]?.ToString(), ReturnArgs.Args.ClientIP, AddPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
37 if (Json?["bannedIP"] is null) throw Error("无IP地址传入");
38 ServerBaseProfile.BannedIPAddressList.Add(new()
39 {
40 IPAddress = Json?["bannedIP"]?.ToString() ?? string.Empty,
41 Notes = Json?["notes"]?.ToString()
42 });
43 OK();
44 break;
45 case "remove_bannedIP":
46 Console.Write($"删除禁止的IP地址请求 移除:{Json?["bannedIP"]}");
47 UserHelper.ValidatePermission(Json?["session"]?.ToString(), Json?["deviceInfo"]?.ToString(), ReturnArgs.Args.ClientIP, RemovePermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
48 var targetIP = ServerBaseProfile.BannedIPAddressList.FirstOrDefault(ip => ip.IPAddress == Json?["bannedIP"]?.ToString()) ?? throw Error("无IP地址传入");
49 await Close(ServerBaseProfile.BannedIPAddressList.Remove(targetIP).ToString());
50 break;
51 }
48 IPAddress = Json?["bannedIP"]?.ToString() ?? string.Empty,
49 Notes = Json?["notes"]?.ToString()
50 });
51 OK();
52 }
53
54 /// <summary>
55 /// 移除禁止的IP地址
56 /// </summary>
57 [EntryPoint("ip/banned/remove")]
58 public async Task RemoveBannedIP()
59 {
60 Console.Write($"删除禁止的IP地址请求 移除:{Json?["bannedIP"]}");
61 UserHelper.ValidatePermission(Json?["session"]?.ToString(), Json?["deviceInfo"]?.ToString(), ReturnArgs.Args.ClientIP, RemovePermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
62 var targetIP = ServerBaseProfile.BannedIPAddressList.FirstOrDefault(ip => ip.IPAddress == Json?["bannedIP"]?.ToString()) ?? throw Error("无IP地址传入");
63 await Close(ServerBaseProfile.BannedIPAddressList.Remove(targetIP).ToString());
52 64 }
53 65 }
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/UserLoginService.cs +7 -3
@@ -1,4 +1,5 @@
1 1 using System.Text.Json;
2 using XFEExtension.NetCore.ServerInteractive.Attributes;
2 3 using XFEExtension.NetCore.ServerInteractive.Models.UserModels;
3 4 using XFEExtension.NetCore.ServerInteractive.Utilities.Helpers;
4 5 using XFEExtension.NetCore.StringExtension;
@@ -8,10 +9,13 @@ namespace XFEExtension.NetCore.ServerInteractive.Utilities.Server.Services.CoreS
8 9 /// <summary>
9 10 /// 用户登录服务
10 11 /// </summary>
11 public class UserLoginService<T> : ServerCoreUserLoginServiceBase<T> where T : class
12 public partial class UserLoginService<T> : ServerCoreUserLoginServiceBase<T> where T : class
12 13 {
13 /// <inheritdoc/>
14 public override async Task RequestReceiveAsync()
14 /// <summary>
15 /// 用户登录入口点
16 /// </summary>
17 [EntryPoint("user/login")]
18 public async Task Login()
15 19 {
16 20 Console.Write("登录请求");
17 21 var account = Json?["account"]?.ToString();
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/UserReloginService.cs +7 -3
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/XFEDataTableManagerService.cs +25 -2
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/XFEServerCore.cs +71 -85
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/XFEServerCoreBuilder.cs +4 -32