🔧 customize HTTP user-agent for fetching subscription from API.

This commit is contained in:
tao-lintian
2024-07-15 21:01:22 +08:00
parent 0946412ea7
commit fd22cd1499
6 changed files with 41 additions and 9 deletions

View File

@ -7,10 +7,26 @@ import (
"time"
)
func Get(url string) (resp *http.Response, err error) {
type GetConfig struct {
userAgent string
}
type GetOption func(*GetConfig)
func WithUserAgent(userAgent string) GetOption {
return func(config *GetConfig) {
config.userAgent = userAgent
}
}
func Get(url string, options ...GetOption) (resp *http.Response, err error) {
retryTimes := config.Default.RequestRetryTimes
haveTried := 0
retryDelay := time.Second // 延迟1秒再重试
getConfig := GetConfig{}
for _, option := range options {
option(&getConfig)
}
for haveTried < retryTimes {
client := &http.Client{}
//client.Timeout = time.Second * 10
@ -20,6 +36,9 @@ func Get(url string) (resp *http.Response, err error) {
time.Sleep(retryDelay)
continue
}
if getConfig.userAgent != "" {
req.Header.Set("User-Agent", getConfig.userAgent)
}
get, err := client.Do(req)
if err != nil {
haveTried++

View File

@ -16,9 +16,9 @@ import (
var subsDir = "subs"
var fileLock sync.RWMutex
func LoadSubscription(url string, refresh bool) ([]byte, error) {
func LoadSubscription(url string, refresh bool, userAgent string) ([]byte, error) {
if refresh {
return FetchSubscriptionFromAPI(url)
return FetchSubscriptionFromAPI(url, userAgent)
}
hash := sha256.Sum224([]byte(url))
fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
@ -27,7 +27,7 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
if !os.IsNotExist(err) {
return nil, err
}
return FetchSubscriptionFromAPI(url)
return FetchSubscriptionFromAPI(url, userAgent)
}
lastGetTime := stat.ModTime().Unix() // 单位是秒
if lastGetTime+config.Default.CacheExpire > time.Now().Unix() {
@ -48,13 +48,13 @@ func LoadSubscription(url string, refresh bool) ([]byte, error) {
}
return subContent, nil
}
return FetchSubscriptionFromAPI(url)
return FetchSubscriptionFromAPI(url, userAgent)
}
func FetchSubscriptionFromAPI(url string) ([]byte, error) {
func FetchSubscriptionFromAPI(url string, userAgent string) ([]byte, error) {
hash := sha256.Sum224([]byte(url))
fileName := filepath.Join(subsDir, hex.EncodeToString(hash[:]))
resp, err := Get(url)
resp, err := Get(url, WithUserAgent(userAgent))
if err != nil {
return nil, err
}