1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-25 01:34:41 -05:00
sub2clash/utils/get.go

43 lines
962 B
Go
Raw Normal View History

2023-09-12 06:40:24 -04:00
package utils
import (
2023-09-12 12:46:17 -04:00
"errors"
2023-09-12 06:40:24 -04:00
"net/http"
2023-09-12 12:46:17 -04:00
"sub2clash/config"
2023-09-12 06:40:24 -04:00
"time"
)
2023-09-12 12:46:17 -04:00
func Get(url string) (resp *http.Response, err error) {
retryTimes := config.Default.RequestRetryTimes
2023-09-12 06:40:24 -04:00
haveTried := 0
retryDelay := time.Second // 延迟1秒再重试
for haveTried < retryTimes {
client := &http.Client{}
client.Timeout = time.Second * 10
req, err := http.NewRequest("GET", url, nil)
if err != nil {
haveTried++
time.Sleep(retryDelay)
continue
}
req.Header.Set(
"User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
)
get, err := client.Do(req)
2023-09-12 06:40:24 -04:00
if err != nil {
haveTried++
time.Sleep(retryDelay)
continue
} else {
2023-09-12 12:46:17 -04:00
// 如果文件大小大于设定,直接返回错误
if get != nil && get.ContentLength > config.Default.RequestMaxFileSize {
return nil, errors.New("文件过大")
}
2023-09-12 06:40:24 -04:00
return get, nil
}
2023-09-12 12:46:17 -04:00
2023-09-12 06:40:24 -04:00
}
return nil, err
}