util.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "encoding/csv"
  4. "github.com/cheggaaa/pb/v3"
  5. "log"
  6. "math/rand"
  7. "net"
  8. "os"
  9. "strconv"
  10. "time"
  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 Address", "Ping count", "Ping received", "Ping received rate", "Ping time", "Download Speed (MB/s)"})
  35. w.WriteAll(convertToString(data))
  36. w.Flush()
  37. }
  38. //"IP Address","Ping Count","Ping received","Ping received rate","Ping time","Download speed"
  39. func (cf *CloudflareIPData) toString() []string {
  40. result := make([]string, 6)
  41. result[0] = cf.ip.String()
  42. result[1] = strconv.Itoa(cf.pingCount)
  43. result[2] = strconv.Itoa(cf.pingReceived)
  44. result[3] = strconv.FormatFloat(float64(cf.getRecvRate()), 'f', 4, 32)
  45. result[4] = strconv.FormatFloat(float64(cf.pingTime), 'f', 4, 32)
  46. result[5] = strconv.FormatFloat(float64(cf.downloadSpeed)/1024/1024, 'f', 4, 32)
  47. return result
  48. }
  49. func convertToString(data []CloudflareIPData) [][]string {
  50. result := make([][]string, 0)
  51. for _, v := range data {
  52. result = append(result, v.toString())
  53. }
  54. return result
  55. }
  56. var pingTime int
  57. var pingRoutine int
  58. var ipEndWith uint8 = 0
  59. type progressEvent int
  60. const (
  61. NoAvailableIPFound progressEvent = iota
  62. AvailableIPFound
  63. NormalPing
  64. )
  65. 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"
  66. var downloadTestTime time.Duration
  67. const downloadBufferSize = 1024
  68. var downloadTestCount int
  69. const defaultTcpPort = 443
  70. const tcpConnectTimeout = time.Second * 1
  71. const failTime = 4
  72. type CloudflareIPDataSet []CloudflareIPData
  73. func initipEndWith() {
  74. rand.Seed(time.Now().UnixNano())
  75. ipEndWith = uint8(rand.Intn(254) + 1)
  76. }
  77. func handleProgressGenerator(pb *pb.ProgressBar) func(e progressEvent) {
  78. return func(e progressEvent) {
  79. switch e {
  80. case NoAvailableIPFound:
  81. pb.Add(pingTime)
  82. case AvailableIPFound:
  83. pb.Add(failTime)
  84. case NormalPing:
  85. pb.Increment()
  86. }
  87. }
  88. }
  89. func (cfs CloudflareIPDataSet) Len() int {
  90. return len(cfs)
  91. }
  92. func (cfs CloudflareIPDataSet) Less(i, j int) bool {
  93. if (cfs)[i].getRecvRate() != cfs[j].getRecvRate() {
  94. return cfs[i].getRecvRate() > cfs[j].getRecvRate()
  95. }
  96. return cfs[i].pingTime < cfs[j].pingTime
  97. }
  98. func (cfs CloudflareIPDataSet) Swap(i, j int) {
  99. cfs[i], cfs[j] = cfs[j], cfs[i]
  100. }