Add dialer options for remote ruleset

This commit is contained in:
Kevin Zhou 2025-03-12 21:20:35 +08:00
parent 75f526cb29
commit 34f6e10fd4
6 changed files with 73 additions and 26 deletions

View File

@ -114,6 +114,7 @@ Takes no effect if `outbound.routing_mark` is set.
See [Dial Fields](/configuration/shared/dial/#domain_resolver) for details. See [Dial Fields](/configuration/shared/dial/#domain_resolver) for details.
Can be overrides by `outbound.domain_resolver`. Can be overrides by `outbound.domain_resolver`.
Can be overrides by `ruleset.domain_resolver`.
#### default_network_strategy #### default_network_strategy

View File

@ -113,6 +113,7 @@ icon: material/alert-decagram
详情参阅 [拨号字段](/configuration/shared/dial/#domain_resolver)。 详情参阅 [拨号字段](/configuration/shared/dial/#domain_resolver)。
可以被 `outbound.domain_resolver` 覆盖。 可以被 `outbound.domain_resolver` 覆盖。
可以被 `ruleset.domain_resolver` 覆盖。
#### network_strategy #### network_strategy

View File

@ -43,8 +43,12 @@
"tag": "", "tag": "",
"format": "source", // or binary "format": "source", // or binary
"url": "", "url": "",
"update_interval": "", // optional
"download_detour": "", // optional "download_detour": "", // optional
"update_interval": "" // optional "detour": "", // optional
"domain_resolver": "" // optional
... // Dial Fields
} }
``` ```
@ -100,14 +104,28 @@ File path of rule-set.
Download URL of rule-set. Download URL of rule-set.
#### download_detour
Tag of the outbound to download rule-set.
Default outbound will be used if empty.
#### update_interval #### update_interval
Update interval of rule-set. Update interval of rule-set.
`1d` will be used if empty. `1d` will be used if empty.
### download_detour
This field is retained for compatibility only, please use the detour field.
When both this field and the detour field have valid content, the content of the detour field takes precedence.
#### detour
Tag of the outbound to download rule-set.
Default outbound will be used if empty.
#### domain_resolver
Set domain resolver to use for resolving domain names.
If this option and router.default_domain_resolver are set at the same time, router.default_domain_resolver will be overwritten
### Dial Fields
See [Dial Fields](/configuration/shared/dial/) for details.

View File

@ -43,8 +43,12 @@
"tag": "", "tag": "",
"format": "source", // or binary "format": "source", // or binary
"url": "", "url": "",
"update_interval": "", // 可选
"download_detour": "", // 可选 "download_detour": "", // 可选
"update_interval": "" // 可选 "detour": "", // 可选
"domain_resolver": "" // 可选
... // 拨号字段
} }
``` ```
@ -100,14 +104,28 @@
规则集的下载 URL。 规则集的下载 URL。
#### download_detour
用于下载规则集的出站的标签。
如果为空,将使用默认出站。
#### update_interval #### update_interval
规则集的更新间隔。 规则集的更新间隔。
默认使用 `1d` 默认使用 `1d`
### download_detour
保留此字段只是为了保证兼容性请使用detour字段
在此字段和detour字段的内容都有效时优先使用detour字段的内容
#### detour
用于下载规则集的出站的标签。
如果为空,将使用默认出站。
#### domain_resolver
用于设置解析域名的域名解析器。
如果此选项和router.default_domain_resolver同时设置router.default_domain_resolver会被覆盖
### 拨号字段
参阅 [拨号字段](/zh/configuration/shared/dial/)。

View File

@ -85,9 +85,11 @@ type LocalRuleSet struct {
} }
type RemoteRuleSet struct { type RemoteRuleSet struct {
DialerOptions
URL string `json:"url"` URL string `json:"url"`
DownloadDetour string `json:"download_detour,omitempty"`
UpdateInterval badoption.Duration `json:"update_interval,omitempty"` UpdateInterval badoption.Duration `json:"update_interval,omitempty"`
// keep this filed for compatibility, use Detour field please
DownloadDetour string `json:"download_detour,omitempty"`
} }
type _HeadlessRule struct { type _HeadlessRule struct {

View File

@ -13,6 +13,7 @@ import (
"time" "time"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/dialer"
"github.com/sagernet/sing-box/common/srs" "github.com/sagernet/sing-box/common/srs"
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
@ -83,17 +84,23 @@ func (s *RemoteRuleSet) String() string {
func (s *RemoteRuleSet) StartContext(ctx context.Context, startContext *adapter.HTTPStartContext) error { func (s *RemoteRuleSet) StartContext(ctx context.Context, startContext *adapter.HTTPStartContext) error {
s.cacheFile = service.FromContext[adapter.CacheFile](s.ctx) s.cacheFile = service.FromContext[adapter.CacheFile](s.ctx)
var dialer N.Dialer
if s.options.RemoteOptions.DownloadDetour != "" { detour := s.options.RemoteOptions.Detour
outbound, loaded := s.outbound.Outbound(s.options.RemoteOptions.DownloadDetour) if detour == "" && s.options.RemoteOptions.DownloadDetour != "" {
if !loaded { detour = s.options.RemoteOptions.DownloadDetour
return E.New("download detour not found: ", s.options.RemoteOptions.DownloadDetour) s.options.RemoteOptions.DialerOptions.Detour = detour
}
dialer = outbound
} else {
dialer = s.outbound.Default()
} }
s.dialer = dialer
outboundDialer, err := dialer.NewWithOptions(dialer.Options{
Context: ctx,
Options: s.options.RemoteOptions.DialerOptions,
RemoteIsDomain: true,
ResolverOnDetour: detour != "",
})
if err != nil {
return err
}
s.dialer = outboundDialer
if s.cacheFile != nil { if s.cacheFile != nil {
if savedSet := s.cacheFile.LoadRuleSet(s.options.Tag); savedSet != nil { if savedSet := s.cacheFile.LoadRuleSet(s.options.Tag); savedSet != nil {
err := s.loadBytes(savedSet.Content) err := s.loadBytes(savedSet.Content)
@ -228,7 +235,7 @@ func (s *RemoteRuleSet) fetchOnce(ctx context.Context, startContext *adapter.HTT
s.logger.Debug("updating rule-set ", s.options.Tag, " from URL: ", s.options.RemoteOptions.URL) s.logger.Debug("updating rule-set ", s.options.Tag, " from URL: ", s.options.RemoteOptions.URL)
var httpClient *http.Client var httpClient *http.Client
if startContext != nil { if startContext != nil {
httpClient = startContext.HTTPClient(s.options.RemoteOptions.DownloadDetour, s.dialer) httpClient = startContext.HTTPClient(s.options.RemoteOptions.Detour, s.dialer)
} else { } else {
httpClient = &http.Client{ httpClient = &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{