FirewallHelper.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using NetFwTypeLib;
  2. using SWRIS.Core;
  3. using System;
  4. namespace SWRIS.Extensions
  5. {
  6. public class FirewallHelper
  7. {
  8. /// <summary>
  9. /// 为WindowsDefender防火墙添加一条通信端口出站规则
  10. /// </summary>
  11. /// <param name="type">规则类型</param>
  12. /// <param name="ruleName">规则名称</param>
  13. /// <param name="appPath">应用程序完整路径</param>
  14. /// <param name="localAddresses">本地地址</param>
  15. /// <param name="localPorts">本地端口</param>
  16. /// <param name="remoteAddresses">远端地址</param>
  17. /// <param name="remotePorts">远端端口</param>
  18. public static bool CreateOutRule(NET_FW_IP_PROTOCOL_ type, string ruleName, string appPath, string localAddresses = null, string localPorts = null, string remoteAddresses = null, string remotePorts = null)
  19. {
  20. //创建防火墙策略类的实例
  21. INetFwPolicy2 policy2 = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
  22. //检查是否有同名规则
  23. foreach (INetFwRule item in policy2.Rules)
  24. {
  25. if (item.Name == ruleName)
  26. {
  27. return true;
  28. }
  29. }
  30. //创建防火墙规则类的实例: 有关该接口的详细介绍:https://docs.microsoft.com/zh-cn/windows/win32/api/netfw/nn-netfw-inetfwrule
  31. INetFwRule rule = (INetFwRule)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwRule"));
  32. //为规则添加名称
  33. rule.Name = ruleName;
  34. //为规则添加描述
  35. rule.Description = "允许程序访问指定端口";
  36. //选择入站规则还是出站规则,IN为入站,OUT为出站
  37. rule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT;
  38. //为规则添加协议类型
  39. rule.Protocol = (int)type;
  40. //为规则添加应用程序(注意这里是应用程序的绝对路径名)
  41. rule.ApplicationName = appPath;
  42. //为规则添加本地IP地址
  43. if (!string.IsNullOrEmpty(localAddresses))
  44. {
  45. rule.LocalAddresses = localAddresses;
  46. }
  47. //为规则添加本地端口
  48. if (!string.IsNullOrEmpty(localPorts))
  49. {
  50. //需要移除空白字符(不能包含空白字符,下同)
  51. rule.LocalPorts = localPorts.Replace(" ", "");// "1-29999, 30003-33332, 33334-55554, 55556-60004, 60008-65535";
  52. }
  53. //为规则添加远程IP地址
  54. if (!string.IsNullOrEmpty(remoteAddresses))
  55. {
  56. rule.RemoteAddresses = remoteAddresses;
  57. }
  58. //为规则添加远程端口
  59. if (!string.IsNullOrEmpty(remotePorts))
  60. {
  61. rule.RemotePorts = remotePorts.Replace(" ", "");
  62. }
  63. //设置规则是阻止还是允许(ALLOW=允许,BLOCK=阻止)
  64. rule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
  65. //分组 名
  66. rule.Grouping = "SCWS";
  67. rule.InterfaceTypes = "All";
  68. //是否启用规则
  69. rule.Enabled = true;
  70. try
  71. {
  72. //添加规则到防火墙策略
  73. policy2.Rules.Add(rule);
  74. }
  75. catch (Exception ex)
  76. {
  77. string error = $"防火墙添加规则出错:{ruleName} {ex.Message}";
  78. LogHelper.Error(error, ex);
  79. return false;
  80. }
  81. return true;
  82. }
  83. /// <summary>
  84. /// 为WindowsDefender防火墙添加一条U3D通信TCP端口出站规则
  85. /// </summary>
  86. /// <param name="appPath">应用程序完整路径</param>
  87. /// <param name="localAddresses">本地地址</param>
  88. /// <param name="localPorts">本地端口</param>
  89. /// <param name="remoteAddresses">远端地址</param>
  90. /// <param name="remotePorts">远端端口</param>
  91. public static bool CreateTCPOutRule(string appPath, string localAddresses = null, string localPorts = null, string remoteAddresses = null, string remotePorts = null)
  92. {
  93. try
  94. {
  95. string ruleName = $"{System.IO.Path.GetFileNameWithoutExtension(appPath)}TCP";
  96. CreateOutRule(NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_TCP, ruleName, appPath, localAddresses, localPorts, remoteAddresses, remotePorts);
  97. }
  98. catch (Exception e)
  99. {
  100. LogHelper.Error(e.Message, e);
  101. return false;
  102. }
  103. return true;
  104. }
  105. /// <summary>
  106. /// 为WindowsDefender防火墙添加一条通信UDP端口出站规则
  107. /// </summary>
  108. /// <param name="appPath">应用程序完整路径</param>
  109. /// <param name="localAddresses">本地地址</param>
  110. /// <param name="localPorts">本地端口</param>
  111. /// <param name="remoteAddresses">远端地址</param>
  112. /// <param name="remotePorts">远端端口</param>
  113. public static bool CreateUDPOutRule(string appPath, string localAddresses = null, string localPorts = null, string remoteAddresses = null, string remotePorts = null)
  114. {
  115. try
  116. {
  117. string ruleName = $"{System.IO.Path.GetFileNameWithoutExtension(appPath)}UDP";
  118. CreateOutRule(NET_FW_IP_PROTOCOL_.NET_FW_IP_PROTOCOL_UDP, ruleName, appPath, localAddresses, localPorts, remoteAddresses, remotePorts);
  119. }
  120. catch (Exception e)
  121. {
  122. LogHelper.Error(e.Message, e);
  123. return false;
  124. }
  125. return true;
  126. }
  127. /// <summary>
  128. /// 删除WindowsDefender防火墙规则
  129. /// <summary>
  130. /// <param name="appPath">应用程序完整路径</param>
  131. public static bool DeleteRule(string appPath)
  132. {
  133. //创建防火墙策略类的实例
  134. INetFwPolicy2 policy2 = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
  135. string ruleName = System.IO.Path.GetFileNameWithoutExtension(appPath);
  136. try
  137. {
  138. //根据规则名称移除规则
  139. policy2.Rules.Remove(ruleName);
  140. }
  141. catch (Exception e)
  142. {
  143. string error = $"防火墙删除规则出错:{ruleName} {e.Message}";
  144. LogHelper.Error(error, e);
  145. return false;
  146. }
  147. return true;
  148. }
  149. }
  150. }