diff --git a/api/handler/default.go b/api/handler/default.go index eafa865..ba36a64 100644 --- a/api/handler/default.go +++ b/api/handler/default.go @@ -32,7 +32,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template template = query.Template } if strings.HasPrefix(template, "http") { - templateBytes, err = common.LoadSubscription(template, query.Refresh) + templateBytes, err = common.LoadSubscription(template, query.Refresh, query.UserAgent) if err != nil { logger.Logger.Debug( "load template failed", zap.String("template", template), zap.Error(err), @@ -61,7 +61,7 @@ func BuildSub(clashType model.ClashType, query validator.SubValidator, template var proxyList []model.Proxy // 加载订阅 for i := range query.Subs { - data, err := common.LoadSubscription(query.Subs[i], query.Refresh) + data, err := common.LoadSubscription(query.Subs[i], query.Refresh, query.UserAgent) subName := "" if strings.Contains(query.Subs[i], "#") { subName = query.Subs[i][strings.LastIndex(query.Subs[i], "#")+1:] diff --git a/api/static/index.html b/api/static/index.html index eddd8b7..5f6fe49 100644 --- a/api/static/index.html +++ b/api/static/index.html @@ -75,6 +75,12 @@ + +
+ + +
@@ -172,4 +178,5 @@
+ \ No newline at end of file diff --git a/api/static/index.js b/api/static/index.js index b41a43e..35bc1f1 100644 --- a/api/static/index.js +++ b/api/static/index.js @@ -66,6 +66,11 @@ function generateURI() { // alert("订阅链接和节点分享链接不能同时为空!"); return ""; } + + // 获取订阅user-agent标识 + const userAgent = document.getElementById("user-agent").value; + queryParams.push(`userAgent=${userAgent}`) + // 获取复选框的值 const refresh = document.getElementById("refresh").checked; queryParams.push(`refresh=${refresh ? "true" : "false"}`); diff --git a/common/get.go b/common/get.go index de5ddd5..4560bca 100644 --- a/common/get.go +++ b/common/get.go @@ -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++ diff --git a/common/sub.go b/common/sub.go index 8ab3082..551dd37 100644 --- a/common/sub.go +++ b/common/sub.go @@ -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 } diff --git a/validator/sub.go b/validator/sub.go index 1557257..5553337 100644 --- a/validator/sub.go +++ b/validator/sub.go @@ -31,6 +31,7 @@ type SubValidator struct { ReplaceTo []string `form:"-" binding:""` NodeListMode bool `form:"nodeList,default=false" binding:""` IgnoreCountryGrooup bool `form:"ignoreCountryGroup,default=false" binding:""` + UserAgent string `form:"userAgent" binding:""` } type RuleProviderStruct struct {