Skip to content

Commit

Permalink
优化 IP最后一段完全随机
Browse files Browse the repository at this point in the history
  • Loading branch information
XIU2 committed Nov 10, 2020
1 parent 8c0e873 commit 3de6b38
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
3 changes: 2 additions & 1 deletion IPRangeLoader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ func loadFirstIPOfRangeFromFile(ipFile string) []net.IPAddr {
if err != nil {
log.Fatal(err)
}
firstIP[15] = ipEndWith
for IPRange.Contains(firstIP) {
randipEndWith() // 随机 IP 的最后一段
firstIP[15] = ipEndWith
firstIPCopy := make([]byte, len(firstIP))
copy(firstIPCopy, firstIP)
firstIPs = append(firstIPs, net.IPAddr{IP: firstIPCopy})
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,23 @@

```
IP 地址 已发送 已接收 丢包率 平均延迟 下载速度 (MB/s)
104.27.200.105 4 4 0.00 135.77 13.21
104.27.205.105 4 4 0.00 136.65 17.33
104.27.207.105 4 4 0.00 136.97 5.55
104.27.193.105 4 4 0.00 146.97 16.54
104.27.206.105 4 4 0.00 148.04 1.65
104.27.192.105 4 4 0.00 148.97 9.08
104.27.195.105 4 4 0.00 150.29 21.00
104.27.197.105 4 4 0.00 151.04 13.61
104.27.203.105 4 4 0.00 151.54 15.73
104.27.204.105 4 4 0.00 151.94 8.32
104.27.199.141 4 4 0.00 139.52 11.71
104.22.73.158 4 4 0.00 141.38 6.74
104.27.204.240 4 4 0.00 142.02 4.65
104.22.72.117 4 4 0.00 143.63 12.00
104.22.75.117 4 4 0.00 145.75 3.92
104.22.77.24 4 4 0.00 146.00 5.86
104.22.66.140 4 4 0.00 146.50 9.47
104.22.78.104 4 4 0.00 146.75 13.00
104.22.69.208 4 4 0.00 147.00 19.07
104.27.194.10 4 4 0.00 148.02 21.05
```

完整结果保存在当前目录下的 `result.csv` 文件中,用**记事本/表格软件**打开,排序为**延迟由低到高**,分别是:

```
IP 地址, 已发送, 已接收, 丢包率, 平均延迟, 下载速度 (MB/s)
104.27.200.105, 4, 4, 0.00, 135.77, 13.21
104.27.199.141, 4, 4, 0.00, 139.52, 11.71
```

选择一个平均延迟与下载速度都不错的 IP 放到 `Hosts` 文件中(指向使用 Cloudflare CDN 的网站域名)。
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ https://github.com/XIU2/CloudflareSpeedTest
}

func main() {
initipEndWith() // 随机数
initRandSeed() // 置随机数种子
failTime = pingTime // 设置接收次数
ips := loadFirstIPOfRangeFromFile(ipFile) // 读入IP
pingCount := len(ips) * pingTime // 计算进度条总数(IP*测试次数)
Expand Down Expand Up @@ -160,7 +160,7 @@ func main() {
}
fmt.Printf("%-16s%-5s%-5s%-5s%-6s%-11s\n", "IP 地址", "已发送", "已接收", "丢包率", "平均延迟", "下载速度 (MB/s)")
for i := 0; i < printResult; i++ {
fmt.Printf("%-18s%-8s%-8s%-8s%-10s%-15s\n", dateString[i][0], dateString[i][1], dateString[i][2], dateString[i][3], dateString[i][4], dateString[i][5])
fmt.Printf("%-18s%-8s%-8s%-8s%-10s%-15s\n", ipPadding(dateString[i][0]), dateString[i][1], dateString[i][2], dateString[i][3], dateString[i][4], dateString[i][5])
}
if outputFile != "" {
fmt.Printf("\n完整内容请查看 %v 文件。请按 回车键 或 Ctrl+C 退出。", outputFile)
Expand Down
18 changes: 17 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,27 @@ var failTime int

type CloudflareIPDataSet []CloudflareIPData

func initipEndWith() {
func initRandSeed() {
rand.Seed(time.Now().UnixNano())
}

func randipEndWith() {
ipEndWith = uint8(rand.Intn(254) + 1)
}

func ipPadding(ip string) string {
var ipLength int
var ipPrint string
ipPrint = ip
ipLength = len(ipPrint)
if ipLength < 15 {
for i := 0; i <= 15-ipLength; i++ {
ipPrint += " "
}
}
return ipPrint
}

func handleProgressGenerator(pb *pb.ProgressBar) func(e progressEvent) {
return func(e progressEvent) {
switch e {
Expand Down

0 comments on commit 3de6b38

Please sign in to comment.