mirror of
https://github.com/bestnite/sub2clash.git
synced 2025-06-17 20:53:18 +08:00
🔧 customize HTTP user-agent for fetching subscription from API.
This commit is contained in:
@ -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++
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user