diff --git a/v2rayN/v2rayN/Forms/MainForm.cs b/v2rayN/v2rayN/Forms/MainForm.cs index dc353783..b96cc3a7 100644 --- a/v2rayN/v2rayN/Forms/MainForm.cs +++ b/v2rayN/v2rayN/Forms/MainForm.cs @@ -88,7 +88,8 @@ namespace v2rayN.Forms { case WM_QUERYENDSESSION: Utils.SaveLog("Windows shutdown UnsetProxy"); - CloseV2ray(); + //CloseV2ray(); + ConfigHandler.ToJsonFile(config); ProxySetting.UnsetProxy(); m.Result = (IntPtr)1; break; @@ -256,7 +257,7 @@ namespace v2rayN.Forms } private void ssMain_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { - if (!string.IsNullOrEmpty(e.ClickedItem.Text)) + if (!Utils.IsNullOrEmpty(e.ClickedItem.Text)) { Utils.SetClipboardData(e.ClickedItem.Text); } @@ -1283,7 +1284,7 @@ namespace v2rayN.Forms ShowForm(); string result = Convert.ToString(e.UserState); - if (string.IsNullOrEmpty(result)) + if (Utils.IsNullOrEmpty(result)) { UI.Show(UIRes.I18N("NoValidQRcodeFound")); } @@ -1320,8 +1321,8 @@ namespace v2rayN.Forms for (int k = 1; k <= config.subItem.Count; k++) { - string id = config.subItem[k - 1].id.Trim(); - string url = config.subItem[k - 1].url.Trim(); + string id = config.subItem[k - 1].id.TrimEx(); + string url = config.subItem[k - 1].url.TrimEx(); string hashCode = $"{k}->"; if (config.subItem[k - 1].enabled == false) { diff --git a/v2rayN/v2rayN/Forms/OptionSettingForm.cs b/v2rayN/v2rayN/Forms/OptionSettingForm.cs index 6cf8ebb4..e45af2b6 100644 --- a/v2rayN/v2rayN/Forms/OptionSettingForm.cs +++ b/v2rayN/v2rayN/Forms/OptionSettingForm.cs @@ -148,14 +148,14 @@ namespace v2rayN.Forms { //日志 bool logEnabled = chklogEnabled.Checked; - string loglevel = cmbloglevel.Text.Trim(); + string loglevel = cmbloglevel.Text.TrimEx(); //Mux bool muxEnabled = chkmuxEnabled.Checked; //本地监听 - string localPort = txtlocalPort.Text.Trim(); - string protocol = cmbprotocol.Text.Trim(); + string localPort = txtlocalPort.Text.TrimEx(); + string protocol = cmbprotocol.Text.TrimEx(); bool udpEnabled = chkudpEnabled.Checked; bool sniffingEnabled = chksniffingEnabled.Checked; if (Utils.IsNullOrEmpty(localPort) || !Utils.IsNumberic(localPort)) @@ -174,8 +174,8 @@ namespace v2rayN.Forms config.inbound[0].sniffingEnabled = sniffingEnabled; //本地监听2 - string localPort2 = txtlocalPort2.Text.Trim(); - string protocol2 = cmbprotocol2.Text.Trim(); + string localPort2 = txtlocalPort2.Text.TrimEx(); + string protocol2 = cmbprotocol2.Text.TrimEx(); bool udpEnabled2 = chkudpEnabled2.Checked; bool sniffingEnabled2 = chksniffingEnabled2.Checked; if (chkAllowIn2.Checked) @@ -215,7 +215,7 @@ namespace v2rayN.Forms config.muxEnabled = muxEnabled; //remoteDNS - config.remoteDNS = txtremoteDNS.Text.Trim(); + config.remoteDNS = txtremoteDNS.Text.TrimEx(); return 0; } @@ -230,9 +230,9 @@ namespace v2rayN.Forms string domainStrategy = cmbdomainStrategy.Text; string routingMode = cmbroutingMode.SelectedIndex.ToString(); - string useragent = txtUseragent.Text.Trim(); - string userdirect = txtUserdirect.Text.Trim(); - string userblock = txtUserblock.Text.Trim(); + string useragent = txtUseragent.Text.TrimEx(); + string userdirect = txtUserdirect.Text.TrimEx(); + string userblock = txtUserblock.Text.TrimEx(); config.domainStrategy = domainStrategy; config.routingMode = routingMode; @@ -250,12 +250,12 @@ namespace v2rayN.Forms /// private int SaveKCP() { - string mtu = txtKcpmtu.Text.Trim(); - string tti = txtKcptti.Text.Trim(); - string uplinkCapacity = txtKcpuplinkCapacity.Text.Trim(); - string downlinkCapacity = txtKcpdownlinkCapacity.Text.Trim(); - string readBufferSize = txtKcpreadBufferSize.Text.Trim(); - string writeBufferSize = txtKcpwriteBufferSize.Text.Trim(); + string mtu = txtKcpmtu.Text.TrimEx(); + string tti = txtKcptti.Text.TrimEx(); + string uplinkCapacity = txtKcpuplinkCapacity.Text.TrimEx(); + string downlinkCapacity = txtKcpdownlinkCapacity.Text.TrimEx(); + string readBufferSize = txtKcpreadBufferSize.Text.TrimEx(); + string writeBufferSize = txtKcpwriteBufferSize.Text.TrimEx(); bool congestion = chkKcpcongestion.Checked; if (Utils.IsNullOrEmpty(mtu) || !Utils.IsNumberic(mtu) @@ -289,7 +289,7 @@ namespace v2rayN.Forms Utils.SetAutoRun(chkAutoRun.Checked); //自定义GFWList - config.urlGFWList = txturlGFWList.Text.Trim(); + config.urlGFWList = txturlGFWList.Text.TrimEx(); config.allowLANConn = chkAllowLANConn.Checked; diff --git a/v2rayN/v2rayN/Forms/QRCodeControl.cs b/v2rayN/v2rayN/Forms/QRCodeControl.cs index b9c86ed3..ea5e7fda 100644 --- a/v2rayN/v2rayN/Forms/QRCodeControl.cs +++ b/v2rayN/v2rayN/Forms/QRCodeControl.cs @@ -26,7 +26,7 @@ namespace v2rayN.Forms if (Index >= 0) { string url = ConfigHandler.GetVmessQRCode(config, Index); - if (string.IsNullOrEmpty(url)) + if (Utils.IsNullOrEmpty(url)) { picQRCode.Image = null; txtUrl.Text = string.Empty; diff --git a/v2rayN/v2rayN/Forms/SubSettingControl.cs b/v2rayN/v2rayN/Forms/SubSettingControl.cs index 26f83e3d..a4ee2189 100644 --- a/v2rayN/v2rayN/Forms/SubSettingControl.cs +++ b/v2rayN/v2rayN/Forms/SubSettingControl.cs @@ -35,8 +35,8 @@ namespace v2rayN.Forms { if (subItem != null) { - subItem.remarks = txtRemarks.Text.Trim(); - subItem.url = txtUrl.Text.Trim(); + subItem.remarks = txtRemarks.Text.TrimEx(); + subItem.url = txtUrl.Text.TrimEx(); subItem.enabled = chkEnabled.Checked; } } diff --git a/v2rayN/v2rayN/Handler/ConfigHandler.cs b/v2rayN/v2rayN/Handler/ConfigHandler.cs index c9d18b62..ba229b80 100644 --- a/v2rayN/v2rayN/Handler/ConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/ConfigHandler.cs @@ -152,14 +152,14 @@ namespace v2rayN.Handler vmessItem.configVersion = 2; vmessItem.configType = (int)EConfigType.Vmess; - vmessItem.address = vmessItem.address.Trim(); - vmessItem.id = vmessItem.id.Trim(); - vmessItem.security = vmessItem.security.Trim(); - vmessItem.network = vmessItem.network.Trim(); - vmessItem.headerType = vmessItem.headerType.Trim(); - vmessItem.requestHost = vmessItem.requestHost.Trim(); - vmessItem.path = vmessItem.path.Trim(); - vmessItem.streamSecurity = vmessItem.streamSecurity.Trim(); + vmessItem.address = vmessItem.address.TrimEx(); + vmessItem.id = vmessItem.id.TrimEx(); + vmessItem.security = vmessItem.security.TrimEx(); + vmessItem.network = vmessItem.network.TrimEx(); + vmessItem.headerType = vmessItem.headerType.TrimEx(); + vmessItem.requestHost = vmessItem.requestHost.TrimEx(); + vmessItem.path = vmessItem.path.TrimEx(); + vmessItem.streamSecurity = vmessItem.streamSecurity.TrimEx(); if (index >= 0) { @@ -328,7 +328,7 @@ namespace v2rayN.Handler { VmessQRCode vmessQRCode = new VmessQRCode(); vmessQRCode.v = vmessItem.configVersion.ToString(); - vmessQRCode.ps = vmessItem.remarks.Trim(); //备注也许很长 ; + vmessQRCode.ps = vmessItem.remarks.TrimEx(); //备注也许很长 ; vmessQRCode.add = vmessItem.address; vmessQRCode.port = vmessItem.port.ToString(); vmessQRCode.id = vmessItem.id; @@ -566,9 +566,9 @@ namespace v2rayN.Handler vmessItem.configVersion = 2; vmessItem.configType = (int)EConfigType.Shadowsocks; - vmessItem.address = vmessItem.address.Trim(); - vmessItem.id = vmessItem.id.Trim(); - vmessItem.security = vmessItem.security.Trim(); + vmessItem.address = vmessItem.address.TrimEx(); + vmessItem.id = vmessItem.id.TrimEx(); + vmessItem.security = vmessItem.security.TrimEx(); if (index >= 0) { @@ -607,7 +607,7 @@ namespace v2rayN.Handler vmessItem.configVersion = 2; vmessItem.configType = (int)EConfigType.Socks; - vmessItem.address = vmessItem.address.Trim(); + vmessItem.address = vmessItem.address.TrimEx(); if (index >= 0) { diff --git a/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs b/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs index 1890a4c7..350bfccd 100644 --- a/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs @@ -235,7 +235,7 @@ namespace v2rayN.Handler for (int k = 0; k < userRule.Count; k++) { - string url = userRule[k].Trim(); + string url = userRule[k].TrimEx(); if (Utils.IsNullOrEmpty(url)) { continue; @@ -400,8 +400,8 @@ namespace v2rayN.Handler serversItem.address = config.address(); serversItem.port = config.port(); - if (!string.IsNullOrEmpty(config.security()) - && !string.IsNullOrEmpty(config.id())) + if (!Utils.IsNullOrEmpty(config.security()) + && !Utils.IsNullOrEmpty(config.id())) { var socksUsersItem = new SocksUsersItem(); socksUsersItem.user = config.security(); @@ -1073,7 +1073,7 @@ namespace v2rayN.Handler try { //载入配置文件 - string result = clipboardData.Trim();// Utils.GetClipboardData(); + string result = clipboardData.TrimEx();// Utils.GetClipboardData(); if (Utils.IsNullOrEmpty(result)) { msg = UIRes.I18N("FailedReadConfiguration"); @@ -1104,23 +1104,26 @@ namespace v2rayN.Handler vmessItem.network = Global.DefaultNetwork; vmessItem.headerType = Global.None; - // v2ray 鬼才机场主们往往会各种不填,或者null,后面trim的时候会出Exception - Func check_null = nullableString => nullableString == null ? "" : nullableString; - + vmessItem.configVersion = Utils.ToInt(vmessQRCode.v); - vmessItem.remarks = check_null(vmessQRCode.ps); - vmessItem.address = check_null(vmessQRCode.add); + vmessItem.remarks = Utils.ToString(vmessQRCode.ps); + vmessItem.address = Utils.ToString(vmessQRCode.add); vmessItem.port = Utils.ToInt(vmessQRCode.port); - vmessItem.id = check_null(vmessQRCode.id); + vmessItem.id = Utils.ToString(vmessQRCode.id); vmessItem.alterId = Utils.ToInt(vmessQRCode.aid); - // 上面有默认值, 不需要手动赋值空字符串 - if(vmessQRCode.net != null) + + if (!Utils.IsNullOrEmpty(vmessQRCode.net)) + { vmessItem.network = vmessQRCode.net; - if(vmessQRCode.type != null) - vmessItem.headerType = check_null(vmessQRCode.type); - vmessItem.requestHost = check_null(vmessQRCode.host); - vmessItem.path = check_null(vmessQRCode.host); - vmessItem.streamSecurity = check_null(vmessQRCode.tls); + } + if (!Utils.IsNullOrEmpty(vmessQRCode.type)) + { + vmessItem.headerType = vmessQRCode.type; + } + + vmessItem.requestHost = Utils.ToString(vmessQRCode.host); + vmessItem.path = Utils.ToString(vmessQRCode.host); + vmessItem.streamSecurity = Utils.ToString(vmessQRCode.tls); } ConfigHandler.UpgradeServerVersion(ref vmessItem); diff --git a/v2rayN/v2rayN/HttpProxyHandler/HttpWebServerB.cs b/v2rayN/v2rayN/HttpProxyHandler/HttpWebServerB.cs index b3b43530..ebb68702 100644 --- a/v2rayN/v2rayN/HttpProxyHandler/HttpWebServerB.cs +++ b/v2rayN/v2rayN/HttpProxyHandler/HttpWebServerB.cs @@ -57,7 +57,7 @@ namespace v2rayN.HttpProxyHandler // Returns the data received from the host to the console. string returndata = Encoding.UTF8.GetString(bytes); - if (!string.IsNullOrEmpty(returndata) + if (!Utils.IsNullOrEmpty(returndata) && returndata.IndexOf("/pac/") >= 0 && netStream.CanWrite) { diff --git a/v2rayN/v2rayN/HttpProxyHandler/ProxySetting.cs b/v2rayN/v2rayN/HttpProxyHandler/ProxySetting.cs index ee1c3e8b..638a1131 100644 --- a/v2rayN/v2rayN/HttpProxyHandler/ProxySetting.cs +++ b/v2rayN/v2rayN/HttpProxyHandler/ProxySetting.cs @@ -19,7 +19,7 @@ namespace v2rayN.HttpProxyHandler { InternetPerConnOptionList list = new InternetPerConnOptionList(); - int optionCount = string.IsNullOrEmpty(strProxy) ? 1 : (string.IsNullOrEmpty(exceptions) ? 2 : 3); + int optionCount = Utils.IsNullOrEmpty(strProxy) ? 1 : (Utils.IsNullOrEmpty(exceptions) ? 2 : 3); InternetConnectionOption[] options = new InternetConnectionOption[optionCount]; // USE a proxy server ... options[0].m_Option = PerConnOption.INTERNET_PER_CONN_FLAGS; diff --git a/v2rayN/v2rayN/Mode/Config.cs b/v2rayN/v2rayN/Mode/Config.cs index fc7ce509..929efece 100644 --- a/v2rayN/v2rayN/Mode/Config.cs +++ b/v2rayN/v2rayN/Mode/Config.cs @@ -112,7 +112,7 @@ namespace v2rayN.Mode { return string.Empty; } - return vmess[index].address.Trim(); + return vmess[index].address.TrimEx(); } public int port() @@ -130,7 +130,7 @@ namespace v2rayN.Mode { return string.Empty; } - return vmess[index].id.Trim(); + return vmess[index].id.TrimEx(); } public int alterId() @@ -148,7 +148,7 @@ namespace v2rayN.Mode { return string.Empty; } - return vmess[index].security.Trim(); + return vmess[index].security.TrimEx(); } public string remarks() @@ -157,7 +157,7 @@ namespace v2rayN.Mode { return string.Empty; } - return vmess[index].remarks.Trim(); + return vmess[index].remarks.TrimEx(); } public string network() { @@ -165,7 +165,7 @@ namespace v2rayN.Mode { return Global.DefaultNetwork; } - return vmess[index].network.Trim(); + return vmess[index].network.TrimEx(); } public string headerType() { @@ -173,7 +173,7 @@ namespace v2rayN.Mode { return Global.None; } - return vmess[index].headerType.Replace(" ", "").Trim(); + return vmess[index].headerType.Replace(" ", "").TrimEx(); } public string requestHost() { @@ -181,7 +181,7 @@ namespace v2rayN.Mode { return string.Empty; } - return vmess[index].requestHost.Replace(" ", "").Trim(); + return vmess[index].requestHost.Replace(" ", "").TrimEx(); } public string path() { @@ -189,7 +189,7 @@ namespace v2rayN.Mode { return string.Empty; } - return vmess[index].path.Replace(" ", "").Trim(); + return vmess[index].path.Replace(" ", "").TrimEx(); } public string streamSecurity() { diff --git a/v2rayN/v2rayN/Mode/VmessQRCode.cs b/v2rayN/v2rayN/Mode/VmessQRCode.cs index 0c86243b..0a9cc8ab 100644 --- a/v2rayN/v2rayN/Mode/VmessQRCode.cs +++ b/v2rayN/v2rayN/Mode/VmessQRCode.cs @@ -49,5 +49,5 @@ namespace v2rayN.Mode /// 底层传输安全 /// public string tls { get; set; } = string.Empty; - } + } } diff --git a/v2rayN/v2rayN/Properties/AssemblyInfo.cs b/v2rayN/v2rayN/Properties/AssemblyInfo.cs index 5e3d85a2..1d22b918 100644 --- a/v2rayN/v2rayN/Properties/AssemblyInfo.cs +++ b/v2rayN/v2rayN/Properties/AssemblyInfo.cs @@ -33,4 +33,4 @@ using System.Runtime.InteropServices; // 方法是按如下所示使用“*”: //[assembly: AssemblyVersion("1.0.*")] //[assembly: AssemblyVersion("1.0.0")] -[assembly: AssemblyFileVersion("2.35")] +[assembly: AssemblyFileVersion("2.36")] diff --git a/v2rayN/v2rayN/StringEx.cs b/v2rayN/v2rayN/StringEx.cs index 428567e9..59916b32 100644 --- a/v2rayN/v2rayN/StringEx.cs +++ b/v2rayN/v2rayN/StringEx.cs @@ -43,5 +43,10 @@ namespace v2rayN yield return line; } } + + public static string TrimEx(this string value) + { + return value == null ? string.Empty : value.Trim(); + } } } diff --git a/v2rayN/v2rayN/Tool/Utils.cs b/v2rayN/v2rayN/Tool/Utils.cs index f206454d..fb8e7a20 100644 --- a/v2rayN/v2rayN/Tool/Utils.cs +++ b/v2rayN/v2rayN/Tool/Utils.cs @@ -212,7 +212,7 @@ namespace v2rayN { try { - plainText = plainText.Trim() + plainText = plainText.TrimEx() .Replace("\n", "") .Replace("\r\n", "") .Replace("\r", "") @@ -250,6 +250,18 @@ namespace v2rayN } } + public static string ToString(object obj) + { + try + { + return (obj == null ? string.Empty : obj.ToString()); + } + catch + { + return string.Empty; + } + } + #endregion @@ -304,7 +316,7 @@ namespace v2rayN } //清除要验证字符串中的空格 - //ip = ip.Trim(); + //ip = ip.TrimEx(); //可能是CIDR if (ip.IndexOf(@"/") > 0) { @@ -340,7 +352,7 @@ namespace v2rayN } //清除要验证字符串中的空格 - //domain = domain.Trim(); + //domain = domain.TrimEx(); //模式字符串 string pattern = @"^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$";