@@ -20,58 +20,11 @@ impl Middleware<DnsContext, DnsRequest, DnsResponse, DnsError> for AddressMiddle
20
20
& self ,
21
21
ctx : & mut DnsContext ,
22
22
req : & DnsRequest ,
23
- next : crate :: middleware :: Next < ' _ , DnsContext , DnsRequest , DnsResponse , DnsError > ,
23
+ next : Next < ' _ , DnsContext , DnsRequest , DnsResponse , DnsError > ,
24
24
) -> Result < DnsResponse , DnsError > {
25
25
let query_type = req. query ( ) . query_type ( ) ;
26
26
27
- let mut rdata = None ;
28
-
29
- let no_rule_soa = ctx. server_opts . no_rule_soa ( ) ;
30
-
31
- if !no_rule_soa
32
- && ( ctx. cfg ( ) . force_aaaa_soa ( ) && query_type == RecordType :: AAAA
33
- || ctx. cfg ( ) . force_qtype_soa ( ) . contains ( & query_type) )
34
- {
35
- // force SOA
36
- rdata = Some ( RData :: default_soa ( ) ) ;
37
- } else if matches ! ( query_type, RecordType :: AAAA | RecordType :: A if !ctx. server_opts. no_rule_addr( ) )
38
- {
39
- let mut node = ctx. domain_rule . as_ref ( ) ;
40
-
41
- while let Some ( rule) = node {
42
- use crate :: dns_conf:: DomainAddress :: * ;
43
-
44
- if let Some ( address) = rule. address {
45
- rdata = match address {
46
- IPv4 ( ipv4) => Some ( RData :: A ( ipv4) ) ,
47
- IPv6 ( ipv6) => Some ( RData :: AAAA ( ipv6) ) ,
48
- SOA if !no_rule_soa => Some ( RData :: default_soa ( ) ) ,
49
- SOAv4 if !no_rule_soa && query_type == RecordType :: A => {
50
- Some ( RData :: default_soa ( ) )
51
- }
52
- SOAv6 if !no_rule_soa && query_type == RecordType :: AAAA => {
53
- Some ( RData :: default_soa ( ) )
54
- }
55
- IGN => {
56
- node = rule. zone ( ) ;
57
- continue ;
58
- }
59
- IGNv4 if query_type == RecordType :: A => {
60
- node = rule. zone ( ) ;
61
- continue ;
62
- }
63
- IGNv6 if query_type == RecordType :: AAAA => continue ,
64
- _ => None , // skip rules
65
- } ;
66
- break ;
67
- } else {
68
- node = rule. zone ( ) ;
69
- continue ;
70
- }
71
- }
72
- }
73
-
74
- if let Some ( rdata) = rdata {
27
+ if let Some ( rdata) = handle_rule_addr ( query_type, ctx) {
75
28
let local_ttl = ctx. cfg ( ) . local_ttl ( ) ;
76
29
77
30
let query = req. query ( ) . original ( ) . clone ( ) ;
@@ -111,3 +64,54 @@ impl Middleware<DnsContext, DnsRequest, DnsResponse, DnsError> for AddressMiddle
111
64
}
112
65
}
113
66
}
67
+
68
+ fn handle_rule_addr ( query_type : RecordType , ctx : & DnsContext ) -> Option < RData > {
69
+ use RecordType :: { A , AAAA } ;
70
+
71
+ let cfg = ctx. cfg ( ) ;
72
+ let server_opts = ctx. server_opts ( ) ;
73
+ let rule = ctx. domain_rule . as_ref ( ) ;
74
+
75
+ let no_rule_soa = server_opts. no_rule_soa ( ) ;
76
+
77
+ if !no_rule_soa {
78
+ // force AAAA query return SOA
79
+ if query_type == AAAA && ( server_opts. force_aaaa_soa ( ) || cfg. force_aaaa_soa ( ) ) {
80
+ return Some ( RData :: default_soa ( ) ) ;
81
+ }
82
+
83
+ // force AAAA query return SOA
84
+ if cfg. force_qtype_soa ( ) . contains ( & query_type) {
85
+ return Some ( RData :: default_soa ( ) ) ;
86
+ }
87
+ }
88
+
89
+ // skip address rule.
90
+ if server_opts. no_rule_addr ( ) || !query_type. is_ip_addr ( ) {
91
+ return None ;
92
+ }
93
+
94
+ let mut node = rule;
95
+
96
+ while let Some ( rule) = node {
97
+ use crate :: dns_conf:: DomainAddress :: * ;
98
+
99
+ if let Some ( address) = rule. address {
100
+ match address {
101
+ IPv4 ( ipv4) if query_type == A => return Some ( RData :: A ( ipv4) ) ,
102
+ IPv6 ( ipv6) if query_type == AAAA => return Some ( RData :: AAAA ( ipv6) ) ,
103
+ SOA if !no_rule_soa => return Some ( RData :: default_soa ( ) ) ,
104
+ SOAv4 if !no_rule_soa && query_type == A => return Some ( RData :: default_soa ( ) ) ,
105
+ SOAv6 if !no_rule_soa && query_type == AAAA => return Some ( RData :: default_soa ( ) ) ,
106
+ IGN => return None , // ignore rule
107
+ IGNv4 if query_type == A => return None ,
108
+ IGNv6 if query_type == AAAA => return None ,
109
+ _ => ( ) ,
110
+ } ;
111
+ }
112
+
113
+ node = rule. zone ( ) ; // find parent rule
114
+ }
115
+
116
+ None
117
+ }
0 commit comments