mirror of https://github.com/2dust/v2rayN
Improved true connection test
parent
ec8f6478df
commit
33322e8795
|
@ -253,22 +253,20 @@ namespace v2rayN.Handler
|
|||
return null;
|
||||
}
|
||||
|
||||
public int RunAvailabilityCheck(IWebProxy? webProxy)
|
||||
public async Task<int> RunAvailabilityCheck(IWebProxy? webProxy)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (webProxy == null)
|
||||
{
|
||||
var httpPort = LazyConfig.Instance.GetLocalPort(Global.InboundHttp);
|
||||
webProxy = new WebProxy(Global.Loopback, httpPort);
|
||||
webProxy = GetWebProxy(true);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var config = LazyConfig.Instance.GetConfig();
|
||||
string status = GetRealPingTime(config.speedTestItem.speedPingTestUrl, webProxy, 10, out int responseTime);
|
||||
bool noError = Utils.IsNullOrEmpty(status);
|
||||
return noError ? responseTime : -1;
|
||||
int responseTime = await GetRealPingTime(config.speedTestItem.speedPingTestUrl, webProxy, 10);
|
||||
return responseTime;
|
||||
}
|
||||
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;
|
||||
responseTime = -1;
|
||||
int responseTime = -1;
|
||||
try
|
||||
{
|
||||
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
|
||||
myHttpWebRequest.Timeout = downloadTimeout * 1000;
|
||||
myHttpWebRequest.Proxy = webProxy;
|
||||
|
||||
Stopwatch timer = Stopwatch.StartNew();
|
||||
|
||||
using HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
|
||||
if (myHttpWebResponse.StatusCode is not HttpStatusCode.OK and not HttpStatusCode.NoContent)
|
||||
using var cts = new CancellationTokenSource();
|
||||
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;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Utils.SaveLog(ex.Message, ex);
|
||||
msg = ex.Message;
|
||||
//Utils.SaveLog(ex.Message, ex);
|
||||
}
|
||||
return msg;
|
||||
return responseTime;
|
||||
}
|
||||
|
||||
private WebProxy? GetWebProxy(bool blProxy)
|
||||
|
|
|
@ -170,12 +170,12 @@ namespace v2rayN.Handler
|
|||
{
|
||||
continue;
|
||||
}
|
||||
tasks.Add(Task.Run(() =>
|
||||
tasks.Add(Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
WebProxy webProxy = new(Global.Loopback, it.port);
|
||||
string output = GetRealPingTime(downloadHandle, webProxy);
|
||||
string output = await GetRealPingTime(downloadHandle, webProxy);
|
||||
|
||||
ProfileExHandler.Instance.SetTestDelay(it.indexId, output);
|
||||
UpdateFunc(it.indexId, output);
|
||||
|
@ -334,11 +334,11 @@ namespace v2rayN.Handler
|
|||
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;
|
||||
return FormatOut(Utils.IsNullOrEmpty(status) ? responseTime : -1, Global.DelayUnit);
|
||||
return FormatOut(responseTime, Global.DelayUnit);
|
||||
}
|
||||
|
||||
private int GetTcpingTime(string url, int port)
|
||||
|
|
|
@ -293,9 +293,9 @@ namespace v2rayN.Handler
|
|||
|
||||
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));
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue