From cd891cec331c1460f60894cd6d653a4dc714dde2 Mon Sep 17 00:00:00 2001
From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com>
Date: Sun, 19 Jul 2020 01:39:53 +0800
Subject: [PATCH] =?UTF-8?q?=E9=98=B2=E7=81=AB=E5=A2=99=E7=B1=BB=E5=BC=82?=
=?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Netch/Utils/Firewall.cs | 67 +++++++++++++++++++++++------------------
Netch/Utils/Logging.cs | 18 ++++++++---
2 files changed, 52 insertions(+), 33 deletions(-)
diff --git a/Netch/Utils/Firewall.cs b/Netch/Utils/Firewall.cs
index 407708a1..5042e629 100644
--- a/Netch/Utils/Firewall.cs
+++ b/Netch/Utils/Firewall.cs
@@ -1,5 +1,6 @@
using System;
using System.IO;
+using System.Linq;
using NetFwTypeLib;
namespace Netch.Utils
@@ -37,7 +38,14 @@ namespace Netch.Utils
///
public static void RemoveFwRules()
{
- RemoveFwRules(NetchAutoRule);
+ try
+ {
+ RemoveFwRules(NetchAutoRule);
+ }
+ catch (Exception e)
+ {
+ Logging.Warning("添加防火墙规则错误\n" + e);
+ }
}
///
@@ -45,16 +53,23 @@ namespace Netch.Utils
///
public static void AddNetchFwRules()
{
- if (GetFwRulePath(Netch).StartsWith(Global.NetchDir) && GetFwRulesNumber(Netch) >= ProgramPath.Length) return;
- RemoveNetchFwRules();
- foreach (var p in ProgramPath)
+ try
{
- var path = Path.GetFullPath(p);
- if (File.Exists(path))
+ if (GetFwRulePath(Netch).StartsWith(Global.NetchDir) && GetFwRulesNumber(Netch) >= ProgramPath.Length) return;
+ RemoveNetchFwRules();
+ foreach (var p in ProgramPath)
{
- AddFwRule("Netch", path);
+ var path = Path.GetFullPath(p);
+ if (File.Exists(path))
+ {
+ AddFwRule("Netch", path);
+ }
}
}
+ catch (Exception e)
+ {
+ Logging.Warning("添加防火墙规则错误(如已关闭防火墙则可无视此错误)\n" + e);
+ }
}
///
@@ -62,7 +77,15 @@ namespace Netch.Utils
///
private static void RemoveNetchFwRules()
{
- RemoveFwRules(Netch);
+ try
+ {
+ RemoveFwRules(Netch);
+ }
+ catch (Exception e)
+ {
+ Logging.Warning("清除防火墙规则错误\n" + e);
+ // ignored
+ }
}
#region 封装
@@ -76,7 +99,7 @@ namespace Netch.Utils
rule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
// ApplicationName 大小不敏感
rule.ApplicationName = exeFullPath;
- // rule.Description = "Used to block all internet access.";
+ // rule.Description = "";
rule.Direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN;
rule.Enabled = true;
rule.InterfaceTypes = "All";
@@ -87,17 +110,10 @@ namespace Netch.Utils
private static void RemoveFwRules(string ruleName)
{
- try
+ var c = GetFwRulesNumber(ruleName);
+ foreach (var _ in new bool[c])
{
- var c = GetFwRulesNumber(ruleName);
- foreach (var _ in new bool[c])
- {
- FwPolicy.Rules.Remove(ruleName);
- }
- }
- catch (Exception e)
- {
- Logging.Info("Netch 自带程序添加防火墙出错(如已关闭防火墙则可无视此错误)\n" + e);
+ FwPolicy.Rules.Remove(ruleName);
}
}
@@ -111,8 +127,8 @@ namespace Netch.Utils
{
try
{
- var rule = (INetFwRule2)FwPolicy.Rules.Item(ruleName);
- return rule.ApplicationName;
+ var rule = (INetFwRule2) FwPolicy.Rules.Item(ruleName);
+ return rule.ApplicationName;
}
catch (Exception)
{
@@ -122,14 +138,7 @@ namespace Netch.Utils
private static int GetFwRulesNumber(string ruleName)
{
- // https://stackoverflow.com/a/53601691
- var i = 0;
- foreach (INetFwRule2 rule in FwPolicy.Rules)
- {
- if (rule.Name == ruleName)
- i++;
- }
- return i;
+ return FwPolicy.Rules.Cast().Count(rule => rule.Name == ruleName);
}
#endregion
diff --git a/Netch/Utils/Logging.cs b/Netch/Utils/Logging.cs
index fb5f340d..4b293f21 100644
--- a/Netch/Utils/Logging.cs
+++ b/Netch/Utils/Logging.cs
@@ -5,6 +5,7 @@ namespace Netch.Utils
{
public static class Logging
{
+ private const string LogFile = "logging\\application.log";
///
/// 信息
@@ -12,16 +13,25 @@ namespace Netch.Utils
/// 内容
public static void Info(string text)
{
- File.AppendAllText("logging\\application.log", $@"[{DateTime.Now}][INFO] {text}{Global.EOF}");
+ File.AppendAllText(LogFile, $@"[{DateTime.Now}][INFO] {text}{Global.EOF}");
}
-
+
+ ///
+ /// 信息
+ ///
+ /// 内容
+ public static void Warning(string text)
+ {
+ File.AppendAllText(LogFile, $@"[{DateTime.Now}][WARNING] {text}{Global.EOF}");
+ }
+
///
/// 错误
///
/// 内容
public static void Error(string text)
{
- File.AppendAllText("logging\\application.log", $@"[{DateTime.Now}][ERROR] {text}{Global.EOF}");
+ File.AppendAllText(LogFile, $@"[{DateTime.Now}][ERROR] {text}{Global.EOF}");
}
}
-}
+}
\ No newline at end of file