diff --git a/Netch/Controllers/HTTPController.cs b/Netch/Controllers/HTTPController.cs index ed8afe11..852f6355 100644 --- a/Netch/Controllers/HTTPController.cs +++ b/Netch/Controllers/HTTPController.cs @@ -1,5 +1,5 @@ -using Microsoft.Win32; -using System; +using System; +using Microsoft.Win32; namespace Netch.Controllers { @@ -22,14 +22,12 @@ namespace Netch.Controllers { if (server.Type == "Socks5") { - if (!String.IsNullOrWhiteSpace(server.Username) && !String.IsNullOrWhiteSpace(server.Password)) + if (!string.IsNullOrWhiteSpace(server.Username) && !string.IsNullOrWhiteSpace(server.Password)) { return false; } - else - { - pPrivoxyController.Start(server, mode); - } + + pPrivoxyController.Start(server, mode); } else { diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs index ef4f82d6..d6dc5486 100644 --- a/Netch/Controllers/MainController.cs +++ b/Netch/Controllers/MainController.cs @@ -9,7 +9,7 @@ namespace Netch.Controllers public static Process GetProcess() { var process = new Process(); - process.StartInfo.WorkingDirectory = String.Format("{0}\\bin", Directory.GetCurrentDirectory()); + process.StartInfo.WorkingDirectory = string.Format("{0}\\bin", Directory.GetCurrentDirectory()); process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.RedirectStandardInput = true; @@ -23,33 +23,33 @@ namespace Netch.Controllers /// /// SS 控制器 /// - public SSController pSSController = null; + public SSController pSSController; /// /// SSR 控制器 /// - public SSRController pSSRController = null; + public SSRController pSSRController; /// /// V2Ray 控制器 /// - public VMessController pVMessController = null; + public VMessController pVMessController; /// /// NF 控制器 /// - public NFController pNFController = null; + public NFController pNFController; /// /// HTTP 控制器 /// - public HTTPController pHTTPController = null; + public HTTPController pHTTPController; /// /// TUN/TAP 控制器 /// - public TUNTAPController pTUNTAPController = null; + public TUNTAPController pTUNTAPController; /// /// 启动 @@ -96,8 +96,6 @@ namespace Netch.Controllers } result = pVMessController.Start(server, mode); break; - default: - break; } if (result) @@ -189,9 +187,9 @@ namespace Netch.Controllers } - public void KillProcess(String name) { - Process[] processes = Process.GetProcessesByName(name); - foreach (Process p in processes) + public void KillProcess(string name) { + var processes = Process.GetProcessesByName(name); + foreach (var p in processes) { if (IsChildProcess(p, name)) { @@ -205,8 +203,8 @@ namespace Netch.Controllers bool result; try { - string FileName = (Directory.GetCurrentDirectory() + "\\bin\\" + name + ".exe").ToLower(); - string procFileName = process.MainModule.FileName.ToLower(); + var FileName = (Directory.GetCurrentDirectory() + "\\bin\\" + name + ".exe").ToLower(); + var procFileName = process.MainModule.FileName.ToLower(); result = FileName.Equals(procFileName, StringComparison.Ordinal); } catch (Exception e) diff --git a/Netch/Controllers/NFController.cs b/Netch/Controllers/NFController.cs index 5d9e5ce3..f12a5c8c 100644 --- a/Netch/Controllers/NFController.cs +++ b/Netch/Controllers/NFController.cs @@ -19,7 +19,7 @@ namespace Netch.Controllers /// /// 上传 /// 下载 - public delegate void BandwidthUpdateHandler(long upload, Int64 download); + public delegate void BandwidthUpdateHandler(long upload, long download); /// /// 进程实例 @@ -45,7 +45,7 @@ namespace Netch.Controllers } // 生成驱动文件路径 - var driver = String.Format("{0}\\drivers\\netfilter2.sys", Environment.SystemDirectory); + var driver = string.Format("{0}\\drivers\\netfilter2.sys", Environment.SystemDirectory); // 检查驱动是否存在 if (!File.Exists(driver)) @@ -139,9 +139,9 @@ namespace Netch.Controllers return false; } - fallback = $"-r {result.ToString()}:{server.Port} -p \"{processes}\""; + fallback = $"-r {result}:{server.Port} -p \"{processes}\""; - if (!String.IsNullOrWhiteSpace(server.Username) && !String.IsNullOrWhiteSpace(server.Password)) + if (!string.IsNullOrWhiteSpace(server.Username) && !string.IsNullOrWhiteSpace(server.Password)) { fallback += $" -username \"{server.Username}\" -password \"{server.Password}\""; } @@ -156,7 +156,7 @@ namespace Netch.Controllers Instance.BeginErrorReadLine(); var IsFallback = false; - for (int i = 0; i < 1000; i++) + for (var i = 0; i < 1000; i++) { Thread.Sleep(10); @@ -219,9 +219,9 @@ namespace Netch.Controllers public void OnOutputDataReceived(object sender, DataReceivedEventArgs e) { - if (!String.IsNullOrWhiteSpace(e.Data)) + if (!string.IsNullOrWhiteSpace(e.Data)) { - File.AppendAllText("logging\\redirector.log", String.Format("{0}\r\n", e.Data)); + File.AppendAllText("logging\\redirector.log", string.Format("{0}\r\n", e.Data)); if (State == Models.State.Starting) { @@ -250,7 +250,7 @@ namespace Netch.Controllers if (uploadSplited.Length == 2 && downloadSplited.Length == 2) { - if (long.TryParse(uploadSplited[1], out long upload) && long.TryParse(downloadSplited[1], out long download)) + if (long.TryParse(uploadSplited[1], out var upload) && long.TryParse(downloadSplited[1], out var download)) { Task.Run(() => OnBandwidthUpdated(upload, download)); } diff --git a/Netch/Controllers/PrivoxyController.cs b/Netch/Controllers/PrivoxyController.cs index 24d76d70..9eedf5ac 100644 --- a/Netch/Controllers/PrivoxyController.cs +++ b/Netch/Controllers/PrivoxyController.cs @@ -46,13 +46,13 @@ namespace Netch.Controllers } - Instance = new Process() + Instance = new Process { StartInfo = { - FileName = String.Format("{0}\\bin\\Privoxy.exe", Directory.GetCurrentDirectory()), + FileName = string.Format("{0}\\bin\\Privoxy.exe", Directory.GetCurrentDirectory()), Arguments = "..\\data\\privoxy.conf", - WorkingDirectory = String.Format("{0}\\bin", Directory.GetCurrentDirectory()), + WorkingDirectory = string.Format("{0}\\bin", Directory.GetCurrentDirectory()), WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = true, CreateNoWindow = true diff --git a/Netch/Controllers/SSController.cs b/Netch/Controllers/SSController.cs index 24b9c3b0..8ec5fc0a 100644 --- a/Netch/Controllers/SSController.cs +++ b/Netch/Controllers/SSController.cs @@ -33,7 +33,7 @@ namespace Netch.Controllers Instance = MainController.GetProcess(); Instance.StartInfo.FileName = "bin\\Shadowsocks.exe"; - if (!String.IsNullOrWhiteSpace(server.Plugin) && !String.IsNullOrWhiteSpace(server.PluginOption)) + if (!string.IsNullOrWhiteSpace(server.Plugin) && !string.IsNullOrWhiteSpace(server.PluginOption)) { Instance.StartInfo.Arguments = $"-s {server.Hostname} -p {server.Port} -b {Global.Settings.LocalAddress} -l {Global.Settings.Socks5LocalPort} -m {server.EncryptMethod} -k \"{server.Password}\" -u --plugin {server.Plugin} --plugin-opts \"{server.PluginOption}\""; } @@ -54,7 +54,7 @@ namespace Netch.Controllers Instance.Start(); Instance.BeginOutputReadLine(); Instance.BeginErrorReadLine(); - for (int i = 0; i < 1000; i++) + for (var i = 0; i < 1000; i++) { Thread.Sleep(10); @@ -97,7 +97,7 @@ namespace Netch.Controllers public void OnOutputDataReceived(object sender, DataReceivedEventArgs e) { - if (!String.IsNullOrWhiteSpace(e.Data)) + if (!string.IsNullOrWhiteSpace(e.Data)) { // File.AppendAllText("logging\\shadowsocks.log", $"{e.Data}\r\n"); diff --git a/Netch/Controllers/SSRController.cs b/Netch/Controllers/SSRController.cs index a137d140..f8dc4178 100644 --- a/Netch/Controllers/SSRController.cs +++ b/Netch/Controllers/SSRController.cs @@ -34,21 +34,21 @@ namespace Netch.Controllers Instance.StartInfo.FileName = "bin\\ShadowsocksR.exe"; Instance.StartInfo.Arguments = $"-s {server.Hostname} -p {server.Port} -k \"{server.Password}\" -m {server.EncryptMethod}"; - if (!String.IsNullOrEmpty(server.Protocol)) + if (!string.IsNullOrEmpty(server.Protocol)) { Instance.StartInfo.Arguments += $" -O {server.Protocol}"; - if (!String.IsNullOrEmpty(server.ProtocolParam)) + if (!string.IsNullOrEmpty(server.ProtocolParam)) { Instance.StartInfo.Arguments += $" -G \"{server.ProtocolParam}\""; } } - if (!String.IsNullOrEmpty(server.OBFS)) + if (!string.IsNullOrEmpty(server.OBFS)) { Instance.StartInfo.Arguments += $" -o {server.OBFS}"; - if (!String.IsNullOrEmpty(server.OBFSParam)) + if (!string.IsNullOrEmpty(server.OBFSParam)) { Instance.StartInfo.Arguments += $" -g \"{server.OBFSParam}\""; } @@ -68,7 +68,7 @@ namespace Netch.Controllers Instance.Start(); Instance.BeginOutputReadLine(); Instance.BeginErrorReadLine(); - for (int i = 0; i < 1000; i++) + for (var i = 0; i < 1000; i++) { Thread.Sleep(10); @@ -111,7 +111,7 @@ namespace Netch.Controllers public void OnOutputDataReceived(object sender, DataReceivedEventArgs e) { - if (!String.IsNullOrWhiteSpace(e.Data)) + if (!string.IsNullOrWhiteSpace(e.Data)) { File.AppendAllText("logging\\shadowsocksr.log", $"{e.Data}\r\n"); diff --git a/Netch/Controllers/TUNTAPController.cs b/Netch/Controllers/TUNTAPController.cs index 4bf9e29c..98a475ad 100644 --- a/Netch/Controllers/TUNTAPController.cs +++ b/Netch/Controllers/TUNTAPController.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Diagnostics; using System.IO; using System.Net; @@ -101,12 +101,12 @@ namespace Netch.Controllers if (SavedMode.Type == 2) // 处理仅规则内走直连 { // 将 TUN/TAP 网卡权重放到最高 - var instance = new Process() + var instance = new Process { StartInfo = { - FileName = String.Format("netsh"), - Arguments = String.Format("interface ip set interface {0} metric=0", Global.TUNTAP.Index), + FileName = "netsh", + Arguments = string.Format("interface ip set interface {0} metric=0", Global.TUNTAP.Index), WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = true, CreateNoWindow = true @@ -166,7 +166,7 @@ namespace Netch.Controllers { if (SavedMode.Type == 2) { - NativeMethods.DeleteRoute("0.0.0.0", 0, Global.Settings.TUNTAP.Gateway.ToString(), Global.TUNTAP.Index, 10); + NativeMethods.DeleteRoute("0.0.0.0", 0, Global.Settings.TUNTAP.Gateway, Global.TUNTAP.Index, 10); foreach (var ip in SavedMode.Rule) { @@ -273,8 +273,8 @@ namespace Netch.Controllers SetupBypass(); Instance = new Process(); - Instance.StartInfo.WorkingDirectory = String.Format("{0}\\bin", Directory.GetCurrentDirectory()); - Instance.StartInfo.FileName = String.Format("{0}\\bin\\tun2socks.exe", Directory.GetCurrentDirectory()); + Instance.StartInfo.WorkingDirectory = string.Format("{0}\\bin", Directory.GetCurrentDirectory()); + Instance.StartInfo.FileName = string.Format("{0}\\bin\\tun2socks.exe", Directory.GetCurrentDirectory()); string dns; if (Global.Settings.TUNTAP.UseCustomDNS) @@ -297,11 +297,11 @@ namespace Netch.Controllers if (server.Type == "Socks5") { - Instance.StartInfo.Arguments = String.Format("-proxyServer {0}:{1} -tunAddr {2} -tunMask {3} -tunGw {4} -tunDns {5} -tunName \"{6}\"", server.Hostname, server.Port, Global.Settings.TUNTAP.Address, Global.Settings.TUNTAP.Netmask, Global.Settings.TUNTAP.Gateway, dns, Global.TUNTAP.Adapter.Name); + Instance.StartInfo.Arguments = string.Format("-proxyServer {0}:{1} -tunAddr {2} -tunMask {3} -tunGw {4} -tunDns {5} -tunName \"{6}\"", server.Hostname, server.Port, Global.Settings.TUNTAP.Address, Global.Settings.TUNTAP.Netmask, Global.Settings.TUNTAP.Gateway, dns, Global.TUNTAP.Adapter.Name); } else { - Instance.StartInfo.Arguments = String.Format("-proxyServer 127.0.0.1:{0} -tunAddr {1} -tunMask {2} -tunGw {3} -tunDns {4} -tunName \"{5}\"", Global.Settings.Socks5LocalPort, Global.Settings.TUNTAP.Address, Global.Settings.TUNTAP.Netmask, Global.Settings.TUNTAP.Gateway, dns, Global.TUNTAP.Adapter.Name); + Instance.StartInfo.Arguments = string.Format("-proxyServer 127.0.0.1:{0} -tunAddr {1} -tunMask {2} -tunGw {3} -tunDns {4} -tunName \"{5}\"", Global.Settings.Socks5LocalPort, Global.Settings.TUNTAP.Address, Global.Settings.TUNTAP.Netmask, Global.Settings.TUNTAP.Gateway, dns, Global.TUNTAP.Adapter.Name); } Instance.StartInfo.CreateNoWindow = true; @@ -319,7 +319,7 @@ namespace Netch.Controllers Instance.BeginOutputReadLine(); Instance.PriorityClass = ProcessPriorityClass.RealTime; - for (int i = 0; i < 1000; i++) + for (var i = 0; i < 1000; i++) { Thread.Sleep(10); @@ -362,9 +362,9 @@ namespace Netch.Controllers public void OnOutputDataReceived(object sender, DataReceivedEventArgs e) { - if (!String.IsNullOrEmpty(e.Data)) + if (!string.IsNullOrEmpty(e.Data)) { - File.AppendAllText("logging\\tun2socks.log", String.Format("{0}\r\n", e.Data.Trim())); + File.AppendAllText("logging\\tun2socks.log", string.Format("{0}\r\n", e.Data.Trim())); if (State == Models.State.Starting) { diff --git a/Netch/Controllers/UpdateChecker.cs b/Netch/Controllers/UpdateChecker.cs index 627d1651..9d6719da 100644 --- a/Netch/Controllers/UpdateChecker.cs +++ b/Netch/Controllers/UpdateChecker.cs @@ -1,10 +1,10 @@ -using Netch.Models.GitHubRelease; -using Newtonsoft.Json; -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Net.Http; using System.Threading.Tasks; +using Netch.Models.GitHubRelease; +using Newtonsoft.Json; namespace Netch.Controllers { diff --git a/Netch/Controllers/VMessController.cs b/Netch/Controllers/VMessController.cs index 82bd271a..0da2d177 100644 --- a/Netch/Controllers/VMessController.cs +++ b/Netch/Controllers/VMessController.cs @@ -31,32 +31,32 @@ namespace Netch.Controllers return false; } - File.WriteAllText("data\\last.json", Newtonsoft.Json.JsonConvert.SerializeObject(new Models.Information.VMess.Config() + File.WriteAllText("data\\last.json", Newtonsoft.Json.JsonConvert.SerializeObject(new Models.Information.VMess.Config { - inbounds = new List() + inbounds = new List { - new Models.Information.VMess.Inbounds() + new Models.Information.VMess.Inbounds { settings = new Models.Information.VMess.InboundSettings(), port = Global.Settings.Socks5LocalPort, listen = Global.Settings.LocalAddress } }, - outbounds = new List() + outbounds = new List { - new Models.Information.VMess.Outbounds() + new Models.Information.VMess.Outbounds { - settings = new Models.Information.VMess.OutboundSettings() + settings = new Models.Information.VMess.OutboundSettings { - vnext = new List() + vnext = new List { - new Models.Information.VMess.VNext() + new Models.Information.VMess.VNext { address = server.Hostname, port = server.Port, users = new List { - new Models.Information.VMess.User() + new Models.Information.VMess.User { id = server.UserID, alterId = server.AlterID, @@ -66,66 +66,66 @@ namespace Netch.Controllers } } }, - streamSettings = new Models.Information.VMess.StreamSettings() + streamSettings = new Models.Information.VMess.StreamSettings { network = server.TransferProtocol, - security = server.TLSSecure == true ? "tls" : "", - wsSettings = server.TransferProtocol == "ws" ? new Models.Information.VMess.WebSocketSettings() + security = server.TLSSecure ? "tls" : "", + wsSettings = server.TransferProtocol == "ws" ? new Models.Information.VMess.WebSocketSettings { path = server.Path == "" ? "/" : server.Path, - headers = new Models.Information.VMess.WSHeaders() + headers = new Models.Information.VMess.WSHeaders { Host = server.Host == "" ? server.Hostname : server.Host } } : null, - tcpSettings = server.FakeType == "http" ? new Models.Information.VMess.TCPSettings() + tcpSettings = server.FakeType == "http" ? new Models.Information.VMess.TCPSettings { - header = new Models.Information.VMess.TCPHeaders() + header = new Models.Information.VMess.TCPHeaders { type = server.FakeType, - request = new Models.Information.VMess.TCPRequest() + request = new Models.Information.VMess.TCPRequest { path = server.Path == "" ? "/" : server.Path, - headers = new Models.Information.VMess.TCPRequestHeaders() + headers = new Models.Information.VMess.TCPRequestHeaders { Host = server.Host == "" ? server.Hostname : server.Host } } } } : null, - kcpSettings = server.TransferProtocol == "kcp" ? new Models.Information.VMess.KCPSettings() + kcpSettings = server.TransferProtocol == "kcp" ? new Models.Information.VMess.KCPSettings { - header = new Models.Information.VMess.TCPHeaders() + header = new Models.Information.VMess.TCPHeaders { type = server.FakeType } } : null, - quicSettings = server.TransferProtocol == "quic" ? new Models.Information.VMess.QUICSettings() + quicSettings = server.TransferProtocol == "quic" ? new Models.Information.VMess.QUICSettings { security = server.QUICSecure, key = server.QUICSecret, - header = new Models.Information.VMess.TCPHeaders() + header = new Models.Information.VMess.TCPHeaders { type = server.FakeType } } : null, - httpSettings = server.TransferProtocol == "h2" ? new Models.Information.VMess.HTTPSettings() + httpSettings = server.TransferProtocol == "h2" ? new Models.Information.VMess.HTTPSettings { host = server.Host == "" ? server.Hostname : server.Host, path = server.Path == "" ? "/" : server.Path } : null, - tlsSettings = new Models.Information.VMess.TLSSettings() + tlsSettings = new Models.Information.VMess.TLSSettings { allowInsecure = true, serverName = server.Host == "" ? server.Hostname : server.Host } }, - mux = new Models.Information.VMess.OutboundMux() + mux = new Models.Information.VMess.OutboundMux { enabled = server.UseMux } }, - new Models.Information.VMess.Outbounds() + new Models.Information.VMess.Outbounds { tag = "direct", protocol = "freedom", @@ -134,11 +134,11 @@ namespace Netch.Controllers mux = null } }, - routing = new Models.Information.VMess.Routing() + routing = new Models.Information.VMess.Routing { - rules = new List() + rules = new List { - mode.BypassChina == true ? new Models.Information.VMess.RoutingRules() + mode.BypassChina ? new Models.Information.VMess.RoutingRules { type = "field", ip = new List @@ -152,7 +152,7 @@ namespace Netch.Controllers "geosite:cn" }, outboundTag = "direct" - } : new Models.Information.VMess.RoutingRules() + } : new Models.Information.VMess.RoutingRules { type = "field", ip = new List @@ -185,7 +185,7 @@ namespace Netch.Controllers Instance.Start(); Instance.BeginOutputReadLine(); Instance.BeginErrorReadLine(); - for (int i = 0; i < 1000; i++) + for (var i = 0; i < 1000; i++) { Thread.Sleep(10); @@ -232,7 +232,7 @@ namespace Netch.Controllers public void OnOutputDataReceived(object sender, DataReceivedEventArgs e) { - if (!String.IsNullOrWhiteSpace(e.Data)) + if (!string.IsNullOrWhiteSpace(e.Data)) { File.AppendAllText("logging\\v2ray.log", $"{e.Data}\r\n"); diff --git a/Netch/Forms/GlobalBypassIPForm.cs b/Netch/Forms/GlobalBypassIPForm.cs index d3844ec4..e5844141 100644 --- a/Netch/Forms/GlobalBypassIPForm.cs +++ b/Netch/Forms/GlobalBypassIPForm.cs @@ -34,11 +34,11 @@ namespace Netch.Forms private void AddButton_Click(object sender, EventArgs e) { - if (!String.IsNullOrEmpty(IPTextBox.Text)) + if (!string.IsNullOrEmpty(IPTextBox.Text)) { if (IPAddress.TryParse(IPTextBox.Text, out var address)) { - IPListBox.Items.Add(String.Format("{0}/{1}", address, PrefixComboBox.SelectedItem)); + IPListBox.Items.Add(string.Format("{0}/{1}", address, PrefixComboBox.SelectedItem)); } else { @@ -68,7 +68,7 @@ namespace Netch.Forms Global.Settings.BypassIPs.Clear(); foreach (var ip in IPListBox.Items) { - Global.Settings.BypassIPs.Add(ip as String); + Global.Settings.BypassIPs.Add(ip as string); } Utils.Configuration.Save(); diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index f91872a4..44ab7287 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -1,5 +1,4 @@ -using Netch.Controllers; -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; @@ -9,6 +8,7 @@ using System.ServiceProcess; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; +using Netch.Controllers; namespace Netch.Forms { @@ -27,12 +27,12 @@ namespace Netch.Forms /// /// 上一次上传的流量 /// - public long LastUploadBandwidth = 0; + public long LastUploadBandwidth; /// /// 上一次下载的流量 /// - public long LastDownloadBandwidth = 0; + public long LastDownloadBandwidth; /// /// 是否第一次打开 @@ -142,7 +142,7 @@ namespace Netch.Forms if (splited.Length >= 2) { - if (int.TryParse(splited[1], out int result)) + if (int.TryParse(splited[1], out var result)) { mode.Type = result; } @@ -155,9 +155,9 @@ namespace Netch.Forms if (splited.Length >= 3) { - if (int.TryParse(splited[2], out int result)) + if (int.TryParse(splited[2], out var result)) { - mode.BypassChina = (result == 1); + mode.BypassChina = result == 1; } else { @@ -168,7 +168,7 @@ namespace Netch.Forms } else { - if (!text.StartsWith("#") && !String.IsNullOrWhiteSpace(text)) + if (!text.StartsWith("#") && !string.IsNullOrWhiteSpace(text)) { mode.Rule.Add(text.Trim()); } @@ -186,7 +186,7 @@ namespace Netch.Forms } var array = Global.ModeFiles.ToArray(); - Array.Sort(array, (a, b) => String.Compare(a.Remark, b.Remark, StringComparison.Ordinal)); + Array.Sort(array, (a, b) => string.Compare(a.Remark, b.Remark, StringComparison.Ordinal)); ModeComboBox.Items.AddRange(array); @@ -199,7 +199,7 @@ namespace Netch.Forms ModeComboBox.Items.Clear(); Global.ModeFiles.Add(mode); var array = Global.ModeFiles.ToArray(); - Array.Sort(array, (a, b) => String.Compare(a.Remark, b.Remark, StringComparison.Ordinal)); + Array.Sort(array, (a, b) => string.Compare(a.Remark, b.Remark, StringComparison.Ordinal)); ModeComboBox.Items.AddRange(array); SelectLastMode(); @@ -389,7 +389,7 @@ namespace Netch.Forms private void ImportServersFromClipboardToolStripMenuItem_Click(object sender, EventArgs e) { var texts = Clipboard.GetText(); - if (!String.IsNullOrWhiteSpace(texts)) + if (!string.IsNullOrWhiteSpace(texts)) { var result = Utils.ShareLink.Parse(texts); @@ -409,37 +409,37 @@ namespace Netch.Forms private void AddSocks5ServerToolStripMenuItem_Click(object sender, EventArgs e) { - (new Server.Socks5()).Show(); + new Server.Socks5().Show(); Hide(); } private void AddShadowsocksServerToolStripMenuItem_Click(object sender, EventArgs e) { - (new Server.Shadowsocks()).Show(); + new Server.Shadowsocks().Show(); Hide(); } private void AddShadowsocksRServerToolStripMenuItem_Click(object sender, EventArgs e) { - (new Server.ShadowsocksR()).Show(); + new Server.ShadowsocksR().Show(); Hide(); } private void AddVMessServerToolStripMenuItem_Click(object sender, EventArgs e) { - (new Server.VMess()).Show(); + new Server.VMess().Show(); Hide(); } private void CreateProcessModeToolStripButton_Click(object sender, EventArgs e) { - (new Mode.Process()).Show(); + new Mode.Process().Show(); Hide(); } private void ManageSubscribeLinksToolStripMenuItem_Click(object sender, EventArgs e) { - (new SubscribeForm()).Show(); + new SubscribeForm().Show(); Hide(); } @@ -466,7 +466,7 @@ namespace Netch.Forms { if (Global.Settings.UseProxyToUpdateSubscription) { - var mode = new Models.Mode() + var mode = new Models.Mode { Remark = "ProxyUpdate", Type = 5 @@ -479,7 +479,7 @@ namespace Netch.Forms using var client = new Override.WebClient(); try { - if (!String.IsNullOrEmpty(item.UserAgent)) + if (!string.IsNullOrEmpty(item.UserAgent)) { client.Headers.Add("User-Agent", item.UserAgent); } @@ -529,7 +529,6 @@ namespace Netch.Forms } catch (Exception) { - continue; } } @@ -628,7 +627,7 @@ namespace Netch.Forms } catch (Exception ex) { - MessageBox.Show(this, Utils.i18N.Translate("Error") + Utils.i18N.Translate(": ") + ex.ToString(), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show(this, Utils.i18N.Translate("Error") + Utils.i18N.Translate(": ") + ex, Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); } } else @@ -680,16 +679,16 @@ namespace Netch.Forms switch (Global.Settings.Server[ServerComboBox.SelectedIndex].Type) { case "Socks5": - (new Server.Socks5(ServerComboBox.SelectedIndex)).Show(); + new Server.Socks5(ServerComboBox.SelectedIndex).Show(); break; case "SS": - (new Server.Shadowsocks(ServerComboBox.SelectedIndex)).Show(); + new Server.Shadowsocks(ServerComboBox.SelectedIndex).Show(); break; case "SSR": - (new Server.ShadowsocksR(ServerComboBox.SelectedIndex)).Show(); + new Server.ShadowsocksR(ServerComboBox.SelectedIndex).Show(); break; case "VMess": - (new Server.VMess(ServerComboBox.SelectedIndex)).Show(); + new Server.VMess(ServerComboBox.SelectedIndex).Show(); break; default: return; @@ -715,7 +714,7 @@ namespace Netch.Forms if (ServerComboBox.Items.Count > 0) { - ServerComboBox.SelectedIndex = (index != 0) ? index - 1 : index; + ServerComboBox.SelectedIndex = index != 0 ? index - 1 : index; } Utils.Configuration.Save(); } @@ -898,10 +897,8 @@ namespace Netch.Forms return; } // 否则直接调用停止按钮的方法 - else - { - ControlButton_Click(sender, e); - } + + ControlButton_Click(sender, e); } SaveConfigs(); @@ -933,7 +930,7 @@ namespace Netch.Forms private void AboutToolStripButton_Click(object sender, EventArgs e) { - (new AboutForm()).Show(); + new AboutForm().Show(); Hide(); } @@ -951,21 +948,19 @@ namespace Netch.Forms private void ProfileButton_Click(object sender, EventArgs e) { - int index = ProfileButtons.IndexOf((Button)sender); + var index = ProfileButtons.IndexOf((Button)sender); //Utils.Logging.Info(String.Format("Button no.{0} clicked", index)); - if (Control.ModifierKeys == Keys.Control) + if (ModifierKeys == Keys.Control) { if (ServerComboBox.SelectedIndex == -1) { MessageBox.Show(Utils.i18N.Translate("Please select a server first"), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); - return; } else if (ModeComboBox.SelectedIndex == -1) { MessageBox.Show(Utils.i18N.Translate("Please select an mode first"), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); - return; } else { @@ -980,7 +975,7 @@ namespace Netch.Forms ProfileNameText.Text = LoadProfile(index); // start the profile - bool need2ndStart = true; + var need2ndStart = true; if (State == Models.State.Waiting || State == Models.State.Stopped) { need2ndStart = false; @@ -1019,7 +1014,7 @@ namespace Netch.Forms public void InitProfile() { - int num_profile = 4; + var num_profile = 4; ProfileTable.ColumnCount = num_profile; while (Global.Settings.profiles.Count < num_profile) @@ -1028,12 +1023,12 @@ namespace Netch.Forms } // buttons - for (int i = 0; i < num_profile; ++i) + for (var i = 0; i < num_profile; ++i) { var b = new Button(); ProfileTable.Controls.Add(b, i, 0); b.Location = new Point(i * 100, 0); - b.Click += new EventHandler(ProfileButton_Click); + b.Click += ProfileButton_Click; b.Dock = DockStyle.Fill; b.Text = "None"; ProfileButtons.Add(b); @@ -1049,25 +1044,25 @@ namespace Netch.Forms } // equal column - this.ProfileTable.ColumnStyles.Clear(); - for (int i = 1; i <= this.ProfileTable.RowCount; i++) + ProfileTable.ColumnStyles.Clear(); + for (var i = 1; i <= ProfileTable.RowCount; i++) { ProfileTable.RowStyles.Add(new RowStyle(SizeType.Percent, 1)); } - for (int i = 1; i <= this.ProfileTable.ColumnCount; i++) + for (var i = 1; i <= ProfileTable.ColumnCount; i++) { ProfileTable.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 1)); } } - private String LoadProfile(int index) + private string LoadProfile(int index) { - Models.Profile p = Global.Settings.profiles[index]; + var p = Global.Settings.profiles[index]; if (p.IsDummy) throw new Exception("Profile not found."); - bool result = false; + var result = false; foreach (Models.Server server in ServerComboBox.Items) { @@ -1103,7 +1098,7 @@ namespace Netch.Forms { var selectedServer = (Models.Server)ServerComboBox.SelectedItem; var selectedMode = (Models.Mode)ModeComboBox.SelectedItem; - String name = ProfileNameText.Text; + var name = ProfileNameText.Text; Global.Settings.profiles[index] = new Models.Profile(selectedServer, selectedMode, name); diff --git a/Netch/Forms/Mode/Process.cs b/Netch/Forms/Mode/Process.cs index 5d117737..96a18b2e 100644 --- a/Netch/Forms/Mode/Process.cs +++ b/Netch/Forms/Mode/Process.cs @@ -17,11 +17,11 @@ namespace Netch.Forms.Mode /// 扫描目录 /// /// 路径 - public void ScanDirectory(String DirName) + public void ScanDirectory(string DirName) { try { - DirectoryInfo RDirInfo = new DirectoryInfo(DirName); + var RDirInfo = new DirectoryInfo(DirName); if (!RDirInfo.Exists) { return; @@ -32,17 +32,17 @@ namespace Netch.Forms.Mode return; } - System.Collections.Generic.Stack DirStack = new System.Collections.Generic.Stack(); + var DirStack = new System.Collections.Generic.Stack(); DirStack.Push(DirName); while (DirStack.Count > 0) { - DirectoryInfo DirInfo = new DirectoryInfo(DirStack.Pop()); - foreach (DirectoryInfo DirChildInfo in DirInfo.GetDirectories()) + var DirInfo = new DirectoryInfo(DirStack.Pop()); + foreach (var DirChildInfo in DirInfo.GetDirectories()) { DirStack.Push(DirChildInfo.FullName); } - foreach (FileInfo FileChildInfo in DirInfo.GetFiles()) + foreach (var FileChildInfo in DirInfo.GetFiles()) { if (FileChildInfo.Name.EndsWith(".exe") && !RuleListBox.Items.Contains(FileChildInfo.Name)) { @@ -92,7 +92,7 @@ namespace Netch.Forms.Mode private void AddButton_Click(object sender, EventArgs e) { - if (!String.IsNullOrWhiteSpace(ProcessNameTextBox.Text)) + if (!string.IsNullOrWhiteSpace(ProcessNameTextBox.Text)) { var process = ProcessNameTextBox.Text; if (!process.EndsWith(".exe")) @@ -105,7 +105,7 @@ namespace Netch.Forms.Mode RuleListBox.Items.Add(process); } - ProcessNameTextBox.Text = String.Empty; + ProcessNameTextBox.Text = string.Empty; } else { @@ -146,7 +146,7 @@ namespace Netch.Forms.Mode Utils.Configuration.Save(); - if (!String.IsNullOrWhiteSpace(RemarkTextBox.Text)) + if (!string.IsNullOrWhiteSpace(RemarkTextBox.Text)) { var ModeFilename = Path.Combine("mode", FilenameTextBox.Text); @@ -159,7 +159,7 @@ namespace Netch.Forms.Mode if (RuleListBox.Items.Count != 0) { - var mode = new Models.Mode() + var mode = new Models.Mode { BypassChina = false, FileName = ModeFilename, @@ -170,7 +170,7 @@ namespace Netch.Forms.Mode var text = $"# {RemarkTextBox.Text}, 0\r\n"; foreach (var item in RuleListBox.Items) { - var process = item as String; + var process = item as string; mode.Rule.Add(process); text += process + "\r\n"; } diff --git a/Netch/Forms/Server/Shadowsocks.cs b/Netch/Forms/Server/Shadowsocks.cs index b7a6d7cd..7d99b78e 100644 --- a/Netch/Forms/Server/Shadowsocks.cs +++ b/Netch/Forms/Server/Shadowsocks.cs @@ -90,7 +90,7 @@ namespace Netch.Forms.Server } if (Index == -1) { - Global.Settings.Server.Add(new Models.Server() + Global.Settings.Server.Add(new Models.Server { Remark = RemarkTextBox.Text, Type = "SS", @@ -104,7 +104,7 @@ namespace Netch.Forms.Server } else { - Global.Settings.Server[Index] = new Models.Server() + Global.Settings.Server[Index] = new Models.Server { Remark = RemarkTextBox.Text, Group = Global.Settings.Server[Index].Group, diff --git a/Netch/Forms/Server/ShadowsocksR.cs b/Netch/Forms/Server/ShadowsocksR.cs index a8344a85..472bad9f 100644 --- a/Netch/Forms/Server/ShadowsocksR.cs +++ b/Netch/Forms/Server/ShadowsocksR.cs @@ -106,7 +106,7 @@ namespace Netch.Forms.Server } if (Index == -1) { - Global.Settings.Server.Add(new Models.Server() + Global.Settings.Server.Add(new Models.Server { Remark = RemarkTextBox.Text, Type = "SSR", @@ -122,7 +122,7 @@ namespace Netch.Forms.Server } else { - Global.Settings.Server[Index] = new Models.Server() + Global.Settings.Server[Index] = new Models.Server { Remark = RemarkTextBox.Text, Group = Global.Settings.Server[Index].Group, diff --git a/Netch/Forms/Server/Socks5.cs b/Netch/Forms/Server/Socks5.cs index a1489ee4..6cfb5188 100644 --- a/Netch/Forms/Server/Socks5.cs +++ b/Netch/Forms/Server/Socks5.cs @@ -77,7 +77,7 @@ namespace Netch.Forms.Server } if (Index == -1) { - Global.Settings.Server.Add(new Models.Server() + Global.Settings.Server.Add(new Models.Server { Remark = RemarkTextBox.Text, Type = "Socks5", @@ -89,7 +89,7 @@ namespace Netch.Forms.Server } else { - Global.Settings.Server[Index] = new Models.Server() + Global.Settings.Server[Index] = new Models.Server { Remark = RemarkTextBox.Text, Group = Global.Settings.Server[Index].Group, diff --git a/Netch/Forms/Server/Vmess.cs b/Netch/Forms/Server/Vmess.cs index aba31b58..3fd99246 100644 --- a/Netch/Forms/Server/Vmess.cs +++ b/Netch/Forms/Server/Vmess.cs @@ -118,7 +118,7 @@ namespace Netch.Forms.Server } if (Index == -1) { - Global.Settings.Server.Add(new Models.Server() + Global.Settings.Server.Add(new Models.Server { Remark = RemarkTextBox.Text, Type = "VMess", @@ -139,7 +139,7 @@ namespace Netch.Forms.Server } else { - Global.Settings.Server[Index] = new Models.Server() + Global.Settings.Server[Index] = new Models.Server { Remark = RemarkTextBox.Text, Type = "VMess", diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index ed49a45a..8f2571ca 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -133,7 +133,7 @@ namespace Netch.Forms try { - var Socks5Port = Int32.Parse(Socks5PortTextBox.Text); + var Socks5Port = int.Parse(Socks5PortTextBox.Text); if (Socks5Port > 0 && Socks5Port < 65536) { @@ -154,7 +154,7 @@ namespace Netch.Forms try { - var HTTPPort = Int32.Parse(HTTPPortTextBox.Text); + var HTTPPort = int.Parse(HTTPPortTextBox.Text); if (HTTPPort > 0 && HTTPPort < 65536) { @@ -175,7 +175,7 @@ namespace Netch.Forms try { - var RedirectorPort = Int32.Parse(RedirectorTextBox.Text); + var RedirectorPort = int.Parse(RedirectorTextBox.Text); if (RedirectorPort > 0 && RedirectorPort < 65536) { @@ -226,7 +226,7 @@ namespace Netch.Forms var DNS = ""; foreach (var ip in Global.Settings.TUNTAP.DNS) { - DNS += ip.ToString(); + DNS += ip; DNS += ','; } DNS = DNS.Trim(); diff --git a/Netch/Forms/SubscribeForm.cs b/Netch/Forms/SubscribeForm.cs index 81e723c1..a6daefc3 100644 --- a/Netch/Forms/SubscribeForm.cs +++ b/Netch/Forms/SubscribeForm.cs @@ -17,16 +17,16 @@ namespace Netch.Forms foreach (var item in Global.Settings.SubscribeLink) { - if (!String.IsNullOrEmpty(item.UserAgent)) + if (!string.IsNullOrEmpty(item.UserAgent)) { - SubscribeLinkListView.Items.Add(new ListViewItem(new String[] { + SubscribeLinkListView.Items.Add(new ListViewItem(new[] { item.Remark, item.Link, item.UserAgent})); } else { - SubscribeLinkListView.Items.Add(new ListViewItem(new String[] { + SubscribeLinkListView.Items.Add(new ListViewItem(new[] { item.Remark, item.Link, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"})); @@ -63,7 +63,7 @@ namespace Netch.Forms { if (SubscribeLinkListView.SelectedItems.Count > 0) { - for (int i = SubscribeLinkListView.SelectedItems.Count - 1; i >= 0; i--) + for (var i = SubscribeLinkListView.SelectedItems.Count - 1; i >= 0; i--) { var item = SubscribeLinkListView.SelectedItems[i]; var link = Global.Settings.SubscribeLink[item.Index]; @@ -89,21 +89,21 @@ namespace Netch.Forms private void AddButton_Click(object sender, EventArgs e) { - if (!String.IsNullOrWhiteSpace(RemarkTextBox.Text)) + if (!string.IsNullOrWhiteSpace(RemarkTextBox.Text)) { - if (!String.IsNullOrWhiteSpace(LinkTextBox.Text)) + if (!string.IsNullOrWhiteSpace(LinkTextBox.Text)) { if (LinkTextBox.Text.StartsWith("HTTP://", StringComparison.OrdinalIgnoreCase) || LinkTextBox.Text.StartsWith("HTTPS://", StringComparison.OrdinalIgnoreCase)) { - Global.Settings.SubscribeLink.Add(new Models.SubscribeLink() + Global.Settings.SubscribeLink.Add(new Models.SubscribeLink { Remark = RemarkTextBox.Text, Link = LinkTextBox.Text, UserAgent = UserAgentTextBox.Text }); - RemarkTextBox.Text = String.Empty; - LinkTextBox.Text = String.Empty; + RemarkTextBox.Text = string.Empty; + LinkTextBox.Text = string.Empty; UserAgentTextBox.Text = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"; InitSubscribeLink(); diff --git a/Netch/Global.cs b/Netch/Global.cs index 4bda41df..56915e58 100644 --- a/Netch/Global.cs +++ b/Netch/Global.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Net; using System.Net.NetworkInformation; @@ -25,7 +24,7 @@ namespace Netch /// /// SS 加密列表 /// - public static List SS = new List() + public static List SS = new List { "rc4-md5", "aes-128-gcm", @@ -51,7 +50,7 @@ namespace Netch /// /// SSR 加密列表 /// - public static List SSR = new List() + public static List SSR = new List { "none", "table", @@ -79,7 +78,7 @@ namespace Netch /// /// VMess 解密列表 /// - public static List VMess = new List() + public static List VMess = new List { "auto", "none", @@ -90,7 +89,7 @@ namespace Netch /// /// VMess QUIC 加密列表 /// - public static List VMessQUIC = new List() + public static List VMessQUIC = new List { "none", "aes-128-gcm", @@ -101,7 +100,7 @@ namespace Netch /// /// SSR 协议列表 /// - public static List Protocols = new List() + public static List Protocols = new List { "origin", "verify_deflate", @@ -114,7 +113,7 @@ namespace Netch /// /// SSR 混淆列表 /// - public static List OBFSs = new List() + public static List OBFSs = new List { "plain", "http_simple", @@ -125,7 +124,7 @@ namespace Netch /// /// V2Ray 传输协议 /// - public static List TransferProtocols = new List() + public static List TransferProtocols = new List { "tcp", "kcp", @@ -137,7 +136,7 @@ namespace Netch /// /// V2Ray 伪装类型 /// - public static List FakeTypes = new List() + public static List FakeTypes = new List { "none", "http", @@ -187,7 +186,7 @@ namespace Netch /// /// 组件 ID /// - public static string ComponentID = String.Empty; + public static string ComponentID = string.Empty; } /// diff --git a/Netch/Models/LegacyServer.cs b/Netch/Models/LegacyServer.cs index e37fc00e..87bebc8b 100644 --- a/Netch/Models/LegacyServer.cs +++ b/Netch/Models/LegacyServer.cs @@ -1,6 +1,4 @@ -using System; - -namespace Netch.Models +namespace Netch.Models { public class LegacyServer { @@ -42,7 +40,7 @@ namespace Netch.Models /// /// 用户 ID(V2) /// - public string UserID = String.Empty; + public string UserID = string.Empty; /// /// 额外 ID(V2) @@ -82,17 +80,17 @@ namespace Netch.Models /// /// 伪装类型(V2) /// - public string FakeType = String.Empty; + public string FakeType = string.Empty; /// /// 伪装域名(V2:HTTP、WebSocket、HTTP/2) /// - public string Host = String.Empty; + public string Host = string.Empty; /// /// 传输路径(V2:WebSocket、HTTP/2) /// - public string Path = String.Empty; + public string Path = string.Empty; /// /// QUIC 加密方式(V2) @@ -102,7 +100,7 @@ namespace Netch.Models /// /// QUIC 加密密钥(V2) /// - public string QUICSecret = String.Empty; + public string QUICSecret = string.Empty; /// /// TLS 底层传输安全(V2) diff --git a/Netch/Models/LegacySetting.cs b/Netch/Models/LegacySetting.cs index 53ef7149..d99b6b9d 100644 --- a/Netch/Models/LegacySetting.cs +++ b/Netch/Models/LegacySetting.cs @@ -45,12 +45,12 @@ namespace Netch.Models /// /// 服务器列表 /// - public List Server = new List(); + public List Server = new List(); /// /// 订阅链接列表 /// - public List SubscribeLink = new List(); + public List SubscribeLink = new List(); /// /// 全局绕过 IP 列表 diff --git a/Netch/Models/Mode.cs b/Netch/Models/Mode.cs index aab586d3..0b7ed6cc 100644 --- a/Netch/Models/Mode.cs +++ b/Netch/Models/Mode.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; namespace Netch.Models { @@ -42,7 +41,7 @@ namespace Netch.Models /// 备注 public override string ToString() { - return String.Format("[{0}] {1}", Type + 1, Remark); + return string.Format("[{0}] {1}", Type + 1, Remark); } /// @@ -74,7 +73,7 @@ namespace Netch.Models FileString = $"# {Remark}, {Type}, {(BypassChina ? 1 : 0)}\r\n"; } - foreach (String item in Rule) + foreach (var item in Rule) { FileString = $"{FileString}{item}\r\n"; } diff --git a/Netch/Models/Profile.cs b/Netch/Models/Profile.cs index a19a0b84..156d118c 100644 --- a/Netch/Models/Profile.cs +++ b/Netch/Models/Profile.cs @@ -1,16 +1,14 @@ -using System; - -namespace Netch.Models +namespace Netch.Models { public class Profile { - public String ServerRemark; - public String ModeRemark; - public String ProfileName; + public string ServerRemark; + public string ModeRemark; + public string ProfileName; public bool IsDummy = true; - public Profile(Server server, Mode mode, String name) + public Profile(Server server, Mode mode, string name) { ServerRemark = server.Remark; ModeRemark = mode.Remark; diff --git a/Netch/Models/Server.cs b/Netch/Models/Server.cs index 7d952725..b60deb88 100644 --- a/Netch/Models/Server.cs +++ b/Netch/Models/Server.cs @@ -51,7 +51,7 @@ namespace Netch.Models /// /// 用户 ID(VMess) /// - public string UserID = String.Empty; + public string UserID = string.Empty; /// /// 额外 ID(VMess) @@ -101,17 +101,17 @@ namespace Netch.Models /// /// 伪装类型(VMess) /// - public string FakeType = String.Empty; + public string FakeType = string.Empty; /// /// 伪装域名(VMess:HTTP、WebSocket、HTTP/2) /// - public string Host = String.Empty; + public string Host = string.Empty; /// /// 传输路径(VMess:WebSocket、HTTP/2) /// - public string Path = String.Empty; + public string Path = string.Empty; /// /// QUIC 加密方式(VMess) @@ -121,7 +121,7 @@ namespace Netch.Models /// /// QUIC 加密密钥(VMess) /// - public string QUICSecret = String.Empty; + public string QUICSecret = string.Empty; /// /// TLS 底层传输安全(VMess) @@ -144,7 +144,7 @@ namespace Netch.Models /// 备注 public override string ToString() { - if (String.IsNullOrWhiteSpace(Remark)) + if (string.IsNullOrWhiteSpace(Remark)) { Remark = $"{Hostname}:{Port}"; } @@ -179,9 +179,9 @@ namespace Netch.Models } var list = new Task[3]; - for (int i = 0; i < 3; i++) + for (var i = 0; i < 3; i++) { - list[i] = Task.Run(() => + list[i] = Task.Run(() => { try { @@ -190,7 +190,7 @@ namespace Netch.Models var watch = new Stopwatch(); watch.Start(); - var task = client.BeginConnect(new IPEndPoint(destination, Port), (result) => + var task = client.BeginConnect(new IPEndPoint(destination, Port), result => { watch.Stop(); }, 0); diff --git a/Netch/Models/Setting.cs b/Netch/Models/Setting.cs index 40a6b3d7..e9da97a8 100644 --- a/Netch/Models/Setting.cs +++ b/Netch/Models/Setting.cs @@ -107,12 +107,12 @@ namespace Netch.Models /// /// 订阅链接列表 /// - public List SubscribeLink = new List(); + public List SubscribeLink = new List(); /// /// 服务器列表 /// - public List Server = new List(); + public List Server = new List(); /// /// 全局绕过 IP 列表 diff --git a/Netch/Netch.cs b/Netch/Netch.cs index 28bde44a..097f643f 100644 --- a/Netch/Netch.cs +++ b/Netch/Netch.cs @@ -28,7 +28,7 @@ namespace Netch } // 预创建目录 - var directories = new String[] { "mode", "data", "i18n", "logging" }; + var directories = new[] { "mode", "data", "i18n", "logging" }; foreach (var item in directories) { // 检查是否已经存在 diff --git a/Netch/Override/SearchComboBox.cs b/Netch/Override/SearchComboBox.cs index 267edca1..0e3d4ef0 100644 --- a/Netch/Override/SearchComboBox.cs +++ b/Netch/Override/SearchComboBox.cs @@ -1,5 +1,4 @@ -using System.Diagnostics; -using System.Linq; +using System.Linq; namespace System.Windows.Forms { diff --git a/Netch/Resolver.cs b/Netch/Resolver.cs index 1157b846..81f9a3ce 100644 --- a/Netch/Resolver.cs +++ b/Netch/Resolver.cs @@ -51,10 +51,6 @@ namespace Netch // 跳过 } } - else - { - // 跳过 - } } return Task.FromResult(response); diff --git a/Netch/Utils/Bandwidth.cs b/Netch/Utils/Bandwidth.cs index f5c46ba0..ba8c8e6a 100644 --- a/Netch/Utils/Bandwidth.cs +++ b/Netch/Utils/Bandwidth.cs @@ -18,7 +18,7 @@ i++; } while ((result /= 1024) > 1024); - return System.String.Format("{0} {1}", System.Math.Round(result, 2), units[i]); + return string.Format("{0} {1}", System.Math.Round(result, 2), units[i]); } } } diff --git a/Netch/Utils/Configuration.cs b/Netch/Utils/Configuration.cs index 11549bc1..2319e8d4 100644 --- a/Netch/Utils/Configuration.cs +++ b/Netch/Utils/Configuration.cs @@ -37,7 +37,7 @@ namespace Netch.Utils if (Global.Settings.Server[0].Hostname == null) { var LegacySettingConfig = Newtonsoft.Json.JsonConvert.DeserializeObject(File.ReadAllText(SETTINGS_JSON)); - for (int i = 0; i < LegacySettingConfig.Server.Count; i++) + for (var i = 0; i < LegacySettingConfig.Server.Count; i++) { Global.Settings.Server[i].Hostname = LegacySettingConfig.Server[i].Address; if (Global.Settings.Server[i].Type == "Shadowsocks") @@ -100,22 +100,22 @@ namespace Netch.Utils } else { - Logging.Info($"GetBestRoute 搜索失败"); + Logging.Info("GetBestRoute 搜索失败"); return false; } - foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces()) + foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces()) { - IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); - IPv4InterfaceProperties p = adapterProperties.GetIPv4Properties(); + var adapterProperties = adapter.GetIPProperties(); + var p = adapterProperties.GetIPv4Properties(); // 通过索引查找对应适配器的 IPv4 地址 if (p.Index == Global.Adapter.Index) { var AddressGot = false; - String AdapterIPs = ""; + var AdapterIPs = ""; - foreach (UnicastIPAddressInformation ip in adapterProperties.UnicastAddresses) + foreach (var ip in adapterProperties.UnicastAddresses) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { @@ -124,7 +124,7 @@ namespace Netch.Utils Logging.Info($"当前出口 IPv4 地址:{Global.Adapter.Address}"); break; } - AdapterIPs = $"{ip.Address.ToString()} | "; + AdapterIPs = $"{ip.Address} | "; } if (!AddressGot) @@ -134,7 +134,7 @@ namespace Netch.Utils AdapterIPs = AdapterIPs.Substring(0, AdapterIPs.Length - 3); Logging.Info($"所有出口地址:{AdapterIPs}"); } - Logging.Info($"出口无 IPv4 地址,当前只支持 IPv4 地址"); + Logging.Info("出口无 IPv4 地址,当前只支持 IPv4 地址"); return false; } break; @@ -143,7 +143,7 @@ namespace Netch.Utils // 搜索 TUN/TAP 适配器的索引 Global.TUNTAP.ComponentID = TUNTAP.GetComponentID(); - if (String.IsNullOrEmpty(Global.TUNTAP.ComponentID)) + if (string.IsNullOrEmpty(Global.TUNTAP.ComponentID)) { MessageBox.Show(i18N.Translate("Please install TAP-Windows and create an TUN/TAP adapter manually"), i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information); return false; diff --git a/Netch/Utils/Logging.cs b/Netch/Utils/Logging.cs index 144a2d62..f19d3887 100644 --- a/Netch/Utils/Logging.cs +++ b/Netch/Utils/Logging.cs @@ -16,7 +16,7 @@ namespace Netch.Utils /// 内容 public static void Info(string text) { - File.AppendAllText("logging\\application.log", String.Format("[{0}] {1}{2}", DateTime.Now, text, EOF)); + File.AppendAllText("logging\\application.log", string.Format("[{0}] {1}{2}", DateTime.Now, text, EOF)); } } } diff --git a/Netch/Utils/ShareLink.cs b/Netch/Utils/ShareLink.cs index 48d315b5..a796133a 100644 --- a/Netch/Utils/ShareLink.cs +++ b/Netch/Utils/ShareLink.cs @@ -1,13 +1,12 @@ -using Netch.Models; -using Netch.Models.SS; -using Newtonsoft.Json; -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Web; -using VMess = Netch.Models.VMess; +using Netch.Models; +using Netch.Models.SS; +using Newtonsoft.Json; namespace Netch.Utils { @@ -311,7 +310,7 @@ namespace Netch.Utils } else if (text.StartsWith("ssd://")) { - var json = Newtonsoft.Json.JsonConvert.DeserializeObject(URLSafeBase64Decode(text.Substring(6))); + var json = JsonConvert.DeserializeObject(URLSafeBase64Decode(text.Substring(6))); foreach (var server in json.servers) { @@ -320,11 +319,11 @@ namespace Netch.Utils data.Remark = server.remarks; data.Hostname = server.server; - data.Port = (server.port != 0) ? server.port : json.port; - data.Password = (server.password != null) ? server.password : json.password; - data.EncryptMethod = (server.encryption != null) ? server.encryption : json.encryption; - data.Plugin = (string.IsNullOrEmpty(json.plugin)) ? (string.IsNullOrEmpty(server.plugin) ? null : server.plugin) : json.plugin; - data.PluginOption = (string.IsNullOrEmpty(json.plugin_options)) ? (string.IsNullOrEmpty(server.plugin_options) ? null : server.plugin_options) : json.plugin_options; + data.Port = server.port != 0 ? server.port : json.port; + data.Password = server.password != null ? server.password : json.password; + data.EncryptMethod = server.encryption != null ? server.encryption : json.encryption; + data.Plugin = string.IsNullOrEmpty(json.plugin) ? string.IsNullOrEmpty(server.plugin) ? null : server.plugin : json.plugin; + data.PluginOption = string.IsNullOrEmpty(json.plugin_options) ? string.IsNullOrEmpty(server.plugin_options) ? null : server.plugin_options : json.plugin_options; if (Global.EncryptMethods.SS.Contains(data.EncryptMethod)) { @@ -489,7 +488,7 @@ namespace Netch.Utils data.Type = "VMess"; text = text.Substring(8); - var vmess = Newtonsoft.Json.JsonConvert.DeserializeObject(URLSafeBase64Decode(text)); + var vmess = JsonConvert.DeserializeObject(URLSafeBase64Decode(text)); data.Remark = vmess.ps; data.Hostname = vmess.add; @@ -527,11 +526,9 @@ namespace Netch.Utils Logging.Info(string.Format("不支持的 VMess QUIC 加密方式:{0}", vmess.host)); return null; } - else - { - data.QUICSecure = vmess.host; - data.QUICSecret = vmess.path; - } + + data.QUICSecure = vmess.host; + data.QUICSecret = vmess.path; } else @@ -568,7 +565,7 @@ namespace Netch.Utils else if (text.StartsWith("Netch://")) { text = text.Substring(8); - var NetchLink = Newtonsoft.Json.JsonConvert.DeserializeObject(URLSafeBase64Decode(text)); + var NetchLink = JsonConvert.DeserializeObject(URLSafeBase64Decode(text)); if (!string.IsNullOrEmpty(NetchLink.Hostname) || NetchLink.Port > 65536 || NetchLink.Port > 0) { return null; diff --git a/Netch/Win32Native.cs b/Netch/Win32Native.cs index 4338fc1b..3cd02fa7 100644 --- a/Netch/Win32Native.cs +++ b/Netch/Win32Native.cs @@ -15,7 +15,7 @@ namespace Netch Other = 1, Invalid = 2, Direct = 3, - Indirect = 4, + Indirect = 4 } public enum ForwardProtocol @@ -36,7 +36,7 @@ namespace Netch BGP = 14, // 0x0000000E NT_AUTOSTATIC = 10002, // 0x00002712 NT_STATIC = 10006, // 0x00002716 - NT_STATIC_NON_DOD = 10007, // 0x00002717 + NT_STATIC_NON_DOD = 10007 // 0x00002717 } public class RouteEntry @@ -60,188 +60,98 @@ namespace Netch public int Index { - get - { - return this._index; - } - set - { - this._index = value; - } + get => _index; + set => _index = value; } public IPAddress Destination { - get - { - return this._destination; - } - set - { - this._destination = value; - } + get => _destination; + set => _destination = value; } public IPAddress Mask { - get - { - return this._mask; - } - set - { - this._mask = value; - } + get => _mask; + set => _mask = value; } public int Policy { - get - { - return this._policy; - } - set - { - this._policy = value; - } + get => _policy; + set => _policy = value; } public IPAddress NextHop { - get - { - return this._nextHop; - } - set - { - this._nextHop = value; - } + get => _nextHop; + set => _nextHop = value; } - public NetworkInterface RelatedInterface - { - get - { - return this._interface; - } - } + public NetworkInterface RelatedInterface => _interface; public string InterfaceName { get { - if (this.RelatedInterface == null) + if (RelatedInterface == null) return string.Empty; - return this.RelatedInterface.Name; + return RelatedInterface.Name; } } public ForwardType ForwardType { - get - { - return this._type; - } - set - { - this._type = value; - } + get => _type; + set => _type = value; } public ForwardProtocol Protocol { - get - { - return this._protocol; - } - set - { - this._protocol = value; - } + get => _protocol; + set => _protocol = value; } public int Age { - get - { - return this._age; - } - set - { - this._age = value; - } + get => _age; + set => _age = value; } public int NextHopAS { - get - { - return this._nextHopAS; - } - set - { - this._nextHopAS = value; - } + get => _nextHopAS; + set => _nextHopAS = value; } public int Metric1 { - get - { - return this._metric1; - } - set - { - this._metric1 = value; - } + get => _metric1; + set => _metric1 = value; } public int Metric2 { - get - { - return this._metric2; - } - set - { - this._metric2 = value; - } + get => _metric2; + set => _metric2 = value; } public int Metric3 { - get - { - return this._metric3; - } - set - { - this._metric3 = value; - } + get => _metric3; + set => _metric3 = value; } public int Metric4 { - get - { - return this._metric4; - } - set - { - this._metric4 = value; - } + get => _metric4; + set => _metric4 = value; } public int Metric5 { - get - { - return this._metric5; - } - set - { - this._metric5 = value; - } + get => _metric5; + set => _metric5 = value; } public RouteEntry( @@ -261,21 +171,21 @@ namespace Netch int metric5, int idx) { - this._age = age; - this._policy = policy; - this._protocol = proto; - this._type = type; - this._destination = new IPAddress((long)destination); - this._mask = new IPAddress((long)mask); - this._nextHop = new IPAddress((long)nextHop); - this._nextHopAS = nextHopAS; - this._interface = intf; - this._metric1 = metric1; - this._metric2 = metric2; - this._metric3 = metric3; - this._metric4 = metric4; - this._metric5 = metric5; - this._index = idx; + _age = age; + _policy = policy; + _protocol = proto; + _type = type; + _destination = new IPAddress(destination); + _mask = new IPAddress(mask); + _nextHop = new IPAddress(nextHop); + _nextHopAS = nextHopAS; + _interface = intf; + _metric1 = metric1; + _metric2 = metric2; + _metric3 = metric3; + _metric4 = metric4; + _metric5 = metric5; + _index = idx; } } @@ -320,48 +230,48 @@ namespace Netch public IDictionary GetAdapters() { - return (IDictionary)this._adapters; + return _adapters; } public NetworkInterface GetAdapter(int interfaceIndex) { - NetworkInterface networkInterface = (NetworkInterface)null; - this._adapters.TryGetValue(interfaceIndex, out networkInterface); + NetworkInterface networkInterface = null; + _adapters.TryGetValue(interfaceIndex, out networkInterface); return networkInterface; } public int GetAdapterIndex(NetworkInterface networkInterface) { - return this._adapters.First>((Func, bool>)(a => a.Value == networkInterface)).Key; + return _adapters.First(a => a.Value == networkInterface).Key; } public AdaptersTable() { - IntPtr num1 = IntPtr.Zero; - int pdwSize = 0; - int num2 = 0; - num2 = Win32Native.GetIfTable(IntPtr.Zero, ref pdwSize, true); - NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); + var num1 = IntPtr.Zero; + var pdwSize = 0; + var num2 = 0; + num2 = GetIfTable(IntPtr.Zero, ref pdwSize, true); + var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); try { num1 = Marshal.AllocHGlobal(pdwSize); - if (Win32Native.GetIfTable(num1, ref pdwSize, true) != 0) + if (GetIfTable(num1, ref pdwSize, true) != 0) return; - int num3 = Marshal.ReadInt32(num1); - IntPtr ptr = new IntPtr(num1.ToInt32() + 4); - for (int index = 0; index < num3; ++index) + var num3 = Marshal.ReadInt32(num1); + var ptr = new IntPtr(num1.ToInt32() + 4); + for (var index = 0; index < num3; ++index) { - MIB_IFROW structure = (MIB_IFROW)Marshal.PtrToStructure(ptr, typeof(MIB_IFROW)); - MIB_IFROW pIfRow = new MIB_IFROW(); + var structure = (MIB_IFROW)Marshal.PtrToStructure(ptr, typeof(MIB_IFROW)); + var pIfRow = new MIB_IFROW(); pIfRow.dwIndex = structure.dwIndex; - if (Win32Native.GetIfEntry(ref pIfRow) == 0) + if (GetIfEntry(ref pIfRow) == 0) { - string str = Encoding.ASCII.GetString(structure.bDescr, 0, pIfRow.dwDescrLen - 1); - foreach (NetworkInterface networkInterface in networkInterfaces) + var str = Encoding.ASCII.GetString(structure.bDescr, 0, pIfRow.dwDescrLen - 1); + foreach (var networkInterface in networkInterfaces) { if (networkInterface.Description == str) { - this._adapters.Add(structure.dwIndex, networkInterface); + _adapters.Add(structure.dwIndex, networkInterface); break; } } @@ -399,7 +309,7 @@ namespace Netch public static implicit operator MIB_IPFORWARDROW(RouteEntry value) { - MIB_IPFORWARDROW mibIpforwardrow = new MIB_IPFORWARDROW(); + var mibIpforwardrow = new MIB_IPFORWARDROW(); mibIpforwardrow.dwForwardAge = value.Age; mibIpforwardrow.dwForwardDest = BitConverter.ToUInt32(value.Destination.GetAddressBytes(), 0); mibIpforwardrow.dwForwardMask = BitConverter.ToUInt32(value.Mask.GetAddressBytes(), 0); @@ -413,7 +323,7 @@ namespace Netch mibIpforwardrow.dwForwardPolicy = value.Policy; mibIpforwardrow.dwForwardProto = value.Protocol; mibIpforwardrow.dwForwardType = value.ForwardType; - AdaptersTable adaptersTable = new AdaptersTable(); + var adaptersTable = new AdaptersTable(); mibIpforwardrow.dwForwardIfIndex = adaptersTable.GetAdapterIndex(value.RelatedInterface); return mibIpforwardrow; }