From 3ae84675084b3b90c8a6481110328e67c023ed3d Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Sun, 14 Feb 2021 13:49:13 +0800 Subject: [PATCH] Refactor Mode, Save mode into mode\\Custom\\ --- Netch/Forms/Mode/Process.cs | 40 ++++++---------- Netch/Models/Mode.cs | 92 +++++++++++++++++++++++-------------- Netch/Updater/Updater.cs | 15 +----- Netch/Utils/ModeHelper.cs | 22 +-------- 4 files changed, 74 insertions(+), 95 deletions(-) diff --git a/Netch/Forms/Mode/Process.cs b/Netch/Forms/Mode/Process.cs index 48d5708a..82c79834 100644 --- a/Netch/Forms/Mode/Process.cs +++ b/Netch/Forms/Mode/Process.cs @@ -18,20 +18,13 @@ namespace Netch.Forms.Mode private readonly Models.Mode _mode; /// - /// 是否被编辑过 - /// - public bool Edited { get; private set; } - - /// - /// 编辑模式 + /// 编辑模式 /// /// 模式 public Process(Models.Mode mode) { if (mode.Type != 0) - { throw new Exception("请传入进程模式"); - } InitializeComponent(); CheckForIllegalCrossThreadCalls = false; @@ -48,7 +41,7 @@ namespace Netch.Forms.Mode #endregion - FilenameTextBox.Text = mode.FileName; + FilenameTextBox.Text = mode.RelativePath; RemarkTextBox.Text = mode.Remark; } @@ -61,16 +54,21 @@ namespace Netch.Forms.Mode } /// - /// 扫描目录 + /// 是否被编辑过 + /// + public bool Edited { get; private set; } + + /// + /// 扫描目录 /// /// 路径 public void ScanDirectory(string DirName) { try { - RuleListBox.Items.AddRange(Directory.GetFiles(DirName, "*.exe", SearchOption.AllDirectories)); + RuleListBox.Items.AddRange(Directory.GetFiles(DirName, "*.exe", SearchOption.AllDirectories).Select(f => Path.GetFileName(f)).ToArray()); } - catch (Exception e) + catch (Exception) { // ignored } @@ -83,7 +81,7 @@ namespace Netch.Forms.Mode } /// - /// listBox右键菜单 + /// listBox右键菜单 /// private void RuleListBox_MouseUp(object sender, MouseEventArgs e) { @@ -91,12 +89,10 @@ namespace Netch.Forms.Mode if (RuleListBox.SelectedIndex == -1) return; if (e.Button == MouseButtons.Right) - { contextMenuStrip.Show(RuleListBox, e.Location); - } } - void deleteRule_Click(object sender, EventArgs e) + private void deleteRule_Click(object sender, EventArgs e) { if (RuleListBox.SelectedIndex == -1) return; RuleListBox.Items.RemoveAt(RuleListBox.SelectedIndex); @@ -122,9 +118,7 @@ namespace Netch.Forms.Mode var process = ProcessNameTextBox.Text; if (!RuleListBox.Items.Contains(process)) - { RuleListBox.Items.Add(process); - } Edited = true; RuleListBox.SelectedIndex = RuleListBox.Items.IndexOf(process); @@ -176,7 +170,7 @@ namespace Netch.Forms.Mode _mode.Rule.Clear(); _mode.Rule.AddRange(RuleListBox.Items.Cast()); - ModeHelper.WriteFile(_mode); + _mode.WriteFile(); Global.MainForm.InitMode(); Edited = false; MessageBoxX.Show(i18N.Translate("Mode updated successfully")); @@ -191,17 +185,15 @@ namespace Netch.Forms.Mode return; } - var mode = new Models.Mode + var mode = new Models.Mode(fullName) { BypassChina = false, - FileName = FilenameTextBox.Text, - RelativePath = relativePath, Type = 0, Remark = RemarkTextBox.Text }; mode.Rule.AddRange(RuleListBox.Items.Cast()); - ModeHelper.WriteFile(mode); + mode.WriteFile(); ModeHelper.Add(mode); MessageBoxX.Show(i18N.Translate("Mode added successfully")); } @@ -218,9 +210,7 @@ namespace Netch.Forms.Mode var invalidFileChars = Path.GetInvalidFileNameChars(); var fileName = new StringBuilder(RemarkTextBox.Text); foreach (var c in invalidFileChars) - { fileName.Replace(c, '_'); - } FilenameTextBox.Text = fileName.ToString(); } diff --git a/Netch/Models/Mode.cs b/Netch/Models/Mode.cs index 151f1969..0abdc103 100644 --- a/Netch/Models/Mode.cs +++ b/Netch/Models/Mode.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using Netch.Utils; @@ -7,41 +8,51 @@ namespace Netch.Models { public class Mode { + public readonly string FullName; + /// - /// 备注 + /// 规则 + /// + public readonly List Rule = new(); + + /// + /// 绕过中国(0. 不绕过 1. 绕过) + /// + public bool BypassChina = false; + /// + /// 备注 /// public string Remark; /// - /// 文件相对路径(必须是存在的文件) - /// - public string RelativePath; - - /// - /// 无后缀文件名 - /// - public string FileName; - - /// - /// 类型 - /// 0. Socks5 + 进程加速 - /// 1. Socks5 + TUN/TAP 规则内 IP CIDR 加速 - /// 2. Socks5 + TUN/TAP 全局,绕过规则内 IP CIDR - /// 3. Socks5 + HTTP 代理(设置到系统代理) - /// 4. Socks5 代理(不设置到系统代理) - /// 5. Socks5 + HTTP 代理(不设置到系统代理) + /// 类型 + /// + /// 0. Socks5 + 进程加速 + /// + /// 1. Socks5 + TUN/TAP 规则内 IP CIDR 加速 + /// + /// 2. Socks5 + TUN/TAP 全局,绕过规则内 IP CIDR + /// + /// 3. Socks5 + HTTP 代理(设置到系统代理) + /// + /// 4. Socks5 代理(不设置到系统代理) + /// + /// 5. Socks5 + HTTP 代理(不设置到系统代理) + /// /// public int Type = 0; + public Mode(string fullName) + { + FullName = fullName; + } + public Mode() + { + } /// - /// 绕过中国(0. 不绕过 1. 绕过) + /// 文件相对路径(必须是存在的文件) /// - public bool BypassChina = false; - - /// - /// 规则 - /// - public readonly List Rule = new List(); + public string RelativePath => ModeHelper.GetRelativePath(FullName); public List FullRule { @@ -81,13 +92,9 @@ namespace Netch.Models else { if (mode.Rule.Any(rule => rule.StartsWith("#include"))) - { Logging.Warning("Cannot reference mode that reference other mode"); - } else - { result.AddRange(mode.FullRule); - } } } } @@ -101,9 +108,18 @@ namespace Netch.Models } } + public void WriteFile() + { + var dir = Path.GetDirectoryName(FullName); + if (!Directory.Exists(dir)) + Directory.CreateDirectory(dir); + + // 写入到模式文件里 + File.WriteAllText(FullName, ToFileString()); + } /// - /// 获取备注 + /// 获取备注 /// /// 备注 public override string ToString() @@ -112,7 +128,7 @@ namespace Netch.Models } /// - /// 获取模式文件字符串 + /// 获取模式文件字符串 /// /// 模式文件字符串 public string ToFileString() @@ -122,10 +138,16 @@ namespace Netch.Models } public static class ModeExtension { - /// 是否会转发 UDP - public static bool TestNatRequired(this Mode mode) => mode.Type is 0 or 1 or 2; + /// 是否会转发 UDP + public static bool TestNatRequired(this Mode mode) + { + return mode.Type is 0 or 1 or 2; + } - /// Socks5 分流是否能被有效实施 - public static bool ClientRouting(this Mode mode) => mode.Type is not (1 or 2); + /// Socks5 分流是否能被有效实施 + public static bool ClientRouting(this Mode mode) + { + return mode.Type is not (1 or 2); + } } } \ No newline at end of file diff --git a/Netch/Updater/Updater.cs b/Netch/Updater/Updater.cs index 48a2ba71..16436bde 100644 --- a/Netch/Updater/Updater.cs +++ b/Netch/Updater/Updater.cs @@ -3,13 +3,10 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; -using System.Security.AccessControl; using System.Text; using System.Windows.Forms; -using Netch.Controllers; using Netch.Forms; using Netch.Properties; -using Netch.Utils; namespace Netch.Updater { @@ -88,9 +85,7 @@ namespace Netch.Updater try { foreach (var DirChildInfo in DirInfo.GetDirectories()) - { DirStack.Push(DirChildInfo.FullName); - } } catch { @@ -100,12 +95,8 @@ namespace Netch.Updater try { foreach (var FileChildInfo in DirInfo.GetFiles()) - { if (FileChildInfo.Name == filename) - { return FileChildInfo; - } - } } catch { @@ -127,21 +118,17 @@ namespace Netch.Updater else { foreach (var dir in Directory.GetDirectories(sourceDirName, "*", SearchOption.AllDirectories)) - { if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); - } foreach (var f in Directory.GetFiles(sourceDirName, "*", SearchOption.AllDirectories)) - { try { File.Move(f, f.Replace(sourceDirName, destDirName)); } - catch (Exception e) + catch (Exception) { throw new Exception("Updater wasn't able to move file: " + f); } - } } } diff --git a/Netch/Utils/ModeHelper.cs b/Netch/Utils/ModeHelper.cs index b16d7d48..a751e077 100644 --- a/Netch/Utils/ModeHelper.cs +++ b/Netch/Utils/ModeHelper.cs @@ -62,11 +62,7 @@ namespace Netch.Utils private static void LoadModeFile(string fullName) { - var mode = new Mode - { - FileName = Path.GetFileNameWithoutExtension(fullName), - RelativePath = GetRelativePath(fullName) - }; + var mode = new Mode(fullName); var content = File.ReadAllLines(fullName); if (content.Length == 0) return; @@ -105,21 +101,6 @@ namespace Netch.Utils Global.Modes.Add(mode); } - public static void WriteFile(Mode mode) - { - if (!Directory.Exists(ModeDirectory)) - Directory.CreateDirectory(ModeDirectory); - - var fullName = GetFullPath(mode.RelativePath ?? mode.FileName + ".txt"); - - if (mode.RelativePath == null && File.Exists(fullName)) - throw new Exception("新建模式的文件名已存在,请贡献者检查代码"); - - // 写入到模式文件里 - File.WriteAllText(fullName, mode.ToFileString()); - mode.RelativePath = GetRelativePath(fullName); - } - private static void Sort() { Global.Modes.Sort((a, b) => string.Compare(a.Remark, b.Remark, StringComparison.Ordinal)); @@ -142,7 +123,6 @@ namespace Netch.Utils Global.MainForm.InitMode(); } - public static bool SkipServerController(Server server, Mode mode) { return mode.Type switch