From fe0721f077aa7f8eaa7197542ede21505b11676d Mon Sep 17 00:00:00 2001 From: Mac_Zhou Date: Tue, 25 Apr 2023 11:00:46 +0800 Subject: [PATCH] Add support for CloudFront x-amz-cf-pop (#376) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 增加了对CloudFront筛选节点 2. 节点地区码使用正则表达式 --- task/httping.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/task/httping.go b/task/httping.go index e722b0d..b608df2 100644 --- a/task/httping.go +++ b/task/httping.go @@ -7,6 +7,7 @@ import ( "log" "net" "net/http" + "regexp" "strings" "sync" "time" @@ -17,6 +18,7 @@ var ( HttpingStatusCode int HttpingCFColo string HttpingCFColomap *sync.Map + OutRegexp = regexp.MustCompile(`[A-Z]{3}`) ) // pingReceived pingTotalTime @@ -59,7 +61,13 @@ func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration) { io.Copy(io.Discard, resp.Body) if HttpingCFColo != "" { - cfRay := resp.Header.Get("CF-RAY") + // 支持CloudFront + cfRay := func() string { + if resp.Header.Get("Server") == "cloudflare" { + return resp.Header.Get("CF-RAY") + } + return resp.Header.Get("x-amz-cf-pop") + }() colo := p.getColo(cfRay) if colo == "" { return 0, 0 @@ -115,9 +123,8 @@ func (p *Ping) getColo(b string) string { if b == "" { return "" } - idColo := strings.Split(b, "-") - out := idColo[1] + out := OutRegexp.FindString(b) if HttpingCFColomap == nil { return out