mirror of
https://github.com/2dust/v2rayN.git
synced 2026-04-16 12:35:46 +00:00
Switch speedtest IP fallback to Cloudflare trace first
This commit is contained in:
parent
5d76a3e26e
commit
0638be614d
1 changed files with 48 additions and 2 deletions
|
|
@ -5,8 +5,8 @@ public static class ConnectionHandler
|
|||
private static readonly string _tag = "ConnectionHandler";
|
||||
private static readonly string[] _speedtestIpApiUrls =
|
||||
[
|
||||
"https://api.ipapi.is",
|
||||
"https://api.ip.sb/geoip"
|
||||
"https://www.cloudflare.com/cdn-cgi/trace",
|
||||
"https://api.ipapi.is"
|
||||
];
|
||||
|
||||
public static async Task<string> RunAvailabilityCheck()
|
||||
|
|
@ -70,6 +70,11 @@ public static class ConnectionHandler
|
|||
}
|
||||
|
||||
var result = await response.Content.ReadAsStringAsync(cts.Token).ConfigureAwait(false);
|
||||
if (url.Contains("/cdn-cgi/trace", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return ParseCloudflareTrace(result);
|
||||
}
|
||||
|
||||
return JsonUtils.Deserialize<IPAPIInfo>(result);
|
||||
}
|
||||
catch
|
||||
|
|
@ -78,6 +83,47 @@ public static class ConnectionHandler
|
|||
}
|
||||
}
|
||||
|
||||
private static IPAPIInfo? ParseCloudflareTrace(string? traceContent)
|
||||
{
|
||||
if (traceContent.IsNullOrEmpty())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var tracePairs = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (var line in traceContent.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
|
||||
{
|
||||
var idx = line.IndexOf('=');
|
||||
if (idx <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var key = line[..idx];
|
||||
var value = line[(idx + 1)..];
|
||||
tracePairs[key] = value;
|
||||
}
|
||||
|
||||
tracePairs.TryGetValue("ip", out var ip);
|
||||
if (ip.IsNullOrEmpty())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
tracePairs.TryGetValue("loc", out var loc);
|
||||
|
||||
return new IPAPIInfo
|
||||
{
|
||||
ip = ip,
|
||||
clientIp = ip,
|
||||
ip_addr = ip,
|
||||
query = ip,
|
||||
country = loc,
|
||||
country_code = loc,
|
||||
countryCode = loc
|
||||
};
|
||||
}
|
||||
|
||||
private static string? FormatCountryAndIp(IPAPIInfo? ipInfo, bool compact)
|
||||
{
|
||||
if (ipInfo == null)
|
||||
|
|
|
|||
Loading…
Reference in a new issue