From 291fd491ff2a4a978e547d25cf623d07f6517263 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Thu, 17 Mar 2022 19:07:06 +0800 Subject: [PATCH] Improve speedtest --- v2rayN/v2rayN/Handler/SpeedtestHandler.cs | 8 ++-- v2rayN/v2rayN/Handler/V2rayConfigHandler.cs | 8 ++++ v2rayN/v2rayN/Mode/ServerTestItem.cs | 4 ++ v2rayN/v2rayN/Tool/Utils.cs | 46 ++++++++++++++++----- 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs index 2e7d7746..db05ec2f 100644 --- a/v2rayN/v2rayN/Handler/SpeedtestHandler.cs +++ b/v2rayN/v2rayN/Handler/SpeedtestHandler.cs @@ -125,11 +125,11 @@ namespace v2rayN.Handler List tasks = new List(); foreach (var it in _selecteds) { - if (it.configType == (int)EConfigType.Custom) + if (!it.allowTest) { continue; } - if (it.port <= 0) + if (it.configType == (int)EConfigType.Custom) { continue; } @@ -222,11 +222,11 @@ namespace v2rayN.Handler var timeout = 10; foreach (var it in _selecteds) { - if (it.configType == (int)EConfigType.Custom) + if (!it.allowTest) { continue; } - if (it.port <= 0) + if (it.configType == (int)EConfigType.Custom) { continue; } diff --git a/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs b/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs index cf78774a..5324683c 100644 --- a/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs +++ b/v2rayN/v2rayN/Handler/V2rayConfigHandler.cs @@ -1440,6 +1440,13 @@ namespace v2rayN.Handler { continue; } + if (it.configType == (int)EConfigType.Vmess || it.configType == (int)EConfigType.VLESS) + { + if (!Utils.IsGuidByParse(configCopy.GetVmessItem(it.indexId).id)) + { + continue; + } + } //find unuse port var port = httpPort; @@ -1461,6 +1468,7 @@ namespace v2rayN.Handler continue; } it.port = port; + it.allowTest = true; Inbounds inbound = new Inbounds { diff --git a/v2rayN/v2rayN/Mode/ServerTestItem.cs b/v2rayN/v2rayN/Mode/ServerTestItem.cs index c15aa9ea..2d204a8d 100644 --- a/v2rayN/v2rayN/Mode/ServerTestItem.cs +++ b/v2rayN/v2rayN/Mode/ServerTestItem.cs @@ -21,5 +21,9 @@ namespace v2rayN.Mode { get; set; } + public bool allowTest + { + get; set; + } } } diff --git a/v2rayN/v2rayN/Tool/Utils.cs b/v2rayN/v2rayN/Tool/Utils.cs index 7de1c760..f0f3076d 100644 --- a/v2rayN/v2rayN/Tool/Utils.cs +++ b/v2rayN/v2rayN/Tool/Utils.cs @@ -22,6 +22,7 @@ using v2rayN.Base; using Newtonsoft.Json.Linq; using System.Web; using log4net; +using System.Linq; namespace v2rayN { @@ -748,20 +749,26 @@ namespace v2rayN public static bool PortInUse(int port) { bool inUse = false; - - IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); - IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners(); - - var lstIpEndPoints = new List(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()); - - foreach (IPEndPoint endPoint in ipEndPoints) + try { - if (endPoint.Port == port) + IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); + IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners(); + + var lstIpEndPoints = new List(IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners()); + + foreach (IPEndPoint endPoint in ipEndPoints) { - inUse = true; - break; + if (endPoint.Port == port) + { + inUse = true; + break; + } } } + catch (Exception ex) + { + SaveLog(ex.Message, ex); + } return inUse; } #endregion @@ -912,6 +919,25 @@ namespace v2rayN return fileName; } + + public static IPAddress GetDefaultGateway() + { + return NetworkInterface + .GetAllNetworkInterfaces() + .Where(n => n.OperationalStatus == OperationalStatus.Up) + .Where(n => n.NetworkInterfaceType != NetworkInterfaceType.Loopback) + .SelectMany(n => n.GetIPProperties()?.GatewayAddresses) + .Select(g => g?.Address) + .Where(a => a != null) + // .Where(a => a.AddressFamily == AddressFamily.InterNetwork) + // .Where(a => Array.FindIndex(a.GetAddressBytes(), b => b != 0) >= 0) + .FirstOrDefault(); + } + + public static bool IsGuidByParse(string strSrc) + { + return Guid.TryParse(strSrc, out Guid g); + } #endregion #region TempPath