Accept "any" outbound in dns rule

This commit is contained in:
世界 2023-03-29 10:30:31 +08:00
parent 88fafd4e30
commit b3fb86d415
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 20 additions and 33 deletions

View file

@ -232,6 +232,8 @@ Invert match result.
Match outbound. Match outbound.
`any` can be used as a value to match any outbound.
#### server #### server
==Required== ==Required==
@ -254,18 +256,4 @@ Disable cache and save cache in this query.
#### rules #### rules
Included default rules. Included default rules.
#### invert
Invert match result.
#### server
==Required==
Tag of the target dns server.
#### disable_cache
Disable cache and save cache in this query.

View file

@ -231,6 +231,8 @@ DNS 查询类型。值可以为整数或者类型名称字符串。
匹配出站。 匹配出站。
`any` 可作为值用于匹配任意出站。
#### server #### server
==必填== ==必填==
@ -253,18 +255,4 @@ DNS 查询类型。值可以为整数或者类型名称字符串。
#### rules #### rules
包括的默认规则。 包括的默认规则。
#### invert
反选匹配结果。
#### server
==必填==
目标 DNS 服务器的标签。
#### disable_cache
在此查询中禁用缓存。

View file

@ -23,7 +23,10 @@
"disable_cache": true "disable_cache": true
}, },
{ {
"domain": "mydomain.com", "outbound": "any",
"server": "local"
},
{
"geosite": "cn", "geosite": "cn",
"server": "local" "server": "local"
} }

View file

@ -12,17 +12,25 @@ var _ RuleItem = (*OutboundItem)(nil)
type OutboundItem struct { type OutboundItem struct {
outbounds []string outbounds []string
outboundMap map[string]bool outboundMap map[string]bool
matchAny bool
} }
func NewOutboundRule(outbounds []string) *OutboundItem { func NewOutboundRule(outbounds []string) *OutboundItem {
rule := &OutboundItem{outbounds, make(map[string]bool)} rule := &OutboundItem{outbounds: outbounds, outboundMap: make(map[string]bool)}
for _, outbound := range outbounds { for _, outbound := range outbounds {
rule.outboundMap[outbound] = true if outbound == "any" {
rule.matchAny = true
} else {
rule.outboundMap[outbound] = true
}
} }
return rule return rule
} }
func (r *OutboundItem) Match(metadata *adapter.InboundContext) bool { func (r *OutboundItem) Match(metadata *adapter.InboundContext) bool {
if r.matchAny && metadata.Outbound != "" {
return true
}
return r.outboundMap[metadata.Outbound] return r.outboundMap[metadata.Outbound]
} }