mirror of
https://github.com/2dust/v2rayN.git
synced 2025-08-23 03:16:53 +00:00
39 lines
1.2 KiB
C#
39 lines
1.2 KiB
C#
namespace ServiceLib.Handler;
|
|
|
|
public static class ConnectionHandler
|
|
{
|
|
public static async Task<string> RunAvailabilityCheck()
|
|
{
|
|
var downloadHandle = new DownloadService();
|
|
var time = await downloadHandle.RunAvailabilityCheck(null);
|
|
var ip = time > 0 ? await GetIPInfo(downloadHandle) ?? Global.None : Global.None;
|
|
|
|
return string.Format(ResUI.TestMeOutput, time, ip);
|
|
}
|
|
|
|
private static async Task<string?> GetIPInfo(DownloadService downloadHandle)
|
|
{
|
|
var url = AppHandler.Instance.Config.SpeedTestItem.IPAPIUrl;
|
|
if (url.IsNullOrEmpty())
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var result = await downloadHandle.TryDownloadString(url, true, "");
|
|
if (result == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var ipInfo = JsonUtils.Deserialize<IPAPIInfo>(result);
|
|
if (ipInfo == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var ip = ipInfo.ip ?? ipInfo.clientIp ?? ipInfo.ip_addr ?? ipInfo.query;
|
|
var country = ipInfo.country_code ?? ipInfo.country ?? ipInfo.countryCode ?? ipInfo.location?.country_code;
|
|
|
|
return $"({country ?? "unknown"}) {ip}";
|
|
}
|
|
}
|