防火墙类异常处理

This commit is contained in:
ChsBuffer
2020-07-19 01:39:53 +08:00
parent 635a033434
commit cd891cec33
2 changed files with 52 additions and 33 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using NetFwTypeLib;
namespace Netch.Utils
@@ -37,7 +38,14 @@ namespace Netch.Utils
/// </summary>
public static void RemoveFwRules()
{
RemoveFwRules(NetchAutoRule);
try
{
RemoveFwRules(NetchAutoRule);
}
catch (Exception e)
{
Logging.Warning("添加防火墙规则错误\n" + e);
}
}
/// <summary>
@@ -45,16 +53,23 @@ namespace Netch.Utils
/// </summary>
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);
}
}
/// <summary>
@@ -62,7 +77,15 @@ namespace Netch.Utils
/// </summary>
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<INetFwRule2>().Count(rule => rule.Name == ruleName);
}
#endregion

View File

@@ -5,6 +5,7 @@ namespace Netch.Utils
{
public static class Logging
{
private const string LogFile = "logging\\application.log";
/// <summary>
/// 信息
@@ -12,16 +13,25 @@ namespace Netch.Utils
/// <param name="text">内容</param>
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}");
}
/// <summary>
/// 信息
/// </summary>
/// <param name="text">内容</param>
public static void Warning(string text)
{
File.AppendAllText(LogFile, $@"[{DateTime.Now}][WARNING] {text}{Global.EOF}");
}
/// <summary>
/// 错误
/// </summary>
/// <param name="text">内容</param>
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}");
}
}
}
}