mirror of
https://github.com/netchx/netch.git
synced 2026-03-18 18:13:21 +08:00
Replace NetFwTypeLib COMReference with WindowsFirewallHelper
This commit is contained in:
@@ -37,14 +37,6 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<COMReference Include="NetFwTypeLib.dll">
|
||||
<Guid>58fbcf7c-e7a9-467c-80b3-fc65e8fcca08</Guid>
|
||||
<VersionMajor>1</VersionMajor>
|
||||
<VersionMinor>0</VersionMinor>
|
||||
<Lcid>0</Lcid>
|
||||
<WrapperTool>tlbimp</WrapperTool>
|
||||
<Isolated>false</Isolated>
|
||||
</COMReference>
|
||||
<COMReference Include="NETCONLib.dll">
|
||||
<Guid>{43E734CA-043D-4A70-9A2C-A8F254063D91}</Guid>
|
||||
<VersionMajor>1</VersionMajor>
|
||||
@@ -59,11 +51,13 @@
|
||||
<PackageReference Include="ILMerge" Version="3.0.41" />
|
||||
<PackageReference Include="MaxMind.GeoIP2" Version="4.0.1" />
|
||||
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="2.0.65" />
|
||||
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0"/>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
|
||||
<PackageReference Include="System.Reflection.Metadata" Version="5.0.0" />
|
||||
<PackageReference Include="TaskScheduler" Version="2.9.1" />
|
||||
<PackageReference Include="WindowsAPICodePack-Shell" Version="1.1.1" />
|
||||
<PackageReference Include="WindowsFirewallHelper" Version="2.0.4.70-beta2" />
|
||||
<PackageReference Include="WindowsJobAPI" Version="5.0.1" />
|
||||
<PackageReference Include="WindowsProxy" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -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";
|
||||
|
||||
/// <summary>
|
||||
/// 添加防火墙规则 (非 Netch 自带程序)
|
||||
/// </summary>
|
||||
/// <param name="exeFullPath"></param>
|
||||
public static void AddFwRule(string exeFullPath)
|
||||
{
|
||||
AddFwRule(NetchAutoRule, exeFullPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除防火墙规则 (非 Netch 自带程序)
|
||||
/// </summary>
|
||||
public static void RemoveFwRules()
|
||||
{
|
||||
try
|
||||
{
|
||||
RemoveFwRules(NetchAutoRule);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Warning("添加防火墙规则错误\n" + e);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Netch 自带程序添加防火墙
|
||||
/// Netch 自带程序添加防火墙
|
||||
/// </summary>
|
||||
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
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除防火墙规则 (Netch 自带程序)
|
||||
/// 清除防火墙规则 (Netch 自带程序)
|
||||
/// </summary>
|
||||
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<INetFwRule2>().Count(rule => rule.Name == ruleName);
|
||||
FirewallManager.Instance.Rules.Add(rule);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in New Issue
Block a user