using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using Netch.Utils; namespace Netch.Models { public class Mode { /// /// 备注 /// 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 代理(不设置到系统代理) /// public int Type = 0; /// /// 绕过中国(0. 不绕过 1. 绕过) /// public bool BypassChina = false; /// /// 规则 /// public readonly List Rule = new List(); /// /// 获取备注 /// /// 备注 public override string ToString() { return $"[{Type + 1}] {Remark}"; } /// /// 获取模式文件字符串 /// /// 模式文件字符串 public string ToFileString() { string fileString; switch (Type) { case 0: // 进程模式 fileString = $"# {Remark}"; break; case 1: // TUN/TAP 规则内 IP CIDR,无 Bypass China 设置 fileString = $"# {Remark}, {Type}, 0"; break; default: fileString = $"# {Remark}, {Type}, {(BypassChina ? 1 : 0)}"; break; } fileString += Global.EOF; fileString = Rule.Aggregate(fileString, (current, item) => $"{current}{item}{Global.EOF}"); // 去除最后的行尾符 fileString = fileString.Substring(0, fileString.Length - 2); return fileString; } /// /// 写入模式文件 /// public void ToFile(string Dir) { if (!System.IO.Directory.Exists(Dir)) { System.IO.Directory.CreateDirectory(Dir); } var NewPath = System.IO.Path.Combine(Dir, FileName); if (System.IO.File.Exists(NewPath + ".txt")) { // 重命名该模式文件名 NewPath += "_"; while (System.IO.File.Exists(NewPath + ".txt")) { // 循环重命名该模式文件名,直至不重名 NewPath += "_"; } } FileName = System.IO.Path.GetFileName(NewPath); // 加上文件名后缀 NewPath += ".txt"; // 写入到模式文件里 System.IO.File.WriteAllText(NewPath, ToFileString()); } /// /// 删除模式文件 /// public void DeleteFile() { var fullPath = Path.Combine(Modes.ModeDirectory, RelativePath); if (File.Exists(fullPath)) { File.Delete(fullPath); } } public string TypeToString() { return Type switch { 0 => "Process", 1 => "TUNTAP", 2 => "TUNTAP", 3 => "SYSTEM", 4 => "S5", 5 => "S5+HTTP", _ => "ERROR", }; } } }