httping.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package task
  2. import (
  3. //"crypto/tls"
  4. //"fmt"
  5. "io"
  6. "log"
  7. "net"
  8. "net/http"
  9. "regexp"
  10. "strings"
  11. "sync"
  12. "time"
  13. )
  14. var (
  15. Httping bool
  16. HttpingStatusCode int
  17. HttpingCFColo string
  18. HttpingCFColomap *sync.Map
  19. OutRegexp = regexp.MustCompile(`[A-Z]{3}`)
  20. )
  21. // pingReceived pingTotalTime
  22. func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration) {
  23. hc := http.Client{
  24. Timeout: time.Second * 2,
  25. Transport: &http.Transport{
  26. DialContext: getDialContext(ip),
  27. //TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // 跳过证书验证
  28. },
  29. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  30. return http.ErrUseLastResponse // 阻止重定向
  31. },
  32. }
  33. // 先访问一次获得 HTTP 状态码 及 Cloudflare Colo
  34. {
  35. requ, err := http.NewRequest(http.MethodHead, URL, nil)
  36. if err != nil {
  37. return 0, 0
  38. }
  39. requ.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36")
  40. resp, err := hc.Do(requ)
  41. if err != nil {
  42. return 0, 0
  43. }
  44. defer resp.Body.Close()
  45. //fmt.Println("IP:", ip, "StatusCode:", resp.StatusCode, resp.Request.URL)
  46. // 如果未指定的 HTTP 状态码,或指定的状态码不合规,则默认只认为 200、301、302 才算 HTTPing 通过
  47. if HttpingStatusCode == 0 || HttpingStatusCode < 100 && HttpingStatusCode > 599 {
  48. if resp.StatusCode != 200 && resp.StatusCode != 301 && resp.StatusCode != 302 {
  49. return 0, 0
  50. }
  51. } else {
  52. if resp.StatusCode != HttpingStatusCode {
  53. return 0, 0
  54. }
  55. }
  56. io.Copy(io.Discard, resp.Body)
  57. // 只有指定了地区才匹配机场三字码
  58. if HttpingCFColo != "" {
  59. // 通过头部 Server 值判断是 Cloudflare 还是 AWS CloudFront 并设置 cfRay 为各自的机场三字码完整内容
  60. cfRay := func() string {
  61. if resp.Header.Get("Server") == "cloudflare" {
  62. return resp.Header.Get("CF-RAY") // 示例 cf-ray: 7bd32409eda7b020-SJC
  63. }
  64. return resp.Header.Get("x-amz-cf-pop") // 示例 X-Amz-Cf-Pop: SIN52-P1
  65. }()
  66. colo := p.getColo(cfRay)
  67. if colo == "" { // 没有匹配到三字码或不符合指定地区则直接结束该 IP 测试
  68. return 0, 0
  69. }
  70. }
  71. }
  72. // 循环测速计算延迟
  73. success := 0
  74. var delay time.Duration
  75. for i := 0; i < PingTimes; i++ {
  76. requ, err := http.NewRequest(http.MethodHead, URL, nil)
  77. if err != nil {
  78. log.Fatal("意外的错误,情报告:", err)
  79. return 0, 0
  80. }
  81. requ.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36")
  82. if i == PingTimes-1 {
  83. requ.Header.Set("Connection", "close")
  84. }
  85. startTime := time.Now()
  86. resp, err := hc.Do(requ)
  87. if err != nil {
  88. continue
  89. }
  90. success++
  91. io.Copy(io.Discard, resp.Body)
  92. _ = resp.Body.Close()
  93. duration := time.Since(startTime)
  94. delay += duration
  95. }
  96. return success, delay
  97. }
  98. func MapColoMap() *sync.Map {
  99. if HttpingCFColo == "" {
  100. return nil
  101. }
  102. // 将参数指定的地区三字码转为大写并格式化
  103. colos := strings.Split(strings.ToUpper(HttpingCFColo), ",")
  104. colomap := &sync.Map{}
  105. for _, colo := range colos {
  106. colomap.Store(colo, colo)
  107. }
  108. return colomap
  109. }
  110. func (p *Ping) getColo(b string) string {
  111. if b == "" {
  112. return ""
  113. }
  114. // 正则匹配并返回 机场三字码
  115. out := OutRegexp.FindString(b)
  116. if HttpingCFColomap == nil {
  117. return out
  118. }
  119. // 匹配 机场三字码 是否为指定的地区
  120. _, ok := HttpingCFColomap.Load(out)
  121. if ok {
  122. return out
  123. }
  124. return ""
  125. }