From 638714804bee3a67d363dcb8c5f1b8dbdfe24891 Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Sun, 16 Aug 2020 07:30:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20=E6=B7=BB=E5=8A=A0/?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E6=9C=8D=E5=8A=A1=E5=99=A8=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8CVmess=E9=BB=98=E8=AE=A4=E5=BC=80=E5=90=AFMUX?= =?UTF-8?q?=E5=A4=9A=E8=B7=AF=E5=A4=8D=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Netch/Forms/Server/Shadowsocks.cs | 93 ++++++++---------- Netch/Forms/Server/ShadowsocksR.cs | 115 +++++++++------------- Netch/Forms/Server/Socks5.cs | 74 +++++++-------- Netch/Forms/Server/Trojan.cs | 67 ++++++------- Netch/Forms/Server/Vmess.cs | 148 +++++++++++------------------ Netch/Models/Server.cs | 22 ++--- 6 files changed, 208 insertions(+), 311 deletions(-) diff --git a/Netch/Forms/Server/Shadowsocks.cs b/Netch/Forms/Server/Shadowsocks.cs index 2847093b..05d2f6a1 100644 --- a/Netch/Forms/Server/Shadowsocks.cs +++ b/Netch/Forms/Server/Shadowsocks.cs @@ -1,6 +1,5 @@ using System; using System.Drawing; -using System.Text.RegularExpressions; using System.Windows.Forms; using Netch.Utils; @@ -8,7 +7,7 @@ namespace Netch.Forms.Server { public partial class Shadowsocks : Form { - public int Index; + private readonly Models.Server _server; /// /// 初始化 @@ -18,11 +17,15 @@ namespace Netch.Forms.Server { InitializeComponent(); - Index = index; + _server = index != -1 + ? Global.Settings.Server[index] + : new Models.Server {EncryptMethod = Global.EncryptMethods.SS[0]}; } private void Shadowsocks_Load(object sender, EventArgs e) { + #region InitText + ConfigurationGroupBox.Text = i18N.Translate(ConfigurationGroupBox.Text); RemarkLabel.Text = i18N.Translate(RemarkLabel.Text); AddressLabel.Text = i18N.Translate(AddressLabel.Text); @@ -32,25 +35,17 @@ namespace Netch.Forms.Server PluginOptionsLabel.Text = i18N.Translate(PluginOptionsLabel.Text); ControlButton.Text = i18N.Translate(ControlButton.Text); - foreach (var encrypt in Global.EncryptMethods.SS) - { - EncryptMethodComboBox.Items.Add(encrypt); - } + EncryptMethodComboBox.Items.AddRange(Global.EncryptMethods.SS.ToArray()); - if (Index != -1) - { - RemarkTextBox.Text = Global.Settings.Server[Index].Remark; - AddressTextBox.Text = Global.Settings.Server[Index].Hostname; - PortTextBox.Text = Global.Settings.Server[Index].Port.ToString(); - PasswordTextBox.Text = Global.Settings.Server[Index].Password; - EncryptMethodComboBox.SelectedIndex = Global.EncryptMethods.SS.IndexOf(Global.Settings.Server[Index].EncryptMethod); - PluginTextBox.Text = Global.Settings.Server[Index].Plugin; - PluginOptionsTextBox.Text = Global.Settings.Server[Index].PluginOption; - } - else - { - EncryptMethodComboBox.SelectedIndex = 0; - } + #endregion + + RemarkTextBox.Text = _server.Remark; + AddressTextBox.Text = _server.Hostname; + PortTextBox.Text = _server.Port.ToString(); + PasswordTextBox.Text = _server.Password; + EncryptMethodComboBox.SelectedIndex = Global.EncryptMethods.SS.IndexOf(_server.EncryptMethod); + PluginTextBox.Text = _server.Plugin; + PluginOptionsTextBox.Text = _server.PluginOption; } private void Shadowsocks_FormClosing(object sender, FormClosingEventArgs e) @@ -60,16 +55,17 @@ namespace Netch.Forms.Server private void ComboBox_DrawItem(object sender, DrawItemEventArgs e) { - var cbx = sender as ComboBox; - if (cbx != null) + if (sender is ComboBox cbx) { e.DrawBackground(); if (e.Index >= 0) { - var sf = new StringFormat(); - sf.LineAlignment = StringAlignment.Center; - sf.Alignment = StringAlignment.Center; + var sf = new StringFormat + { + LineAlignment = StringAlignment.Center, + Alignment = StringAlignment.Center + }; var brush = new SolidBrush(cbx.ForeColor); @@ -85,39 +81,24 @@ namespace Netch.Forms.Server private void ControlButton_Click(object sender, EventArgs e) { - if (!Regex.Match(PortTextBox.Text, "^[0-9]+$").Success) + if (!int.TryParse(PortTextBox.Text, out var port)) { return; } - if (Index == -1) + + _server.Remark = RemarkTextBox.Text; + _server.Type = "SS"; + _server.Hostname = AddressTextBox.Text; + _server.Port = port; + _server.Password = PasswordTextBox.Text; + _server.EncryptMethod = EncryptMethodComboBox.Text; + _server.Plugin = PluginTextBox.Text; + _server.PluginOption = PluginOptionsTextBox.Text; + _server.Country = null; + + if (Global.Settings.Server.IndexOf(_server) == -1) { - Global.Settings.Server.Add(new Models.Server - { - Remark = RemarkTextBox.Text, - Type = "SS", - Hostname = AddressTextBox.Text, - Port = int.Parse(PortTextBox.Text), - Password = PasswordTextBox.Text, - EncryptMethod = EncryptMethodComboBox.Text, - Plugin = PluginTextBox.Text, - PluginOption = PluginOptionsTextBox.Text - }); - } - else - { - Global.Settings.Server[Index] = new Models.Server - { - Remark = RemarkTextBox.Text, - Group = Global.Settings.Server[Index].Group, - Type = "SS", - Hostname = AddressTextBox.Text, - Port = int.Parse(PortTextBox.Text), - Password = PasswordTextBox.Text, - EncryptMethod = EncryptMethodComboBox.Text, - Plugin = PluginTextBox.Text, - PluginOption = PluginOptionsTextBox.Text, - Country = null - }; + Global.Settings.Server.Add(_server); } Configuration.Save(); @@ -126,4 +107,4 @@ namespace Netch.Forms.Server Close(); } } -} +} \ No newline at end of file diff --git a/Netch/Forms/Server/ShadowsocksR.cs b/Netch/Forms/Server/ShadowsocksR.cs index 1753457c..56d11a06 100644 --- a/Netch/Forms/Server/ShadowsocksR.cs +++ b/Netch/Forms/Server/ShadowsocksR.cs @@ -1,6 +1,5 @@ using System; using System.Drawing; -using System.Text.RegularExpressions; using System.Windows.Forms; using Netch.Utils; @@ -8,7 +7,7 @@ namespace Netch.Forms.Server { public partial class ShadowsocksR : Form { - public int Index; + private readonly Models.Server _server; /// /// 初始化 @@ -18,11 +17,15 @@ namespace Netch.Forms.Server { InitializeComponent(); - Index = index; + _server = index != -1 + ? Global.Settings.Server[index] + : new Models.Server {EncryptMethod = Global.EncryptMethods.SSR[0]}; } private void ShadowsocksR_Load(object sender, EventArgs e) { + #region InitText + ConfigurationGroupBox.Text = i18N.Translate(ConfigurationGroupBox.Text); RemarkLabel.Text = i18N.Translate(RemarkLabel.Text); AddressLabel.Text = i18N.Translate(AddressLabel.Text); @@ -34,39 +37,23 @@ namespace Netch.Forms.Server OBFSParamLabel.Text = i18N.Translate(OBFSParamLabel.Text); ControlButton.Text = i18N.Translate(ControlButton.Text); - foreach (var encrypt in Global.EncryptMethods.SSR) - { - EncryptMethodComboBox.Items.Add(encrypt); - } + EncryptMethodComboBox.Items.AddRange(Global.EncryptMethods.SSR.ToArray()); - foreach (var protocol in Global.Protocols) - { - ProtocolComboBox.Items.Add(protocol); - } + ProtocolComboBox.Items.AddRange(Global.Protocols.ToArray()); + OBFSComboBox.Items.AddRange(Global.OBFSs.ToArray()); - foreach (var obfs in Global.OBFSs) - { - OBFSComboBox.Items.Add(obfs); - } + #endregion - if (Index != -1) - { - RemarkTextBox.Text = Global.Settings.Server[Index].Remark; - AddressTextBox.Text = Global.Settings.Server[Index].Hostname; - PortTextBox.Text = Global.Settings.Server[Index].Port.ToString(); - PasswordTextBox.Text = Global.Settings.Server[Index].Password; - EncryptMethodComboBox.SelectedIndex = Global.EncryptMethods.SSR.IndexOf(Global.Settings.Server[Index].EncryptMethod); - ProtocolComboBox.SelectedIndex = Global.Protocols.IndexOf(Global.Settings.Server[Index].Protocol); - ProtocolParamTextBox.Text = Global.Settings.Server[Index].ProtocolParam; - OBFSComboBox.SelectedIndex = Global.OBFSs.IndexOf(Global.Settings.Server[Index].OBFS); - OBFSOptionParamTextBox.Text = Global.Settings.Server[Index].OBFSParam; - } - else - { - EncryptMethodComboBox.SelectedIndex = 0; - ProtocolComboBox.SelectedIndex = 0; - OBFSComboBox.SelectedIndex = 0; - } + + RemarkTextBox.Text = _server.Remark; + AddressTextBox.Text = _server.Hostname; + PortTextBox.Text = _server.Port.ToString(); + PasswordTextBox.Text = _server.Password; + EncryptMethodComboBox.SelectedIndex = Global.EncryptMethods.SSR.IndexOf(_server.EncryptMethod); + ProtocolComboBox.SelectedIndex = Global.Protocols.IndexOf(_server.Protocol); + ProtocolParamTextBox.Text = _server.ProtocolParam; + OBFSComboBox.SelectedIndex = Global.OBFSs.IndexOf(_server.OBFS); + OBFSOptionParamTextBox.Text = _server.OBFSParam; } private void ShadowsocksR_FormClosing(object sender, FormClosingEventArgs e) @@ -76,16 +63,17 @@ namespace Netch.Forms.Server private void ComboBox_DrawItem(object sender, DrawItemEventArgs e) { - var cbx = sender as ComboBox; - if (cbx != null) + if (sender is ComboBox cbx) { e.DrawBackground(); if (e.Index >= 0) { - var sf = new StringFormat(); - sf.LineAlignment = StringAlignment.Center; - sf.Alignment = StringAlignment.Center; + var sf = new StringFormat + { + LineAlignment = StringAlignment.Center, + Alignment = StringAlignment.Center + }; var brush = new SolidBrush(cbx.ForeColor); @@ -101,43 +89,26 @@ namespace Netch.Forms.Server private void ControlButton_Click(object sender, EventArgs e) { - if (!Regex.Match(PortTextBox.Text, "^[0-9]+$").Success) + if (!int.TryParse(PortTextBox.Text, out var port)) { return; } - if (Index == -1) + + _server.Remark = RemarkTextBox.Text; + _server.Type = "SSR"; + _server.Hostname = AddressTextBox.Text; + _server.Port = port; + _server.Password = PasswordTextBox.Text; + _server.EncryptMethod = EncryptMethodComboBox.Text; + _server.Protocol = ProtocolComboBox.Text; + _server.ProtocolParam = ProtocolParamTextBox.Text; + _server.OBFS = OBFSComboBox.Text; + _server.OBFSParam = OBFSOptionParamTextBox.Text; + _server.Country = null; + + if (Global.Settings.Server.IndexOf(_server) == -1) { - Global.Settings.Server.Add(new Models.Server - { - Remark = RemarkTextBox.Text, - Type = "SSR", - Hostname = AddressTextBox.Text, - Port = int.Parse(PortTextBox.Text), - Password = PasswordTextBox.Text, - EncryptMethod = EncryptMethodComboBox.Text, - Protocol = ProtocolComboBox.Text, - ProtocolParam = ProtocolParamTextBox.Text, - OBFS = OBFSComboBox.Text, - OBFSParam = OBFSOptionParamTextBox.Text - }); - } - else - { - Global.Settings.Server[Index] = new Models.Server - { - Remark = RemarkTextBox.Text, - Group = Global.Settings.Server[Index].Group, - Type = "SSR", - Hostname = AddressTextBox.Text, - Port = int.Parse(PortTextBox.Text), - Password = PasswordTextBox.Text, - EncryptMethod = EncryptMethodComboBox.Text, - Protocol = ProtocolComboBox.Text, - ProtocolParam = ProtocolParamTextBox.Text, - OBFS = OBFSComboBox.Text, - OBFSParam = OBFSOptionParamTextBox.Text, - Country = null - }; + Global.Settings.Server.Add(_server); } Configuration.Save(); @@ -146,4 +117,4 @@ namespace Netch.Forms.Server Close(); } } -} +} \ No newline at end of file diff --git a/Netch/Forms/Server/Socks5.cs b/Netch/Forms/Server/Socks5.cs index 05b4c7cb..4bd0ae4e 100644 --- a/Netch/Forms/Server/Socks5.cs +++ b/Netch/Forms/Server/Socks5.cs @@ -1,6 +1,5 @@ using System; using System.Drawing; -using System.Text.RegularExpressions; using System.Windows.Forms; using Netch.Utils; @@ -8,7 +7,7 @@ namespace Netch.Forms.Server { public partial class Socks5 : Form { - public int Index; + private readonly Models.Server _server; /// /// 初始化 @@ -18,11 +17,15 @@ namespace Netch.Forms.Server { InitializeComponent(); - Index = index; + _server = index != -1 + ? Global.Settings.Server[index] + : new Models.Server(); } private void Shadowsocks_Load(object sender, EventArgs e) { + #region InitText + ConfigurationGroupBox.Text = i18N.Translate(ConfigurationGroupBox.Text); RemarkLabel.Text = i18N.Translate(RemarkLabel.Text); AddressLabel.Text = i18N.Translate(AddressLabel.Text); @@ -30,14 +33,13 @@ namespace Netch.Forms.Server PasswordLabel.Text = i18N.Translate(PasswordLabel.Text); ControlButton.Text = i18N.Translate(ControlButton.Text); - if (Index != -1) - { - RemarkTextBox.Text = Global.Settings.Server[Index].Remark; - AddressTextBox.Text = Global.Settings.Server[Index].Hostname; - PortTextBox.Text = Global.Settings.Server[Index].Port.ToString(); - UsernameTextBox.Text = Global.Settings.Server[Index].Username; - PasswordTextBox.Text = Global.Settings.Server[Index].Password; - } + #endregion + + RemarkTextBox.Text = _server.Remark; + AddressTextBox.Text = _server.Hostname; + PortTextBox.Text = _server.Port.ToString(); + UsernameTextBox.Text = _server.Username; + PasswordTextBox.Text = _server.Password; } private void Shadowsocks_FormClosing(object sender, FormClosingEventArgs e) @@ -47,16 +49,17 @@ namespace Netch.Forms.Server private void ComboBox_DrawItem(object sender, DrawItemEventArgs e) { - var cbx = sender as ComboBox; - if (cbx != null) + if (sender is ComboBox cbx) { e.DrawBackground(); if (e.Index >= 0) { - var sf = new StringFormat(); - sf.LineAlignment = StringAlignment.Center; - sf.Alignment = StringAlignment.Center; + var sf = new StringFormat + { + LineAlignment = StringAlignment.Center, + Alignment = StringAlignment.Center + }; var brush = new SolidBrush(cbx.ForeColor); @@ -72,35 +75,22 @@ namespace Netch.Forms.Server private void ControlButton_Click(object sender, EventArgs e) { - if (!Regex.Match(PortTextBox.Text, "^[0-9]+$").Success) + if (!int.TryParse(PortTextBox.Text, out var port)) { return; } - if (Index == -1) + + _server.Remark = RemarkTextBox.Text; + _server.Type = "Socks5"; + _server.Hostname = AddressTextBox.Text; + _server.Port = port; + _server.Username = UsernameTextBox.Text; + _server.Password = PasswordTextBox.Text; + _server.Country = null; + + if (Global.Settings.Server.IndexOf(_server) == -1) { - Global.Settings.Server.Add(new Models.Server - { - Remark = RemarkTextBox.Text, - Type = "Socks5", - Hostname = AddressTextBox.Text, - Port = int.Parse(PortTextBox.Text), - Username = UsernameTextBox.Text, - Password = PasswordTextBox.Text - }); - } - else - { - Global.Settings.Server[Index] = new Models.Server - { - Remark = RemarkTextBox.Text, - Group = Global.Settings.Server[Index].Group, - Type = "Socks5", - Hostname = AddressTextBox.Text, - Port = int.Parse(PortTextBox.Text), - Username = UsernameTextBox.Text, - Password = PasswordTextBox.Text, - Country = null - }; + Global.Settings.Server.Add(_server); } Configuration.Save(); @@ -109,4 +99,4 @@ namespace Netch.Forms.Server Close(); } } -} +} \ No newline at end of file diff --git a/Netch/Forms/Server/Trojan.cs b/Netch/Forms/Server/Trojan.cs index e2042dbd..b5c0c4a8 100644 --- a/Netch/Forms/Server/Trojan.cs +++ b/Netch/Forms/Server/Trojan.cs @@ -1,6 +1,5 @@ using System; using System.Drawing; -using System.Text.RegularExpressions; using System.Windows.Forms; using Netch.Utils; @@ -8,7 +7,7 @@ namespace Netch.Forms.Server { public partial class Trojan : Form { - public int Index; + private readonly Models.Server _server; /// /// 初始化 @@ -18,7 +17,10 @@ namespace Netch.Forms.Server { InitializeComponent(); - Index = index; + + _server = index != -1 + ? Global.Settings.Server[index] + : new Models.Server(); } private void Trojan_Load(object sender, EventArgs e) @@ -29,13 +31,10 @@ namespace Netch.Forms.Server PasswordLabel.Text = i18N.Translate(PasswordLabel.Text); ControlButton.Text = i18N.Translate(ControlButton.Text); - if (Index != -1) - { - RemarkTextBox.Text = Global.Settings.Server[Index].Remark; - AddressTextBox.Text = Global.Settings.Server[Index].Hostname; - PortTextBox.Text = Global.Settings.Server[Index].Port.ToString(); - PasswordTextBox.Text = Global.Settings.Server[Index].Password; - } + RemarkTextBox.Text = _server.Remark; + AddressTextBox.Text = _server.Hostname; + PortTextBox.Text = _server.Port.ToString(); + PasswordTextBox.Text = _server.Password; } private void Trojan_FormClosing(object sender, FormClosingEventArgs e) @@ -45,16 +44,17 @@ namespace Netch.Forms.Server private void ComboBox_DrawItem(object sender, DrawItemEventArgs e) { - var cbx = sender as ComboBox; - if (cbx != null) + if (sender is ComboBox cbx) { e.DrawBackground(); if (e.Index >= 0) { - var sf = new StringFormat(); - sf.LineAlignment = StringAlignment.Center; - sf.Alignment = StringAlignment.Center; + var sf = new StringFormat + { + LineAlignment = StringAlignment.Center, + Alignment = StringAlignment.Center + }; var brush = new SolidBrush(cbx.ForeColor); @@ -70,33 +70,22 @@ namespace Netch.Forms.Server private void ControlButton_Click(object sender, EventArgs e) { - if (!Regex.Match(PortTextBox.Text, "^[0-9]+$").Success) + if (!int.TryParse(PortTextBox.Text, out var port)) { return; } - if (Index == -1) + + + _server.Remark = RemarkTextBox.Text; + _server.Type = "Trojan"; + _server.Hostname = AddressTextBox.Text; + _server.Port = port; + _server.Password = PasswordTextBox.Text; + _server.Country = null; + + if (Global.Settings.Server.IndexOf(_server) == -1) { - Global.Settings.Server.Add(new Models.Server - { - Remark = RemarkTextBox.Text, - Type = "Trojan", - Hostname = AddressTextBox.Text, - Port = int.Parse(PortTextBox.Text), - Password = PasswordTextBox.Text - }); - } - else - { - Global.Settings.Server[Index] = new Models.Server - { - Remark = RemarkTextBox.Text, - Group = Global.Settings.Server[Index].Group, - Type = "Trojan", - Hostname = AddressTextBox.Text, - Port = int.Parse(PortTextBox.Text), - Password = PasswordTextBox.Text, - Country = null - }; + Global.Settings.Server.Add(_server); } Configuration.Save(); @@ -105,4 +94,4 @@ namespace Netch.Forms.Server Close(); } } -} +} \ No newline at end of file diff --git a/Netch/Forms/Server/Vmess.cs b/Netch/Forms/Server/Vmess.cs index 9cebf0ff..1cfffe19 100644 --- a/Netch/Forms/Server/Vmess.cs +++ b/Netch/Forms/Server/Vmess.cs @@ -1,6 +1,5 @@ using System; using System.Drawing; -using System.Text.RegularExpressions; using System.Windows.Forms; using Netch.Utils; @@ -8,27 +7,30 @@ namespace Netch.Forms.Server { public partial class VMess : Form { - public int Index; + private static Models.Server server; public VMess(int index = -1) { InitializeComponent(); - Index = index; + server = index != -1 + ? Global.Settings.Server[index] + : new Models.Server {EncryptMethod = Global.EncryptMethods.VMess[0]}; } private void ComboBox_DrawItem(object sender, DrawItemEventArgs e) { - var cbx = sender as ComboBox; - if (cbx != null) + if (sender is ComboBox cbx) { e.DrawBackground(); if (e.Index >= 0) { - var sf = new StringFormat(); - sf.LineAlignment = StringAlignment.Center; - sf.Alignment = StringAlignment.Center; + var sf = new StringFormat + { + LineAlignment = StringAlignment.Center, + Alignment = StringAlignment.Center + }; var brush = new SolidBrush(cbx.ForeColor); @@ -44,6 +46,8 @@ namespace Netch.Forms.Server private void VMess_Load(object sender, EventArgs e) { + #region InitText + ConfigurationGroupBox.Text = i18N.Translate(ConfigurationGroupBox.Text); RemarkLabel.Text = i18N.Translate(RemarkLabel.Text); AddressLabel.Text = i18N.Translate(AddressLabel.Text); @@ -60,50 +64,27 @@ namespace Netch.Forms.Server UseMuxCheckBox.Text = i18N.Translate(UseMuxCheckBox.Text); ControlButton.Text = i18N.Translate(ControlButton.Text); - foreach (var encrypt in Global.EncryptMethods.VMess) - { - EncryptMethodComboBox.Items.Add(encrypt); - } + EncryptMethodComboBox.Items.AddRange(Global.EncryptMethods.VMess.ToArray()); + TransferProtocolComboBox.Items.AddRange(Global.TransferProtocols.ToArray()); + FakeTypeComboBox.Items.AddRange(Global.FakeTypes.ToArray()); + QUICSecurityComboBox.Items.AddRange(Global.EncryptMethods.VMessQUIC.ToArray()); - foreach (var protocol in Global.TransferProtocols) - { - TransferProtocolComboBox.Items.Add(protocol); - } + #endregion - foreach (var fake in Global.FakeTypes) - { - FakeTypeComboBox.Items.Add(fake); - } - - foreach (var security in Global.EncryptMethods.VMessQUIC) - { - QUICSecurityComboBox.Items.Add(security); - } - - if (Index != -1) - { - RemarkTextBox.Text = Global.Settings.Server[Index].Remark; - AddressTextBox.Text = Global.Settings.Server[Index].Hostname; - PortTextBox.Text = Global.Settings.Server[Index].Port.ToString(); - UserIDTextBox.Text = Global.Settings.Server[Index].UserID; - AlterIDTextBox.Text = Global.Settings.Server[Index].AlterID.ToString(); - EncryptMethodComboBox.SelectedIndex = Global.EncryptMethods.VMess.IndexOf(Global.Settings.Server[Index].EncryptMethod); - TransferProtocolComboBox.SelectedIndex = Global.TransferProtocols.IndexOf(Global.Settings.Server[Index].TransferProtocol); - FakeTypeComboBox.SelectedIndex = Global.FakeTypes.IndexOf(Global.Settings.Server[Index].FakeType); - HostTextBox.Text = Global.Settings.Server[Index].Host; - PathTextBox.Text = Global.Settings.Server[Index].Path; - QUICSecurityComboBox.SelectedIndex = Global.EncryptMethods.VMessQUIC.IndexOf(Global.Settings.Server[Index].QUICSecure); - QUICSecretTextBox.Text = Global.Settings.Server[Index].QUICSecret; - TLSSecureCheckBox.Checked = Global.Settings.Server[Index].TLSSecure; - UseMuxCheckBox.Checked = Global.Settings.Server[Index].UseMux; - } - else - { - EncryptMethodComboBox.SelectedIndex = 0; - TransferProtocolComboBox.SelectedIndex = 0; - FakeTypeComboBox.SelectedIndex = 0; - QUICSecurityComboBox.SelectedIndex = 0; - } + RemarkTextBox.Text = server.Remark; + AddressTextBox.Text = server.Hostname; + PortTextBox.Text = server.Port.ToString(); + UserIDTextBox.Text = server.UserID; + AlterIDTextBox.Text = server.AlterID.ToString(); + EncryptMethodComboBox.SelectedIndex = Global.EncryptMethods.VMess.IndexOf(server.EncryptMethod); + TransferProtocolComboBox.SelectedIndex = Global.TransferProtocols.IndexOf(server.TransferProtocol); + FakeTypeComboBox.SelectedIndex = Global.FakeTypes.IndexOf(server.FakeType); + HostTextBox.Text = server.Host; + PathTextBox.Text = server.Path; + QUICSecurityComboBox.SelectedIndex = Global.EncryptMethods.VMessQUIC.IndexOf(server.QUICSecure); + QUICSecretTextBox.Text = server.QUICSecret; + TLSSecureCheckBox.Checked = server.TLSSecure; + UseMuxCheckBox.Checked = server.UseMux; } private void VMess_FormClosing(object sender, FormClosingEventArgs e) @@ -113,57 +94,42 @@ namespace Netch.Forms.Server private void ControlButton_Click(object sender, EventArgs e) { - if (!Regex.Match(PortTextBox.Text, "^[0-9]+$").Success) + if (!int.TryParse(PortTextBox.Text, out var port)) { return; } + if (AlterIDTextBox.Text == "") { MessageBoxX.Show(i18N.Translate("Please fill in alterID")); return; } - if (Index == -1) + + if (!int.TryParse(PortTextBox.Text, out var afterId)) { - Global.Settings.Server.Add(new Models.Server - { - Remark = RemarkTextBox.Text, - Type = "VMess", - Hostname = AddressTextBox.Text, - Port = int.Parse(PortTextBox.Text), - UserID = UserIDTextBox.Text, - AlterID = int.Parse(AlterIDTextBox.Text), - EncryptMethod = EncryptMethodComboBox.Text, - TransferProtocol = TransferProtocolComboBox.Text, - FakeType = FakeTypeComboBox.Text, - Host = HostTextBox.Text, - Path = PathTextBox.Text, - QUICSecure = QUICSecurityComboBox.Text, - QUICSecret = QUICSecretTextBox.Text, - TLSSecure = TLSSecureCheckBox.Checked, - UseMux = UseMuxCheckBox.Checked - }); + return; } - else + + server.Remark = RemarkTextBox.Text; + server.Type = "VMess"; + server.Hostname = AddressTextBox.Text; + server.Port = port; + server.UserID = UserIDTextBox.Text; + server.AlterID = afterId; + server.EncryptMethod = EncryptMethodComboBox.Text; + server.TransferProtocol = TransferProtocolComboBox.Text; + server.FakeType = FakeTypeComboBox.Text; + server.Host = HostTextBox.Text; + server.Path = PathTextBox.Text; + server.QUICSecure = QUICSecurityComboBox.Text; + server.QUICSecret = QUICSecretTextBox.Text; + server.TLSSecure = TLSSecureCheckBox.Checked; + server.UseMux = UseMuxCheckBox.Checked; + server.Country = null; + + if (Global.Settings.Server.IndexOf(server) == -1) { - Global.Settings.Server[Index] = new Models.Server - { - Remark = RemarkTextBox.Text, - Type = "VMess", - Hostname = AddressTextBox.Text, - Port = int.Parse(PortTextBox.Text), - UserID = UserIDTextBox.Text, - AlterID = int.Parse(AlterIDTextBox.Text), - EncryptMethod = EncryptMethodComboBox.Text, - TransferProtocol = TransferProtocolComboBox.Text, - FakeType = FakeTypeComboBox.Text, - Host = HostTextBox.Text, - Path = PathTextBox.Text, - QUICSecure = QUICSecurityComboBox.Text, - QUICSecret = QUICSecretTextBox.Text, - TLSSecure = TLSSecureCheckBox.Checked, - UseMux = UseMuxCheckBox.Checked, - Country = null - }; + Global.Settings.Server.Add(server); } Configuration.Save(); @@ -172,4 +138,4 @@ namespace Netch.Forms.Server Close(); } } -} +} \ No newline at end of file diff --git a/Netch/Models/Server.cs b/Netch/Models/Server.cs index b5240604..7e1b5c44 100644 --- a/Netch/Models/Server.cs +++ b/Netch/Models/Server.cs @@ -76,7 +76,7 @@ namespace Netch.Models /// /// 协议(SSR) /// - public string Protocol; + public string Protocol = Global.Protocols[0]; /// /// 协议参数(SSR) @@ -86,7 +86,7 @@ namespace Netch.Models /// /// 混淆(SSR) /// - public string OBFS; + public string OBFS = Global.OBFSs[0]; /// /// 混淆参数(SSR) @@ -96,12 +96,12 @@ namespace Netch.Models /// /// 传输协议(VMess) /// - public string TransferProtocol = "tcp"; + public string TransferProtocol = Global.TransferProtocols[0]; /// /// 伪装类型(VMess) /// - public string FakeType = string.Empty; + public string FakeType = Global.FakeTypes[0]; /// /// 伪装域名(VMess:HTTP、WebSocket、HTTP/2) @@ -116,7 +116,7 @@ namespace Netch.Models /// /// QUIC 加密方式(VMess) /// - public string QUICSecure = "none"; + public string QUICSecure = Global.EncryptMethods.VMessQUIC[0]; /// /// QUIC 加密密钥(VMess) @@ -131,7 +131,7 @@ namespace Netch.Models /// /// Mux 多路复用(VMess) /// - public bool UseMux = false; + public bool UseMux = true; /// /// 延迟 @@ -144,10 +144,10 @@ namespace Netch.Models public string Country; /// - /// 获取备注 - /// - /// 备注 - public override string ToString() + /// 获取备注 + /// + /// 备注 + public override string ToString() { if (string.IsNullOrWhiteSpace(Remark)) { @@ -220,4 +220,4 @@ namespace Netch.Models } } } -} +} \ No newline at end of file