mirror of
https://github.com/XIU2/CloudflareSpeedTest.git
synced 2026-03-06 22:59:33 +08:00
修复 调试模式下因重定向后的地址出错导致下载测速失败时输出的最终地址不准确的问题
This commit is contained in:
@@ -110,13 +110,38 @@ func getDialContext(ip *net.IPAddr) func(ctx context.Context, network, address s
|
||||
}
|
||||
}
|
||||
|
||||
// 统一的请求报错调试输出
|
||||
func printDownloadDebugInfo(ip *net.IPAddr, err error, statusCode int, url, lastRedirectURL string, response *http.Response) {
|
||||
finalURL := url // 默认的最终 URL,这样当 response 为空时也能输出
|
||||
if lastRedirectURL != "" {
|
||||
finalURL = lastRedirectURL // 如果 lastRedirectURL 不是空,说明重定向过,优先输出最后一次要重定向至的目标
|
||||
} else if response != nil && response.Request != nil && response.Request.URL != nil {
|
||||
finalURL = response.Request.URL.String() // 如果 response 不为 nil,且 Request 和 URL 都不为 nil,则获取最后一次成功的响应地址
|
||||
}
|
||||
if url != finalURL { // 如果 URL 和最终地址不一致,说明有重定向,是该重定向后的地址引起的错误
|
||||
if statusCode > 0 { // 如果状态码大于 0,说明是后续 HTTP 状态码引起的错误
|
||||
fmt.Printf("\033[31m[调试] IP: %s, 下载测速终止,HTTP 状态码: %d, 下载测速地址: %s, 出错的重定向后地址: %s\033[0m\n", ip.String(), statusCode, url, finalURL)
|
||||
} else {
|
||||
fmt.Printf("\033[31m[调试] IP: %s, 下载测速失败,错误信息: %v, 下载测速地址: %s, 出错的重定向后地址: %s\033[0m\n", ip.String(), err, url, finalURL)
|
||||
}
|
||||
} else { // 如果 URL 和最终地址一致,说明没有重定向
|
||||
if statusCode > 0 { // 如果状态码大于 0,说明是后续 HTTP 状态码引起的错误
|
||||
fmt.Printf("\033[31m[调试] IP: %s, 下载测速终止,HTTP 状态码: %d, 下载测速地址: %s\033[0m\n", ip.String(), statusCode, url)
|
||||
} else {
|
||||
fmt.Printf("\033[31m[调试] IP: %s, 下载测速失败,错误信息: %v, 下载测速地址: %s\033[0m\n", ip.String(), err, url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return download Speed
|
||||
func downloadHandler(ip *net.IPAddr) (float64, string) {
|
||||
var lastRedirectURL string // 用于记录最后一次重定向目标,以便在访问错误时输出
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{DialContext: getDialContext(ip)},
|
||||
Timeout: Timeout,
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
if len(via) > 10 { // 限制最多重定向 10 次
|
||||
lastRedirectURL = req.URL.String() // 记录每次重定向的目标,以便在访问错误时输出
|
||||
if len(via) > 10 { // 限制最多重定向 10 次
|
||||
if utils.Debug { // 调试模式下,输出更多信息
|
||||
fmt.Printf("\033[31m[调试] IP: %s, 下载测速地址重定向次数过多,终止测速,下载测速地址: %s\033[0m\n", ip.String(), req.URL.String())
|
||||
}
|
||||
@@ -141,21 +166,18 @@ func downloadHandler(ip *net.IPAddr) (float64, string) {
|
||||
response, err := client.Do(req)
|
||||
if err != nil {
|
||||
if utils.Debug { // 调试模式下,输出更多信息
|
||||
finalURL := URL // 默认的最终 URL,这样当 response 为空时也能输出
|
||||
if response != nil && response.Request != nil && response.Request.URL != nil { // 如果 response 和 URL 存在,则获取最终 URL
|
||||
finalURL = response.Request.URL.String()
|
||||
}
|
||||
fmt.Printf("\033[31m[调试] IP: %s, 下载测速失败,错误信息: %v, 下载测速地址: %s, 最终地址(如有重定向): %s\033[0m\n", ip.String(), err, URL, finalURL)
|
||||
printDownloadDebugInfo(ip, err, 0, URL, lastRedirectURL, response)
|
||||
}
|
||||
return 0.0, ""
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != 200 {
|
||||
if utils.Debug { // 调试模式下,输出更多信息
|
||||
fmt.Printf("\033[31m[调试] IP: %s, 下载测速终止,HTTP 状态码: %d, 下载测速地址: %s, 最终地址(如有重定向): %s\033[0m\n", ip.String(), response.StatusCode, URL, response.Request.URL.String())
|
||||
printDownloadDebugInfo(ip, nil, response.StatusCode, URL, lastRedirectURL, response)
|
||||
}
|
||||
return 0.0, ""
|
||||
}
|
||||
|
||||
// 通过头部参数获取地区码
|
||||
colo := getHeaderColo(response.Header)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user