Skip to content

Commit

Permalink
Add support for CloudFront x-amz-cf-pop (#376)
Browse files Browse the repository at this point in the history
1. 增加了对CloudFront筛选节点
2. 节点地区码使用正则表达式
  • Loading branch information
mac-zhou authored Apr 25, 2023
1 parent 201619e commit fe0721f
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions task/httping.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net"
"net/http"
"regexp"
"strings"
"sync"
"time"
Expand All @@ -17,6 +18,7 @@ var (
HttpingStatusCode int
HttpingCFColo string
HttpingCFColomap *sync.Map
OutRegexp = regexp.MustCompile(`[A-Z]{3}`)
)

// pingReceived pingTotalTime
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fe0721f

Please sign in to comment.