MCPcopy
hub / github.com/badafans/better-cloudflare-ip / cloudflareTest

Function cloudflareTest

main.go:143–211  ·  view source on GitHub ↗

cloudflareTest 核心测试逻辑

(ipType int, useTLS bool, taskNum int, speed int)

Source from the content-addressed store, hash-verified

141
142// cloudflareTest 核心测试逻辑
143func cloudflareTest(ipType int, useTLS bool, taskNum int, speed int) (string, int, int, string) {
144 downloadAllData()
145 filename := dataPath("ips-v4.txt")
146 if ipType == 6 {
147 filename = dataPath("ips-v6.txt")
148 }
149 content, err := getFileContent(filename)
150 if err != nil {
151 fmt.Println("读取 IP 列表失败:", err)
152 return "", 0, 0, ""
153 }
154 ipList := parseIPList(content)
155 fmt.Printf("正在从 %d 个子网中随机生成 IP...\n", len(ipList))
156
157 sampleSize := 100
158 if len(ipList) < sampleSize {
159 sampleSize = len(ipList)
160 }
161
162 for {
163 var rttResults []RTTResult
164 for {
165 sampled := randomSample(ipList, sampleSize)
166
167 var testIPs []string
168 if ipType == 6 {
169 testIPs = getRandomIPv6s(sampled)
170 } else {
171 testIPs = getRandomIPv4s(sampled)
172 }
173
174 fmt.Printf("已生成 %d 个测试 IP,开始 RTT 测试...\n", len(testIPs))
175
176 rttResults = runRTTTest(testIPs, taskNum, useTLS)
177 if len(rttResults) > 0 {
178 break
179 }
180 fmt.Println("当前所有 IP 都存在 RTT 丢包,继续新的 RTT 测试...")
181 }
182
183 fmt.Println("待测速的 IP 地址")
184 for _, r := range rttResults {
185 fmt.Printf("%s 往返延迟 %d 毫秒\n", r.IP, r.LatencyMs)
186 }
187
188 // 速度测试
189 for _, r := range rttResults {
190 fmt.Println("正在测试", r.IP)
191 speedPort := 80
192 if useTLS {
193 speedPort = 443
194 }
195 maxSpeed, tcpMs, dc := runSpeedTestSimple(r.IP, speedPort, useTLS)
196 fmt.Printf("%s 峰值速度 %d kB/s", r.IP, maxSpeed)
197 if dc != "" {
198 fmt.Printf(", 数据中心 %s", lookupDataCenter(dc))
199 }
200 fmt.Println()

Callers 1

runIPSelectorFunction · 0.85

Calls 10

downloadAllDataFunction · 0.85
dataPathFunction · 0.85
getFileContentFunction · 0.85
parseIPListFunction · 0.85
randomSampleFunction · 0.85
getRandomIPv6sFunction · 0.85
getRandomIPv4sFunction · 0.85
runRTTTestFunction · 0.85
runSpeedTestSimpleFunction · 0.85
lookupDataCenterFunction · 0.85

Tested by

no test coverage detected