1
0
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:
2026-05-23 18:41:55 +10:00
parent b63662347a
commit 2a98e7c1f0
6 changed files with 28 additions and 10 deletions
+7 -6
View File
@@ -36,6 +36,7 @@ func Convert(
sortKey string,
sortType string,
groupRules map[string][]string,
userAgent string,
) (string, error) {
result := ""
var err error
@@ -44,7 +45,7 @@ func Convert(
groupType = C.TypeSelector
}
outbounds, err := ConvertSubscriptionsToSProxy(subscriptions)
outbounds, err := ConvertSubscriptionsToSProxy(subscriptions, userAgent)
if err != nil {
return "", err
}
@@ -104,7 +105,7 @@ func Convert(
outbounds = AddCountryGroup(outbounds, groupType, sortKey, sortType, groupRules)
}
if templatePath != "" {
templateData, err := ReadTemplate(templatePath)
templateData, err := ReadTemplate(templatePath, userAgent)
if err != nil {
return "", err
}
@@ -226,12 +227,12 @@ func AddCountryGroup(proxies []model.Outbound, groupType string, sortKey string,
return append(proxies, groups...)
}
func ReadTemplate(template string) (string, error) {
func ReadTemplate(template string, userAgent string) (string, error) {
var data string
var err error
isNetworkFile, _ := regexp.MatchString(`^https?://`, template)
if isNetworkFile {
data, err = util.Fetch(template, 3)
data, err = util.Fetch(template, 3, userAgent)
if err != nil {
return "", err
}
@@ -320,10 +321,10 @@ func ConvertCProxyToSProxy(proxy string) (model.Outbound, error) {
return model.Outbound{}, errors.New("unknown proxy format")
}
func ConvertSubscriptionsToSProxy(urls []string) ([]model.Outbound, error) {
func ConvertSubscriptionsToSProxy(urls []string, userAgent string) ([]model.Outbound, error) {
proxyList := make([]model.Outbound, 0)
for _, url := range urls {
data, err := util.Fetch(url, 3)
data, err := util.Fetch(url, 3, userAgent)
if err != nil {
return nil, err
}