diff --git a/Netch/Netch.csproj b/Netch/Netch.csproj
index 9a0180f0..467f830a 100644
--- a/Netch/Netch.csproj
+++ b/Netch/Netch.csproj
@@ -37,14 +37,6 @@
-
- 58fbcf7c-e7a9-467c-80b3-fc65e8fcca08
- 1
- 0
- 0
- tlbimp
- false
-
{43E734CA-043D-4A70-9A2C-A8F254063D91}
1
@@ -59,11 +51,13 @@
+
+
diff --git a/Netch/Utils/Firewall.cs b/Netch/Utils/Firewall.cs
index cdc3766e..04107f9f 100644
--- a/Netch/Utils/Firewall.cs
+++ b/Netch/Utils/Firewall.cs
@@ -1,12 +1,13 @@
using System;
using System.IO;
using System.Linq;
-using NetFwTypeLib;
+using WindowsFirewallHelper;
namespace Netch.Utils
{
public static class Firewall
{
+ private const string Netch = "Netch";
private static readonly string[] ProgramPath =
{
"bin/NTT.exe",
@@ -15,53 +16,30 @@ namespace Netch.Utils
"bin/ShadowsocksR.exe",
"bin/Trojan.exe",
"bin/tun2socks.exe",
- "bin/v2ray.exe",
+ "bin/xray.exe",
"Netch.exe"
};
- private const string Netch = "Netch";
- private const string NetchAutoRule = "NetchAutoRule";
-
///
- /// 添加防火墙规则 (非 Netch 自带程序)
- ///
- ///
- public static void AddFwRule(string exeFullPath)
- {
- AddFwRule(NetchAutoRule, exeFullPath);
- }
-
- ///
- /// 清除防火墙规则 (非 Netch 自带程序)
- ///
- public static void RemoveFwRules()
- {
- try
- {
- RemoveFwRules(NetchAutoRule);
- }
- catch (Exception e)
- {
- Logging.Warning("添加防火墙规则错误\n" + e);
- }
- }
-
- ///
- /// Netch 自带程序添加防火墙
+ /// Netch 自带程序添加防火墙
///
public static void AddNetchFwRules()
{
try
{
- if (GetFwRulePath(Netch).StartsWith(Global.NetchDir) && GetFwRulesNumber(Netch) >= ProgramPath.Length) return;
- RemoveNetchFwRules();
+ var rule = FirewallManager.Instance.Rules.FirstOrDefault(r => r.Name == Netch);
+ if (rule != null)
+ {
+ if (rule.Name.StartsWith(Global.NetchDir))
+ return;
+ RemoveNetchFwRules();
+ }
+
foreach (var p in ProgramPath)
{
var path = Path.GetFullPath(p);
if (File.Exists(path))
- {
- AddFwRule("Netch", path);
- }
+ AddFwRule(Netch, path);
}
}
catch (Exception e)
@@ -71,71 +49,29 @@ namespace Netch.Utils
}
///
- /// 清除防火墙规则 (Netch 自带程序)
+ /// 清除防火墙规则 (Netch 自带程序)
///
private static void RemoveNetchFwRules()
{
try
{
- RemoveFwRules(Netch);
+ foreach (var rule in FirewallManager.Instance.Rules.Where(r => r.Name == Netch))
+ FirewallManager.Instance.Rules.Remove(rule);
}
catch (Exception e)
{
Logging.Warning("清除防火墙规则错误\n" + e);
- // ignored
}
}
#region 封装
- private static readonly INetFwPolicy2 FwPolicy = (INetFwPolicy2) Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
-
private static void AddFwRule(string ruleName, string exeFullPath)
{
- var rule = NewFwRule();
+ var rule = FirewallManager.Instance.CreateApplicationRule(ruleName, FirewallAction.Allow, exeFullPath);
+ rule.Direction = FirewallDirection.Inbound;
- rule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
- // ApplicationName 大小不敏感
- rule.ApplicationName = exeFullPath;
- // rule.Description = "";
- rule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN;
- rule.Enabled = true;
- rule.InterfaceTypes = "All";
- rule.Name = ruleName;
-
- FwPolicy.Rules.Add(rule);
- }
-
- private static void RemoveFwRules(string ruleName)
- {
- var c = GetFwRulesNumber(ruleName);
- foreach (var _ in new bool[c])
- {
- FwPolicy.Rules.Remove(ruleName);
- }
- }
-
- private static INetFwRule NewFwRule()
- {
- return (INetFwRule) Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
- }
-
- private static string GetFwRulePath(string ruleName)
- {
- try
- {
- var rule = (INetFwRule2) FwPolicy.Rules.Item(ruleName);
- return rule.ApplicationName;
- }
- catch (Exception)
- {
- return "";
- }
- }
-
- private static int GetFwRulesNumber(string ruleName)
- {
- return FwPolicy.Rules.Cast().Count(rule => rule.Name == ruleName);
+ FirewallManager.Instance.Rules.Add(rule);
}
#endregion