@@ -110,13 +110,38 @@ func getDialContext(ip *net.IPAddr) func(ctx context.Context, network, address s
110110 }
111111}
112112
113+ // 统一的请求报错调试输出
114+ func printDownloadDebugInfo (ip * net.IPAddr , err error , statusCode int , url , lastRedirectURL string , response * http.Response ) {
115+ finalURL := url // 默认的最终 URL,这样当 response 为空时也能输出
116+ if lastRedirectURL != "" {
117+ finalURL = lastRedirectURL // 如果 lastRedirectURL 不是空,说明重定向过,优先输出最后一次要重定向至的目标
118+ } else if response != nil && response .Request != nil && response .Request .URL != nil {
119+ finalURL = response .Request .URL .String () // 如果 response 不为 nil,且 Request 和 URL 都不为 nil,则获取最后一次成功的响应地址
120+ }
121+ if url != finalURL { // 如果 URL 和最终地址不一致,说明有重定向,是该重定向后的地址引起的错误
122+ if statusCode > 0 { // 如果状态码大于 0,说明是后续 HTTP 状态码引起的错误
123+ fmt .Printf ("\033 [31m[调试] IP: %s, 下载测速终止,HTTP 状态码: %d, 下载测速地址: %s, 出错的重定向后地址: %s\033 [0m\n " , ip .String (), statusCode , url , finalURL )
124+ } else {
125+ fmt .Printf ("\033 [31m[调试] IP: %s, 下载测速失败,错误信息: %v, 下载测速地址: %s, 出错的重定向后地址: %s\033 [0m\n " , ip .String (), err , url , finalURL )
126+ }
127+ } else { // 如果 URL 和最终地址一致,说明没有重定向
128+ if statusCode > 0 { // 如果状态码大于 0,说明是后续 HTTP 状态码引起的错误
129+ fmt .Printf ("\033 [31m[调试] IP: %s, 下载测速终止,HTTP 状态码: %d, 下载测速地址: %s\033 [0m\n " , ip .String (), statusCode , url )
130+ } else {
131+ fmt .Printf ("\033 [31m[调试] IP: %s, 下载测速失败,错误信息: %v, 下载测速地址: %s\033 [0m\n " , ip .String (), err , url )
132+ }
133+ }
134+ }
135+
113136// return download Speed
114137func downloadHandler (ip * net.IPAddr ) (float64 , string ) {
138+ var lastRedirectURL string // 用于记录最后一次重定向目标,以便在访问错误时输出
115139 client := & http.Client {
116140 Transport : & http.Transport {DialContext : getDialContext (ip )},
117141 Timeout : Timeout ,
118142 CheckRedirect : func (req * http.Request , via []* http.Request ) error {
119- if len (via ) > 10 { // 限制最多重定向 10 次
143+ lastRedirectURL = req .URL .String () // 记录每次重定向的目标,以便在访问错误时输出
144+ if len (via ) > 10 { // 限制最多重定向 10 次
120145 if utils .Debug { // 调试模式下,输出更多信息
121146 fmt .Printf ("\033 [31m[调试] IP: %s, 下载测速地址重定向次数过多,终止测速,下载测速地址: %s\033 [0m\n " , ip .String (), req .URL .String ())
122147 }
@@ -141,21 +166,18 @@ func downloadHandler(ip *net.IPAddr) (float64, string) {
141166 response , err := client .Do (req )
142167 if err != nil {
143168 if utils .Debug { // 调试模式下,输出更多信息
144- finalURL := URL // 默认的最终 URL,这样当 response 为空时也能输出
145- if response != nil && response .Request != nil && response .Request .URL != nil { // 如果 response 和 URL 存在,则获取最终 URL
146- finalURL = response .Request .URL .String ()
147- }
148- fmt .Printf ("\033 [31m[调试] IP: %s, 下载测速失败,错误信息: %v, 下载测速地址: %s, 最终地址(如有重定向): %s\033 [0m\n " , ip .String (), err , URL , finalURL )
169+ printDownloadDebugInfo (ip , err , 0 , URL , lastRedirectURL , response )
149170 }
150171 return 0.0 , ""
151172 }
152173 defer response .Body .Close ()
153174 if response .StatusCode != 200 {
154175 if utils .Debug { // 调试模式下,输出更多信息
155- fmt . Printf ( " \033 [31m[调试] IP: %s, 下载测速终止,HTTP 状态码: %d, 下载测速地址: %s, 最终地址(如有重定向): %s \033 [0m \n " , ip . String (), response .StatusCode , URL , response . Request . URL . String () )
176+ printDownloadDebugInfo ( ip , nil , response .StatusCode , URL , lastRedirectURL , response )
156177 }
157178 return 0.0 , ""
158179 }
180+
159181 // 通过头部参数获取地区码
160182 colo := getHeaderColo (response .Header )
161183
0 commit comments