From 36718e774c6a75e64ba4f52f58f0957c845969fe Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Fri, 9 Oct 2020 23:02:47 +0800 Subject: [PATCH] feat: mode reference( #include ) --- Netch/Controllers/NFController.cs | 2 +- Netch/Controllers/TUNTAPController.cs | 4 +- Netch/Models/Mode.cs | 56 +++++++++++++++++++++++++++ Netch/Utils/ModeHelper.cs | 5 +-- 4 files changed, 61 insertions(+), 6 deletions(-) diff --git a/Netch/Controllers/NFController.cs b/Netch/Controllers/NFController.cs index 7849a28a..1490dce1 100644 --- a/Netch/Controllers/NFController.cs +++ b/Netch/Controllers/NFController.cs @@ -62,7 +62,7 @@ namespace Netch.Controllers } aio_dial((int) NameList.TYPE_CLRNAME, ""); - foreach (var rule in mode.Rule) + foreach (var rule in mode.FullRule) { aio_dial((int) NameList.TYPE_ADDNAME, rule); } diff --git a/Netch/Controllers/TUNTAPController.cs b/Netch/Controllers/TUNTAPController.cs index b4460cc5..4689f049 100644 --- a/Netch/Controllers/TUNTAPController.cs +++ b/Netch/Controllers/TUNTAPController.cs @@ -142,7 +142,7 @@ namespace Netch.Controllers case 1: // 代理规则 Logging.Info("代理 → 规则 IP"); - _proxyIPs.AddRange(_savedMode.Rule.Select(IPNetwork.Parse)); + _proxyIPs.AddRange(_savedMode.FullRule.Select(IPNetwork.Parse)); //处理 NAT 类型检测,由于协议的原因,无法仅通过域名确定需要代理的 IP,自己记录解析了返回的 IP,仅支持默认检测服务器 if (Global.Settings.STUN_Server == "stun.stunprotocol.org") @@ -189,7 +189,7 @@ namespace Netch.Controllers ); Logging.Info("绕行 → 规则 IP"); - _directIPs.AddRange(_savedMode.Rule.Select(IPNetwork.Parse)); + _directIPs.AddRange(_savedMode.FullRule.Select(IPNetwork.Parse)); Logging.Info("代理 → 全局"); diff --git a/Netch/Models/Mode.cs b/Netch/Models/Mode.cs index 776fb00a..40d5766d 100644 --- a/Netch/Models/Mode.cs +++ b/Netch/Models/Mode.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Text; using Netch.Utils; namespace Netch.Models @@ -42,6 +43,61 @@ namespace Netch.Models /// public readonly List Rule = new List(); + public List FullRule + { + get + { + var result = new List(); + foreach (var s in Rule) + { + if (string.IsNullOrWhiteSpace(s)) + continue; + if (s.StartsWith("//")) + continue; + + if (s.StartsWith("#include")) + { + var relativePath = new StringBuilder(s.Substring(8).Trim()); + relativePath.Replace("<", ""); + relativePath.Replace(">", ""); + relativePath.Replace(".h", ".txt"); + + var mode = Global.Modes.FirstOrDefault(m => m.RelativePath.Equals(relativePath.ToString())); + + if (mode == null) + { + Logging.Warning($"{relativePath} file included in {Remark} not found"); + } + else + { + if (mode.Type != Type) + { + Logging.Warning($"{mode.Remark}'s mode is not as same as {Remark}'s mode"); + } + else + { + if (mode.Rule.Any(rule => rule.StartsWith("#include"))) + { + Logging.Warning("Cannot reference mode that reference other mode"); + } + else + { + result.AddRange(mode.FullRule); + } + } + } + } + else + { + result.Add(s); + } + } + + return result; + } + } + + /// /// 获取备注 /// diff --git a/Netch/Utils/ModeHelper.cs b/Netch/Utils/ModeHelper.cs index 8ac591d3..d8fc4908 100644 --- a/Netch/Utils/ModeHelper.cs +++ b/Netch/Utils/ModeHelper.cs @@ -60,7 +60,7 @@ namespace Netch.Utils for (var i = 0; i < content.Length; i++) { - var text = content[i]; + var text = content[i].Trim(); if (i == 0) { @@ -83,8 +83,7 @@ namespace Netch.Utils } else { - if (!text.StartsWith("#") && !string.IsNullOrWhiteSpace(text)) - mode.Rule.Add(text.Trim()); + mode.Rule.Add(text); } }