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

提升空值兼容性,增强API健壮性与容错能力

本次提交将 QueryableJsonNode 及相关参数改为可空类型,全面增加了 null 安全访问和参数校验,优化了用户验证、数据表格、日志、IP 管理等服务的异常处理,防止因空引用导致崩溃。同步升级版本号至 1.2.0。

227ccfa
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

11 个文件 +42 -37
Modified XFEExtension.NetCore.ServerInteractive/Implements/CoreService/XFEServerCoreServiceBase.cs +1 -1
@@ -22,7 +22,7 @@ public abstract class XFEServerCoreServiceBase : IXFEServerCoreServiceBase
22 22 /// <inheritdoc/>
23 23 public XFEServerCore XFEServerCore { get; set; }
24 24 /// <inheritdoc/>
25 public QueryableJsonNode Json { get; set; }
25 public QueryableJsonNode? Json { get; set; }
26 26 /// <inheritdoc/>
27 27 public ServerCoreReturnArgs ReturnArgs { get; set; }
28 28 /// <inheritdoc/>
Modified XFEExtension.NetCore.ServerInteractive/Interfaces/CoreService/IXFEServerCoreServiceBase.cs +1 -1
@@ -38,7 +38,7 @@ public interface IXFEServerCoreServiceBase
38 38 /// <summary>
39 39 /// 当前请求的 json 节点
40 40 /// </summary>
41 QueryableJsonNode Json { get; set; }
41 QueryableJsonNode? Json { get; set; }
42 42
43 43 /// <summary>
44 44 /// 当前请求的返回参数
Modified XFEExtension.NetCore.ServerInteractive/Utilities/DataTable/XFEDataTable.cs +5 -5
@@ -138,7 +138,7 @@ public class XFEDataTable<T> : IXFEDataTable where T : IIDModel
138 138 Console.Write($"【{r.Args.ClientIP}】获取{TableShowName}列表请求");
139 139 UserHelper.ValidatePermission(requestJsonNode["session"], requestJsonNode["computerInfo"], r.Args.ClientIP, GetPermissionLevel, GetEncryptedUserLoginModelFunction(), GetUsersFunction(), r);
140 140 List<T> tableList = [.. GetTableFunction()];
141 int pageCount = requestJsonNode["pageCount"].GetValue<int>();
141 int pageCount = requestJsonNode["pageCount"]?.GetValue<int>() ?? -1;
142 142 if (pageCount == -1)
143 143 {
144 144 await r.Args.ReplyAndClose(JsonSerializer.Serialize(new
@@ -150,7 +150,7 @@ public class XFEDataTable<T> : IXFEDataTable where T : IIDModel
150 150 }
151 151 else
152 152 {
153 int page = requestJsonNode["page"].GetValue<int>();
153 int page = requestJsonNode["page"]?.GetValue<int>() ?? -1;
154 154 await r.Args.ReplyAndClose(JsonSerializer.Serialize(new
155 155 {
156 156 totalCount = tableList.Count,
@@ -161,7 +161,7 @@ public class XFEDataTable<T> : IXFEDataTable where T : IIDModel
161 161 break;
162 162 case "add":
163 163 Console.Write($"【{r.Args.ClientIP}】添加{TableShowName}请求");
164 var item = JsonSerializer.Deserialize<T>(Convert.FromBase64String(requestJsonNode["data"]), JsonSerializerOptions);
164 var item = JsonSerializer.Deserialize<T>(Convert.FromBase64String(requestJsonNode["data"]?.ToString() ?? string.Empty), JsonSerializerOptions);
165 165 if (item is null)
166 166 {
167 167 statusCode = HttpStatusCode.BadRequest;
@@ -181,7 +181,7 @@ public class XFEDataTable<T> : IXFEDataTable where T : IIDModel
181 181 break;
182 182 case "remove":
183 183 Console.Write($"【{r.Args.ClientIP}】删除{TableShowName}请求");
184 var id = requestJsonNode["id"].ToString();
184 var id = requestJsonNode["id"]?.ToString();
185 185 Console.Write($":{id}");
186 186 UserHelper.ValidatePermission(requestJsonNode["session"], requestJsonNode["computerInfo"], r.Args.ClientIP, RemovePermissionLevel, GetEncryptedUserLoginModelFunction(), GetUsersFunction(), r);
187 187 if (id.IsNullOrWhiteSpace())
@@ -194,7 +194,7 @@ public class XFEDataTable<T> : IXFEDataTable where T : IIDModel
194 194 break;
195 195 case "change":
196 196 Console.Write($"【{r.Args.ClientIP}】更改{TableShowName}请求");
197 item = JsonSerializer.Deserialize<T>(Convert.FromBase64String(requestJsonNode["data"]), JsonSerializerOptions);
197 item = JsonSerializer.Deserialize<T>(Convert.FromBase64String(requestJsonNode["data"]?.ToString() ?? string.Empty), JsonSerializerOptions);
198 198 if (item is null)
199 199 {
200 200 statusCode = HttpStatusCode.BadRequest;
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Helpers/UserHelper.cs +12 -5
@@ -4,6 +4,7 @@ using System.Text.RegularExpressions;
4 4 using XFEExtension.NetCore.ServerInteractive.Interfaces;
5 5 using XFEExtension.NetCore.ServerInteractive.Models.ServerModels;
6 6 using XFEExtension.NetCore.ServerInteractive.Models.UserModels;
7 using XFEExtension.NetCore.StringExtension;
7 8
8 9 namespace XFEExtension.NetCore.ServerInteractive.Utilities.Helpers;
9 10
@@ -132,8 +133,12 @@ public static class UserHelper
132 133 /// <param name="requiredPermissionLevel"></param>
133 134 /// <param name="userInfoList"></param>
134 135 /// <returns></returns>
135 public static UserOperateResult ValidateUserPermission(string userName, string password, int requiredPermissionLevel, IEnumerable<IUserInfo> userInfoList)
136 public static UserOperateResult ValidateUserPermission(string? userName, string? password, int requiredPermissionLevel, IEnumerable<IUserInfo> userInfoList)
136 137 {
138 if (userName.IsNullOrWhiteSpace())
139 return UserOperateResult.UserNotFound;
140 if (password.IsNullOrWhiteSpace())
141 return UserOperateResult.InvalidPassword;
137 142 var result = GetUser(userName, password, userInfoList, out var user);
138 143 if (result != UserOperateResult.Success)
139 144 return result;
@@ -152,8 +157,10 @@ public static class UserHelper
152 157 /// <param name="encryptedUserLoginModels"></param>
153 158 /// <param name="userInfoList"></param>
154 159 /// <returns></returns>
155 public static UserOperateResult ValidateUserPermission(string sessionId, string computerInfo, string ipAddress, int requiredPermissionLevel, IEnumerable<EncryptedUserLoginModel> encryptedUserLoginModels, IEnumerable<IUserInfo> userInfoList)
160 public static UserOperateResult ValidateUserPermission(string? sessionId, string? computerInfo, string ipAddress, int requiredPermissionLevel, IEnumerable<EncryptedUserLoginModel> encryptedUserLoginModels, IEnumerable<IUserInfo> userInfoList)
156 161 {
162 if (sessionId.IsNullOrWhiteSpace() || computerInfo.IsNullOrWhiteSpace())
163 return UserOperateResult.UserNotFound;
157 164 var result = GetUser(sessionId, computerInfo, ipAddress, encryptedUserLoginModels, userInfoList, out var user);
158 165 if (result != UserOperateResult.Success)
159 166 return result;
@@ -186,7 +193,7 @@ public static class UserHelper
186 193 /// <param name="userInfoList"></param>
187 194 /// <param name="statusCode"></param>
188 195 /// <exception cref="StopAction"></exception>
189 public static void ValidatePermission(string userName, string password, int requiredPermissionLevel, IEnumerable<IUserInfo> userInfoList, ref HttpStatusCode statusCode)
196 public static void ValidatePermission(string? userName, string? password, int requiredPermissionLevel, IEnumerable<IUserInfo> userInfoList, ref HttpStatusCode statusCode)
190 197 {
191 198 var result = ValidateUserPermission(userName, password, requiredPermissionLevel, userInfoList);
192 199 if (result != UserOperateResult.Success)
@@ -205,7 +212,7 @@ public static class UserHelper
205 212 /// <param name="userInfoList"></param>
206 213 /// <param name="r"></param>
207 214 /// <exception cref="StopAction"></exception>
208 public static void ValidatePermission(string userName, string password, int requiredPermissionLevel, IEnumerable<IUserInfo> userInfoList, ServerCoreReturnArgs r)
215 public static void ValidatePermission(string? userName, string? password, int requiredPermissionLevel, IEnumerable<IUserInfo> userInfoList, ServerCoreReturnArgs r)
209 216 {
210 217 var result = ValidateUserPermission(userName, password, requiredPermissionLevel, userInfoList);
211 218 if (result != UserOperateResult.Success)
@@ -226,7 +233,7 @@ public static class UserHelper
226 233 /// <param name="userInfoList"></param>
227 234 /// <param name="r"></param>
228 235 /// <exception cref="StopAction"></exception>
229 public static void ValidatePermission(string sessionId, string computerInfo, string ipAddress, int requiredPermissionLevel, IEnumerable<EncryptedUserLoginModel> encryptedUserLoginModels, IEnumerable<IUserInfo> userInfoList, ServerCoreReturnArgs r)
236 public static void ValidatePermission(string? sessionId, string? computerInfo, string ipAddress, int requiredPermissionLevel, IEnumerable<EncryptedUserLoginModel> encryptedUserLoginModels, IEnumerable<IUserInfo> userInfoList, ServerCoreReturnArgs r)
230 237 {
231 238 var result = ValidateUserPermission(sessionId, computerInfo, ipAddress, requiredPermissionLevel, encryptedUserLoginModels, userInfoList);
232 239 if (result != UserOperateResult.Success)
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/CoreLogService.cs +4 -4
@@ -23,14 +23,14 @@ public class CoreLogService : ServerCoreUserServiceBase
23 23 {
24 24 case "get_log":
25 25 Console.Write("获取服务器日志请求");
26 UserHelper.ValidatePermission(Json["session"], Json["computerInfo"], 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("结束日期格式不正确");
26 UserHelper.ValidatePermission(Json?["session"], Json?["computerInfo"], 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 29 await Close(XFEConsole.XFEConsole.Log.Export(startDatetime, endDatetime));
30 30 break;
31 31 case "clear_log":
32 32 Console.Write("清除服务器日志请求");
33 UserHelper.ValidatePermission(Json["session"], Json["computerInfo"], ReturnArgs.Args.ClientIP, ClearPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
33 UserHelper.ValidatePermission(Json?["session"], Json?["computerInfo"], ReturnArgs.Args.ClientIP, ClearPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
34 34 if (File.Exists("server.log"))
35 35 File.Delete("server.log");
36 36 XFEConsole.XFEConsole.Log.Clear();
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/IpBannerService.cs +9 -9
@@ -29,24 +29,24 @@ public class IpBannerService : ServerCoreUserServiceBase
29 29 {
30 30 case "get_bannedIpList":
31 31 Console.Write("获取禁止的IP地址列表请求");
32 UserHelper.ValidatePermission(Json["session"], Json["computerInfo"], ReturnArgs.Args.ClientIP, GetPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
32 UserHelper.ValidatePermission(Json?["session"]?.ToString(), Json?["computerInfo"]?.ToString(), ReturnArgs.Args.ClientIP, GetPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
33 33 await Close(ServerBaseProfile.BannedIPAddressList.ToJson());
34 34 break;
35 35 case "add_bannedIp":
36 Console.Write($"添加禁止的IP地址请求 添加:{Json["bannedIp"]}");
37 UserHelper.ValidatePermission(Json["session"], Json["computerInfo"], ReturnArgs.Args.ClientIP, AddPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
38 if (Json["bannedIp"] is null) throw Error("无IP地址传入");
36 Console.Write($"添加禁止的IP地址请求 添加:{Json?["bannedIp"]}");
37 UserHelper.ValidatePermission(Json?["session"]?.ToString(), Json?["computerInfo"]?.ToString(), ReturnArgs.Args.ClientIP, AddPermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
38 if (Json?["bannedIp"] is null) throw Error("无IP地址传入");
39 39 ServerBaseProfile.BannedIPAddressList.Add(new()
40 40 {
41 IPAddress = Json["bannedIp"],
42 Notes = Json["notes"]
41 IPAddress = Json?["bannedIp"]?.ToString() ?? string.Empty,
42 Notes = Json?["notes"]?.ToString()
43 43 });
44 44 OK();
45 45 break;
46 46 case "remove_bannedIp":
47 Console.Write($"删除禁止的IP地址请求 移除:{Json["bannedIp"]}");
48 UserHelper.ValidatePermission(Json["session"], Json["computerInfo"], ReturnArgs.Args.ClientIP, RemovePermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
49 var targetIp = ServerBaseProfile.BannedIPAddressList.FirstOrDefault(ip => ip.IPAddress == Json["bannedIp"].ToString()) ?? throw Error("无IP地址传入", HttpStatusCode.BadRequest);
47 Console.Write($"删除禁止的IP地址请求 移除:{Json?["bannedIp"]}");
48 UserHelper.ValidatePermission(Json?["session"]?.ToString(), Json?["computerInfo"]?.ToString(), ReturnArgs.Args.ClientIP, RemovePermission, GetEncryptedUserLoginModelFunction(), GetUserFunction(), ReturnArgs);
49 var targetIp = ServerBaseProfile.BannedIPAddressList.FirstOrDefault(ip => ip.IPAddress == Json?["bannedIp"]?.ToString()) ?? throw Error("无IP地址传入", HttpStatusCode.BadRequest);
50 50 await Close(ServerBaseProfile.BannedIPAddressList.Remove(targetIp).ToString());
51 51 break;
52 52 default:
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/UserLoginService.cs +3 -3
@@ -14,9 +14,9 @@ public class UserLoginService<T> : ServerCoreUserLoginServiceBase<T> where T : c
14 14 public override async Task RequestReceiveAsync()
15 15 {
16 16 Console.Write("登录请求");
17 var account = Json["account"]?.ToString();
18 var password = Json["password"]?.ToString();
19 var computerInfo = Json["computerInfo"]?.ToString();
17 var account = Json?["account"]?.ToString();
18 var password = Json?["password"]?.ToString();
19 var computerInfo = Json?["computerInfo"]?.ToString();
20 20 if (account.IsNullOrWhiteSpace()) throw Error("账户名不能为空");
21 21 if (password.IsNullOrWhiteSpace()) throw Error("登录密码不能为空");
22 22 if (computerInfo.IsNullOrWhiteSpace()) throw Error("电脑信息不能为空");
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/UserReloginService.cs +2 -2
@@ -16,9 +16,9 @@ public class UserReloginService<T> : ServerCoreUserLoginServiceBase<T> where T :
16 16 public override async Task RequestReceiveAsync()
17 17 {
18 18 Console.Write($"校验登录请求:");
19 var session = Regex.Unescape(Json["session"].ToString());
19 var session = Regex.Unescape(Json?["session"]?.ToString() ?? string.Empty);
20 20 Console.Write(session[..10]);
21 var computerInfo = Json["computerInfo"].ToString();
21 var computerInfo = Json?["computerInfo"]?.ToString();
22 22 if (session.IsNullOrWhiteSpace()) throw Error("Session值不能为空");
23 23 if (computerInfo.IsNullOrWhiteSpace()) throw Error("电脑信息不能为空");
24 24 var split = session.Split('|');
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/Services/CoreService/XFEDataTableManagerService.cs +1 -1
@@ -14,5 +14,5 @@ public class XFEDataTableManagerService : ServerCoreStandardServiceBase
14 14 public XFEDataTableManager TableManager { get; set; }
15 15
16 16 /// <inheritdoc/>
17 public override async Task RequestReceiveAsync() => await TableManager.Execute(Execute, Json, ReturnArgs!);
17 public override async Task RequestReceiveAsync() => await TableManager.Execute(Execute, Json ?? new(new()), ReturnArgs!);
18 18 }
Modified XFEExtension.NetCore.ServerInteractive/Utilities/Server/XFEServerCore.cs +1 -2
@@ -94,7 +94,7 @@ public abstract class XFEServerCore : ServerCoreServiceBase
94 94 try
95 95 {
96 96 queryableJsonNode = e.RequestBody ?? throw new ProcessStandardRequestException("请求的API接口不正确");
97 execute = queryableJsonNode["execute"] ?? string.Empty;
97 execute = queryableJsonNode?["execute"]?.ToString() ?? string.Empty;
98 98 }
99 99 catch (Exception ex)
100 100 {
@@ -113,7 +113,6 @@ public abstract class XFEServerCore : ServerCoreServiceBase
113 113 {
114 114 if (queryableJsonNode is null && !AcceptNonStandardJson)
115 115 throw new ProcessStandardRequestException("QueryableJsonNode为空");
116 queryableJsonNode ??= new QueryableJsonNode(new JsonNode());
117 116 if (!execute.IsNullOrEmpty())
118 117 {
119 118 Console.Write($"({ServerCoreName})【{clientIP}】请求方法-{execute}:");
Modified XFEExtension.NetCore.ServerInteractive/XFEExtension.NetCore.ServerInteractive.csproj +3 -4
@@ -5,7 +5,7 @@
5 5 <ImplicitUsings>enable</ImplicitUsings>
6 6 <Nullable>enable</Nullable>
7 7 <GenerateDocumentationFile>True</GenerateDocumentationFile>
8 <Version>1.1.18</Version>
8 <Version>1.2.0</Version>
9 9 <Title>XFEExtension.NetCore.ServerInteractive</Title>
10 10 <RepositoryUrl>https://github.com/XFEstudio/XFEExtension.NetCore.ServerInteractive</RepositoryUrl>
11 11 <AnalysisLevel>latest</AnalysisLevel>
@@ -21,10 +21,9 @@
21 21 <PackageReleaseNotes>
22 22 ## 调整
23 23
24 增强错误处理并升级版本号至1.1.18
24 提升空值兼容性,增强API健壮性与容错能力
25 25
26 在 ServerCoreReturnArgs 的 Error 方法中增加 IsStandardError 标记,提升错误标准化处理能力。
27 项目版本号从 1.1.17 升级到 1.1.18。
26 本次提交将 QueryableJsonNode 及相关参数改为可空类型,全面增加了 null 安全访问和参数校验,优化了用户验证、数据表格、日志、IP 管理等服务的异常处理,防止因空引用导致崩溃。同步升级版本号至 1.2.0。
28 27
29 28 ## 新增
30 29