1
0

util.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "encoding/csv"
  4. "log"
  5. "math/rand"
  6. "net"
  7. "os"
  8. "strconv"
  9. "time"
  10. "github.com/cheggaaa/pb/v3"
  11. )
  12. type CloudflareIPData struct {
  13. ip net.IPAddr
  14. pingCount int
  15. pingReceived int
  16. recvRate float32
  17. downloadSpeed float32
  18. pingTime float32
  19. }
  20. func (cf *CloudflareIPData) getRecvRate() float32 {
  21. if cf.recvRate == 0 {
  22. cf.recvRate = float32(cf.pingReceived) / float32(cf.pingCount)
  23. }
  24. return cf.recvRate
  25. }
  26. func ExportCsv(filePath string, data []CloudflareIPData) {
  27. fp, err := os.Create(filePath)
  28. if err != nil {
  29. log.Fatalf("创建文件["+filePath+"]句柄失败,%v", err)
  30. return
  31. }
  32. defer fp.Close()
  33. w := csv.NewWriter(fp) //创建一个新的写入文件流
  34. w.Write([]string{"IP 地址", "测试次数", "成功次数", "成功比率", "平均延迟", "下载速度 (MB/s)"})
  35. w.WriteAll(convertToString(data))
  36. w.Flush()
  37. }
  38. func (cf *CloudflareIPData) toString() []string {
  39. result := make([]string, 6)
  40. result[0] = cf.ip.String()
  41. result[1] = strconv.Itoa(cf.pingCount)
  42. result[2] = strconv.Itoa(cf.pingReceived)
  43. result[3] = strconv.FormatFloat(float64(cf.getRecvRate()), 'f', 2, 32)
  44. result[4] = strconv.FormatFloat(float64(cf.pingTime), 'f', 2, 32)
  45. result[5] = strconv.FormatFloat(float64(cf.downloadSpeed)/1024/1024, 'f', 2, 32)
  46. return result
  47. }
  48. func convertToString(data []CloudflareIPData) [][]string {
  49. result := make([][]string, 0)
  50. for _, v := range data {
  51. result = append(result, v.toString())
  52. }
  53. return result
  54. }
  55. var pingTime int
  56. var pingRoutine int
  57. var ipEndWith uint8 = 0
  58. type progressEvent int
  59. const (
  60. NoAvailableIPFound progressEvent = iota
  61. AvailableIPFound
  62. NormalPing
  63. )
  64. const url string = "https://apple.freecdn.workers.dev/105/media/us/iphone-11-pro/2019/3bd902e4-0752-4ac1-95f8-6225c32aec6d/films/product/iphone-11-pro-product-tpl-cc-us-2019_1280x720h.mp4"
  65. var downloadTestTime time.Duration
  66. const downloadBufferSize = 1024
  67. var downloadTestCount int
  68. //const defaultTcpPort = 443
  69. const tcpConnectTimeout = time.Second * 1
  70. var failTime int
  71. type CloudflareIPDataSet []CloudflareIPData
  72. func initipEndWith() {
  73. rand.Seed(time.Now().UnixNano())
  74. ipEndWith = uint8(rand.Intn(254) + 1)
  75. }
  76. func handleProgressGenerator(pb *pb.ProgressBar) func(e progressEvent) {
  77. return func(e progressEvent) {
  78. switch e {
  79. case NoAvailableIPFound:
  80. pb.Add(pingTime)
  81. case AvailableIPFound:
  82. pb.Add(failTime)
  83. case NormalPing:
  84. pb.Increment()
  85. }
  86. }
  87. }
  88. func (cfs CloudflareIPDataSet) Len() int {
  89. return len(cfs)
  90. }
  91. func (cfs CloudflareIPDataSet) Less(i, j int) bool {
  92. if (cfs)[i].getRecvRate() != cfs[j].getRecvRate() {
  93. return cfs[i].getRecvRate() > cfs[j].getRecvRate()
  94. }
  95. return cfs[i].pingTime < cfs[j].pingTime
  96. }
  97. func (cfs CloudflareIPDataSet) Swap(i, j int) {
  98. cfs[i], cfs[j] = cfs[j], cfs[i]
  99. }