mirror of
https://github.com/bestnite/sub2sing-box.git
synced 2026-06-08 16:04:43 +00:00
2a98e7c1f0
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
36 lines
638 B
Go
Executable File
36 lines
638 B
Go
Executable File
package util
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
func Fetch(url string, maxRetryTimes int, userAgent string) (string, error) {
|
|
retryTime := 0
|
|
var err error
|
|
var resp *http.Response
|
|
for retryTime < maxRetryTimes {
|
|
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), nil
|
|
}
|
|
return "", err
|
|
}
|