Merge pull request #1 from 2dust/master

sync
pull/32/head
CGQAQ 2019-08-23 14:08:18 +08:00 committed by GitHub
commit 197d8f1ace
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 90 additions and 69 deletions

View File

@ -88,7 +88,8 @@ namespace v2rayN.Forms
{ {
case WM_QUERYENDSESSION: case WM_QUERYENDSESSION:
Utils.SaveLog("Windows shutdown UnsetProxy"); Utils.SaveLog("Windows shutdown UnsetProxy");
CloseV2ray(); //CloseV2ray();
ConfigHandler.ToJsonFile(config);
ProxySetting.UnsetProxy(); ProxySetting.UnsetProxy();
m.Result = (IntPtr)1; m.Result = (IntPtr)1;
break; break;
@ -256,7 +257,7 @@ namespace v2rayN.Forms
} }
private void ssMain_ItemClicked(object sender, ToolStripItemClickedEventArgs e) 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); Utils.SetClipboardData(e.ClickedItem.Text);
} }
@ -1283,7 +1284,7 @@ namespace v2rayN.Forms
ShowForm(); ShowForm();
string result = Convert.ToString(e.UserState); string result = Convert.ToString(e.UserState);
if (string.IsNullOrEmpty(result)) if (Utils.IsNullOrEmpty(result))
{ {
UI.Show(UIRes.I18N("NoValidQRcodeFound")); UI.Show(UIRes.I18N("NoValidQRcodeFound"));
} }
@ -1320,8 +1321,8 @@ namespace v2rayN.Forms
for (int k = 1; k <= config.subItem.Count; k++) for (int k = 1; k <= config.subItem.Count; k++)
{ {
string id = config.subItem[k - 1].id.Trim(); string id = config.subItem[k - 1].id.TrimEx();
string url = config.subItem[k - 1].url.Trim(); string url = config.subItem[k - 1].url.TrimEx();
string hashCode = $"{k}->"; string hashCode = $"{k}->";
if (config.subItem[k - 1].enabled == false) if (config.subItem[k - 1].enabled == false)
{ {

View File

@ -148,14 +148,14 @@ namespace v2rayN.Forms
{ {
//日志 //日志
bool logEnabled = chklogEnabled.Checked; bool logEnabled = chklogEnabled.Checked;
string loglevel = cmbloglevel.Text.Trim(); string loglevel = cmbloglevel.Text.TrimEx();
//Mux //Mux
bool muxEnabled = chkmuxEnabled.Checked; bool muxEnabled = chkmuxEnabled.Checked;
//本地监听 //本地监听
string localPort = txtlocalPort.Text.Trim(); string localPort = txtlocalPort.Text.TrimEx();
string protocol = cmbprotocol.Text.Trim(); string protocol = cmbprotocol.Text.TrimEx();
bool udpEnabled = chkudpEnabled.Checked; bool udpEnabled = chkudpEnabled.Checked;
bool sniffingEnabled = chksniffingEnabled.Checked; bool sniffingEnabled = chksniffingEnabled.Checked;
if (Utils.IsNullOrEmpty(localPort) || !Utils.IsNumberic(localPort)) if (Utils.IsNullOrEmpty(localPort) || !Utils.IsNumberic(localPort))
@ -174,8 +174,8 @@ namespace v2rayN.Forms
config.inbound[0].sniffingEnabled = sniffingEnabled; config.inbound[0].sniffingEnabled = sniffingEnabled;
//本地监听2 //本地监听2
string localPort2 = txtlocalPort2.Text.Trim(); string localPort2 = txtlocalPort2.Text.TrimEx();
string protocol2 = cmbprotocol2.Text.Trim(); string protocol2 = cmbprotocol2.Text.TrimEx();
bool udpEnabled2 = chkudpEnabled2.Checked; bool udpEnabled2 = chkudpEnabled2.Checked;
bool sniffingEnabled2 = chksniffingEnabled2.Checked; bool sniffingEnabled2 = chksniffingEnabled2.Checked;
if (chkAllowIn2.Checked) if (chkAllowIn2.Checked)
@ -215,7 +215,7 @@ namespace v2rayN.Forms
config.muxEnabled = muxEnabled; config.muxEnabled = muxEnabled;
//remoteDNS //remoteDNS
config.remoteDNS = txtremoteDNS.Text.Trim(); config.remoteDNS = txtremoteDNS.Text.TrimEx();
return 0; return 0;
} }
@ -230,9 +230,9 @@ namespace v2rayN.Forms
string domainStrategy = cmbdomainStrategy.Text; string domainStrategy = cmbdomainStrategy.Text;
string routingMode = cmbroutingMode.SelectedIndex.ToString(); string routingMode = cmbroutingMode.SelectedIndex.ToString();
string useragent = txtUseragent.Text.Trim(); string useragent = txtUseragent.Text.TrimEx();
string userdirect = txtUserdirect.Text.Trim(); string userdirect = txtUserdirect.Text.TrimEx();
string userblock = txtUserblock.Text.Trim(); string userblock = txtUserblock.Text.TrimEx();
config.domainStrategy = domainStrategy; config.domainStrategy = domainStrategy;
config.routingMode = routingMode; config.routingMode = routingMode;
@ -250,12 +250,12 @@ namespace v2rayN.Forms
/// <returns></returns> /// <returns></returns>
private int SaveKCP() private int SaveKCP()
{ {
string mtu = txtKcpmtu.Text.Trim(); string mtu = txtKcpmtu.Text.TrimEx();
string tti = txtKcptti.Text.Trim(); string tti = txtKcptti.Text.TrimEx();
string uplinkCapacity = txtKcpuplinkCapacity.Text.Trim(); string uplinkCapacity = txtKcpuplinkCapacity.Text.TrimEx();
string downlinkCapacity = txtKcpdownlinkCapacity.Text.Trim(); string downlinkCapacity = txtKcpdownlinkCapacity.Text.TrimEx();
string readBufferSize = txtKcpreadBufferSize.Text.Trim(); string readBufferSize = txtKcpreadBufferSize.Text.TrimEx();
string writeBufferSize = txtKcpwriteBufferSize.Text.Trim(); string writeBufferSize = txtKcpwriteBufferSize.Text.TrimEx();
bool congestion = chkKcpcongestion.Checked; bool congestion = chkKcpcongestion.Checked;
if (Utils.IsNullOrEmpty(mtu) || !Utils.IsNumberic(mtu) if (Utils.IsNullOrEmpty(mtu) || !Utils.IsNumberic(mtu)
@ -289,7 +289,7 @@ namespace v2rayN.Forms
Utils.SetAutoRun(chkAutoRun.Checked); Utils.SetAutoRun(chkAutoRun.Checked);
//自定义GFWList //自定义GFWList
config.urlGFWList = txturlGFWList.Text.Trim(); config.urlGFWList = txturlGFWList.Text.TrimEx();
config.allowLANConn = chkAllowLANConn.Checked; config.allowLANConn = chkAllowLANConn.Checked;

View File

@ -26,7 +26,7 @@ namespace v2rayN.Forms
if (Index >= 0) if (Index >= 0)
{ {
string url = ConfigHandler.GetVmessQRCode(config, Index); string url = ConfigHandler.GetVmessQRCode(config, Index);
if (string.IsNullOrEmpty(url)) if (Utils.IsNullOrEmpty(url))
{ {
picQRCode.Image = null; picQRCode.Image = null;
txtUrl.Text = string.Empty; txtUrl.Text = string.Empty;

View File

@ -35,8 +35,8 @@ namespace v2rayN.Forms
{ {
if (subItem != null) if (subItem != null)
{ {
subItem.remarks = txtRemarks.Text.Trim(); subItem.remarks = txtRemarks.Text.TrimEx();
subItem.url = txtUrl.Text.Trim(); subItem.url = txtUrl.Text.TrimEx();
subItem.enabled = chkEnabled.Checked; subItem.enabled = chkEnabled.Checked;
} }
} }

View File

@ -152,14 +152,14 @@ namespace v2rayN.Handler
vmessItem.configVersion = 2; vmessItem.configVersion = 2;
vmessItem.configType = (int)EConfigType.Vmess; vmessItem.configType = (int)EConfigType.Vmess;
vmessItem.address = vmessItem.address.Trim(); vmessItem.address = vmessItem.address.TrimEx();
vmessItem.id = vmessItem.id.Trim(); vmessItem.id = vmessItem.id.TrimEx();
vmessItem.security = vmessItem.security.Trim(); vmessItem.security = vmessItem.security.TrimEx();
vmessItem.network = vmessItem.network.Trim(); vmessItem.network = vmessItem.network.TrimEx();
vmessItem.headerType = vmessItem.headerType.Trim(); vmessItem.headerType = vmessItem.headerType.TrimEx();
vmessItem.requestHost = vmessItem.requestHost.Trim(); vmessItem.requestHost = vmessItem.requestHost.TrimEx();
vmessItem.path = vmessItem.path.Trim(); vmessItem.path = vmessItem.path.TrimEx();
vmessItem.streamSecurity = vmessItem.streamSecurity.Trim(); vmessItem.streamSecurity = vmessItem.streamSecurity.TrimEx();
if (index >= 0) if (index >= 0)
{ {
@ -328,7 +328,7 @@ namespace v2rayN.Handler
{ {
VmessQRCode vmessQRCode = new VmessQRCode(); VmessQRCode vmessQRCode = new VmessQRCode();
vmessQRCode.v = vmessItem.configVersion.ToString(); vmessQRCode.v = vmessItem.configVersion.ToString();
vmessQRCode.ps = vmessItem.remarks.Trim(); //备注也许很长 ; vmessQRCode.ps = vmessItem.remarks.TrimEx(); //备注也许很长 ;
vmessQRCode.add = vmessItem.address; vmessQRCode.add = vmessItem.address;
vmessQRCode.port = vmessItem.port.ToString(); vmessQRCode.port = vmessItem.port.ToString();
vmessQRCode.id = vmessItem.id; vmessQRCode.id = vmessItem.id;
@ -566,9 +566,9 @@ namespace v2rayN.Handler
vmessItem.configVersion = 2; vmessItem.configVersion = 2;
vmessItem.configType = (int)EConfigType.Shadowsocks; vmessItem.configType = (int)EConfigType.Shadowsocks;
vmessItem.address = vmessItem.address.Trim(); vmessItem.address = vmessItem.address.TrimEx();
vmessItem.id = vmessItem.id.Trim(); vmessItem.id = vmessItem.id.TrimEx();
vmessItem.security = vmessItem.security.Trim(); vmessItem.security = vmessItem.security.TrimEx();
if (index >= 0) if (index >= 0)
{ {
@ -607,7 +607,7 @@ namespace v2rayN.Handler
vmessItem.configVersion = 2; vmessItem.configVersion = 2;
vmessItem.configType = (int)EConfigType.Socks; vmessItem.configType = (int)EConfigType.Socks;
vmessItem.address = vmessItem.address.Trim(); vmessItem.address = vmessItem.address.TrimEx();
if (index >= 0) if (index >= 0)
{ {

View File

@ -235,7 +235,7 @@ namespace v2rayN.Handler
for (int k = 0; k < userRule.Count; k++) for (int k = 0; k < userRule.Count; k++)
{ {
string url = userRule[k].Trim(); string url = userRule[k].TrimEx();
if (Utils.IsNullOrEmpty(url)) if (Utils.IsNullOrEmpty(url))
{ {
continue; continue;
@ -400,8 +400,8 @@ namespace v2rayN.Handler
serversItem.address = config.address(); serversItem.address = config.address();
serversItem.port = config.port(); serversItem.port = config.port();
if (!string.IsNullOrEmpty(config.security()) if (!Utils.IsNullOrEmpty(config.security())
&& !string.IsNullOrEmpty(config.id())) && !Utils.IsNullOrEmpty(config.id()))
{ {
var socksUsersItem = new SocksUsersItem(); var socksUsersItem = new SocksUsersItem();
socksUsersItem.user = config.security(); socksUsersItem.user = config.security();
@ -1073,7 +1073,7 @@ namespace v2rayN.Handler
try try
{ {
//载入配置文件 //载入配置文件
string result = clipboardData.Trim();// Utils.GetClipboardData(); string result = clipboardData.TrimEx();// Utils.GetClipboardData();
if (Utils.IsNullOrEmpty(result)) if (Utils.IsNullOrEmpty(result))
{ {
msg = UIRes.I18N("FailedReadConfiguration"); msg = UIRes.I18N("FailedReadConfiguration");
@ -1104,23 +1104,26 @@ namespace v2rayN.Handler
vmessItem.network = Global.DefaultNetwork; vmessItem.network = Global.DefaultNetwork;
vmessItem.headerType = Global.None; vmessItem.headerType = Global.None;
// v2ray 鬼才机场主们往往会各种不填或者null后面trim的时候会出Exception
Func<String, String> check_null = nullableString => nullableString == null ? "" : nullableString;
vmessItem.configVersion = Utils.ToInt(vmessQRCode.v); vmessItem.configVersion = Utils.ToInt(vmessQRCode.v);
vmessItem.remarks = check_null(vmessQRCode.ps); vmessItem.remarks = Utils.ToString(vmessQRCode.ps);
vmessItem.address = check_null(vmessQRCode.add); vmessItem.address = Utils.ToString(vmessQRCode.add);
vmessItem.port = Utils.ToInt(vmessQRCode.port); vmessItem.port = Utils.ToInt(vmessQRCode.port);
vmessItem.id = check_null(vmessQRCode.id); vmessItem.id = Utils.ToString(vmessQRCode.id);
vmessItem.alterId = Utils.ToInt(vmessQRCode.aid); vmessItem.alterId = Utils.ToInt(vmessQRCode.aid);
// 上面有默认值, 不需要手动赋值空字符串
if(vmessQRCode.net != null) if (!Utils.IsNullOrEmpty(vmessQRCode.net))
{
vmessItem.network = vmessQRCode.net; vmessItem.network = vmessQRCode.net;
if(vmessQRCode.type != null) }
vmessItem.headerType = check_null(vmessQRCode.type); if (!Utils.IsNullOrEmpty(vmessQRCode.type))
vmessItem.requestHost = check_null(vmessQRCode.host); {
vmessItem.path = check_null(vmessQRCode.host); vmessItem.headerType = vmessQRCode.type;
vmessItem.streamSecurity = check_null(vmessQRCode.tls); }
vmessItem.requestHost = Utils.ToString(vmessQRCode.host);
vmessItem.path = Utils.ToString(vmessQRCode.path);
vmessItem.streamSecurity = Utils.ToString(vmessQRCode.tls);
} }
ConfigHandler.UpgradeServerVersion(ref vmessItem); ConfigHandler.UpgradeServerVersion(ref vmessItem);

View File

@ -57,7 +57,7 @@ namespace v2rayN.HttpProxyHandler
// Returns the data received from the host to the console. // Returns the data received from the host to the console.
string returndata = Encoding.UTF8.GetString(bytes); string returndata = Encoding.UTF8.GetString(bytes);
if (!string.IsNullOrEmpty(returndata) if (!Utils.IsNullOrEmpty(returndata)
&& returndata.IndexOf("/pac/") >= 0 && returndata.IndexOf("/pac/") >= 0
&& netStream.CanWrite) && netStream.CanWrite)
{ {

View File

@ -19,7 +19,7 @@ namespace v2rayN.HttpProxyHandler
{ {
InternetPerConnOptionList list = new InternetPerConnOptionList(); 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]; InternetConnectionOption[] options = new InternetConnectionOption[optionCount];
// USE a proxy server ... // USE a proxy server ...
options[0].m_Option = PerConnOption.INTERNET_PER_CONN_FLAGS; options[0].m_Option = PerConnOption.INTERNET_PER_CONN_FLAGS;

View File

@ -112,7 +112,7 @@ namespace v2rayN.Mode
{ {
return string.Empty; return string.Empty;
} }
return vmess[index].address.Trim(); return vmess[index].address.TrimEx();
} }
public int port() public int port()
@ -130,7 +130,7 @@ namespace v2rayN.Mode
{ {
return string.Empty; return string.Empty;
} }
return vmess[index].id.Trim(); return vmess[index].id.TrimEx();
} }
public int alterId() public int alterId()
@ -148,7 +148,7 @@ namespace v2rayN.Mode
{ {
return string.Empty; return string.Empty;
} }
return vmess[index].security.Trim(); return vmess[index].security.TrimEx();
} }
public string remarks() public string remarks()
@ -157,7 +157,7 @@ namespace v2rayN.Mode
{ {
return string.Empty; return string.Empty;
} }
return vmess[index].remarks.Trim(); return vmess[index].remarks.TrimEx();
} }
public string network() public string network()
{ {
@ -165,7 +165,7 @@ namespace v2rayN.Mode
{ {
return Global.DefaultNetwork; return Global.DefaultNetwork;
} }
return vmess[index].network.Trim(); return vmess[index].network.TrimEx();
} }
public string headerType() public string headerType()
{ {
@ -173,7 +173,7 @@ namespace v2rayN.Mode
{ {
return Global.None; return Global.None;
} }
return vmess[index].headerType.Replace(" ", "").Trim(); return vmess[index].headerType.Replace(" ", "").TrimEx();
} }
public string requestHost() public string requestHost()
{ {
@ -181,7 +181,7 @@ namespace v2rayN.Mode
{ {
return string.Empty; return string.Empty;
} }
return vmess[index].requestHost.Replace(" ", "").Trim(); return vmess[index].requestHost.Replace(" ", "").TrimEx();
} }
public string path() public string path()
{ {
@ -189,7 +189,7 @@ namespace v2rayN.Mode
{ {
return string.Empty; return string.Empty;
} }
return vmess[index].path.Replace(" ", "").Trim(); return vmess[index].path.Replace(" ", "").TrimEx();
} }
public string streamSecurity() public string streamSecurity()
{ {

View File

@ -49,5 +49,5 @@ namespace v2rayN.Mode
/// 底层传输安全 /// 底层传输安全
/// </summary> /// </summary>
public string tls { get; set; } = string.Empty; public string tls { get; set; } = string.Empty;
} }
} }

View File

@ -33,4 +33,4 @@ using System.Runtime.InteropServices;
// 方法是按如下所示使用“*”: // 方法是按如下所示使用“*”:
//[assembly: AssemblyVersion("1.0.*")] //[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyVersion("1.0.0")] //[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyFileVersion("2.35")] [assembly: AssemblyFileVersion("2.36")]

View File

@ -43,5 +43,10 @@ namespace v2rayN
yield return line; yield return line;
} }
} }
public static string TrimEx(this string value)
{
return value == null ? string.Empty : value.Trim();
}
} }
} }

View File

@ -212,7 +212,7 @@ namespace v2rayN
{ {
try try
{ {
plainText = plainText.Trim() plainText = plainText.TrimEx()
.Replace("\n", "") .Replace("\n", "")
.Replace("\r\n", "") .Replace("\r\n", "")
.Replace("\r", "") .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 #endregion
@ -304,7 +316,7 @@ namespace v2rayN
} }
//清除要验证字符串中的空格 //清除要验证字符串中的空格
//ip = ip.Trim(); //ip = ip.TrimEx();
//可能是CIDR //可能是CIDR
if (ip.IndexOf(@"/") > 0) 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})+$"; string pattern = @"^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$";