Skip to content

Commit 6a211a0

Browse files
authored
DNS: Add allowUnexpectedIPs for DnsServerObject (#4497)
Closes #4424
1 parent 335845a commit 6a211a0

File tree

4 files changed

+55
-34
lines changed

4 files changed

+55
-34
lines changed

app/dns/config.pb.go

+20-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/dns/config.proto

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ message NameServer {
2828
repeated xray.app.router.GeoIP geoip = 3;
2929
repeated OriginalRule original_rules = 4;
3030
QueryStrategy query_strategy = 7;
31+
bool allowUnexpectedIPs = 8;
3132
}
3233

3334
enum DomainMatchingType {

app/dns/nameserver.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ type Server interface {
2525

2626
// Client is the interface for DNS client.
2727
type Client struct {
28-
server Server
29-
clientIP net.IP
30-
skipFallback bool
31-
domains []string
32-
expectIPs []*router.GeoIPMatcher
28+
server Server
29+
clientIP net.IP
30+
skipFallback bool
31+
domains []string
32+
expectIPs []*router.GeoIPMatcher
33+
allowUnexpectedIPs bool
3334
}
3435

3536
var errExpectedIPNonMatch = errors.New("expectIPs not match")
@@ -166,6 +167,7 @@ func NewClient(
166167
client.skipFallback = ns.SkipFallback
167168
client.domains = rules
168169
client.expectIPs = matchers
170+
client.allowUnexpectedIPs = ns.AllowUnexpectedIPs
169171
return nil
170172
})
171173
return client, err
@@ -203,6 +205,9 @@ func (c *Client) MatchExpectedIPs(domain string, ips []net.IP) ([]net.IP, error)
203205
}
204206
}
205207
if len(newIps) == 0 {
208+
if c.allowUnexpectedIPs {
209+
return ips, nil
210+
}
206211
return nil, errExpectedIPNonMatch
207212
}
208213
errors.LogDebug(context.Background(), "domain ", domain, " expectIPs ", newIps, " matched at server ", c.Name())

infra/conf/dns.go

+24-20
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import (
1212
)
1313

1414
type NameServerConfig struct {
15-
Address *Address `json:"address"`
16-
ClientIP *Address `json:"clientIp"`
17-
Port uint16 `json:"port"`
18-
SkipFallback bool `json:"skipFallback"`
19-
Domains []string `json:"domains"`
20-
ExpectIPs StringList `json:"expectIps"`
21-
QueryStrategy string `json:"queryStrategy"`
15+
Address *Address `json:"address"`
16+
ClientIP *Address `json:"clientIp"`
17+
Port uint16 `json:"port"`
18+
SkipFallback bool `json:"skipFallback"`
19+
Domains []string `json:"domains"`
20+
ExpectIPs StringList `json:"expectIps"`
21+
QueryStrategy string `json:"queryStrategy"`
22+
AllowUnexpectedIPs bool `json:"allowUnexpectedIps"`
2223
}
2324

2425
func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
@@ -29,13 +30,14 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
2930
}
3031

3132
var advanced struct {
32-
Address *Address `json:"address"`
33-
ClientIP *Address `json:"clientIp"`
34-
Port uint16 `json:"port"`
35-
SkipFallback bool `json:"skipFallback"`
36-
Domains []string `json:"domains"`
37-
ExpectIPs StringList `json:"expectIps"`
38-
QueryStrategy string `json:"queryStrategy"`
33+
Address *Address `json:"address"`
34+
ClientIP *Address `json:"clientIp"`
35+
Port uint16 `json:"port"`
36+
SkipFallback bool `json:"skipFallback"`
37+
Domains []string `json:"domains"`
38+
ExpectIPs StringList `json:"expectIps"`
39+
QueryStrategy string `json:"queryStrategy"`
40+
AllowUnexpectedIPs bool `json:"allowUnexpectedIps"`
3941
}
4042
if err := json.Unmarshal(data, &advanced); err == nil {
4143
c.Address = advanced.Address
@@ -45,6 +47,7 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
4547
c.Domains = advanced.Domains
4648
c.ExpectIPs = advanced.ExpectIPs
4749
c.QueryStrategy = advanced.QueryStrategy
50+
c.AllowUnexpectedIPs = advanced.AllowUnexpectedIPs
4851
return nil
4952
}
5053

@@ -111,12 +114,13 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) {
111114
Address: c.Address.Build(),
112115
Port: uint32(c.Port),
113116
},
114-
ClientIp: myClientIP,
115-
SkipFallback: c.SkipFallback,
116-
PrioritizedDomain: domains,
117-
Geoip: geoipList,
118-
OriginalRules: originalRules,
119-
QueryStrategy: resolveQueryStrategy(c.QueryStrategy),
117+
ClientIp: myClientIP,
118+
SkipFallback: c.SkipFallback,
119+
PrioritizedDomain: domains,
120+
Geoip: geoipList,
121+
OriginalRules: originalRules,
122+
QueryStrategy: resolveQueryStrategy(c.QueryStrategy),
123+
AllowUnexpectedIPs: c.AllowUnexpectedIPs,
120124
}, nil
121125
}
122126

0 commit comments

Comments
 (0)