From ed96432b7988de987b3dfaf9d3e2b4cba194c4df Mon Sep 17 00:00:00 2001 From: MadDogOwner Date: Tue, 23 Jul 2024 18:26:45 +0800 Subject: [PATCH] installer: improve region detection reliability using multiple APIs (#394) --- script/install.ps1 | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/script/install.ps1 b/script/install.ps1 index 5a9ed99..e90b7d7 100644 --- a/script/install.ps1 +++ b/script/install.ps1 @@ -49,8 +49,20 @@ if ([string]::IsNullOrWhiteSpace($agenttag)) { } } #Region判断 -$ipapi= Invoke-RestMethod -Uri "https://api.myip.com/" -UserAgent "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1" -$region=$ipapi.cc +$ipapi = "" +$region = "Unknown" +foreach ($url in ("https://dash.cloudflare.com/cdn-cgi/trace","https://cf-ns.com/cdn-cgi/trace","https://1.0.0.1/cdn-cgi/trace")) { + try { + $ipapi = Invoke-RestMethod -Uri $url -TimeoutSec 5 -UseBasicParsing + if ($ipapi -match "loc=(\w+)" ) { + $region = $Matches[1] + break + } + } + catch { + Write-Host "Error occurred while querying $url : $_" + } +} echo $ipapi if($region -ne "CN"){ $download = "https://github.com/$agentrepo/releases/download/$agenttag/$file"