httping.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package task
  2. import (
  3. //"crypto/tls"
  4. //"fmt"
  5. "io"
  6. "log"
  7. "net"
  8. "net/http"
  9. "strings"
  10. "sync"
  11. "time"
  12. )
  13. var (
  14. Httping bool
  15. HttpingStatusCode int
  16. HttpingCFColo string
  17. HttpingCFColomap *sync.Map
  18. )
  19. // pingReceived pingTotalTime
  20. func (p *Ping) httping(ip *net.IPAddr) (int, time.Duration) {
  21. hc := http.Client{
  22. Timeout: time.Second * 2,
  23. Transport: &http.Transport{
  24. DialContext: getDialContext(ip),
  25. //TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, // 跳过证书验证
  26. },
  27. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  28. return http.ErrUseLastResponse // 阻止重定向
  29. },
  30. }
  31. // 先访问一次获得 HTTP 状态码 及 Cloudflare Colo
  32. {
  33. requ, err := http.NewRequest(http.MethodHead, URL, nil)
  34. if err != nil {
  35. return 0, 0
  36. }
  37. 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")
  38. resp, err := hc.Do(requ)
  39. if err != nil {
  40. return 0, 0
  41. }
  42. defer resp.Body.Close()
  43. //fmt.Println("IP:", ip, "StatusCode:", resp.StatusCode, resp.Request.URL)
  44. if HttpingStatusCode == 0 || HttpingStatusCode < 100 && HttpingStatusCode > 599 {
  45. if resp.StatusCode != 200 && resp.StatusCode != 301 && resp.StatusCode != 302 {
  46. return 0, 0
  47. }
  48. } else {
  49. if resp.StatusCode != HttpingStatusCode {
  50. return 0, 0
  51. }
  52. }
  53. io.Copy(io.Discard, resp.Body)
  54. if HttpingCFColo != "" {
  55. cfRay := resp.Header.Get("CF-RAY")
  56. colo := p.getColo(cfRay)
  57. if colo == "" {
  58. return 0, 0
  59. }
  60. }
  61. }
  62. // 循环测速计算延迟
  63. success := 0
  64. var delay time.Duration
  65. for i := 0; i < PingTimes; i++ {
  66. requ, err := http.NewRequest(http.MethodHead, URL, nil)
  67. if err != nil {
  68. log.Fatal("意外的错误,情报告:", err)
  69. return 0, 0
  70. }
  71. 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")
  72. if i == PingTimes-1 {
  73. requ.Header.Set("Connection", "close")
  74. }
  75. startTime := time.Now()
  76. resp, err := hc.Do(requ)
  77. if err != nil {
  78. continue
  79. }
  80. success++
  81. io.Copy(io.Discard, resp.Body)
  82. _ = resp.Body.Close()
  83. duration := time.Since(startTime)
  84. delay += duration
  85. }
  86. return success, delay
  87. }
  88. func MapColoMap() *sync.Map {
  89. if HttpingCFColo == "" {
  90. return nil
  91. }
  92. colos := strings.Split(HttpingCFColo, ",")
  93. colomap := &sync.Map{}
  94. for _, colo := range colos {
  95. colomap.Store(colo, colo)
  96. }
  97. return colomap
  98. }
  99. func (p *Ping) getColo(b string) string {
  100. if b == "" {
  101. return ""
  102. }
  103. idColo := strings.Split(b, "-")
  104. out := idColo[1]
  105. if HttpingCFColomap == nil {
  106. return out
  107. }
  108. _, ok := HttpingCFColomap.Load(out)
  109. if ok {
  110. return out
  111. }
  112. return ""
  113. }