sub2clash/utils/get.go

39 lines
803 B
Go
Raw Normal View History

2023-09-12 18:40:24 +08:00
package utils
import (
2023-09-13 00:46:17 +08:00
"errors"
2023-09-12 18:40:24 +08:00
"net/http"
2023-09-13 00:46:17 +08:00
"sub2clash/config"
2023-09-12 18:40:24 +08:00
"time"
)
2023-09-13 00:46:17 +08:00
func Get(url string) (resp *http.Response, err error) {
retryTimes := config.Default.RequestRetryTimes
2023-09-12 18:40:24 +08:00
haveTried := 0
retryDelay := time.Second // 延迟1秒再重试
for haveTried < retryTimes {
client := &http.Client{}
2023-09-23 08:48:45 +08:00
//client.Timeout = time.Second * 10
req, err := http.NewRequest("GET", url, nil)
if err != nil {
haveTried++
time.Sleep(retryDelay)
continue
}
get, err := client.Do(req)
2023-09-12 18:40:24 +08:00
if err != nil {
haveTried++
time.Sleep(retryDelay)
continue
} else {
2023-09-13 00:46:17 +08:00
// 如果文件大小大于设定,直接返回错误
if get != nil && get.ContentLength > config.Default.RequestMaxFileSize {
return nil, errors.New("文件过大")
}
2023-09-12 18:40:24 +08:00
return get, nil
}
2023-09-13 00:46:17 +08:00
2023-09-12 18:40:24 +08:00
}
return nil, err
}