sp_firewallControl.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef SP_FIREWALL_CONTROL_H
  2. #define SP_FIREWALL_CONTROL_H
  3. #include <windows.h>
  4. #include <netfw.h>
  5. #include <vector>
  6. #include <string>
  7. #include <optional>
  8. #include "SpBase.h"
  9. // 防火墙规则详细信息结构体
  10. struct FirewallRuleInfo {
  11. std::wstring name;
  12. std::wstring description;
  13. std::wstring applicationName;
  14. std::wstring serviceName;
  15. long direction; // 1=入站, 2=出站
  16. bool enabled;
  17. };
  18. // 添加规则方向枚举
  19. enum class FirewallRuleDirection {
  20. Inbound = NET_FW_RULE_DIR_IN,
  21. Outbound = NET_FW_RULE_DIR_OUT
  22. };
  23. // 添加规则动作枚举
  24. enum class FirewallRuleAction {
  25. Allow = NET_FW_ACTION_ALLOW,
  26. Block = NET_FW_ACTION_BLOCK
  27. };
  28. class FirewallController {
  29. public:
  30. // 初始化COM环境和防火墙策略
  31. static bool Initialize();
  32. // 查询匹配名称的防火墙规则(支持通配符*和?)
  33. static std::vector<FirewallRuleInfo> QueryRules(const std::wstring& ruleNamePattern);
  34. static bool AddFirewallRule(
  35. const std::wstring& ruleName,
  36. const std::wstring& appPath,
  37. FirewallRuleDirection direction,
  38. FirewallRuleAction action,
  39. const std::wstring& protocol = L"",
  40. const std::wstring& localPorts = L"",
  41. const std::wstring& remoteAddresses = L"*",
  42. const std::wstring& description = L""
  43. );
  44. static bool DeleteFirewallRule(const std::wstring& ruleName);
  45. static bool CleanupRulesExceptWhitelist(
  46. const std::wstring& ruleNamePattern,
  47. const std::vector<std::wstring>& whitelistPaths);
  48. // 释放资源
  49. static void Shutdown();
  50. private:
  51. static INetFwPolicy2* firewallPolicy;
  52. static bool comInitialized;
  53. // 通配符匹配函数
  54. static bool WildcardMatch(const std::wstring& pattern, const std::wstring& text);
  55. };
  56. /*
  57. case: VTM upgrade
  58. may add 4 firewall rules
  59. */
  60. SPBASE_API bool sp_AddFirewallRule(const char *ruleName, const char *appPath);
  61. SPBASE_API bool sp_AddFirewallRuleByPath(const char *pszPath);
  62. SPBASE_API bool sp_CheckAllRules();
  63. #endif // SP_FIREWALL_CONTROL_H