mirror of
https://github.com/bestnite/sub2sing-box.git
synced 2026-06-08 16:04:43 +00:00
feat: add custom User-Agent support for subscription and template requests
Add --user-agent / -u CLI flag and user-agent config field to allow customizing the User-Agent header when fetching subscriptions and remote templates. This is useful for providers that require a specific UA to access their subscription links. Closes #23
This commit is contained in:
+11
-3
@@ -5,23 +5,31 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Fetch(url string, maxRetryTimes int) (string, error) {
|
||||
func Fetch(url string, maxRetryTimes int, userAgent string) (string, error) {
|
||||
retryTime := 0
|
||||
var err error
|
||||
var resp *http.Response
|
||||
for retryTime < maxRetryTimes {
|
||||
resp, err = http.Get(url)
|
||||
req, reqErr := http.NewRequest("GET", url, nil)
|
||||
if reqErr != nil {
|
||||
return "", reqErr
|
||||
}
|
||||
if userAgent != "" {
|
||||
req.Header.Set("User-Agent", userAgent)
|
||||
}
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
retryTime++
|
||||
continue
|
||||
}
|
||||
var data []byte
|
||||
data, err = io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if err != nil {
|
||||
retryTime++
|
||||
continue
|
||||
}
|
||||
return string(data), err
|
||||
return string(data), nil
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user