download.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package task
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "net"
  7. "net/http"
  8. "sort"
  9. "time"
  10. "CloudflareSpeedTest/utils"
  11. "github.com/VividCortex/ewma"
  12. )
  13. const (
  14. bufferSize = 1024
  15. defaultURL = "https://cf.xiu2.xyz/url"
  16. defaultTimeout = 10 * time.Second
  17. defaultDisableDownload = false
  18. defaultTestNum = 10
  19. defaultMinSpeed float64 = 0.0
  20. )
  21. var (
  22. // download test url
  23. URL = defaultURL
  24. // download timeout
  25. Timeout = defaultTimeout
  26. // disable download
  27. Disable = defaultDisableDownload
  28. TestCount = defaultTestNum
  29. MinSpeed = defaultMinSpeed
  30. )
  31. func checkDownloadDefault() {
  32. if URL == "" {
  33. URL = defaultURL
  34. }
  35. if Timeout <= 0 {
  36. Timeout = defaultTimeout
  37. }
  38. if TestCount <= 0 {
  39. TestCount = defaultTestNum
  40. }
  41. if MinSpeed <= 0.0 {
  42. MinSpeed = defaultMinSpeed
  43. }
  44. }
  45. func TestDownloadSpeed(ipSet utils.PingDelaySet) (speedSet utils.DownloadSpeedSet) {
  46. checkDownloadDefault()
  47. if Disable {
  48. return utils.DownloadSpeedSet(ipSet)
  49. }
  50. if len(ipSet) <= 0 { // IP数组长度(IP数量) 大于 0 时才会继续下载测速
  51. fmt.Println("\n[信息] 延迟测速结果 IP 数量为 0,跳过下载测速。")
  52. return
  53. }
  54. testNum := TestCount
  55. if len(ipSet) < TestCount || MinSpeed > 0 { // 如果IP数组长度(IP数量) 小于下载测速数量(-dn),则次数修正为IP数
  56. testNum = len(ipSet)
  57. }
  58. if testNum < TestCount {
  59. TestCount = testNum
  60. }
  61. fmt.Printf("开始下载测速(下载速度下限:%.2f MB/s,下载测速数量:%d,下载测速队列:%d):\n", MinSpeed, TestCount, testNum)
  62. bar := utils.NewBar(TestCount)
  63. for i := 0; i < testNum; i++ {
  64. speed := downloadHandler(ipSet[i].IP)
  65. ipSet[i].DownloadSpeed = speed
  66. // 在每个 IP 下载测速后,以 [下载速度下限] 条件过滤结果
  67. if speed >= MinSpeed*1024*1024 {
  68. bar.Grow(1)
  69. speedSet = append(speedSet, ipSet[i]) // 高于下载速度下限时,添加到新数组中
  70. if len(speedSet) == TestCount { // 凑够满足条件的 IP 时(下载测速数量 -dn),就跳出循环
  71. break
  72. }
  73. }
  74. }
  75. bar.Done()
  76. if len(speedSet) == 0 { // 没有符合速度限制的数据,返回所有测试数据
  77. speedSet = utils.DownloadSpeedSet(ipSet)
  78. }
  79. // 按速度排序
  80. sort.Sort(speedSet)
  81. return
  82. }
  83. func getDialContext(ip *net.IPAddr) func(ctx context.Context, network, address string) (net.Conn, error) {
  84. var fakeSourceAddr string
  85. if isIPv4(ip.String()) {
  86. fakeSourceAddr = fmt.Sprintf("%s:%d", ip.String(), TCPPort)
  87. } else {
  88. fakeSourceAddr = fmt.Sprintf("[%s]:%d", ip.String(), TCPPort)
  89. }
  90. return func(ctx context.Context, network, address string) (net.Conn, error) {
  91. return (&net.Dialer{}).DialContext(ctx, network, fakeSourceAddr)
  92. }
  93. }
  94. // return download Speed
  95. func downloadHandler(ip *net.IPAddr) float64 {
  96. client := &http.Client{
  97. Transport: &http.Transport{DialContext: getDialContext(ip)},
  98. Timeout: Timeout,
  99. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  100. if len(via) > 10 { // 限制最多重定向 10 次
  101. return http.ErrUseLastResponse
  102. }
  103. if req.Header.Get("Referer") == defaultURL { // 当使用默认下载测速地址时,重定向不携带 Referer
  104. req.Header.Del("Referer")
  105. }
  106. return nil
  107. },
  108. }
  109. req, err := http.NewRequest("GET", URL, nil)
  110. if err != nil {
  111. return 0.0
  112. }
  113. req.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")
  114. response, err := client.Do(req)
  115. if err != nil {
  116. return 0.0
  117. }
  118. defer response.Body.Close()
  119. if response.StatusCode != 200 {
  120. return 0.0
  121. }
  122. timeStart := time.Now() // 开始时间(当前)
  123. timeEnd := timeStart.Add(Timeout) // 加上下载测速时间得到的结束时间
  124. contentLength := response.ContentLength // 文件大小
  125. buffer := make([]byte, bufferSize)
  126. var (
  127. contentRead int64 = 0
  128. timeSlice = Timeout / 100
  129. timeCounter = 1
  130. lastContentRead int64 = 0
  131. )
  132. var nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
  133. e := ewma.NewMovingAverage()
  134. // 循环计算,如果文件下载完了(两者相等),则退出循环(终止测速)
  135. for contentLength != contentRead {
  136. currentTime := time.Now()
  137. if currentTime.After(nextTime) {
  138. timeCounter++
  139. nextTime = timeStart.Add(timeSlice * time.Duration(timeCounter))
  140. e.Add(float64(contentRead - lastContentRead))
  141. lastContentRead = contentRead
  142. }
  143. // 如果超出下载测速时间,则退出循环(终止测速)
  144. if currentTime.After(timeEnd) {
  145. break
  146. }
  147. bufferRead, err := response.Body.Read(buffer)
  148. if err != nil {
  149. if err != io.EOF { // 文件下载完了,或因网络等问题导致链接中断,则退出循环(终止测速)
  150. break
  151. }
  152. e.Add(float64(contentRead-lastContentRead) / (float64(nextTime.Sub(currentTime)) / float64(timeSlice)))
  153. }
  154. contentRead += int64(bufferRead)
  155. }
  156. return e.Value() / (Timeout.Seconds() / 120)
  157. }