Skip to content

Commit

Permalink
Add accept empty DNS rule option
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Aug 3, 2024
1 parent e8aee80 commit 6807dd8
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 42 deletions.
5 changes: 4 additions & 1 deletion adapter/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ type InboundContext struct {

// rule cache

IPCIDRMatchSource bool
IPCIDRMatchSource bool
IPCIDRAcceptEmpty bool

SourceAddressMatch bool
SourcePortMatch bool
DestinationAddressMatch bool
Expand All @@ -62,6 +64,7 @@ type InboundContext struct {

func (c *InboundContext) ResetRuleCache() {
c.IPCIDRMatchSource = false
c.IPCIDRAcceptEmpty = false
c.SourceAddressMatch = false
c.SourcePortMatch = false
c.DestinationAddressMatch = false
Expand Down
29 changes: 27 additions & 2 deletions docs/configuration/dns/rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
icon: material/new-box
---

!!! quote "Changes in sing-box 1.10.0"

:material-delete-clock: [rule_set_ipcidr_match_source](#rule_set_ipcidr_match_source)
:material-plus: [rule_set_ip_cidr_match_source](#rule_set_ip_cidr_match_source)
:material-plus: [rule_set_ip_cidr_accept_empty](#rule_set_ip_cidr_accept_empty)

!!! quote "Changes in sing-box 1.9.0"

:material-plus: [geoip](#geoip)
Expand Down Expand Up @@ -117,7 +123,10 @@ icon: material/new-box
"geoip-cn",
"geosite-cn"
],
// deprecated
"rule_set_ipcidr_match_source": false,
"rule_set_ip_cidr_match_source": false,
"rule_set_ip_cidr_accept_empty": false,
"invert": false,
"outbound": [
"direct"
Expand Down Expand Up @@ -309,7 +318,17 @@ Match [Rule Set](/configuration/route/#rule_set).

!!! question "Since sing-box 1.9.0"

Make `ipcidr` in rule sets match the source IP.
!!! failure "Deprecated in sing-box 1.10.0"

`rule_set_ipcidr_match_source` is renamed to `rule_set_ip_cidr_match_source` and will be remove in sing-box 1.11.0.

Make `ip_cidr` rule items in rule sets match the source IP.

#### rule_set_ip_cidr_match_source

!!! question "Since sing-box 1.10.0"

Make `ip_cidr` rule items in rule sets match the source IP.

#### invert

Expand Down Expand Up @@ -347,7 +366,7 @@ Will overrides `dns.client_subnet` and `servers.[].client_subnet`.

### Address Filter Fields

Only takes effect for IP address requests. When the query results do not match the address filtering rule items, the current rule will be skipped.
Only takes effect for address requests (A/AAAA/HTTPS). When the query results do not match the address filtering rule items, the current rule will be skipped.

!!! info ""

Expand Down Expand Up @@ -375,6 +394,12 @@ Match IP CIDR with query response.

Match private IP with query response.

#### rule_set_ip_cidr_accept_empty

!!! question "Since sing-box 1.10.0"

Make `ip_cidr` rules in rule sets accept empty query response.

### Logical Fields

#### type
Expand Down
31 changes: 28 additions & 3 deletions docs/configuration/dns/rule.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
icon: material/new-box
---

!!! quote "sing-box 1.10.0 中的更改"

:material-delete-clock: [rule_set_ipcidr_match_source](#rule_set_ipcidr_match_source)
:material-plus: [rule_set_ip_cidr_match_source](#rule_set_ip_cidr_match_source)
:material-plus: [rule_set_ip_cidr_accept_empty](#rule_set_ip_cidr_accept_empty)

!!! quote "sing-box 1.9.0 中的更改"

:material-plus: [geoip](#geoip)
Expand Down Expand Up @@ -117,7 +123,10 @@ icon: material/new-box
"geoip-cn",
"geosite-cn"
],
// 已弃用
"rule_set_ipcidr_match_source": false,
"rule_set_ip_cidr_match_source": false,
"rule_set_ip_cidr_accept_empty": false,
"invert": false,
"outbound": [
"direct"
Expand Down Expand Up @@ -307,7 +316,17 @@ DNS 查询类型。值可以为整数或者类型名称字符串。

!!! question "自 sing-box 1.9.0 起"

使规则集中的 `ipcidr` 规则匹配源 IP。
!!! failure "已在 sing-box 1.10.0 废弃"

`rule_set_ipcidr_match_source` 已重命名为 `rule_set_ip_cidr_match_source` 且将在 sing-box 1.11.0 移除。

使规则集中的 `ip_cidr` 规则匹配源 IP。

#### rule_set_ip_cidr_match_source

!!! question "自 sing-box 1.10.0 起"

使规则集中的 `ip_cidr` 规则匹配源 IP。

#### invert

Expand Down Expand Up @@ -345,7 +364,7 @@ DNS 查询类型。值可以为整数或者类型名称字符串。

### 地址筛选字段

仅对IP地址请求生效。 当查询结果与地址筛选规则项不匹配时,将跳过当前规则。
仅对地址请求 (A/AAAA/HTTPS) 生效。 当查询结果与地址筛选规则项不匹配时,将跳过当前规则。

!!! info ""

Expand All @@ -365,14 +384,20 @@ DNS 查询类型。值可以为整数或者类型名称字符串。

!!! question "自 sing-box 1.9.0 起"

与查询相应匹配 IP CIDR。
与查询响应匹配 IP CIDR。

#### ip_is_private

!!! question "自 sing-box 1.9.0 起"

与查询响应匹配非公开 IP。

#### rule_set_ip_cidr_accept_empty

!!! question "自 sing-box 1.10.0 起"

使规则集中的 `ip_cidr` 规则接受空查询响应。

### 逻辑字段

#### type
Expand Down
12 changes: 6 additions & 6 deletions docs/configuration/inbound/tun.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ tun 接口的 IPv4 和 IPv6 前缀。

!!! failure "已在 sing-box 1.10.0 废弃"

`inet4_address` 已合并到 `address` 且将在 sing-box 1.11.0 移除.
`inet4_address` 已合并到 `address` 且将在 sing-box 1.11.0 移除

==必填==

Expand All @@ -178,7 +178,7 @@ tun 接口的 IPv4 前缀。

!!! failure "已在 sing-box 1.10.0 废弃"

`inet6_address` 已合并到 `address` 且将在 sing-box 1.11.0 移除.
`inet6_address` 已合并到 `address` 且将在 sing-box 1.11.0 移除

tun 接口的 IPv6 前缀。

Expand Down Expand Up @@ -288,15 +288,15 @@ tun 接口的 IPv6 前缀。

!!! failure "已在 sing-box 1.10.0 废弃"

`inet4_route_address` 已合并到 `route_address` 且将在 sing-box 1.11.0 移除.
`inet4_route_address` 已合并到 `route_address` 且将在 sing-box 1.11.0 移除

启用 `auto_route` 时使用自定义路由而不是默认路由。

#### inet6_route_address

!!! failure "已在 sing-box 1.10.0 废弃"

`inet6_route_address` 已合并到 `route_address` 且将在 sing-box 1.11.0 移除.
`inet6_route_address` 已合并到 `route_address` 且将在 sing-box 1.11.0 移除

启用 `auto_route` 时使用自定义路由而不是默认路由。

Expand All @@ -310,15 +310,15 @@ tun 接口的 IPv6 前缀。

!!! failure "已在 sing-box 1.10.0 废弃"

`inet4_route_exclude_address` 已合并到 `route_exclude_address` 且将在 sing-box 1.11.0 移除.
`inet4_route_exclude_address` 已合并到 `route_exclude_address` 且将在 sing-box 1.11.0 移除

启用 `auto_route` 时排除自定义路由。

#### inet6_route_exclude_address

!!! failure "已在 sing-box 1.10.0 废弃"

`inet6_route_exclude_address` 已合并到 `route_exclude_address` 且将在 sing-box 1.11.0 移除.
`inet6_route_exclude_address` 已合并到 `route_exclude_address` 且将在 sing-box 1.11.0 移除

启用 `auto_route` 时排除自定义路由。

Expand Down
23 changes: 22 additions & 1 deletion docs/configuration/route/rule.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
---
icon: material/alert-decagram
---

!!! quote "Changes in sing-box 1.10.0"

:material-delete-clock: [rule_set_ipcidr_match_source](#rule_set_ipcidr_match_source)
:material-plus: [rule_set_ip_cidr_match_source](#rule_set_ip_cidr_match_source)

!!! quote "Changes in sing-box 1.8.0"

:material-plus: [rule_set](#rule_set)
Expand Down Expand Up @@ -105,7 +114,9 @@
"geoip-cn",
"geosite-cn"
],
// deprecated
"rule_set_ipcidr_match_source": false,
"rule_set_ip_cidr_match_source": false,
"invert": false,
"outbound": "direct"
},
Expand Down Expand Up @@ -303,7 +314,17 @@ Match [Rule Set](/configuration/route/#rule_set).

!!! question "Since sing-box 1.8.0"

Make `ipcidr` in rule sets match the source IP.
!!! failure "Deprecated in sing-box 1.10.0"

`rule_set_ipcidr_match_source` is renamed to `rule_set_ip_cidr_match_source` and will be remove in sing-box 1.11.0.

Make `ip_cidr` in rule sets match the source IP.

#### rule_set_ip_cidr_match_source

!!! question "Since sing-box 1.10.0"

Make `ip_cidr` in rule sets match the source IP.

#### invert

Expand Down
23 changes: 22 additions & 1 deletion docs/configuration/route/rule.zh.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
---
icon: material/alert-decagram
---

!!! quote "sing-box 1.10.0 中的更改"

:material-delete-clock: [rule_set_ipcidr_match_source](#rule_set_ipcidr_match_source)
:material-plus: [rule_set_ip_cidr_match_source](#rule_set_ip_cidr_match_source)

!!! quote "sing-box 1.8.0 中的更改"

:material-plus: [rule_set](#rule_set)
Expand Down Expand Up @@ -103,7 +112,9 @@
"geoip-cn",
"geosite-cn"
],
// 已弃用
"rule_set_ipcidr_match_source": false,
"rule_set_ip_cidr_match_source": false,
"invert": false,
"outbound": "direct"
},
Expand Down Expand Up @@ -301,7 +312,17 @@

!!! question "自 sing-box 1.8.0 起"

使规则集中的 `ipcidr` 规则匹配源 IP。
!!! failure "已在 sing-box 1.10.0 废弃"

`rule_set_ipcidr_match_source` 已重命名为 `rule_set_ip_cidr_match_source` 且将在 sing-box 1.11.0 移除。

使规则集中的 `ip_cidr` 规则匹配源 IP。

#### rule_set_ip_cidr_match_source

!!! question "自 sing-box 1.10.0 起"

使规则集中的 `ip_cidr` 规则匹配源 IP。

#### invert

Expand Down
25 changes: 22 additions & 3 deletions option/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (r Rule) IsValid() bool {
}
}

type DefaultRule struct {
type _DefaultRule struct {
Inbound Listable[string] `json:"inbound,omitempty"`
IPVersion int `json:"ip_version,omitempty"`
Network Listable[string] `json:"network,omitempty"`
Expand Down Expand Up @@ -94,12 +94,31 @@ type DefaultRule struct {
WIFISSID Listable[string] `json:"wifi_ssid,omitempty"`
WIFIBSSID Listable[string] `json:"wifi_bssid,omitempty"`
RuleSet Listable[string] `json:"rule_set,omitempty"`
RuleSetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
RuleSetIPCIDRMatchSource bool `json:"rule_set_ip_cidr_match_source,omitempty"`
Invert bool `json:"invert,omitempty"`
Outbound string `json:"outbound,omitempty"`

// Deprecated: renamed to rule_set_ip_cidr_match_source
Deprecated_RulesetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
}

type DefaultRule _DefaultRule

func (r *DefaultRule) UnmarshalJSON(bytes []byte) error {
err := json.Unmarshal(bytes, (*_DefaultRule)(r))
if err != nil {
return err
}
//nolint:staticcheck
//goland:noinspection GoDeprecation
if r.Deprecated_RulesetIPCIDRMatchSource {
r.Deprecated_RulesetIPCIDRMatchSource = false
r.RuleSetIPCIDRMatchSource = true
}
return nil
}

func (r DefaultRule) IsValid() bool {
func (r *DefaultRule) IsValid() bool {
var defaultValue DefaultRule
defaultValue.Invert = r.Invert
defaultValue.Outbound = r.Outbound
Expand Down
26 changes: 23 additions & 3 deletions option/rule_dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (r DNSRule) IsValid() bool {
}
}

type DefaultDNSRule struct {
type _DefaultDNSRule struct {
Inbound Listable[string] `json:"inbound,omitempty"`
IPVersion int `json:"ip_version,omitempty"`
QueryType Listable[DNSQueryType] `json:"query_type,omitempty"`
Expand Down Expand Up @@ -96,15 +96,35 @@ type DefaultDNSRule struct {
WIFISSID Listable[string] `json:"wifi_ssid,omitempty"`
WIFIBSSID Listable[string] `json:"wifi_bssid,omitempty"`
RuleSet Listable[string] `json:"rule_set,omitempty"`
RuleSetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
RuleSetIPCIDRMatchSource bool `json:"rule_set_ip_cidr_match_source,omitempty"`
RuleSetIPCIDRAcceptEmpty bool `json:"rule_set_ip_cidr_accept_empty,omitempty"`
Invert bool `json:"invert,omitempty"`
Server string `json:"server,omitempty"`
DisableCache bool `json:"disable_cache,omitempty"`
RewriteTTL *uint32 `json:"rewrite_ttl,omitempty"`
ClientSubnet *AddrPrefix `json:"client_subnet,omitempty"`

// Deprecated: renamed to rule_set_ip_cidr_match_source
Deprecated_RulesetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
}

type DefaultDNSRule _DefaultDNSRule

func (r *DefaultDNSRule) UnmarshalJSON(bytes []byte) error {
err := json.UnmarshalDisallowUnknownFields(bytes, (*_DefaultDNSRule)(r))
if err != nil {
return err
}
//nolint:staticcheck
//goland:noinspection GoDeprecation
if r.Deprecated_RulesetIPCIDRMatchSource {
r.Deprecated_RulesetIPCIDRMatchSource = false
r.RuleSetIPCIDRMatchSource = true
}
return nil
}

func (r DefaultDNSRule) IsValid() bool {
func (r *DefaultDNSRule) IsValid() bool {
var defaultValue DefaultDNSRule
defaultValue.Invert = r.Invert
defaultValue.Server = r.Server
Expand Down
Loading

0 comments on commit 6807dd8

Please sign in to comment.