From 5803b94ae99db764cc96ea5e004d698a25820045 Mon Sep 17 00:00:00 2001 From: Connection Refused Date: Fri, 2 Apr 2021 02:40:13 +0800 Subject: [PATCH] Update NativeMethods --- Netch/Controllers/Guard.cs | 6 +- Netch/Controllers/MainController.cs | 14 +- Netch/Controllers/NFController.cs | 12 +- Netch/Controllers/NTTController.cs | 4 +- Netch/Controllers/PcapController.cs | 10 +- Netch/Controllers/TUNController.cs | 42 +++-- Netch/Controllers/UpdateChecker.cs | 6 +- Netch/Forms/AboutForm.cs | 6 +- Netch/Forms/GlobalBypassIPForm.cs | 8 +- Netch/Forms/MainForm.cs | 124 +++++++------- Netch/Forms/MessageBoxX.cs | 34 ++-- Netch/Forms/Mode/Process.cs | 12 +- Netch/Forms/Mode/Route.cs | 14 +- Netch/Forms/ServerForm.cs | 18 +- Netch/Forms/SettingForm.cs | 22 +-- Netch/Forms/SubscribeForm.cs | 8 +- Netch/Global.cs | 8 +- Netch/Interops/NFAPI.cs | 7 +- Netch/Interops/RedirectorInterop.cs | 3 +- Netch/Interops/TUNInterop.cs | 1 - Netch/Models/IAdapter.cs | 2 +- Netch/Models/IServerUtil.cs | 4 +- Netch/Models/Loggers/ConsoleLogger.cs | 4 +- Netch/Models/Loggers/FileLogger.cs | 6 +- Netch/Models/Mode.cs | 4 +- Netch/Models/OutboundAdapter.cs | 5 +- Netch/Models/Server.cs | 4 +- Netch/Models/Setting.cs | 6 +- Netch/Models/StatusText.cs | 2 +- Netch/Models/TunAdapter.cs | 13 +- Netch/NativeMethods.cs | 65 +++---- Netch/Netch.cs | 10 +- Netch/Properties/AssemblyInfo.cs | 6 +- Netch/Servers/Shadowsocks/SSController.cs | 8 +- Netch/Servers/Shadowsocks/SSUtil.cs | 44 ++--- Netch/Servers/Shadowsocks/Shadowsocks.cs | 4 +- Netch/Servers/ShadowsocksR/SSRController.cs | 8 +- Netch/Servers/ShadowsocksR/SSRUtil.cs | 16 +- Netch/Servers/ShadowsocksR/ShadowsocksR.cs | 4 +- Netch/Servers/Socks5/S5Controller.cs | 2 +- Netch/Servers/Socks5/S5Util.cs | 14 +- Netch/Servers/Trojan/TrojanController.cs | 14 +- Netch/Servers/Trojan/TrojanUtil.cs | 14 +- Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs | 162 +++++++++--------- Netch/Servers/V2ray/V2rayController.cs | 8 +- Netch/Servers/V2ray/V2rayUtils.cs | 16 +- Netch/Servers/VLESS/VLESS.cs | 2 +- Netch/Servers/VLESS/VLESSForm/VLESSForm.cs | 6 +- Netch/Servers/VLESS/VLESSUtil.cs | 8 +- Netch/Servers/VMess/Form/VMessForm.cs | 8 +- Netch/Servers/VMess/VMess.cs | 2 +- Netch/Servers/VMess/VMessUtil.cs | 48 +++--- Netch/Updater/Updater.cs | 10 +- Netch/Utils/Bandwidth.cs | 16 +- Netch/Utils/Configuration.cs | 4 +- Netch/Utils/ModeHelper.cs | 20 +-- Netch/Utils/PortHelper.cs | 6 +- .../ServerConverterWithTypeDiscriminator.cs | 6 +- Netch/Utils/ServerHelper.cs | 8 +- Netch/Utils/ShareLink.cs | 12 +- Netch/Utils/StringExtension.cs | 2 +- Netch/Utils/Subscription.cs | 2 +- Netch/Utils/Utils.cs | 8 +- Netch/Utils/WebUtil.cs | 6 +- Netch/Utils/i18N.cs | 8 +- 65 files changed, 477 insertions(+), 509 deletions(-) diff --git a/Netch/Controllers/Guard.cs b/Netch/Controllers/Guard.cs index 10e1e39b..ee09e581 100644 --- a/Netch/Controllers/Guard.cs +++ b/Netch/Controllers/Guard.cs @@ -1,3 +1,5 @@ +using Netch.Models; +using Netch.Utils; using System; using System.Collections.Generic; using System.ComponentModel; @@ -7,15 +9,13 @@ using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; -using Netch.Models; -using Netch.Utils; using Timer = System.Timers.Timer; namespace Netch.Controllers { public abstract class Guard { - private readonly Timer _flushFileStreamTimer = new(300) {AutoReset = true}; + private readonly Timer _flushFileStreamTimer = new(300) { AutoReset = true }; private FileStream? _logFileStream; diff --git a/Netch/Controllers/MainController.cs b/Netch/Controllers/MainController.cs index 6160c1d6..f9c977b4 100644 --- a/Netch/Controllers/MainController.cs +++ b/Netch/Controllers/MainController.cs @@ -1,9 +1,9 @@ -using System; -using System.IO; -using System.Threading.Tasks; using Netch.Models; using Netch.Servers.Socks5; using Netch.Utils; +using System; +using System.IO; +using System.Threading.Tasks; namespace Netch.Controllers { @@ -58,13 +58,13 @@ namespace Netch.Controllers Server = server; Mode = mode; - // 刷新DNS缓存 - NativeMethods.FlushDNSResolverCache(); + // 刷新 DNS 缓存 + NativeMethods.RefreshDNSCache(); if (DnsUtils.Lookup(server.Hostname) == null) throw new MessageException(i18N.Translate("Lookup Server hostname failed")); - // 添加Netch到防火墙 + // 添加 Netch 到防火墙 Firewall.AddNetchFwRules(); try @@ -122,7 +122,7 @@ namespace Netch.Controllers ModeController = ModeHelper.GetModeControllerByType(mode.Type, out var port, out var portName); if (port != null) - TryReleaseTcpPort((ushort) port, portName); + TryReleaseTcpPort((ushort)port, portName); Global.MainForm.StatusText(i18N.TranslateFormat("Starting {0}", ModeController.Name)); diff --git a/Netch/Controllers/NFController.cs b/Netch/Controllers/NFController.cs index 0c8848ba..ee25ca06 100644 --- a/Netch/Controllers/NFController.cs +++ b/Netch/Controllers/NFController.cs @@ -1,13 +1,13 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.ServiceProcess; -using Netch.Interops; +using Netch.Interops; using Netch.Models; using Netch.Servers.Shadowsocks; using Netch.Servers.Socks5; using Netch.Utils; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.ServiceProcess; using static Netch.Interops.RedirectorInterop; namespace Netch.Controllers diff --git a/Netch/Controllers/NTTController.cs b/Netch/Controllers/NTTController.cs index 10021bbd..c556f098 100644 --- a/Netch/Controllers/NTTController.cs +++ b/Netch/Controllers/NTTController.cs @@ -1,8 +1,8 @@ -using System; +using Netch.Utils; +using System; using System.IO; using System.Linq; using System.Threading.Tasks; -using Netch.Utils; namespace Netch.Controllers { diff --git a/Netch/Controllers/PcapController.cs b/Netch/Controllers/PcapController.cs index 46d7f1cc..687d689d 100644 --- a/Netch/Controllers/PcapController.cs +++ b/Netch/Controllers/PcapController.cs @@ -1,13 +1,13 @@ -using System; +using Netch.Forms; +using Netch.Models; +using Netch.Servers.Socks5; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; -using Netch.Forms; -using Netch.Models; -using Netch.Servers.Socks5; namespace Netch.Controllers { @@ -17,7 +17,7 @@ namespace Netch.Controllers public override string MainFile { get; protected set; } = "pcap2socks.exe"; - protected override IEnumerable StartedKeywords { get; set; } = new[] {"└"}; + protected override IEnumerable StartedKeywords { get; set; } = new[] { "└" }; private readonly OutboundAdapter _outbound = new(); diff --git a/Netch/Controllers/TUNController.cs b/Netch/Controllers/TUNController.cs index e755a726..9f57ec5b 100644 --- a/Netch/Controllers/TUNController.cs +++ b/Netch/Controllers/TUNController.cs @@ -1,16 +1,14 @@ -using System; +using Netch.Models; +using Netch.Servers.Socks5; +using Netch.Utils; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Threading.Tasks; -using Netch.Models; -using Netch.Servers.Socks5; -using Netch.Utils; -using Vanara.PInvoke; using static Netch.Interops.TUNInterop; -using static Vanara.PInvoke.Ws2_32; namespace Netch.Controllers { @@ -91,9 +89,9 @@ namespace Netch.Controllers _tunAdapter = new TunAdapter(); - NativeMethods.CreateUnicastIP((int) AddressFamily.InterNetwork, + NativeMethods.CreateUnicastIP(AddressFamily.InterNetwork, Global.Settings.TUNTAP.Address, - Utils.Utils.SubnetToCidr(Global.Settings.TUNTAP.Netmask), + (byte)Utils.Utils.SubnetToCidr(Global.Settings.TUNTAP.Netmask), _tunAdapter.InterfaceIndex); SetupRouteTable(mode); @@ -234,19 +232,19 @@ namespace Netch.Controllers if (!TryParseIPNetwork(ipNetwork, out var ip, out var cidr)) return false; - IAdapter adapter = routeType switch - { - RouteType.Outbound => _outboundAdapter, - RouteType.TUNTAP => _tunAdapter, - _ => throw new ArgumentOutOfRangeException(nameof(routeType), routeType, null) - }; + IAdapter adapter = routeType switch + { + RouteType.Outbound => _outboundAdapter, + RouteType.TUNTAP => _tunAdapter, + _ => throw new ArgumentOutOfRangeException(nameof(routeType), routeType, null) + }; - List ipList = routeType switch - { - RouteType.Outbound => _directIPs, - RouteType.TUNTAP => _proxyIPs, - _ => throw new ArgumentOutOfRangeException(nameof(routeType), routeType, null) - }; + List ipList = routeType switch + { + RouteType.Outbound => _directIPs, + RouteType.TUNTAP => _proxyIPs, + _ => throw new ArgumentOutOfRangeException(nameof(routeType), routeType, null) + }; string gateway = adapter.Gateway.ToString(); var index = adapter.InterfaceIndex; @@ -257,13 +255,13 @@ namespace Netch.Controllers switch (action) { case Action.Create: - result = NativeMethods.CreateRoute((int) AddressFamily.InterNetwork, ip, cidr, gateway, index, metric); + result = NativeMethods.CreateRoute(AddressFamily.InterNetwork, ip, (byte)cidr, gateway, index, metric); if (result && record) ipList.Add(ipNetwork); break; case Action.Delete: - result = NativeMethods.DeleteRoute((int) AddressFamily.InterNetwork, ip, cidr, gateway, index, metric); + result = NativeMethods.DeleteRoute(AddressFamily.InterNetwork, ip, (byte)cidr, gateway, index, metric); break; default: throw new ArgumentOutOfRangeException(nameof(action), action, null); diff --git a/Netch/Controllers/UpdateChecker.cs b/Netch/Controllers/UpdateChecker.cs index ad9e8e9a..0b1afedf 100644 --- a/Netch/Controllers/UpdateChecker.cs +++ b/Netch/Controllers/UpdateChecker.cs @@ -1,4 +1,6 @@ -using System; +using Netch.Models.GitHubRelease; +using Netch.Utils; +using System; using System.Collections.Generic; using System.Linq; using System.Net; @@ -6,8 +8,6 @@ using System.Text; using System.Text.Json; using System.Text.RegularExpressions; using System.Threading.Tasks; -using Netch.Models.GitHubRelease; -using Netch.Utils; namespace Netch.Controllers { diff --git a/Netch/Forms/AboutForm.cs b/Netch/Forms/AboutForm.cs index a5a6d659..d15465f8 100644 --- a/Netch/Forms/AboutForm.cs +++ b/Netch/Forms/AboutForm.cs @@ -1,8 +1,8 @@ -using System; +using Netch.Properties; +using Netch.Utils; +using System; using System.Diagnostics; using System.Windows.Forms; -using Netch.Properties; -using Netch.Utils; namespace Netch.Forms { diff --git a/Netch/Forms/GlobalBypassIPForm.cs b/Netch/Forms/GlobalBypassIPForm.cs index eac41ef2..9bdc34e7 100644 --- a/Netch/Forms/GlobalBypassIPForm.cs +++ b/Netch/Forms/GlobalBypassIPForm.cs @@ -1,9 +1,9 @@ -using System; +using Netch.Properties; +using Netch.Utils; +using System; using System.Linq; using System.Net; using System.Windows.Forms; -using Netch.Properties; -using Netch.Utils; namespace Netch.Forms { @@ -54,7 +54,7 @@ namespace Netch.Forms { Global.Settings.TUNTAP.BypassIPs.Clear(); foreach (var ip in IPListBox.Items) - Global.Settings.TUNTAP.BypassIPs.Add((string) ip); + Global.Settings.TUNTAP.BypassIPs.Add((string)ip); Configuration.Save(); MessageBoxX.Show(i18N.Translate("Saved")); diff --git a/Netch/Forms/MainForm.cs b/Netch/Forms/MainForm.cs index 77397667..4268aebf 100644 --- a/Netch/Forms/MainForm.cs +++ b/Netch/Forms/MainForm.cs @@ -1,3 +1,9 @@ +using Microsoft.Win32; +using Netch.Controllers; +using Netch.Forms.Mode; +using Netch.Models; +using Netch.Properties; +using Netch.Utils; using System; using System.Collections.Generic; using System.ComponentModel; @@ -8,12 +14,6 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; -using Microsoft.Win32; -using Netch.Controllers; -using Netch.Forms.Mode; -using Netch.Models; -using Netch.Properties; -using Netch.Utils; namespace Netch.Forms { @@ -41,7 +41,7 @@ namespace Netch.Forms #region i18N Translations - _mainFormText.Add(UninstallServiceToolStripMenuItem.Name, new[] {"Uninstall {0}", "NF Service"}); + _mainFormText.Add(UninstallServiceToolStripMenuItem.Name, new[] { "Uninstall {0}", "NF Service" }); #endregion @@ -63,7 +63,7 @@ namespace Netch.Forms Text = i18N.TranslateFormat("Add [{0}] Server", fullName) }; - _mainFormText.Add(control.Name, new[] {"Add [{0}] Server", fullName}); + _mainFormText.Add(control.Name, new[] { "Add [{0}] Server", fullName }); control.Click += AddServerToolStripMenuItem_Click; ServerToolStripMenuItem.DropDownItems.Add(control); } @@ -184,7 +184,7 @@ namespace Netch.Forms return string.Empty; if (value is object[] values) - return i18N.TranslateFormat((string) values.First(), values.Skip(1).ToArray()); + return i18N.TranslateFormat((string)values.First(), values.Skip(1).ToArray()); return i18N.Translate(value); } @@ -228,7 +228,7 @@ namespace Netch.Forms throw new ArgumentNullException(nameof(sender)); // TODO get Util from Tag - var s = ((ToolStripMenuItem) sender).Text; + var s = ((ToolStripMenuItem)sender).Text; var start = s.IndexOf("[", StringComparison.Ordinal) + 1; var end = s.IndexOf("]", start, StringComparison.Ordinal); @@ -348,7 +348,7 @@ namespace Netch.Forms { await Task.Run(() => { - NativeMethods.FlushDNSResolverCache(); + NativeMethods.RefreshDNSCache(); DnsUtils.ClearCache(); }); @@ -704,7 +704,7 @@ namespace Netch.Forms { try { - Global.Settings.ModeComboBoxSelectedIndex = Global.Modes.IndexOf((Models.Mode) ModeComboBox.SelectedItem); + Global.Settings.ModeComboBoxSelectedIndex = Global.Modes.IndexOf((Models.Mode)ModeComboBox.SelectedItem); } catch { @@ -721,7 +721,7 @@ namespace Netch.Forms return; } - var mode = (Models.Mode) ModeComboBox.SelectedItem; + var mode = (Models.Mode)ModeComboBox.SelectedItem; if (ModifierKeys == Keys.Control) { Utils.Utils.Open(ModeHelper.GetFullPath(mode.RelativePath!)); @@ -756,7 +756,7 @@ namespace Netch.Forms return; } - ModeHelper.Delete((Models.Mode) ModeComboBox.SelectedItem); + ModeHelper.Delete((Models.Mode)ModeComboBox.SelectedItem); SelectLastMode(); } @@ -773,7 +773,7 @@ namespace Netch.Forms { // Clear foreach (var button in ProfileTable.Controls) - ((Button) button).Dispose(); + ((Button)button).Dispose(); ProfileTable.Controls.Clear(); ProfileTable.ColumnStyles.Clear(); @@ -799,7 +799,7 @@ namespace Netch.Forms var columnCount = Global.Settings.ProfileTableColumnCount; ProfileTable.ColumnCount = profileCount >= columnCount ? columnCount : profileCount; - ProfileTable.RowCount = (int) Math.Ceiling(profileCount / (float) columnCount); + ProfileTable.RowCount = (int)Math.Ceiling(profileCount / (float)columnCount); for (var i = 0; i < profileCount; ++i) { @@ -848,8 +848,8 @@ namespace Netch.Forms private Profile CreateProfileAtIndex(int index) { - var server = (Server) ServerComboBox.SelectedItem; - var mode = (Models.Mode) ModeComboBox.SelectedItem; + var server = (Server)ServerComboBox.SelectedItem; + var mode = (Models.Mode)ModeComboBox.SelectedItem; var name = ProfileNameText.Text; Profile? profile; @@ -866,8 +866,8 @@ namespace Netch.Forms if (sender == null) throw new ArgumentNullException(nameof(sender)); - var profileButton = (Button) sender; - var profile = (Profile?) profileButton.Tag; + var profileButton = (Button)sender; + var profile = (Profile?)profileButton.Tag; var index = ProfileTable.Controls.IndexOf(profileButton); switch (ModifierKeys) @@ -1106,14 +1106,14 @@ namespace Netch.Forms if (natType > 0 && natType < 5) { NatTypeStatusLightLabel.Visible = Flags.IsWindows10Upper; - var c = natType switch - { - 1 => Color.LimeGreen, - 2 => Color.Yellow, - 3 => Color.Red, - 4 => Color.Black, - _ => throw new ArgumentOutOfRangeException(nameof(natType), natType, null) - }; + var c = natType switch + { + 1 => Color.LimeGreen, + 2 => Color.Yellow, + 3 => Color.Red, + 4 => Color.Black, + _ => throw new ArgumentOutOfRangeException(nameof(natType), natType, null) + }; NatTypeStatusLightLabel.ForeColor = c; } @@ -1231,7 +1231,7 @@ namespace Netch.Forms Configuration.Save(); } - foreach (var file in new[] {"data\\last.json", "data\\privoxy.conf"}) + foreach (var file in new[] { "data\\last.json", "data\\privoxy.conf" }) if (File.Exists(file)) File.Delete(file); @@ -1400,39 +1400,39 @@ namespace Netch.Forms switch (cbx.Items[e.Index]) { - case Server item: - { - // 计算延迟底色 - var numBoxBackBrush = item.Delay switch {> 200 => Brushes.Red, > 80 => Brushes.Yellow, >= 0 => _greenBrush, _ => Brushes.Gray}; - - // 绘制延迟底色 - e.Graphics.FillRectangle(numBoxBackBrush, _numberBoxX, e.Bounds.Y, _numberBoxWidth, e.Bounds.Height); - - // 绘制延迟字符串 - TextRenderer.DrawText(e.Graphics, - item.Delay.ToString(), - cbx.Font, - new Point(_numberBoxX + _numberBoxWrap, e.Bounds.Y), - Color.Black, - TextFormatFlags.Left); - - break; - } - case Models.Mode item: - { - // 绘制 模式Box 底色 - e.Graphics.FillRectangle(Brushes.Gray, _numberBoxX, e.Bounds.Y, _numberBoxWidth, e.Bounds.Height); - - // 绘制 模式行数 字符串 - TextRenderer.DrawText(e.Graphics, - item.Rule.Count.ToString(), - cbx.Font, - new Point(_numberBoxX + _numberBoxWrap, e.Bounds.Y), - Color.Black, - TextFormatFlags.Left); - - break; - } + case Server item: + { + // 计算延迟底色 + var numBoxBackBrush = item.Delay switch { > 200 => Brushes.Red, > 80 => Brushes.Yellow, >= 0 => _greenBrush, _ => Brushes.Gray }; + + // 绘制延迟底色 + e.Graphics.FillRectangle(numBoxBackBrush, _numberBoxX, e.Bounds.Y, _numberBoxWidth, e.Bounds.Height); + + // 绘制延迟字符串 + TextRenderer.DrawText(e.Graphics, + item.Delay.ToString(), + cbx.Font, + new Point(_numberBoxX + _numberBoxWrap, e.Bounds.Y), + Color.Black, + TextFormatFlags.Left); + + break; + } + case Models.Mode item: + { + // 绘制 模式Box 底色 + e.Graphics.FillRectangle(Brushes.Gray, _numberBoxX, e.Bounds.Y, _numberBoxWidth, e.Bounds.Height); + + // 绘制 模式行数 字符串 + TextRenderer.DrawText(e.Graphics, + item.Rule.Count.ToString(), + cbx.Font, + new Point(_numberBoxX + _numberBoxWrap, e.Bounds.Y), + Color.Black, + TextFormatFlags.Left); + + break; + } } } diff --git a/Netch/Forms/MessageBoxX.cs b/Netch/Forms/MessageBoxX.cs index 312adb6e..c9fa8ebf 100644 --- a/Netch/Forms/MessageBoxX.cs +++ b/Netch/Forms/MessageBoxX.cs @@ -1,7 +1,7 @@ -using System; -using System.Windows.Forms; -using Netch.Models; +using Netch.Models; using Netch.Utils; +using System; +using System.Windows.Forms; namespace Netch.Forms { @@ -22,21 +22,21 @@ namespace Netch.Forms { MessageBoxIcon msgIcon; if (string.IsNullOrWhiteSpace(title)) - title = level switch - { - LogLevel.INFO => "Information", - LogLevel.WARNING => "Warning", - LogLevel.ERROR => "Error", - _ => throw new ArgumentOutOfRangeException(nameof(level), level, null) - }; + title = level switch + { + LogLevel.INFO => "Information", + LogLevel.WARNING => "Warning", + LogLevel.ERROR => "Error", + _ => throw new ArgumentOutOfRangeException(nameof(level), level, null) + }; - msgIcon = level switch - { - LogLevel.INFO => MessageBoxIcon.Information, - LogLevel.WARNING => MessageBoxIcon.Warning, - LogLevel.ERROR => MessageBoxIcon.Exclamation, - _ => throw new ArgumentOutOfRangeException(nameof(level), level, null) - }; + msgIcon = level switch + { + LogLevel.INFO => MessageBoxIcon.Information, + LogLevel.WARNING => MessageBoxIcon.Warning, + LogLevel.ERROR => MessageBoxIcon.Exclamation, + _ => throw new ArgumentOutOfRangeException(nameof(level), level, null) + }; return MessageBox.Show(owner, text, i18N.Translate(title), confirm ? MessageBoxButtons.OKCancel : MessageBoxButtons.OK, msgIcon); } diff --git a/Netch/Forms/Mode/Process.cs b/Netch/Forms/Mode/Process.cs index 520c037b..b2f45769 100644 --- a/Netch/Forms/Mode/Process.cs +++ b/Netch/Forms/Mode/Process.cs @@ -1,13 +1,13 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Windows.Forms; -using Microsoft.WindowsAPICodePack.Dialogs; +using Microsoft.WindowsAPICodePack.Dialogs; using Netch.Controllers; using Netch.Models; using Netch.Properties; using Netch.Utils; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Windows.Forms; namespace Netch.Forms.Mode { diff --git a/Netch/Forms/Mode/Route.cs b/Netch/Forms/Mode/Route.cs index 7a6dd087..b69a5391 100644 --- a/Netch/Forms/Mode/Route.cs +++ b/Netch/Forms/Mode/Route.cs @@ -1,15 +1,15 @@ -using System; -using System.IO; -using System.Windows.Forms; -using Netch.Models; +using Netch.Models; using Netch.Properties; using Netch.Utils; +using System; +using System.IO; +using System.Windows.Forms; namespace Netch.Forms.Mode { public partial class Route : Form { - private readonly TagItem[] _items = {new(1, "Proxy Rule IPs"), new(2, "Bypass Rule IPs")}; + private readonly TagItem[] _items = { new(1, "Proxy Rule IPs"), new(2, "Bypass Rule IPs") }; private readonly Models.Mode? _mode; @@ -62,7 +62,7 @@ namespace Netch.Forms.Mode _mode.Remark = RemarkTextBox.Text; _mode.Rule.Clear(); _mode.Rule.AddRange(richTextBox1.Lines); - _mode.Type = (int) comboBox1.SelectedValue; + _mode.Type = (int)comboBox1.SelectedValue; _mode.WriteFile(); MessageBoxX.Show(i18N.Translate("Mode updated successfully")); @@ -79,7 +79,7 @@ namespace Netch.Forms.Mode var mode = new Models.Mode(fullName) { - Type = (int) comboBox1.SelectedValue, + Type = (int)comboBox1.SelectedValue, Remark = RemarkTextBox.Text }; diff --git a/Netch/Forms/ServerForm.cs b/Netch/Forms/ServerForm.cs index ff452f48..0a0daa7e 100644 --- a/Netch/Forms/ServerForm.cs +++ b/Netch/Forms/ServerForm.cs @@ -1,13 +1,13 @@ #nullable disable +using Netch.Models; +using Netch.Properties; +using Netch.Utils; using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Windows.Forms; -using Netch.Models; -using Netch.Properties; -using Netch.Utils; namespace Netch.Forms { @@ -38,13 +38,13 @@ namespace Netch.Forms InitializeComponent(); _checkActions.Add(RemarkTextBox, s => true); - _saveActions.Add(RemarkTextBox, s => Server.Remark = (string) s); + _saveActions.Add(RemarkTextBox, s => Server.Remark = (string)s); _checkActions.Add(AddressTextBox, s => s != string.Empty); - _saveActions.Add(AddressTextBox, s => Server.Hostname = (string) s); + _saveActions.Add(AddressTextBox, s => Server.Hostname = (string)s); _checkActions.Add(PortTextBox, s => ushort.TryParse(s, out var port) && port != 0); - _saveActions.Add(PortTextBox, s => Server.Port = ushort.Parse((string) s)); + _saveActions.Add(PortTextBox, s => Server.Port = ushort.Parse((string)s)); } protected abstract string TypeName { get; } @@ -99,7 +99,7 @@ namespace Netch.Forms }; _checkActions.Add(textBox, check); - _saveActions.Add(textBox, o => save.Invoke((string) o)); + _saveActions.Add(textBox, o => save.Invoke((string)o)); ConfigurationGroupBox.Controls.AddRange(new Control[] { textBox, @@ -131,7 +131,7 @@ namespace Netch.Forms comboBox.Items.AddRange(values.ToArray()); comboBox.SelectedIndex = values.IndexOf(value); comboBox.DrawItem += Utils.Utils.DrawCenterComboBox; - _saveActions.Add(comboBox, o => save.Invoke((string) o)); + _saveActions.Add(comboBox, o => save.Invoke((string)o)); ConfigurationGroupBox.Controls.AddRange(new Control[] { comboBox, @@ -159,7 +159,7 @@ namespace Netch.Forms Text = remark }; - _saveActions.Add(checkBox, o => save.Invoke((bool) o)); + _saveActions.Add(checkBox, o => save.Invoke((bool)o)); ConfigurationGroupBox.Controls.AddRange(new Control[] { checkBox diff --git a/Netch/Forms/SettingForm.cs b/Netch/Forms/SettingForm.cs index 9e86c88e..ee46df37 100644 --- a/Netch/Forms/SettingForm.cs +++ b/Netch/Forms/SettingForm.cs @@ -1,3 +1,6 @@ +using Netch.Models; +using Netch.Properties; +using Netch.Utils; using System; using System.Collections.Generic; using System.Drawing; @@ -5,9 +8,6 @@ using System.IO; using System.Linq; using System.Net; using System.Windows.Forms; -using Netch.Models; -using Netch.Properties; -using Netch.Utils; namespace Netch.Forms { @@ -37,7 +37,7 @@ namespace Netch.Forms BindCheckBox(AllowDevicesCheckBox, c => Global.Settings.LocalAddress = AllowDevicesCheckBox.Checked ? "0.0.0.0" : "127.0.0.1", - Global.Settings.LocalAddress switch {"127.0.0.1" => false, "0.0.0.0" => true, _ => false}); + Global.Settings.LocalAddress switch { "127.0.0.1" => false, "0.0.0.0" => true, _ => false }); BindCheckBox(ResolveServerHostnameCheckBox, c => Global.Settings.ResolveServerHostname = c, Global.Settings.ResolveServerHostname); @@ -116,7 +116,7 @@ namespace Netch.Forms Global.Settings.Redirector.ChildProcessHandle); BindListComboBox(ProcessProxyProtocolComboBox, - s => Global.Settings.Redirector.ProxyProtocol = (PortType) Enum.Parse(typeof(PortType), s.ToString(), false), + s => Global.Settings.Redirector.ProxyProtocol = (PortType)Enum.Parse(typeof(PortType), s.ToString(), false), Enum.GetNames(typeof(PortType)), Global.Settings.Redirector.ProxyProtocol.ToString()); @@ -284,7 +284,7 @@ namespace Netch.Forms { try { - return check.Invoke((T) Convert.ChangeType(s, typeof(T))); + return check.Invoke((T)Convert.ChangeType(s, typeof(T))); } catch { @@ -292,19 +292,19 @@ namespace Netch.Forms } }); - _saveActions.Add(control, c => save.Invoke((T) Convert.ChangeType(((TextBox) c).Text, typeof(T)))); + _saveActions.Add(control, c => save.Invoke((T)Convert.ChangeType(((TextBox)c).Text, typeof(T)))); } private void BindCheckBox(CheckBox control, Action save, bool value) { control.Checked = value; - _saveActions.Add(control, c => save.Invoke(((CheckBox) c).Checked)); + _saveActions.Add(control, c => save.Invoke(((CheckBox)c).Checked)); } private void BindRadioBox(RadioButton control, Action save, bool value) { control.Checked = value; - _saveActions.Add(control, c => save.Invoke(((RadioButton) c).Checked)); + _saveActions.Add(control, c => save.Invoke(((RadioButton)c).Checked)); } private void BindListComboBox(ComboBox comboBox, Action save, IEnumerable values, T value) where T : notnull @@ -318,7 +318,7 @@ namespace Netch.Forms comboBox.ValueMember = nameof(TagItem.Value); comboBox.DisplayMember = nameof(TagItem.Text); - _saveActions.Add(comboBox, c => save.Invoke(((TagItem) ((ComboBox) c).SelectedItem).Value)); + _saveActions.Add(comboBox, c => save.Invoke(((TagItem)((ComboBox)c).SelectedItem).Value)); Load += (_, _) => { comboBox.SelectedItem = tagItems.SingleOrDefault(t => t.Value.Equals(value)); }; } @@ -327,7 +327,7 @@ namespace Netch.Forms if (values != null) control.Items.AddRange(values); - _saveActions.Add(control, c => save.Invoke(((ComboBox) c).Text)); + _saveActions.Add(control, c => save.Invoke(((ComboBox)c).Text)); _checkActions.Add(control, check.Invoke); Load += (_, _) => { control.Text = value; }; diff --git a/Netch/Forms/SubscribeForm.cs b/Netch/Forms/SubscribeForm.cs index 54eaf1ce..1edbe8fb 100644 --- a/Netch/Forms/SubscribeForm.cs +++ b/Netch/Forms/SubscribeForm.cs @@ -1,9 +1,9 @@ -using System; -using System.Linq; -using System.Windows.Forms; -using Netch.Models; +using Netch.Models; using Netch.Properties; using Netch.Utils; +using System; +using System.Linq; +using System.Windows.Forms; namespace Netch.Forms { diff --git a/Netch/Global.cs b/Netch/Global.cs index 4fc22c72..32b17ee5 100644 --- a/Netch/Global.cs +++ b/Netch/Global.cs @@ -1,12 +1,12 @@ +using Netch.Forms; +using Netch.Interfaces; +using Netch.Models; +using Netch.Models.Loggers; using System; using System.Collections.Generic; using System.Text.Encodings.Web; using System.Text.Json; using System.Windows.Forms; -using Netch.Forms; -using Netch.Interfaces; -using Netch.Models; -using Netch.Models.Loggers; namespace Netch { diff --git a/Netch/Interops/NFAPI.cs b/Netch/Interops/NFAPI.cs index 65db5e40..d4a061c6 100644 --- a/Netch/Interops/NFAPI.cs +++ b/Netch/Interops/NFAPI.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; +using System.Runtime.InteropServices; namespace Netch.Interops { diff --git a/Netch/Interops/RedirectorInterop.cs b/Netch/Interops/RedirectorInterop.cs index 0394a5a4..65338dee 100644 --- a/Netch/Interops/RedirectorInterop.cs +++ b/Netch/Interops/RedirectorInterop.cs @@ -1,5 +1,4 @@ using System.Runtime.InteropServices; -using Netch.Utils; namespace Netch.Interops { @@ -57,7 +56,7 @@ namespace Netch.Interops return aio_free(); } - public const int UdpNameListOffset = (int) NameList.TYPE_UDPLISN - (int) NameList.TYPE_TCPLISN; + public const int UdpNameListOffset = (int)NameList.TYPE_UDPLISN - (int)NameList.TYPE_TCPLISN; private const string Redirector_bin = "Redirector.bin"; diff --git a/Netch/Interops/TUNInterop.cs b/Netch/Interops/TUNInterop.cs index 2afbd49b..90d91b7e 100644 --- a/Netch/Interops/TUNInterop.cs +++ b/Netch/Interops/TUNInterop.cs @@ -1,6 +1,5 @@ using System.Runtime.InteropServices; using System.Text; -using Netch.Utils; namespace Netch.Interops { diff --git a/Netch/Models/IAdapter.cs b/Netch/Models/IAdapter.cs index 5f292f69..147c4e99 100644 --- a/Netch/Models/IAdapter.cs +++ b/Netch/Models/IAdapter.cs @@ -5,7 +5,7 @@ namespace Netch.Models { public interface IAdapter { - int InterfaceIndex { get; } + ulong InterfaceIndex { get; } IPAddress Gateway { get; } diff --git a/Netch/Models/IServerUtil.cs b/Netch/Models/IServerUtil.cs index feb05b90..35610d13 100644 --- a/Netch/Models/IServerUtil.cs +++ b/Netch/Models/IServerUtil.cs @@ -1,6 +1,6 @@ -using System; +using Netch.Controllers; +using System; using System.Collections.Generic; -using Netch.Controllers; namespace Netch.Models { diff --git a/Netch/Models/Loggers/ConsoleLogger.cs b/Netch/Models/Loggers/ConsoleLogger.cs index f5b4d913..d2c55ce6 100644 --- a/Netch/Models/Loggers/ConsoleLogger.cs +++ b/Netch/Models/Loggers/ConsoleLogger.cs @@ -1,5 +1,5 @@ -using System; -using Netch.Interfaces; +using Netch.Interfaces; +using System; namespace Netch.Models.Loggers { diff --git a/Netch/Models/Loggers/FileLogger.cs b/Netch/Models/Loggers/FileLogger.cs index f2bda3e3..7d06c4d8 100644 --- a/Netch/Models/Loggers/FileLogger.cs +++ b/Netch/Models/Loggers/FileLogger.cs @@ -1,12 +1,12 @@ -using System; +using Netch.Interfaces; +using System; using System.IO; -using Netch.Interfaces; namespace Netch.Models.Loggers { public class FileLogger : ILogger { - public string LogFile { get; set; }= Path.Combine(Global.NetchDir, "logging\\application.log"); + public string LogFile { get; set; } = Path.Combine(Global.NetchDir, "logging\\application.log"); private readonly object _fileLock = new(); diff --git a/Netch/Models/Mode.cs b/Netch/Models/Mode.cs index 7e7584dd..a1ebcdd5 100644 --- a/Netch/Models/Mode.cs +++ b/Netch/Models/Mode.cs @@ -1,9 +1,9 @@ -using System; +using Netch.Utils; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; -using Netch.Utils; namespace Netch.Models { diff --git a/Netch/Models/OutboundAdapter.cs b/Netch/Models/OutboundAdapter.cs index bba99543..b83dd404 100644 --- a/Netch/Models/OutboundAdapter.cs +++ b/Netch/Models/OutboundAdapter.cs @@ -2,7 +2,6 @@ using System.Linq; using System.Net; using System.Net.NetworkInformation; -using Netch.Utils; using Vanara.PInvoke; namespace Netch.Models @@ -22,7 +21,7 @@ namespace Netch.Models ni.GetIPProperties().GetIPv4Properties().Index == pRoute.dwForwardIfIndex); Address = new IPAddress(pRoute.dwForwardNextHop.S_addr); - InterfaceIndex = (int) pRoute.dwForwardIfIndex; + InterfaceIndex = (ulong)pRoute.dwForwardIfIndex; Gateway = new IPAddress(pRoute.dwForwardNextHop.S_un_b); Global.Logger.Info($"出口 网关 地址:{Gateway}"); @@ -34,7 +33,7 @@ namespace Netch.Models /// /// 索引 /// - public int InterfaceIndex { get; } + public ulong InterfaceIndex { get; } /// /// 网关 diff --git a/Netch/Models/Server.cs b/Netch/Models/Server.cs index 320f0295..b7043a27 100644 --- a/Netch/Models/Server.cs +++ b/Netch/Models/Server.cs @@ -1,8 +1,8 @@ -using System; +using Netch.Utils; +using System; using System.Collections.Generic; using System.Text.Json.Serialization; using System.Threading.Tasks; -using Netch.Utils; namespace Netch.Models { diff --git a/Netch/Models/Setting.cs b/Netch/Models/Setting.cs index b3baee81..95139145 100644 --- a/Netch/Models/Setting.cs +++ b/Netch/Models/Setting.cs @@ -1,5 +1,5 @@ -using System.Collections.Generic; -using Netch.Utils; +using Netch.Utils; +using System.Collections.Generic; namespace Netch.Models { @@ -265,7 +265,7 @@ namespace Netch.Models public Setting Clone() { - return (Setting) MemberwiseClone(); + return (Setting)MemberwiseClone(); } public void Set(Setting value) diff --git a/Netch/Models/StatusText.cs b/Netch/Models/StatusText.cs index 3a80f417..f0d7395a 100644 --- a/Netch/Models/StatusText.cs +++ b/Netch/Models/StatusText.cs @@ -1,6 +1,6 @@ +using Netch.Utils; using System.Collections.Generic; using System.Linq; -using Netch.Utils; namespace Netch.Models { diff --git a/Netch/Models/TunAdapter.cs b/Netch/Models/TunAdapter.cs index 991f0cad..547f979e 100644 --- a/Netch/Models/TunAdapter.cs +++ b/Netch/Models/TunAdapter.cs @@ -1,8 +1,7 @@ -using System.Linq; +using Netch.Interops; +using System.Linq; using System.Net; using System.Net.NetworkInformation; -using Netch.Interops; -using Netch.Utils; namespace Netch.Models { @@ -10,18 +9,18 @@ namespace Netch.Models { public TunAdapter() { - InterfaceIndex = (int) NativeMethods.ConvertLuidToIndex(TUNInterop.tun_luid()); - NetworkInterface = NetworkInterface.GetAllNetworkInterfaces().First(i => i.GetIPProperties().GetIPv4Properties().Index == InterfaceIndex); + InterfaceIndex = NativeMethods.ConvertLuidToIndex(TUNInterop.tun_luid()); + NetworkInterface = NetworkInterface.GetAllNetworkInterfaces().First(i => i.GetIPProperties().GetIPv4Properties().Index == (int)InterfaceIndex); Gateway = IPAddress.Parse(Global.Settings.TUNTAP.Gateway); Global.Logger.Info($"WinTUN 适配器:{NetworkInterface.Name} {NetworkInterface.Id} {NetworkInterface.Description}, index: {InterfaceIndex}"); } - public int InterfaceIndex { get; } + public ulong InterfaceIndex { get; } public IPAddress Gateway { get; } public NetworkInterface NetworkInterface { get; } } -} \ No newline at end of file +} diff --git a/Netch/NativeMethods.cs b/Netch/NativeMethods.cs index 9270db19..abdf9435 100644 --- a/Netch/NativeMethods.cs +++ b/Netch/NativeMethods.cs @@ -1,50 +1,29 @@ -using System.Runtime.InteropServices; +using System.Net.Sockets; +using System.Runtime.InteropServices; namespace Netch { public static class NativeMethods { - /// - /// 分配 IP 地址 - /// - /// AF_INET / AF_INET6 - /// 目标地址 - /// CIDR - /// 适配器索引 - /// 是否成功 - [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl, EntryPoint = "CreateUnicastIP")] - public static extern bool CreateUnicastIP(int inet, string address, int cidr, int index); - - /// - /// 创建路由规则 - /// - /// AF_INET / AF_INET6 - /// 目标地址 - /// CIDR - /// 网关地址 - /// 适配器索引 - /// 跃点数 - /// 是否成功 - [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl, EntryPoint = "CreateRoute")] - public static extern bool CreateRoute(int inet, string address, int cidr, string gateway, int index, int metric = 0); - - /// - /// 删除路由规则 - /// - /// AF_INET / AF_INET6 - /// 目标地址 - /// 掩码地址 - /// 网关地址 - /// 适配器索引 - /// 跃点数 - /// 是否成功 - [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl, EntryPoint = "DeleteRoute")] - public static extern bool DeleteRoute(int inet, string address, int cidr, string gateway, int index, int metric = 0); - - [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] - public static extern ulong ConvertLuidToIndex(ulong luid); - [DllImport("dnsapi", EntryPoint = "DnsFlushResolverCache")] - public static extern uint FlushDNSResolverCache(); + public static extern uint RefreshDNSCache(); + + [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern ulong ConvertLuidToIndex(ulong id); + + [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern bool CreateIPv4(string address, string netmask, ulong index); + + [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern bool CreateUnicastIP(AddressFamily inet, string address, byte cidr, ulong index); + + [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern bool RefreshIPTable(AddressFamily inet, ulong index); + + [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern bool CreateRoute(AddressFamily inet, string address, byte cidr, string gateway, ulong index, int metric); + + [DllImport("RouteHelper.bin", CallingConvention = CallingConvention.Cdecl)] + public static extern bool DeleteRoute(AddressFamily inet, string address, byte cidr, string gateway, ulong index, int metric); } -} \ No newline at end of file +} diff --git a/Netch/Netch.cs b/Netch/Netch.cs index 593f7bd0..3dcda64c 100644 --- a/Netch/Netch.cs +++ b/Netch/Netch.cs @@ -1,13 +1,13 @@ -using System; +using Netch.Controllers; +using Netch.Forms; +using Netch.Utils; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; -using Netch.Controllers; -using Netch.Forms; -using Netch.Utils; using static Vanara.PInvoke.Kernel32; namespace Netch @@ -41,7 +41,7 @@ namespace Netch Updater.Updater.CleanOld(Global.NetchDir); // 预创建目录 - var directories = new[] {"mode\\Custom", "data", "i18n", "logging"}; + var directories = new[] { "mode\\Custom", "data", "i18n", "logging" }; foreach (var item in directories) if (!Directory.Exists(item)) Directory.CreateDirectory(item); diff --git a/Netch/Properties/AssemblyInfo.cs b/Netch/Properties/AssemblyInfo.cs index f22eadbd..4a839623 100644 --- a/Netch/Properties/AssemblyInfo.cs +++ b/Netch/Properties/AssemblyInfo.cs @@ -1,6 +1,6 @@ -using System.Reflection; +using Netch.Controllers; +using System.Reflection; using System.Runtime.InteropServices; -using Netch.Controllers; // 有关程序集的一般信息由以下 // 控制。更改这些特性值可修改 @@ -35,5 +35,5 @@ using Netch.Controllers; //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion(UpdateChecker.AssemblyVersion)] +[assembly: AssemblyVersion(UpdateChecker.AssemblyVersion)] // [assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/Netch/Servers/Shadowsocks/SSController.cs b/Netch/Servers/Shadowsocks/SSController.cs index 48e934a6..0ec55f31 100644 --- a/Netch/Servers/Shadowsocks/SSController.cs +++ b/Netch/Servers/Shadowsocks/SSController.cs @@ -1,6 +1,6 @@ -using System.Collections.Generic; using Netch.Controllers; using Netch.Models; +using System.Collections.Generic; namespace Netch.Servers.Shadowsocks { @@ -8,9 +8,9 @@ namespace Netch.Servers.Shadowsocks { public override string MainFile { get; protected set; } = "Shadowsocks.exe"; - protected override IEnumerable StartedKeywords { get; set; } = new[] {"listening at"}; + protected override IEnumerable StartedKeywords { get; set; } = new[] { "listening at" }; - protected override IEnumerable StoppedKeywords { get; set; } = new[] {"Invalid config path", "usage", "plugin service exit unexpectedly"}; + protected override IEnumerable StoppedKeywords { get; set; } = new[] { "Invalid config path", "usage", "plugin service exit unexpectedly" }; public override string Name { get; } = "Shadowsocks"; @@ -20,7 +20,7 @@ namespace Netch.Servers.Shadowsocks public void Start(in Server s, in Mode mode) { - var server = (Shadowsocks) s; + var server = (Shadowsocks)s; var command = new SSParameter { diff --git a/Netch/Servers/Shadowsocks/SSUtil.cs b/Netch/Servers/Shadowsocks/SSUtil.cs index a8820afc..32b2f6fc 100644 --- a/Netch/Servers/Shadowsocks/SSUtil.cs +++ b/Netch/Servers/Shadowsocks/SSUtil.cs @@ -1,14 +1,14 @@ -using System; +using Netch.Controllers; +using Netch.Models; +using Netch.Servers.Shadowsocks.Form; +using Netch.Servers.Shadowsocks.Models.SSD; +using Netch.Utils; +using System; using System.Collections.Generic; using System.Linq; using System.Text.Json; using System.Text.RegularExpressions; using System.Web; -using Netch.Controllers; -using Netch.Models; -using Netch.Servers.Shadowsocks.Form; -using Netch.Servers.Shadowsocks.Models.SSD; -using Netch.Utils; namespace Netch.Servers.Shadowsocks { @@ -22,13 +22,13 @@ namespace Netch.Servers.Shadowsocks public string ShortName { get; } = "SS"; - public string[] UriScheme { get; } = {"ss", "ssd"}; + public string[] UriScheme { get; } = { "ss", "ssd" }; public Type ServerType { get; } = typeof(Shadowsocks); public void Edit(Server s) { - new ShadowsocksForm((Shadowsocks) s).ShowDialog(); + new ShadowsocksForm((Shadowsocks)s).ShowDialog(); } public void Create() @@ -38,7 +38,7 @@ namespace Netch.Servers.Shadowsocks public string GetShareLink(Server s) { - var server = (Shadowsocks) s; + var server = (Shadowsocks)s; // ss://method:password@server:port#Remark return "ss://" + ShareLink.URLSafeBase64Encode($"{server.EncryptMethod}:{server.Password}@{server.Hostname}:{server.Port}") + "#" + HttpUtility.UrlEncode(server.Remark); @@ -52,7 +52,7 @@ namespace Netch.Servers.Shadowsocks public IEnumerable ParseUri(string text) { if (text.StartsWith("ss://")) - return new[] {ParseSsUri(text)}; + return new[] { ParseSsUri(text) }; if (text.StartsWith("ssd://")) return ParseSsdUri(text); @@ -62,7 +62,7 @@ namespace Netch.Servers.Shadowsocks public bool CheckServer(Server s) { - var server = (Shadowsocks) s; + var server = (Shadowsocks)s; if (!SSGlobal.EncryptMethods.Contains(server.EncryptMethod)) { Global.Logger.Error($"不支持的 SS 加密方式:{server.EncryptMethod}"); @@ -78,18 +78,18 @@ namespace Netch.Servers.Shadowsocks { var json = JsonSerializer.Deserialize
(ShareLink.URLSafeBase64Decode(s.Substring(6)))!; - return json.servers.Select(server => new Shadowsocks - { - Remark = server.remarks, - Hostname = server.server, - Port = server.port != 0 ? server.port : json.port, - Password = server.password ?? json.password, - EncryptMethod = server.encryption ?? json.encryption, - Plugin = string.IsNullOrEmpty(json.plugin) ? string.IsNullOrEmpty(server.plugin) ? null : server.plugin : json.plugin, - PluginOption = string.IsNullOrEmpty(json.plugin_options) + return json.servers.Select(server => new Shadowsocks + { + Remark = server.remarks, + Hostname = server.server, + Port = server.port != 0 ? server.port : json.port, + Password = server.password ?? json.password, + EncryptMethod = server.encryption ?? json.encryption, + Plugin = string.IsNullOrEmpty(json.plugin) ? string.IsNullOrEmpty(server.plugin) ? null : server.plugin : json.plugin, + PluginOption = string.IsNullOrEmpty(json.plugin_options) ? string.IsNullOrEmpty(server.plugin_options) ? null : server.plugin_options - : json.plugin_options - }) + : json.plugin_options + }) .Where(CheckServer); } diff --git a/Netch/Servers/Shadowsocks/Shadowsocks.cs b/Netch/Servers/Shadowsocks/Shadowsocks.cs index bb494eab..3dcf6e61 100644 --- a/Netch/Servers/Shadowsocks/Shadowsocks.cs +++ b/Netch/Servers/Shadowsocks/Shadowsocks.cs @@ -1,5 +1,5 @@ -using System.Collections.Generic; -using Netch.Models; +using Netch.Models; +using System.Collections.Generic; namespace Netch.Servers.Shadowsocks { diff --git a/Netch/Servers/ShadowsocksR/SSRController.cs b/Netch/Servers/ShadowsocksR/SSRController.cs index 59ac5eba..3c73fb85 100644 --- a/Netch/Servers/ShadowsocksR/SSRController.cs +++ b/Netch/Servers/ShadowsocksR/SSRController.cs @@ -1,6 +1,6 @@ -using System.Collections.Generic; using Netch.Controllers; using Netch.Models; +using System.Collections.Generic; namespace Netch.Servers.ShadowsocksR { @@ -8,9 +8,9 @@ namespace Netch.Servers.ShadowsocksR { public override string MainFile { get; protected set; } = "ShadowsocksR.exe"; - protected override IEnumerable StartedKeywords { get; set; } = new[] {"listening at"}; + protected override IEnumerable StartedKeywords { get; set; } = new[] { "listening at" }; - protected override IEnumerable StoppedKeywords { get; set; } = new[] {"Invalid config path", "usage"}; + protected override IEnumerable StoppedKeywords { get; set; } = new[] { "Invalid config path", "usage" }; public override string Name { get; } = "ShadowsocksR"; @@ -20,7 +20,7 @@ namespace Netch.Servers.ShadowsocksR public void Start(in Server s, in Mode mode) { - var server = (ShadowsocksR) s; + var server = (ShadowsocksR)s; var command = new SSRParameter { diff --git a/Netch/Servers/ShadowsocksR/SSRUtil.cs b/Netch/Servers/ShadowsocksR/SSRUtil.cs index 66ed82d9..52e946bf 100644 --- a/Netch/Servers/ShadowsocksR/SSRUtil.cs +++ b/Netch/Servers/ShadowsocksR/SSRUtil.cs @@ -1,11 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Text.RegularExpressions; -using Netch.Controllers; +using Netch.Controllers; using Netch.Models; using Netch.Servers.Shadowsocks; using Netch.Servers.ShadowsocksR.Form; using Netch.Utils; +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; namespace Netch.Servers.ShadowsocksR { @@ -19,13 +19,13 @@ namespace Netch.Servers.ShadowsocksR public string ShortName { get; } = "SR"; - public string[] UriScheme { get; } = {"ssr"}; + public string[] UriScheme { get; } = { "ssr" }; public Type ServerType { get; } = typeof(ShadowsocksR); public void Edit(Server s) { - new ShadowsocksRForm((ShadowsocksR) s).ShowDialog(); + new ShadowsocksRForm((ShadowsocksR)s).ShowDialog(); } public void Create() @@ -35,7 +35,7 @@ namespace Netch.Servers.ShadowsocksR public string GetShareLink(Server s) { - var server = (ShadowsocksR) s; + var server = (ShadowsocksR)s; // https://github.com/shadowsocksr-backup/shadowsocks-rss/wiki/SSR-QRcode-scheme // ssr://base64(host:port:protocol:method:obfs:base64pass/?obfsparam=base64param&protoparam=base64param&remarks=base64remarks&group=base64group&udpport=0&uot=0) @@ -143,7 +143,7 @@ namespace Netch.Servers.ShadowsocksR public bool CheckServer(Server s) { - var server = (ShadowsocksR) s; + var server = (ShadowsocksR)s; if (!SSRGlobal.EncryptMethods.Contains(server.EncryptMethod)) { Global.Logger.Error($"不支持的 SSR 加密方式:{server.EncryptMethod}"); diff --git a/Netch/Servers/ShadowsocksR/ShadowsocksR.cs b/Netch/Servers/ShadowsocksR/ShadowsocksR.cs index 6b4017ea..fa4988eb 100644 --- a/Netch/Servers/ShadowsocksR/ShadowsocksR.cs +++ b/Netch/Servers/ShadowsocksR/ShadowsocksR.cs @@ -1,5 +1,5 @@ -using System.Collections.Generic; -using Netch.Models; +using Netch.Models; +using System.Collections.Generic; namespace Netch.Servers.ShadowsocksR { diff --git a/Netch/Servers/Socks5/S5Controller.cs b/Netch/Servers/Socks5/S5Controller.cs index e2d04916..77f694e0 100644 --- a/Netch/Servers/Socks5/S5Controller.cs +++ b/Netch/Servers/Socks5/S5Controller.cs @@ -9,7 +9,7 @@ namespace Netch.Servers.Socks5 public override void Start(in Server s, in Mode mode) { - var server = (Socks5) s; + var server = (Socks5)s; if (server.Auth()) base.Start(s, mode); } diff --git a/Netch/Servers/Socks5/S5Util.cs b/Netch/Servers/Socks5/S5Util.cs index 7440130b..a51b8717 100644 --- a/Netch/Servers/Socks5/S5Util.cs +++ b/Netch/Servers/Socks5/S5Util.cs @@ -1,9 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Netch.Controllers; +using Netch.Controllers; using Netch.Models; using Netch.Servers.Socks5.Form; +using System; +using System.Collections.Generic; +using System.Linq; namespace Netch.Servers.Socks5 { @@ -23,7 +23,7 @@ namespace Netch.Servers.Socks5 public void Edit(Server s) { - new Socks5Form((Socks5) s).ShowDialog(); + new Socks5Form((Socks5)s).ShowDialog(); } public void Create() @@ -33,7 +33,7 @@ namespace Netch.Servers.Socks5 public string GetShareLink(Server s) { - var server = (Socks5) s; + var server = (Socks5)s; // https://t.me/socks?server=1.1.1.1&port=443 return $"https://t.me/socks?server={server.Hostname}&port={server.Port}" + $"{(!string.IsNullOrWhiteSpace(server.Username) ? $"&user={server.Username}" : "")}" + @@ -68,7 +68,7 @@ namespace Netch.Servers.Socks5 if (dict.ContainsKey("pass") && !string.IsNullOrWhiteSpace(dict["pass"])) data.Password = dict["pass"]; - return new[] {data}; + return new[] { data }; } public bool CheckServer(Server s) diff --git a/Netch/Servers/Trojan/TrojanController.cs b/Netch/Servers/Trojan/TrojanController.cs index 17119900..f8bf1e66 100644 --- a/Netch/Servers/Trojan/TrojanController.cs +++ b/Netch/Servers/Trojan/TrojanController.cs @@ -1,9 +1,9 @@ -using System.Collections.Generic; -using System.IO; -using System.Text.Json; -using Netch.Controllers; +using Netch.Controllers; using Netch.Models; using Netch.Servers.Trojan.Models; +using System.Collections.Generic; +using System.IO; +using System.Text.Json; namespace Netch.Servers.Trojan { @@ -11,9 +11,9 @@ namespace Netch.Servers.Trojan { public override string MainFile { get; protected set; } = "Trojan.exe"; - protected override IEnumerable StartedKeywords { get; set; } = new[] {"started"}; + protected override IEnumerable StartedKeywords { get; set; } = new[] { "started" }; - protected override IEnumerable StoppedKeywords { get; set; } = new[] {"exiting"}; + protected override IEnumerable StoppedKeywords { get; set; } = new[] { "exiting" }; public override string Name { get; } = "Trojan"; @@ -23,7 +23,7 @@ namespace Netch.Servers.Trojan public void Start(in Server s, in Mode mode) { - var server = (Trojan) s; + var server = (Trojan)s; var trojanConfig = new TrojanConfig { local_addr = this.LocalAddress(), diff --git a/Netch/Servers/Trojan/TrojanUtil.cs b/Netch/Servers/Trojan/TrojanUtil.cs index a047075c..788889be 100644 --- a/Netch/Servers/Trojan/TrojanUtil.cs +++ b/Netch/Servers/Trojan/TrojanUtil.cs @@ -1,10 +1,10 @@ +using Netch.Controllers; +using Netch.Models; +using Netch.Servers.Trojan.Form; using System; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Web; -using Netch.Controllers; -using Netch.Models; -using Netch.Servers.Trojan.Form; namespace Netch.Servers.Trojan { @@ -18,13 +18,13 @@ namespace Netch.Servers.Trojan public string ShortName { get; } = "TR"; - public string[] UriScheme { get; } = {"trojan"}; + public string[] UriScheme { get; } = { "trojan" }; public Type ServerType { get; } = typeof(Trojan); public void Edit(Server s) { - new TrojanForm((Trojan) s).ShowDialog(); + new TrojanForm((Trojan)s).ShowDialog(); } public void Create() @@ -34,7 +34,7 @@ namespace Netch.Servers.Trojan public string GetShareLink(Server s) { - var server = (Trojan) s; + var server = (Trojan)s; return $"trojan://{HttpUtility.UrlEncode(server.Password)}@{server.Hostname}:{server.Port}#{server.Remark}"; } @@ -79,7 +79,7 @@ namespace Netch.Servers.Trojan data.Hostname = match.Groups["server"].Value; data.Port = ushort.Parse(match.Groups["port"].Value); - return new[] {data}; + return new[] { data }; } public bool CheckServer(Server s) diff --git a/Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs b/Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs index 9818a0d4..3b5eff52 100644 --- a/Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs +++ b/Netch/Servers/V2ray/Utils/V2rayConfigUtils.cs @@ -1,8 +1,8 @@ -using System.Collections.Generic; +using Netch.Models; +using Netch.Servers.V2ray.Models; +using System.Collections.Generic; using System.Linq; using System.Text.Json; -using Netch.Models; -using Netch.Servers.V2ray.Models; using V2rayConfig = Netch.Servers.V2ray.Models.V2rayConfig; namespace Netch.Servers.V2ray.Utils @@ -108,9 +108,9 @@ namespace Netch.Servers.V2ray.Utils switch (server) { - case Socks5.Socks5 socks5: - { - outbound.settings.servers = new List + case Socks5.Socks5 socks5: + { + outbound.settings.servers = new List { new() { @@ -128,82 +128,82 @@ namespace Netch.Servers.V2ray.Utils address = server.AutoResolveHostname(), port = server.Port } - }; - - outbound.mux.enabled = false; - outbound.mux.concurrency = -1; - outbound.protocol = "socks"; - break; - } - case VLESS.VLESS vless: - { - var vnextItem = new VnextItem - { - users = new List(), - address = server.AutoResolveHostname(), - port = server.Port - }; - - outbound.settings.vnext = new List {vnextItem}; - - var usersItem = new UsersItem - { - id = vless.UserID, - alterId = 0, - flow = string.Empty, - encryption = vless.EncryptMethod - }; - - vnextItem.users.Add(usersItem); - - var streamSettings = outbound.streamSettings; - boundStreamSettings(vless, ref streamSettings); - - if (vless.TLSSecureType == "xtls") - { - usersItem.flow = string.IsNullOrEmpty(vless.Flow) ? "xtls-rprx-origin" : vless.Flow; - - outbound.mux.enabled = false; - outbound.mux.concurrency = -1; + }; + + outbound.mux.enabled = false; + outbound.mux.concurrency = -1; + outbound.protocol = "socks"; + break; } - else - { - outbound.mux.enabled = vless.UseMux ?? Global.Settings.V2RayConfig.UseMux; - outbound.mux.concurrency = vless.UseMux ?? Global.Settings.V2RayConfig.UseMux ? 8 : -1; + case VLESS.VLESS vless: + { + var vnextItem = new VnextItem + { + users = new List(), + address = server.AutoResolveHostname(), + port = server.Port + }; + + outbound.settings.vnext = new List { vnextItem }; + + var usersItem = new UsersItem + { + id = vless.UserID, + alterId = 0, + flow = string.Empty, + encryption = vless.EncryptMethod + }; + + vnextItem.users.Add(usersItem); + + var streamSettings = outbound.streamSettings; + boundStreamSettings(vless, ref streamSettings); + + if (vless.TLSSecureType == "xtls") + { + usersItem.flow = string.IsNullOrEmpty(vless.Flow) ? "xtls-rprx-origin" : vless.Flow; + + outbound.mux.enabled = false; + outbound.mux.concurrency = -1; + } + else + { + outbound.mux.enabled = vless.UseMux ?? Global.Settings.V2RayConfig.UseMux; + outbound.mux.concurrency = vless.UseMux ?? Global.Settings.V2RayConfig.UseMux ? 8 : -1; + } + + outbound.protocol = "vless"; + outbound.settings.servers = null; + break; + } + case VMess.VMess vmess: + { + var vnextItem = new VnextItem + { + users = new List(), + address = server.AutoResolveHostname(), + port = server.Port + }; + + outbound.settings.vnext = new List { vnextItem }; + + var usersItem = new UsersItem + { + id = vmess.UserID, + alterId = vmess.AlterID, + security = vmess.EncryptMethod + }; + + vnextItem.users.Add(usersItem); + + var streamSettings = outbound.streamSettings; + boundStreamSettings(vmess, ref streamSettings); + + outbound.mux.enabled = vmess.UseMux ?? Global.Settings.V2RayConfig.UseMux; + outbound.mux.concurrency = vmess.UseMux ?? Global.Settings.V2RayConfig.UseMux ? 8 : -1; + outbound.protocol = "vmess"; + break; } - - outbound.protocol = "vless"; - outbound.settings.servers = null; - break; - } - case VMess.VMess vmess: - { - var vnextItem = new VnextItem - { - users = new List(), - address = server.AutoResolveHostname(), - port = server.Port - }; - - outbound.settings.vnext = new List {vnextItem}; - - var usersItem = new UsersItem - { - id = vmess.UserID, - alterId = vmess.AlterID, - security = vmess.EncryptMethod - }; - - vnextItem.users.Add(usersItem); - - var streamSettings = outbound.streamSettings; - boundStreamSettings(vmess, ref streamSettings); - - outbound.mux.enabled = vmess.UseMux ?? Global.Settings.V2RayConfig.UseMux; - outbound.mux.concurrency = vmess.UseMux ?? Global.Settings.V2RayConfig.UseMux ? 8 : -1; - outbound.protocol = "vmess"; - break; - } } v2rayConfig.outbounds.AddRange(new[] @@ -274,7 +274,7 @@ namespace Netch.Servers.V2ray.Utils case "ws": var wsSettings = new WsSettings { - headers = !string.IsNullOrWhiteSpace(server.Host) ? new Headers {Host = server.Host} : null, + headers = !string.IsNullOrWhiteSpace(server.Host) ? new Headers { Host = server.Host } : null, path = !string.IsNullOrWhiteSpace(server.Path) ? server.Path : null }; diff --git a/Netch/Servers/V2ray/V2rayController.cs b/Netch/Servers/V2ray/V2rayController.cs index 53945deb..b2372245 100644 --- a/Netch/Servers/V2ray/V2rayController.cs +++ b/Netch/Servers/V2ray/V2rayController.cs @@ -1,8 +1,8 @@ -using System.Collections.Generic; -using System.IO; using Netch.Controllers; using Netch.Models; using Netch.Servers.V2ray.Utils; +using System.Collections.Generic; +using System.IO; namespace Netch.Servers.V2ray { @@ -10,9 +10,9 @@ namespace Netch.Servers.V2ray { public override string MainFile { get; protected set; } = "xray.exe"; - protected override IEnumerable StartedKeywords { get; set; } = new[] {"started"}; + protected override IEnumerable StartedKeywords { get; set; } = new[] { "started" }; - protected override IEnumerable StoppedKeywords { get; set; } = new[] {"config file not readable", "failed to"}; + protected override IEnumerable StoppedKeywords { get; set; } = new[] { "config file not readable", "failed to" }; public override string Name { get; } = "Xray"; diff --git a/Netch/Servers/V2ray/V2rayUtils.cs b/Netch/Servers/V2ray/V2rayUtils.cs index f3edc30a..7f47e24b 100644 --- a/Netch/Servers/V2ray/V2rayUtils.cs +++ b/Netch/Servers/V2ray/V2rayUtils.cs @@ -1,10 +1,10 @@ +using Netch.Models; +using Netch.Utils; using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Web; -using Netch.Models; -using Netch.Utils; namespace Netch.Servers.V2ray { @@ -13,7 +13,7 @@ namespace Netch.Servers.V2ray public static IEnumerable ParseVUri(string text) { var scheme = ShareLink.GetUriScheme(text).ToLower(); - var server = scheme switch {"vmess" => new VMess.VMess(), "vless" => new VLESS.VLESS(), _ => throw new ArgumentOutOfRangeException()}; + var server = scheme switch { "vmess" => new VMess.VMess(), "vless" => new VLESS.VLESS(), _ => throw new ArgumentOutOfRangeException() }; if (text.Contains("#")) { server.Remark = Uri.UnescapeDataString(text.Split('#')[1]); @@ -25,7 +25,7 @@ namespace Netch.Servers.V2ray var parameter = HttpUtility.ParseQueryString(text.Split('?')[1]); text = text.Substring(0, text.IndexOf("?", StringComparison.Ordinal)); server.TransferProtocol = parameter.Get("type") ?? "tcp"; - server.EncryptMethod = parameter.Get("encryption") ?? scheme switch {"vless" => "none", _ => "auto"}; + server.EncryptMethod = parameter.Get("encryption") ?? scheme switch { "vless" => "none", _ => "auto" }; switch (server.TransferProtocol) { case "tcp": @@ -54,7 +54,7 @@ namespace Netch.Servers.V2ray { server.Host = parameter.Get("sni") ?? ""; if (server.TLSSecureType == "xtls") - ((VLESS.VLESS) server).Flow = parameter.Get("flow") ?? ""; + ((VLESS.VLESS)server).Flow = parameter.Get("flow") ?? ""; } } @@ -67,13 +67,13 @@ namespace Netch.Servers.V2ray server.Hostname = match.Groups["server"].Value; server.Port = ushort.Parse(match.Groups["port"].Value); - return new[] {server}; + return new[] { server }; } public static string GetVShareLink(Server s, string scheme = "vmess") { // https://github.com/XTLS/Xray-core/issues/91 - var server = (VMess.VMess) s; + var server = (VMess.VMess)s; var parameter = new Dictionary(); // protocol-specific fields parameter.Add("type", server.TransferProtocol); @@ -127,7 +127,7 @@ namespace Netch.Servers.V2ray if (server.TLSSecureType == "xtls") { - var flow = ((VLESS.VLESS) server).Flow; + var flow = ((VLESS.VLESS)server).Flow; if (!flow.IsNullOrWhiteSpace()) parameter.Add("flow", flow!.Replace("-udp443", "")); } diff --git a/Netch/Servers/VLESS/VLESS.cs b/Netch/Servers/VLESS/VLESS.cs index 17856364..59178b1e 100644 --- a/Netch/Servers/VLESS/VLESS.cs +++ b/Netch/Servers/VLESS/VLESS.cs @@ -1,5 +1,5 @@ -using System.Collections.Generic; using Netch.Servers.VMess; +using System.Collections.Generic; namespace Netch.Servers.VLESS { diff --git a/Netch/Servers/VLESS/VLESSForm/VLESSForm.cs b/Netch/Servers/VLESS/VLESSForm/VLESSForm.cs index f0d75424..c04f835d 100644 --- a/Netch/Servers/VLESS/VLESSForm/VLESSForm.cs +++ b/Netch/Servers/VLESS/VLESSForm/VLESSForm.cs @@ -1,5 +1,5 @@ -using System.Collections.Generic; using Netch.Forms; +using System.Collections.Generic; namespace Netch.Servers.VLESS.VLESSForm { @@ -30,8 +30,8 @@ namespace Netch.Servers.VLESS.VLESSForm CreateTextBox("QUICSecret", "QUIC Secret", s => true, s => server.QUICSecret = s, server.QUICSecret); CreateComboBox("UseMux", "Use Mux", - new List {"", "true", "false"}, - s => server.UseMux = s switch {"" => null, "true" => true, "false" => false, _ => null}, + new List { "", "true", "false" }, + s => server.UseMux = s switch { "" => null, "true" => true, "false" => false, _ => null }, server.UseMux?.ToString().ToLower() ?? ""); CreateComboBox("TLSSecure", "TLS Secure", VLESSGlobal.TLSSecure, s => server.TLSSecureType = s, server.TLSSecureType); diff --git a/Netch/Servers/VLESS/VLESSUtil.cs b/Netch/Servers/VLESS/VLESSUtil.cs index aa746de3..606be73b 100644 --- a/Netch/Servers/VLESS/VLESSUtil.cs +++ b/Netch/Servers/VLESS/VLESSUtil.cs @@ -1,8 +1,8 @@ -using System; -using System.Collections.Generic; using Netch.Controllers; using Netch.Models; using Netch.Servers.V2ray; +using System; +using System.Collections.Generic; namespace Netch.Servers.VLESS { @@ -16,13 +16,13 @@ namespace Netch.Servers.VLESS public string ShortName { get; } = "VL"; - public string[] UriScheme { get; } = {"vless"}; + public string[] UriScheme { get; } = { "vless" }; public Type ServerType { get; } = typeof(VLESS); public void Edit(Server s) { - new VLESSForm.VLESSForm((VLESS) s).ShowDialog(); + new VLESSForm.VLESSForm((VLESS)s).ShowDialog(); } public void Create() diff --git a/Netch/Servers/VMess/Form/VMessForm.cs b/Netch/Servers/VMess/Form/VMessForm.cs index 572f61c9..9e133d97 100644 --- a/Netch/Servers/VMess/Form/VMessForm.cs +++ b/Netch/Servers/VMess/Form/VMessForm.cs @@ -1,5 +1,5 @@ -using System.Collections.Generic; -using Netch.Forms; +using Netch.Forms; +using System.Collections.Generic; namespace Netch.Servers.VMess.Form { @@ -25,8 +25,8 @@ namespace Netch.Servers.VMess.Form CreateTextBox("QUICSecret", "QUIC Secret", s => true, s => server.QUICSecret = s, server.QUICSecret); CreateComboBox("UseMux", "Use Mux", - new List {"", "true", "false"}, - s => server.UseMux = s switch {"" => null, "true" => true, "false" => false, _ => null}, + new List { "", "true", "false" }, + s => server.UseMux = s switch { "" => null, "true" => true, "false" => false, _ => null }, server.UseMux?.ToString().ToLower() ?? ""); CreateComboBox("TLSSecure", "TLS Secure", VMessGlobal.TLSSecure, s => server.TLSSecureType = s, server.TLSSecureType); diff --git a/Netch/Servers/VMess/VMess.cs b/Netch/Servers/VMess/VMess.cs index ba91cb5c..9e9f2743 100644 --- a/Netch/Servers/VMess/VMess.cs +++ b/Netch/Servers/VMess/VMess.cs @@ -1,5 +1,5 @@ -using System.Collections.Generic; using Netch.Models; +using System.Collections.Generic; namespace Netch.Servers.VMess { diff --git a/Netch/Servers/VMess/VMessUtil.cs b/Netch/Servers/VMess/VMessUtil.cs index 58e3b0a6..33116df7 100644 --- a/Netch/Servers/VMess/VMessUtil.cs +++ b/Netch/Servers/VMess/VMessUtil.cs @@ -1,14 +1,14 @@ -using System; -using System.Collections.Generic; -using System.Text.Encodings.Web; -using System.Text.Json; -using System.Text.Json.Serialization; using Netch.Controllers; using Netch.Models; using Netch.Servers.V2ray; using Netch.Servers.V2ray.Models; using Netch.Servers.VMess.Form; using Netch.Utils; +using System; +using System.Collections.Generic; +using System.Text.Encodings.Web; +using System.Text.Json; +using System.Text.Json.Serialization; namespace Netch.Servers.VMess { @@ -22,13 +22,13 @@ namespace Netch.Servers.VMess public string ShortName { get; } = "V2"; - public string[] UriScheme { get; } = {"vmess"}; + public string[] UriScheme { get; } = { "vmess" }; public Type ServerType { get; } = typeof(VMess); public void Edit(Server s) { - new VMessForm((VMess) s).ShowDialog(); + new VMessForm((VMess)s).ShowDialog(); } public void Create() @@ -40,22 +40,22 @@ namespace Netch.Servers.VMess { if (Global.Settings.V2RayConfig.V2rayNShareLink) { - var server = (VMess) s; + var server = (VMess)s; - var vmessJson = JsonSerializer.Serialize(new V2rayNSharing - { - v = 2, - ps = server.Remark, - add = server.Hostname, - port = server.Port, - id = server.UserID, - aid = server.AlterID, - net = server.TransferProtocol, - type = server.FakeType, - host = server.Host, - path = server.Path, - tls = server.TLSSecureType - }, + var vmessJson = JsonSerializer.Serialize(new V2rayNSharing + { + v = 2, + ps = server.Remark, + add = server.Hostname, + port = server.Port, + id = server.UserID, + aid = server.AlterID, + net = server.TransferProtocol, + type = server.FakeType, + host = server.Host, + path = server.Path, + tls = server.TLSSecureType + }, new JsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping @@ -87,7 +87,7 @@ namespace Netch.Servers.VMess } V2rayNSharing vmess = JsonSerializer.Deserialize(s, - new JsonSerializerOptions {NumberHandling = JsonNumberHandling.WriteAsString | JsonNumberHandling.AllowReadingFromString})!; + new JsonSerializerOptions { NumberHandling = JsonNumberHandling.WriteAsString | JsonNumberHandling.AllowReadingFromString })!; data.Remark = vmess.ps; data.Hostname = vmess.add; @@ -114,7 +114,7 @@ namespace Netch.Servers.VMess data.TLSSecureType = vmess.tls; data.EncryptMethod = "auto"; // V2Ray 加密方式不包括在链接中,主动添加一个 - return new[] {data}; + return new[] { data }; } public bool CheckServer(Server s) diff --git a/Netch/Updater/Updater.cs b/Netch/Updater/Updater.cs index eafea704..4a181878 100644 --- a/Netch/Updater/Updater.cs +++ b/Netch/Updater/Updater.cs @@ -1,3 +1,7 @@ +using Netch.Controllers; +using Netch.Models; +using Netch.Properties; +using Netch.Utils; using System; using System.Collections.Generic; using System.Collections.Immutable; @@ -6,10 +10,6 @@ using System.IO; using System.Linq; using System.Net; using System.Text; -using Netch.Controllers; -using Netch.Models; -using Netch.Properties; -using Netch.Utils; namespace Netch.Updater { @@ -92,7 +92,7 @@ namespace Netch.Updater #region Apply Update - private static readonly ImmutableArray KeepDirectories = new List {"data", "mode\\Custom", "logging"}.ToImmutableArray(); + private static readonly ImmutableArray KeepDirectories = new List { "data", "mode\\Custom", "logging" }.ToImmutableArray(); private void ApplyUpdate() { diff --git a/Netch/Utils/Bandwidth.cs b/Netch/Utils/Bandwidth.cs index 309cfa3e..5cb2a270 100644 --- a/Netch/Utils/Bandwidth.cs +++ b/Netch/Utils/Bandwidth.cs @@ -1,11 +1,11 @@ -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.Diagnostics.Tracing.Parsers; +using Microsoft.Diagnostics.Tracing.Parsers; using Microsoft.Diagnostics.Tracing.Session; using Netch.Controllers; using Netch.Models; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; namespace Netch.Utils { @@ -14,7 +14,7 @@ namespace Netch.Utils public static ulong received; public static TraceEventSession? tSession; - private static readonly string[] Suffix = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}; + private static readonly string[] Suffix = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" }; /// /// 计算流量 @@ -98,7 +98,7 @@ namespace Netch.Utils { if (processList.Contains(data.ProcessID)) lock (counterLock) - received += (ulong) data.size; + received += (ulong)data.size; // Debug.WriteLine($"TcpIpRecv: {ToByteSize(data.size)}"); }; @@ -107,7 +107,7 @@ namespace Netch.Utils { if (processList.Contains(data.ProcessID)) lock (counterLock) - received += (ulong) data.size; + received += (ulong)data.size; // Debug.WriteLine($"UdpIpRecv: {ToByteSize(data.size)}"); }; diff --git a/Netch/Utils/Configuration.cs b/Netch/Utils/Configuration.cs index 9579fb54..7fed7b36 100644 --- a/Netch/Utils/Configuration.cs +++ b/Netch/Utils/Configuration.cs @@ -1,9 +1,9 @@ -using System; +using Netch.Models; +using System; using System.IO; using System.Linq; using System.Text.Json; using System.Text.Json.Serialization; -using Netch.Models; namespace Netch.Utils { diff --git a/Netch/Utils/ModeHelper.cs b/Netch/Utils/ModeHelper.cs index 22b9072a..5a5fd41e 100644 --- a/Netch/Utils/ModeHelper.cs +++ b/Netch/Utils/ModeHelper.cs @@ -1,10 +1,10 @@ -using System; -using System.IO; -using System.Linq; using Netch.Controllers; using Netch.Models; using Netch.Servers.Shadowsocks; using Netch.Servers.Socks5; +using System; +using System.IO; +using System.Linq; namespace Netch.Utils { @@ -112,12 +112,12 @@ namespace Netch.Utils switch (mode.Type) { case 0: - return server switch - { - Socks5 => true, - Shadowsocks shadowsocks when !shadowsocks.HasPlugin() && Global.Settings.Redirector.RedirectorSS => true, - _ => false - }; + return server switch + { + Socks5 => true, + Shadowsocks shadowsocks when !shadowsocks.HasPlugin() && Global.Settings.Redirector.RedirectorSS => true, + _ => false + }; case 1: case 2: return server is Socks5; @@ -126,7 +126,7 @@ namespace Netch.Utils } } - public static readonly int[] ModeTypes = {0, 1, 2, 6}; + public static readonly int[] ModeTypes = { 0, 1, 2, 6 }; public static IModeController GetModeControllerByType(int type, out ushort? port, out string portName) { diff --git a/Netch/Utils/PortHelper.cs b/Netch/Utils/PortHelper.cs index 0a740a98..d8683904 100644 --- a/Netch/Utils/PortHelper.cs +++ b/Netch/Utils/PortHelper.cs @@ -33,9 +33,9 @@ namespace Netch.Utils if (port == 0) throw new ArgumentOutOfRangeException(); - var row = GetTcpTable2().Where(r => ntohs((ushort) r.dwLocalPort) == port).Where(r => r.dwOwningPid is not (0 or 4)); + var row = GetTcpTable2().Where(r => ntohs((ushort)r.dwLocalPort) == port).Where(r => r.dwOwningPid is not (0 or 4)); - return row.Select(r => Process.GetProcessById((int) r.dwOwningPid)); + return row.Select(r => Process.GetProcessById((int)r.dwOwningPid)); } private static void GetReservedPortRange(PortType portType, ref List targetList) @@ -143,7 +143,7 @@ namespace Netch.Utils var random = new Random(); for (ushort i = 0; i < 55535; i++) { - var p = (ushort) random.Next(10000, 65535); + var p = (ushort)random.Next(10000, 65535); try { CheckPort(p, portType); diff --git a/Netch/Utils/ServerConverterWithTypeDiscriminator.cs b/Netch/Utils/ServerConverterWithTypeDiscriminator.cs index aa73e16a..ef48024a 100644 --- a/Netch/Utils/ServerConverterWithTypeDiscriminator.cs +++ b/Netch/Utils/ServerConverterWithTypeDiscriminator.cs @@ -1,7 +1,7 @@ -using System; +using Netch.Models; +using System; using System.Text.Json; using System.Text.Json.Serialization; -using Netch.Models; namespace Netch.Utils { @@ -16,7 +16,7 @@ namespace Netch.Utils try { var type = ServerHelper.GetTypeByTypeName(jsonElement.GetProperty("Type").GetString()!); - return (Server) JsonSerializer.Deserialize(jsonElement.GetRawText(), type)!; + return (Server)JsonSerializer.Deserialize(jsonElement.GetRawText(), type)!; } catch { diff --git a/Netch/Utils/ServerHelper.cs b/Netch/Utils/ServerHelper.cs index 948bccde..235ec7c5 100644 --- a/Netch/Utils/ServerHelper.cs +++ b/Netch/Utils/ServerHelper.cs @@ -1,10 +1,10 @@ +using Netch.Models; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; using System.Timers; -using Netch.Models; using Range = Netch.Models.Range; namespace Netch.Utils @@ -17,7 +17,7 @@ namespace Netch.Utils .GetExportedTypes() .Where(type => type.GetInterfaces().Contains(typeof(IServerUtil))); - ServerUtils = serversUtilsTypes.Select(t => (IServerUtil) Activator.CreateInstance(t)!).OrderBy(util => util.Priority); + ServerUtils = serversUtilsTypes.Select(t => (IServerUtil)Activator.CreateInstance(t)!).OrderBy(util => util.Priority); } public static Type GetTypeByTypeName(string typeName) @@ -57,7 +57,7 @@ namespace Netch.Utils } } - public static int Interval => (int) (Timer.Interval / 1000); + public static int Interval => (int)(Timer.Interval / 1000); private static bool ValueIsEnabled(int value) { @@ -74,7 +74,7 @@ namespace Netch.Utils try { _mux = true; - Parallel.ForEach(Global.Settings.Server, new ParallelOptions {MaxDegreeOfParallelism = 16}, server => { server.Test(); }); + Parallel.ForEach(Global.Settings.Server, new ParallelOptions { MaxDegreeOfParallelism = 16 }, server => { server.Test(); }); _mux = false; TestDelayFinished?.Invoke(null, new EventArgs()); } diff --git a/Netch/Utils/ShareLink.cs b/Netch/Utils/ShareLink.cs index 8070fa5f..d76a6e98 100644 --- a/Netch/Utils/ShareLink.cs +++ b/Netch/Utils/ShareLink.cs @@ -1,12 +1,12 @@ -using System; +using Netch.Models; +using Netch.Servers.Shadowsocks; +using Netch.Servers.Shadowsocks.Models; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.Json; -using Netch.Models; -using Netch.Servers.Shadowsocks; -using Netch.Servers.Shadowsocks.Models; namespace Netch.Utils { @@ -122,7 +122,7 @@ namespace Netch.Utils return JsonSerializer.Deserialize(text, new JsonSerializerOptions { - Converters = {new ServerConverterWithTypeDiscriminator()} + Converters = { new ServerConverterWithTypeDiscriminator() } })!; } @@ -158,7 +158,7 @@ namespace Netch.Utils private static string RemoveEmoji(string text) { - byte[] emojiBytes = {240, 159}; + byte[] emojiBytes = { 240, 159 }; var remark = Encoding.UTF8.GetBytes(text); var startIndex = 0; while (remark.Length > startIndex + 1 && remark[startIndex] == emojiBytes[0] && remark[startIndex + 1] == emojiBytes[1]) diff --git a/Netch/Utils/StringExtension.cs b/Netch/Utils/StringExtension.cs index acd69010..1f99db9d 100644 --- a/Netch/Utils/StringExtension.cs +++ b/Netch/Utils/StringExtension.cs @@ -48,7 +48,7 @@ namespace Netch.Utils var sb = new StringBuilder(); foreach (var t in value) { - var escapeCharacters = new[] {'\\', '*', '+', '?', '|', '{', '}', '[', ']', '(', ')', '^', '$', '.'}; + var escapeCharacters = new[] { '\\', '*', '+', '?', '|', '{', '}', '[', ']', '(', ')', '^', '$', '.' }; if (escapeCharacters.Any(s => s == t)) sb.Append('\\'); diff --git a/Netch/Utils/Subscription.cs b/Netch/Utils/Subscription.cs index 12e4dfbe..4ebaf52c 100644 --- a/Netch/Utils/Subscription.cs +++ b/Netch/Utils/Subscription.cs @@ -1,9 +1,9 @@ +using Netch.Models; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Threading.Tasks; -using Netch.Models; namespace Netch.Utils { diff --git a/Netch/Utils/Utils.cs b/Netch/Utils/Utils.cs index 4f817238..591c909d 100644 --- a/Netch/Utils/Utils.cs +++ b/Netch/Utils/Utils.cs @@ -1,3 +1,5 @@ +using MaxMind.GeoIP2; +using Microsoft.Win32.TaskScheduler; using System; using System.ComponentModel; using System.Diagnostics; @@ -11,8 +13,6 @@ using System.Security.Cryptography; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; -using MaxMind.GeoIP2; -using Microsoft.Win32.TaskScheduler; using Task = System.Threading.Tasks.Task; namespace Netch.Utils @@ -219,8 +219,8 @@ namespace Netch.Utils { case TextBox _: case ComboBox _: - if (((Control) component).ForeColor != color) - ((Control) component).ForeColor = color; + if (((Control)component).ForeColor != color) + ((Control)component).ForeColor = color; break; } diff --git a/Netch/Utils/WebUtil.cs b/Netch/Utils/WebUtil.cs index acb400d5..3c6fdb9c 100644 --- a/Netch/Utils/WebUtil.cs +++ b/Netch/Utils/WebUtil.cs @@ -19,7 +19,7 @@ namespace Netch.Utils public static HttpWebRequest CreateRequest(string url, int? timeout = null, string? userAgent = null) { - var req = (HttpWebRequest) WebRequest.Create(url); + var req = (HttpWebRequest)WebRequest.Create(url); req.UserAgent = string.IsNullOrWhiteSpace(userAgent) ? DefaultUserAgent : userAgent; req.Accept = "*/*"; req.KeepAlive = true; @@ -53,7 +53,7 @@ namespace Netch.Utils /// public static string DownloadString(HttpWebRequest req, out HttpWebResponse rep, string encoding = "UTF-8") { - rep = (HttpWebResponse) req.GetResponse(); + rep = (HttpWebResponse)req.GetResponse(); using var responseStream = rep.GetResponseStream(); using var streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encoding)); @@ -83,7 +83,7 @@ namespace Netch.Utils /// public static async Task DownloadFileAsync(HttpWebRequest req, string fileFullPath) { - using var webResponse = (HttpWebResponse) await req.GetResponseAsync(); + using var webResponse = (HttpWebResponse)await req.GetResponseAsync(); await using var input = webResponse.GetResponseStream(); await using var fileStream = File.OpenWrite(fileFullPath); diff --git a/Netch/Utils/i18N.cs b/Netch/Utils/i18N.cs index a9a0d01c..14dea142 100644 --- a/Netch/Utils/i18N.cs +++ b/Netch/Utils/i18N.cs @@ -1,4 +1,5 @@ -using System.Collections; +using Netch.Properties; +using System.Collections; using System.Collections.Generic; using System.Globalization; using System.IO; @@ -6,7 +7,6 @@ using System.Linq; using System.Text; using System.Text.Json; using System.Windows.Forms; -using Netch.Properties; namespace Netch.Utils { @@ -99,14 +99,14 @@ namespace Netch.Utils { for (var i = 0; i < args.Length; i++) if (args[i] is string) - args[i] = Translate((string) args[i]); + args[i] = Translate((string)args[i]); return string.Format(Translate(format), args); } public static List GetTranslateList() { - var translateFile = new List {"System", "zh-CN", "en-US"}; + var translateFile = new List { "System", "zh-CN", "en-US" }; if (!Directory.Exists("i18n")) return translateFile;