A:增加关键词模糊搜索
This commit is contained in:
1 parent
795acf9b5f
commit
b3d48cb037
3 files changed
+59
-22
No files matched your search
@@ -409,37 +409,44 @@ public partial class App : System.Windows.Application
|
||||
try
|
||||
{
|
||||
var firewallApi = new WindowsFirewallApi();
|
||||
|
||||
|
||||
// 获取查询参数
|
||||
var queryParams = context.QueryParams;
|
||||
var startIndex = !string.IsNullOrEmpty(queryParams["start"]) ? int.Parse(queryParams["start"]) : 0;
|
||||
var limit = !string.IsNullOrEmpty(queryParams["limit"]) ? int.Parse(queryParams["limit"]) : 50;
|
||||
|
||||
|
||||
// 构建筛选条件
|
||||
var filter = new RuleFilter();
|
||||
if (!string.IsNullOrEmpty(queryParams["name"])) filter.NamePattern = queryParams["name"];
|
||||
if (!string.IsNullOrEmpty(queryParams["direction"]) && Enum.TryParse<RuleDirection>(queryParams["direction"], true, out var direction))
|
||||
if (!string.IsNullOrEmpty(queryParams["direction"]) &&
|
||||
Enum.TryParse<RuleDirection>(queryParams["direction"], true, out var direction))
|
||||
filter.Direction = direction;
|
||||
if (!string.IsNullOrEmpty(queryParams["enabled"]) && bool.TryParse(queryParams["enabled"], out var enabled))
|
||||
if (!string.IsNullOrEmpty(queryParams["enabled"]) &&
|
||||
bool.TryParse(queryParams["enabled"], out var enabled))
|
||||
filter.Enabled = enabled;
|
||||
if (!string.IsNullOrEmpty(queryParams["protocol"]) && Enum.TryParse<ProtocolType>(queryParams["protocol"], true, out var protocol))
|
||||
if (!string.IsNullOrEmpty(queryParams["protocol"]) &&
|
||||
Enum.TryParse<ProtocolType>(queryParams["protocol"], true, out var protocol))
|
||||
filter.Protocol = protocol;
|
||||
if (!string.IsNullOrEmpty(queryParams["action"]) && Enum.TryParse<RuleAction>(queryParams["action"], true, out var action))
|
||||
if (!string.IsNullOrEmpty(queryParams["action"]) &&
|
||||
Enum.TryParse<RuleAction>(queryParams["action"], true, out var action))
|
||||
filter.Action = action;
|
||||
if (!string.IsNullOrEmpty(queryParams["application"])) filter.ApplicationName = queryParams["application"];
|
||||
if (!string.IsNullOrEmpty(queryParams["application"]))
|
||||
filter.ApplicationName = queryParams["application"];
|
||||
if (!string.IsNullOrEmpty(queryParams["port"])) filter.Port = queryParams["port"];
|
||||
// 添加关键字搜索支持
|
||||
if (!string.IsNullOrEmpty(queryParams["search"])) filter.SearchKeyword = queryParams["search"];
|
||||
|
||||
// 获取筛选后的规则
|
||||
var allRules = firewallApi.GetRulesByFilter(filter);
|
||||
var totalCount = allRules.Count;
|
||||
|
||||
|
||||
// 分页
|
||||
var pagedRules = allRules.Skip(startIndex).Take(limit).ToList();
|
||||
|
||||
await context.Response.WriteJsonAsync(new ResponseModel<object>
|
||||
{
|
||||
Success = true,
|
||||
Data = new
|
||||
Data = new
|
||||
{
|
||||
rules = pagedRules,
|
||||
totalCount = totalCount,
|
||||
@@ -777,4 +784,4 @@ public class SubscriptionAppInfo : SubscriptionInfo
|
||||
/// 订阅的应用程序路径
|
||||
/// </summary>
|
||||
public string ApplicationPath { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -183,6 +183,11 @@ public class RuleFilter
|
||||
public string? ApplicationName { get; set; }
|
||||
public string? Grouping { get; set; }
|
||||
public string? Port { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 关键字搜索,会在规则名称、描述、应用程序名称、端口等字段中搜索
|
||||
/// </summary>
|
||||
public string? SearchKeyword { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -199,5 +204,4 @@ public class FirewallStatistics
|
||||
public Dictionary<RuleAction, int> RulesByAction { get; set; } = new();
|
||||
public List<string> TopApplications { get; set; } = new();
|
||||
public List<string> TopPorts { get; set; } = new();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -236,6 +236,26 @@ public class WindowsFirewallApi : IFirewallApi
|
||||
var allRules = GetAllRules();
|
||||
var query = allRules.AsQueryable();
|
||||
|
||||
// 关键字搜索 - 在名称、描述、应用程序名称、端口、服务名称等字段中搜索
|
||||
if (!string.IsNullOrEmpty(filter.SearchKeyword))
|
||||
{
|
||||
var keyword = filter.SearchKeyword;
|
||||
query = query.Where(r =>
|
||||
(!string.IsNullOrEmpty(r.Name) && r.Name.Contains(keyword, StringComparison.OrdinalIgnoreCase)) ||
|
||||
(!string.IsNullOrEmpty(r.Description) &&
|
||||
r.Description.Contains(keyword, StringComparison.OrdinalIgnoreCase)) ||
|
||||
(!string.IsNullOrEmpty(r.ApplicationName) &&
|
||||
r.ApplicationName.Contains(keyword, StringComparison.OrdinalIgnoreCase)) ||
|
||||
(!string.IsNullOrEmpty(r.ServiceName) &&
|
||||
r.ServiceName.Contains(keyword, StringComparison.OrdinalIgnoreCase)) ||
|
||||
(!string.IsNullOrEmpty(r.LocalPorts) &&
|
||||
r.LocalPorts.Contains(keyword, StringComparison.OrdinalIgnoreCase)) ||
|
||||
(!string.IsNullOrEmpty(r.RemotePorts) &&
|
||||
r.RemotePorts.Contains(keyword, StringComparison.OrdinalIgnoreCase)) ||
|
||||
(!string.IsNullOrEmpty(r.Grouping) && r.Grouping.Contains(keyword, StringComparison.OrdinalIgnoreCase))
|
||||
);
|
||||
}
|
||||
|
||||
// 按规则名称模式过滤
|
||||
if (!string.IsNullOrEmpty(filter.NamePattern))
|
||||
query = query.Where(r => r.Name.Contains(filter.NamePattern, StringComparison.OrdinalIgnoreCase));
|
||||
@@ -353,21 +373,24 @@ public class WindowsFirewallApi : IFirewallApi
|
||||
// 通过ProgID获取防火墙规则对象类型
|
||||
Type? netFwRuleType = Type.GetTypeFromProgID("HNetCfg.FWRule");
|
||||
if (netFwRuleType == null) return false;
|
||||
|
||||
|
||||
// 创建新的防火墙规则实例
|
||||
INetFwRule? newRule = (INetFwRule?)Activator.CreateInstance(netFwRuleType);
|
||||
if (newRule == null) return false;
|
||||
|
||||
|
||||
// 设置规则的各项属性
|
||||
newRule.Name = request.Name; // 规则名称
|
||||
newRule.Description = request.Description; // 规则描述
|
||||
newRule.ApplicationName = string.IsNullOrWhiteSpace(request.ApplicationName) ? null : request.ApplicationName; // 应用程序路径
|
||||
newRule.ApplicationName =
|
||||
string.IsNullOrWhiteSpace(request.ApplicationName) ? null : request.ApplicationName; // 应用程序路径
|
||||
// 注意:INetFwRule 接口可能没有 ServiceName 属性,这里移除了对它的直接赋值
|
||||
newRule.Protocol = (int)request.Protocol; // 协议类型
|
||||
newRule.LocalAddresses = string.IsNullOrWhiteSpace(request.LocalAddresses) ? "*" : request.LocalAddresses; // 本地地址
|
||||
newRule.RemoteAddresses = string.IsNullOrWhiteSpace(request.RemoteAddresses) ? "*" : request.RemoteAddresses; // 远程地址
|
||||
newRule.LocalAddresses =
|
||||
string.IsNullOrWhiteSpace(request.LocalAddresses) ? "*" : request.LocalAddresses; // 本地地址
|
||||
newRule.RemoteAddresses =
|
||||
string.IsNullOrWhiteSpace(request.RemoteAddresses) ? "*" : request.RemoteAddresses; // 远程地址
|
||||
newRule.Protocol = (int)request.Protocol;
|
||||
|
||||
|
||||
// 设置为 1 表示 ICMP,6 表示 TCP,17 表示 UDP
|
||||
if ((int)request.Protocol == 1) // ICMP
|
||||
{
|
||||
@@ -387,22 +410,25 @@ public class WindowsFirewallApi : IFirewallApi
|
||||
{
|
||||
newRule.LocalPorts = request.LocalPorts; // 本地端口
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.RemotePorts))
|
||||
{
|
||||
newRule.RemotePorts = request.RemotePorts; // 本地端口
|
||||
}
|
||||
}
|
||||
|
||||
newRule.Direction = (NET_FW_RULE_DIRECTION_)request.Direction; // 流量方向
|
||||
newRule.Enabled = request.Enabled; // 是否启用
|
||||
newRule.Profiles = ConvertToNetFwProfiles(request.Profiles); // 适用的配置文件
|
||||
newRule.Action = (NET_FW_ACTION_)request.Action; // 规则动作
|
||||
newRule.Grouping = string.IsNullOrWhiteSpace(request.Grouping) ? null : request.Grouping; // 规则组
|
||||
newRule.InterfaceTypes = string.IsNullOrWhiteSpace(request.InterfaceTypes) ? "All" : request.InterfaceTypes; // 接口类型
|
||||
|
||||
newRule.InterfaceTypes =
|
||||
string.IsNullOrWhiteSpace(request.InterfaceTypes) ? "All" : request.InterfaceTypes; // 接口类型
|
||||
|
||||
// 设置边缘遍历(用于某些特殊网络场景)
|
||||
if (request.EdgeTraversal)
|
||||
newRule.EdgeTraversal = request.EdgeTraversal;
|
||||
|
||||
|
||||
// 将新规则添加到防火墙
|
||||
_fwRules.Add(newRule);
|
||||
return true;
|
||||
@@ -1163,4 +1189,4 @@ public class WindowsFirewallApi : IFirewallApi
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user