mirror of
https://github.com/netchx/netch.git
synced 2026-05-07 22:44:03 +08:00
Refactor Mode, Save mode into mode\\Custom\\
This commit is contained in:
@@ -18,20 +18,13 @@ namespace Netch.Forms.Mode
|
||||
private readonly Models.Mode _mode;
|
||||
|
||||
/// <summary>
|
||||
/// 是否被编辑过
|
||||
/// </summary>
|
||||
public bool Edited { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 编辑模式
|
||||
/// 编辑模式
|
||||
/// </summary>
|
||||
/// <param name="mode">模式</param>
|
||||
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
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 扫描目录
|
||||
/// 是否被编辑过
|
||||
/// </summary>
|
||||
public bool Edited { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 扫描目录
|
||||
/// </summary>
|
||||
/// <param name="DirName">路径</param>
|
||||
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
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// listBox右键菜单
|
||||
/// listBox右键菜单
|
||||
/// </summary>
|
||||
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<string>());
|
||||
|
||||
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<string>());
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// 规则
|
||||
/// </summary>
|
||||
public readonly List<string> Rule = new();
|
||||
|
||||
/// <summary>
|
||||
/// 绕过中国(0. 不绕过 1. 绕过)
|
||||
/// </summary>
|
||||
public bool BypassChina = false;
|
||||
/// <summary>
|
||||
/// 备注
|
||||
/// </summary>
|
||||
public string Remark;
|
||||
|
||||
/// <summary>
|
||||
/// 文件相对路径(必须是存在的文件)
|
||||
/// </summary>
|
||||
public string RelativePath;
|
||||
|
||||
/// <summary>
|
||||
/// 无后缀文件名
|
||||
/// </summary>
|
||||
public string FileName;
|
||||
|
||||
/// <summary>
|
||||
/// 类型<para />
|
||||
/// 0. Socks5 + 进程加速<para />
|
||||
/// 1. Socks5 + TUN/TAP 规则内 IP CIDR 加速<para />
|
||||
/// 2. Socks5 + TUN/TAP 全局,绕过规则内 IP CIDR<para />
|
||||
/// 3. Socks5 + HTTP 代理(设置到系统代理)<para />
|
||||
/// 4. Socks5 代理(不设置到系统代理)<para />
|
||||
/// 5. Socks5 + HTTP 代理(不设置到系统代理)<para />
|
||||
/// 类型
|
||||
/// <para />
|
||||
/// 0. Socks5 + 进程加速
|
||||
/// <para />
|
||||
/// 1. Socks5 + TUN/TAP 规则内 IP CIDR 加速
|
||||
/// <para />
|
||||
/// 2. Socks5 + TUN/TAP 全局,绕过规则内 IP CIDR
|
||||
/// <para />
|
||||
/// 3. Socks5 + HTTP 代理(设置到系统代理)
|
||||
/// <para />
|
||||
/// 4. Socks5 代理(不设置到系统代理)
|
||||
/// <para />
|
||||
/// 5. Socks5 + HTTP 代理(不设置到系统代理)
|
||||
/// <para />
|
||||
/// </summary>
|
||||
public int Type = 0;
|
||||
public Mode(string fullName)
|
||||
{
|
||||
FullName = fullName;
|
||||
}
|
||||
public Mode()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 绕过中国(0. 不绕过 1. 绕过)
|
||||
/// 文件相对路径(必须是存在的文件)
|
||||
/// </summary>
|
||||
public bool BypassChina = false;
|
||||
|
||||
/// <summary>
|
||||
/// 规则
|
||||
/// </summary>
|
||||
public readonly List<string> Rule = new List<string>();
|
||||
public string RelativePath => ModeHelper.GetRelativePath(FullName);
|
||||
|
||||
public List<string> 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());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取备注
|
||||
/// 获取备注
|
||||
/// </summary>
|
||||
/// <returns>备注</returns>
|
||||
public override string ToString()
|
||||
@@ -112,7 +128,7 @@ namespace Netch.Models
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取模式文件字符串
|
||||
/// 获取模式文件字符串
|
||||
/// </summary>
|
||||
/// <returns>模式文件字符串</returns>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user