Improved true connection test

pull/3786/head
2dust 2023-04-28 21:10:13 +08:00
parent ec8f6478df
commit 33322e8795
3 changed files with 23 additions and 27 deletions

View File

@ -253,22 +253,20 @@ namespace v2rayN.Handler
return null; return null;
} }
public int RunAvailabilityCheck(IWebProxy? webProxy) public async Task<int> RunAvailabilityCheck(IWebProxy? webProxy)
{ {
try try
{ {
if (webProxy == null) if (webProxy == null)
{ {
var httpPort = LazyConfig.Instance.GetLocalPort(Global.InboundHttp); webProxy = GetWebProxy(true);
webProxy = new WebProxy(Global.Loopback, httpPort);
} }
try try
{ {
var config = LazyConfig.Instance.GetConfig(); var config = LazyConfig.Instance.GetConfig();
string status = GetRealPingTime(config.speedTestItem.speedPingTestUrl, webProxy, 10, out int responseTime); int responseTime = await GetRealPingTime(config.speedTestItem.speedPingTestUrl, webProxy, 10);
bool noError = Utils.IsNullOrEmpty(status); return responseTime;
return noError ? responseTime : -1;
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -283,31 +281,29 @@ namespace v2rayN.Handler
} }
} }
public string GetRealPingTime(string url, IWebProxy? webProxy, int downloadTimeout, out int responseTime) public async Task<int> GetRealPingTime(string url, IWebProxy? webProxy, int downloadTimeout)
{ {
string msg = string.Empty; int responseTime = -1;
responseTime = -1;
try try
{ {
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.Timeout = downloadTimeout * 1000;
myHttpWebRequest.Proxy = webProxy;
Stopwatch timer = Stopwatch.StartNew(); Stopwatch timer = Stopwatch.StartNew();
using HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); using var cts = new CancellationTokenSource();
if (myHttpWebResponse.StatusCode is not HttpStatusCode.OK and not HttpStatusCode.NoContent) cts.CancelAfter(TimeSpan.FromSeconds(downloadTimeout));
using var client = new HttpClient(new SocketsHttpHandler()
{ {
msg = myHttpWebResponse.StatusDescription; Proxy = webProxy,
} UseProxy = webProxy != null
});
await client.GetAsync(url, cts.Token);
responseTime = timer.Elapsed.Milliseconds; responseTime = timer.Elapsed.Milliseconds;
} }
catch (Exception ex) catch (Exception ex)
{ {
Utils.SaveLog(ex.Message, ex); //Utils.SaveLog(ex.Message, ex);
msg = ex.Message;
} }
return msg; return responseTime;
} }
private WebProxy? GetWebProxy(bool blProxy) private WebProxy? GetWebProxy(bool blProxy)

View File

@ -170,12 +170,12 @@ namespace v2rayN.Handler
{ {
continue; continue;
} }
tasks.Add(Task.Run(() => tasks.Add(Task.Run(async () =>
{ {
try try
{ {
WebProxy webProxy = new(Global.Loopback, it.port); WebProxy webProxy = new(Global.Loopback, it.port);
string output = GetRealPingTime(downloadHandle, webProxy); string output = await GetRealPingTime(downloadHandle, webProxy);
ProfileExHandler.Instance.SetTestDelay(it.indexId, output); ProfileExHandler.Instance.SetTestDelay(it.indexId, output);
UpdateFunc(it.indexId, output); UpdateFunc(it.indexId, output);
@ -334,11 +334,11 @@ namespace v2rayN.Handler
await RunSpeedTestMulti(); await RunSpeedTestMulti();
} }
public string GetRealPingTime(DownloadHandle downloadHandle, IWebProxy webProxy) public async Task<string> GetRealPingTime(DownloadHandle downloadHandle, IWebProxy webProxy)
{ {
string status = downloadHandle.GetRealPingTime(_config.speedTestItem.speedPingTestUrl, webProxy, 10, out int responseTime); int responseTime = await downloadHandle.GetRealPingTime(_config.speedTestItem.speedPingTestUrl, webProxy, 10);
//string output = Utils.IsNullOrEmpty(status) ? FormatOut(responseTime, "ms") : status; //string output = Utils.IsNullOrEmpty(status) ? FormatOut(responseTime, "ms") : status;
return FormatOut(Utils.IsNullOrEmpty(status) ? responseTime : -1, Global.DelayUnit); return FormatOut(responseTime, Global.DelayUnit);
} }
private int GetTcpingTime(string url, int port) private int GetTcpingTime(string url, int port)

View File

@ -293,9 +293,9 @@ namespace v2rayN.Handler
public void RunAvailabilityCheck(Action<bool, string> update) public void RunAvailabilityCheck(Action<bool, string> update)
{ {
Task.Run(() => Task.Run(async () =>
{ {
var time = (new DownloadHandle()).RunAvailabilityCheck(null); var time = await (new DownloadHandle()).RunAvailabilityCheck(null);
update(false, string.Format(ResUI.TestMeOutput, time)); update(false, string.Format(ResUI.TestMeOutput, time));
}); });