Skip to content

Commit 96381e8

Browse files
authored
DNS: Add tag for DnsServerObject (#4515)
1 parent 6a211a0 commit 96381e8

File tree

4 files changed

+81
-57
lines changed

4 files changed

+81
-57
lines changed

app/dns/config.pb.go

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

app/dns/config.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ message NameServer {
2929
repeated OriginalRule original_rules = 4;
3030
QueryStrategy query_strategy = 7;
3131
bool allowUnexpectedIPs = 8;
32+
string tag = 9;
3233
}
3334

3435
enum DomainMatchingType {

app/dns/nameserver.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/xtls/xray-core/app/router"
1010
"github.com/xtls/xray-core/common/errors"
1111
"github.com/xtls/xray-core/common/net"
12+
"github.com/xtls/xray-core/common/session"
1213
"github.com/xtls/xray-core/common/strmatcher"
1314
"github.com/xtls/xray-core/core"
1415
"github.com/xtls/xray-core/features/dns"
@@ -31,6 +32,7 @@ type Client struct {
3132
domains []string
3233
expectIPs []*router.GeoIPMatcher
3334
allowUnexpectedIPs bool
35+
tag string
3436
}
3537

3638
var errExpectedIPNonMatch = errors.New("expectIPs not match")
@@ -168,6 +170,7 @@ func NewClient(
168170
client.domains = rules
169171
client.expectIPs = matchers
170172
client.allowUnexpectedIPs = ns.AllowUnexpectedIPs
173+
client.tag = ns.Tag
171174
return nil
172175
})
173176
return client, err
@@ -181,6 +184,13 @@ func (c *Client) Name() string {
181184
// QueryIP sends DNS query to the name server with the client's IP.
182185
func (c *Client) QueryIP(ctx context.Context, domain string, option dns.IPOption, disableCache bool) ([]net.IP, error) {
183186
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
187+
if len(c.tag) != 0 {
188+
content := session.InboundFromContext(ctx)
189+
errors.LogDebug(ctx, "DNS: client override tag from ", content.Tag, " to ", c.tag)
190+
// create a new context to override the tag
191+
// do not direct set *content.Tag, it might be used by other clients
192+
ctx = session.ContextWithInbound(ctx, &session.Inbound{Tag: c.tag})
193+
}
184194
ips, err := c.server.QueryIP(ctx, domain, c.clientIP, option, disableCache)
185195
cancel()
186196

infra/conf/dns.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type NameServerConfig struct {
2020
ExpectIPs StringList `json:"expectIps"`
2121
QueryStrategy string `json:"queryStrategy"`
2222
AllowUnexpectedIPs bool `json:"allowUnexpectedIps"`
23+
Tag string `json:"tag"`
2324
}
2425

2526
func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
@@ -38,6 +39,7 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
3839
ExpectIPs StringList `json:"expectIps"`
3940
QueryStrategy string `json:"queryStrategy"`
4041
AllowUnexpectedIPs bool `json:"allowUnexpectedIps"`
42+
Tag string `json:"tag"`
4143
}
4244
if err := json.Unmarshal(data, &advanced); err == nil {
4345
c.Address = advanced.Address
@@ -48,6 +50,7 @@ func (c *NameServerConfig) UnmarshalJSON(data []byte) error {
4850
c.ExpectIPs = advanced.ExpectIPs
4951
c.QueryStrategy = advanced.QueryStrategy
5052
c.AllowUnexpectedIPs = advanced.AllowUnexpectedIPs
53+
c.Tag = advanced.Tag
5154
return nil
5255
}
5356

@@ -121,6 +124,7 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) {
121124
OriginalRules: originalRules,
122125
QueryStrategy: resolveQueryStrategy(c.QueryStrategy),
123126
AllowUnexpectedIPs: c.AllowUnexpectedIPs,
127+
Tag: c.Tag,
124128
}, nil
125129
}
126130

0 commit comments

Comments
 (0)