diff --git a/Netch/Models/Mode.cs b/Netch/Models/Mode.cs index 9f2e9764..cb19886c 100644 --- a/Netch/Models/Mode.cs +++ b/Netch/Models/Mode.cs @@ -1,51 +1,45 @@ -using Netch.Utils; -using System; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using Netch.Enums; +using Netch.Utils; namespace Netch.Models { public class Mode { - /// - /// - /// - /// Mode File FullPath - /// - /// + private List? _content; + public Mode(string? fullName) { FullName = fullName; if (FullName == null || !File.Exists(FullName)) return; - (Remark, Type) = ReadHead(FullName); + Load(); } public string? FullName { get; } - /// - /// 规则 - /// public List Content => _content ??= ReadContent(); - private List? _content; - - /// - /// 备注 - /// public string Remark { get; set; } = ""; public ModeType Type { get; set; } = ModeType.Process; - /// - /// 文件相对路径(必须是存在的文件) - /// public string? RelativePath => FullName == null ? null : ModeHelper.GetRelativePath(FullName); + private void Load() + { + if (FullName == null) + return; + + (Remark, Type) = ReadHead(FullName); + _content = null; + } + public IEnumerable GetRules() { foreach (var s in Content) @@ -91,13 +85,15 @@ namespace Netch.Models if (text.First() != '#') throw new FormatException($"{fileName} head not found at Line 0"); - var split = text[1..].SplitTrimEntries(','); + var strings = text[1..].SplitTrimEntries(','); + + var remark = strings[0]; + var typeNumber = int.TryParse(strings.ElementAtOrDefault(1), out var type) ? type : 0; - var typeNumber = int.TryParse(split.ElementAtOrDefault(1), out var type) ? type : 0; if (!Enum.GetValues(typeof(ModeType)).Cast().Contains(typeNumber)) throw new NotSupportedException($"Not support mode \"{typeNumber}\"."); - return (split[0], (ModeType)typeNumber); + return (remark, (ModeType)typeNumber); } private List ReadContent() @@ -108,11 +104,6 @@ namespace Netch.Models return File.ReadLines(FullName).Skip(1).ToList(); } - public void ResetContent() - { - _content = null; - } - public void WriteFile() { var dir = Path.GetDirectoryName(FullName)!; @@ -124,10 +115,6 @@ namespace Netch.Models File.WriteAllText(FullName!, content); } - /// - /// 获取备注 - /// - /// 备注 public override string ToString() { return $"[{(int)Type + 1}] {i18N.Translate(Remark)}"; @@ -139,7 +126,8 @@ namespace Netch.Models /// 是否会转发 UDP public static bool TestNatRequired(this Mode mode) { - return mode.Type is ModeType.Process or ModeType.BypassRuleIPs; + return mode.Type is ModeType.Process && Global.Settings.Redirector.ProxyProtocol.HasFlag(PortType.UDP) || + mode.Type is ModeType.BypassRuleIPs; } } } \ No newline at end of file diff --git a/Netch/Utils/PortHelper.cs b/Netch/Utils/PortHelper.cs index e62f4401..2c5b0c98 100644 --- a/Netch/Utils/PortHelper.cs +++ b/Netch/Utils/PortHelper.cs @@ -25,7 +25,7 @@ namespace Netch.Utils } catch (Exception e) { - Log.Error(e,"获取保留端口错误"); + Log.Error(e, "获取保留端口错误"); } } @@ -163,11 +163,12 @@ namespace Netch.Utils /// /// 检查端口类型 /// + [Flags] public enum PortType { - TCP, - UDP, - Both + TCP = 0x01, + UDP = 0x10, + Both = TCP | UDP } public class PortInUseException : Exception