From 9d086336eaa1062badc68e82df44dbbf1c5e2324 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Sat, 19 Dec 2020 18:31:25 +0800 Subject: [PATCH] Refactor Mode Load --- Netch/Controllers/MainController.cs | 2 +- Netch/Forms/MainForm.cs | 18 ++++--- Netch/Models/Mode.cs | 50 ++++--------------- Netch/Servers/VMess/Utils/V2rayConfigUtils.cs | 1 + Netch/Utils/ModeHelper.cs | 13 +++-- 5 files changed, 29 insertions(+), 55 deletions(-) diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs index 88ba98b6..101ef86a 100644 --- a/Netch/Controllers/MainController.cs +++ b/Netch/Controllers/MainController.cs @@ -82,7 +82,7 @@ namespace Netch.Controllers throw new StartFailedException(); } - if (mode.TestNatRequired) + if (mode.TestNatRequired()) NatTest(); return true; diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index c5a6cb9f..fcec9f07 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -366,21 +366,23 @@ namespace Netch.Forms return; } - var selectedMode = (Models.Mode) ModeComboBox.SelectedItem; - switch (selectedMode.Type) + var mode = (Models.Mode) ModeComboBox.SelectedItem; + if (ModifierKeys == Keys.Control) + { + Utils.Utils.Open(ModeHelper.GetFullPath(mode.RelativePath)); + return; + } + + switch (mode.Type) { case 0: - { Hide(); - new Process(selectedMode).ShowDialog(); + new Process(mode).ShowDialog(); Show(); break; - } default: - { - MessageBoxX.Show($"Current not support editing {selectedMode.TypeToString()} Mode"); + Utils.Utils.Open(ModeHelper.GetFullPath(mode.RelativePath)); break; - } } } diff --git a/Netch/Models/Mode.cs b/Netch/Models/Mode.cs index d6b688b7..8e0b92e0 100644 --- a/Netch/Models/Mode.cs +++ b/Netch/Models/Mode.cs @@ -33,9 +33,6 @@ namespace Netch.Models /// public int Type = 0; - /// 是否会转发 UDP - public bool TestNatRequired => Type is 0 or 1 or 2; - /// /// 绕过中国(0. 不绕过 1. 绕过) /// @@ -120,44 +117,15 @@ namespace Netch.Models /// 模式文件字符串 public string ToFileString() { - StringBuilder fileString = new StringBuilder(); - - switch (Type) - { - case 0: - // 进程模式 - fileString.Append($"# {Remark}"); - break; - case 1: - // TUN/TAP 规则内 IP CIDR,无 Bypass China 设置 - fileString.Append($"# {Remark}, {Type}, 0"); - break; - default: - fileString.Append($"# {Remark}, {Type}, {(BypassChina ? 1 : 0)}"); - break; - } - - if (Rule.Any()) - { - fileString.Append(Global.EOF); - fileString.Append(string.Join(Global.EOF, Rule)); - } - - return fileString.ToString(); - } - - public string TypeToString() - { - return Type switch - { - 0 => "Process", - 1 => "TUNTAP", - 2 => "TUNTAP", - 3 => "SYSTEM", - 4 => "S5", - 5 => "S5+HTTP", - _ => "ERROR", - }; + return $"# {Remark}, {Type}, {(BypassChina ? 1 : 0)}{Global.EOF}{string.Join(Global.EOF, Rule)}"; } } + public static class ModeExtension + { + /// 是否会转发 UDP + public static bool TestNatRequired(this Mode mode) => mode.Type is 0 or 1 or 2; + + /// Socks5 分流是否能被有效实施 + public static bool ClientRouting(this Mode mode) => mode.Type is not 1 or 2; + } } \ No newline at end of file diff --git a/Netch/Servers/VMess/Utils/V2rayConfigUtils.cs b/Netch/Servers/VMess/Utils/V2rayConfigUtils.cs index fe9d5397..daff8e98 100644 --- a/Netch/Servers/VMess/Utils/V2rayConfigUtils.cs +++ b/Netch/Servers/VMess/Utils/V2rayConfigUtils.cs @@ -80,6 +80,7 @@ namespace Netch.Servers.VMess.Utils switch (mode.Type) { case 0: + directRuleObject.ip.Add("geoip:cn"); break; case 1: case 2: diff --git a/Netch/Utils/ModeHelper.cs b/Netch/Utils/ModeHelper.cs index 31d7fa50..4a43f1df 100644 --- a/Netch/Utils/ModeHelper.cs +++ b/Netch/Utils/ModeHelper.cs @@ -68,17 +68,19 @@ namespace Netch.Utils if (i == 0) { + if (text.First() != '#') + return; try { - var splited = text.Substring(text.IndexOf('#') + 1).Split(',').Select(s => s.Trim()).ToArray(); + var splited = text.Substring(1).Split(',').Select(s => s.Trim()).ToArray(); mode.Remark = splited[0]; - var result = int.TryParse(splited.ElementAtOrDefault(1), out var type); - mode.Type = result ? type : 0; + var typeResult = int.TryParse(splited.ElementAtOrDefault(1), out var type); + mode.Type = typeResult ? type : 0; - var result1 = int.TryParse(splited.ElementAtOrDefault(2), out var bypassChina); - mode.BypassChina = result1 && bypassChina == 1; + var bypassChinaResult = int.TryParse(splited.ElementAtOrDefault(2), out var bypassChina); + mode.BypassChina = mode.ClientRouting() && bypassChinaResult && bypassChina == 1; } catch { @@ -137,6 +139,7 @@ namespace Netch.Utils Global.MainForm.InitMode(); } + public static bool SkipServerController(Server server, Mode mode) { return mode.Type switch