DNS: Add tag for DnsServerObject (#4515)

Closes https://github.com/XTLS/Xray-core/issues/4505
This commit is contained in:
风扇滑翔翼 2025-03-21 08:18:59 +08:00 committed by RPRX
parent 6a211a0bb9
commit 6f8e253dec
4 changed files with 81 additions and 57 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/xtls/xray-core/app/router"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/common/strmatcher"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/dns"
@ -31,6 +32,7 @@ type Client struct {
domains []string
expectIPs []*router.GeoIPMatcher
allowUnexpectedIPs bool
tag string
}
var errExpectedIPNonMatch = errors.New("expectIPs not match")
@ -168,6 +170,7 @@ func NewClient(
client.domains = rules
client.expectIPs = matchers
client.allowUnexpectedIPs = ns.AllowUnexpectedIPs
client.tag = ns.Tag
return nil
})
return client, err
@ -181,6 +184,13 @@ func (c *Client) Name() string {
// QueryIP sends DNS query to the name server with the client's IP.
func (c *Client) QueryIP(ctx context.Context, domain string, option dns.IPOption, disableCache bool) ([]net.IP, error) {
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
if len(c.tag) != 0 {
content := session.InboundFromContext(ctx)
errors.LogDebug(ctx, "DNS: client override tag from ", content.Tag, " to ", c.tag)
// create a new context to override the tag
// do not direct set *content.Tag, it might be used by other clients
ctx = session.ContextWithInbound(ctx, &session.Inbound{Tag: c.tag})
}
ips, err := c.server.QueryIP(ctx, domain, c.clientIP, option, disableCache)
cancel()