Skip to content

Commit cb8cd04

Browse files
j2rong4cnMeo597
andauthored
DNS outbound: Replace "reject" with "return" (rCode is 0 by default) (#6214)
#6214 (comment) Example: #6214 (comment) --------- Co-authored-by: Meo597 <197331664+Meo597@users.noreply.github.com>
1 parent 455f6bc commit cb8cd04

7 files changed

Lines changed: 116 additions & 99 deletions

File tree

infra/conf/common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package conf
33
import (
44
"encoding/json"
55
"fmt"
6+
"math"
67
"strconv"
78
"strings"
89

@@ -199,7 +200,7 @@ func (v *PortRange) UnmarshalJSON(data []byte) error {
199200
if err == nil {
200201
v.From = uint32(from)
201202
v.To = uint32(to)
202-
if v.From > v.To {
203+
if v.From > v.To || v.To > math.MaxUint16 {
203204
return errors.New("invalid port range ", v.From, " -> ", v.To)
204205
}
205206
return nil

infra/conf/dns_proxy.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212

1313
type DNSOutboundRuleConfig struct {
1414
Action string `json:"action"`
15-
QType *PortList `json:"qtype"`
15+
QType *PortList `json:"qType"`
1616
Domain *StringList `json:"domain"`
17+
RCode uint32 `json:"rCode"`
1718
}
1819

1920
func (c *DNSOutboundRuleConfig) Build() (*dns.DNSRuleConfig, error) {
@@ -24,8 +25,8 @@ func (c *DNSOutboundRuleConfig) Build() (*dns.DNSRuleConfig, error) {
2425
rule.Action = dns.RuleAction_Direct
2526
case "drop":
2627
rule.Action = dns.RuleAction_Drop
27-
case "reject":
28-
rule.Action = dns.RuleAction_Reject
28+
case "return":
29+
rule.Action = dns.RuleAction_Return
2930
case "hijack":
3031
rule.Action = dns.RuleAction_Hijack
3132
default:
@@ -34,14 +35,8 @@ func (c *DNSOutboundRuleConfig) Build() (*dns.DNSRuleConfig, error) {
3435

3536
if c.QType != nil {
3637
for _, r := range c.QType.Range {
37-
if r.From > r.To {
38-
return nil, errors.New("invalid qtype range: ", r.String())
39-
}
40-
if r.To > 65535 {
41-
return nil, errors.New("dns rule qtype out of range: ", r.String())
42-
}
43-
for qtype := r.From; qtype <= r.To; qtype++ {
44-
rule.Qtype = append(rule.Qtype, int32(qtype))
38+
for qType := r.From; qType <= r.To; qType++ {
39+
rule.QType = append(rule.QType, int32(qType))
4540
}
4641
}
4742
}
@@ -54,6 +49,11 @@ func (c *DNSOutboundRuleConfig) Build() (*dns.DNSRuleConfig, error) {
5449
rule.Domain = rules
5550
}
5651

52+
if c.RCode > 65535 {
53+
return nil, errors.New("rCode out of range: ", c.RCode)
54+
}
55+
rule.RCode = c.RCode
56+
5757
return rule, nil
5858
}
5959

@@ -133,28 +133,30 @@ func (c *DNSOutboundConfig) buildLegacyDNSPolicy() ([]*dns.DNSRuleConfig, error)
133133
if c.BlockTypes != nil && len(*c.BlockTypes) > 0 {
134134
rule := &dns.DNSRuleConfig{Action: dns.RuleAction_Drop}
135135
if mode == "reject" {
136-
rule.Action = dns.RuleAction_Reject
136+
rule.Action = dns.RuleAction_Return
137+
rule.RCode = 5
137138
}
138-
for _, qtype := range *c.BlockTypes {
139-
if qtype < 0 || qtype > 65535 {
140-
return nil, errors.New("legacy blockTypes qtype out of range: ", qtype)
139+
for _, qType := range *c.BlockTypes {
140+
if qType < 0 || qType > 65535 {
141+
return nil, errors.New("legacy blockTypes qType out of range: ", qType)
141142
}
142-
rule.Qtype = append(rule.Qtype, qtype)
143+
rule.QType = append(rule.QType, qType)
143144
}
144145
rules = append(rules, rule)
145146
}
146147

147148
{
148149
rule := &dns.DNSRuleConfig{Action: dns.RuleAction_Hijack}
149-
rule.Qtype = append(rule.Qtype, 1)
150-
rule.Qtype = append(rule.Qtype, 28)
150+
rule.QType = append(rule.QType, 1)
151+
rule.QType = append(rule.QType, 28)
151152
rules = append(rules, rule)
152153
}
153154

154155
{
155-
rule := &dns.DNSRuleConfig{Action: dns.RuleAction_Reject}
156+
rule := &dns.DNSRuleConfig{Action: dns.RuleAction_Return}
156157
if mode == "reject" {
157-
rule.Action = dns.RuleAction_Reject
158+
rule.Action = dns.RuleAction_Return
159+
rule.RCode = 5
158160
} else if mode == "drop" {
159161
rule.Action = dns.RuleAction_Drop
160162
} else if mode == "skip" {

infra/conf/dns_proxy_test.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func TestDnsProxyConfig(t *testing.T) {
3535
Input: `{
3636
"rules": [{
3737
"action": "direct",
38-
"qtype": "1,3,23-24"
38+
"qType": "1,3,23-24"
3939
}, {
4040
"action": "drop",
41-
"qtype": 28,
41+
"qType": 28,
4242
"domain": ["domain:example.com", "full:example.com"]
4343
}]
4444
}`,
@@ -48,11 +48,11 @@ func TestDnsProxyConfig(t *testing.T) {
4848
Rule: []*dns.DNSRuleConfig{
4949
{
5050
Action: dns.RuleAction_Direct,
51-
Qtype: []int32{1, 3, 23, 24},
51+
QType: []int32{1, 3, 23, 24},
5252
},
5353
{
5454
Action: dns.RuleAction_Drop,
55-
Qtype: []int32{28},
55+
QType: []int32{28},
5656
Domain: []*geodata.DomainRule{
5757
{
5858
Value: &geodata.DomainRule_Custom{
@@ -78,7 +78,8 @@ func TestDnsProxyConfig(t *testing.T) {
7878
{
7979
Input: `{
8080
"rules": [{
81-
"action": "reject",
81+
"action": "return",
82+
"rCode": 5,
8283
"domain": "keyword:example"
8384
}]
8485
}`,
@@ -87,7 +88,8 @@ func TestDnsProxyConfig(t *testing.T) {
8788
RewriteServer: &net.Endpoint{},
8889
Rule: []*dns.DNSRuleConfig{
8990
{
90-
Action: dns.RuleAction_Reject,
91+
Action: dns.RuleAction_Return,
92+
RCode: 5,
9193
Domain: []*geodata.DomainRule{
9294
{
9395
Value: &geodata.DomainRule_Custom{
@@ -106,7 +108,7 @@ func TestDnsProxyConfig(t *testing.T) {
106108
Input: `{
107109
"rules": [{
108110
"action": "drop",
109-
"qtype": 257
111+
"qType": 257
110112
}]
111113
}`,
112114
Parser: loadJSON(creator),
@@ -115,7 +117,7 @@ func TestDnsProxyConfig(t *testing.T) {
115117
Rule: []*dns.DNSRuleConfig{
116118
{
117119
Action: dns.RuleAction_Drop,
118-
Qtype: []int32{257},
120+
QType: []int32{257},
119121
},
120122
},
121123
},
@@ -140,10 +142,11 @@ func TestDnsProxyConfigLegacyCompatibility(t *testing.T) {
140142
Rule: []*dns.DNSRuleConfig{
141143
{
142144
Action: dns.RuleAction_Hijack,
143-
Qtype: []int32{1, 28},
145+
QType: []int32{1, 28},
144146
},
145147
{
146-
Action: dns.RuleAction_Reject,
148+
Action: dns.RuleAction_Return,
149+
RCode: 5,
147150
},
148151
},
149152
},
@@ -157,15 +160,17 @@ func TestDnsProxyConfigLegacyCompatibility(t *testing.T) {
157160
RewriteServer: &net.Endpoint{},
158161
Rule: []*dns.DNSRuleConfig{
159162
{
160-
Action: dns.RuleAction_Reject,
161-
Qtype: []int32{1, 65},
163+
Action: dns.RuleAction_Return,
164+
QType: []int32{1, 65},
165+
RCode: 5,
162166
},
163167
{
164168
Action: dns.RuleAction_Hijack,
165-
Qtype: []int32{1, 28},
169+
QType: []int32{1, 28},
166170
},
167171
{
168-
Action: dns.RuleAction_Reject,
172+
Action: dns.RuleAction_Return,
173+
RCode: 5,
169174
},
170175
},
171176
},
@@ -181,11 +186,11 @@ func TestDnsProxyConfigLegacyCompatibility(t *testing.T) {
181186
Rule: []*dns.DNSRuleConfig{
182187
{
183188
Action: dns.RuleAction_Drop,
184-
Qtype: []int32{1},
189+
QType: []int32{1},
185190
},
186191
{
187192
Action: dns.RuleAction_Hijack,
188-
Qtype: []int32{1, 28},
193+
QType: []int32{1, 28},
189194
},
190195
{
191196
Action: dns.RuleAction_Drop,
@@ -204,11 +209,11 @@ func TestDnsProxyConfigLegacyCompatibility(t *testing.T) {
204209
Rule: []*dns.DNSRuleConfig{
205210
{
206211
Action: dns.RuleAction_Drop,
207-
Qtype: []int32{65, 28},
212+
QType: []int32{65, 28},
208213
},
209214
{
210215
Action: dns.RuleAction_Hijack,
211-
Qtype: []int32{1, 28},
216+
QType: []int32{1, 28},
212217
},
213218
{
214219
Action: dns.RuleAction_Direct,
@@ -228,7 +233,7 @@ func TestDnsProxyConfigRejectsMixedLegacyAndNewFields(t *testing.T) {
228233
_, err := loadJSON(creator)(`{
229234
"rules": [{
230235
"action": "direct",
231-
"qtype": 65
236+
"qType": 65
232237
}],
233238
"blockTypes": [65]
234239
}`)

proxy/dns/config.pb.go

Lines changed: 20 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proxy/dns/config.proto

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ import "common/geodata/geodat.proto";
1212
enum RuleAction {
1313
Direct = 0;
1414
Drop = 1;
15-
Reject = 2;
15+
Return = 2;
1616
Hijack = 3;
1717
}
1818

1919
message DNSRuleConfig {
2020
RuleAction action = 1;
21-
repeated int32 qtype = 2;
21+
repeated int32 q_type = 2;
2222
repeated xray.common.geodata.DomainRule domain = 3;
23+
uint32 r_code = 4;
2324
}
2425

2526
message Config {

0 commit comments

Comments
 (0)