DNS: Support returning upstream TTL to clients (#4526)

Closes https://github.com/XTLS/Xray-core/issues/4527
This commit is contained in:
Meo597 2025-03-24 21:33:56 +08:00 committed by GitHub
parent 2d3210e4b8
commit 4afe2d0cff
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 168 additions and 157 deletions

View file

@ -21,7 +21,7 @@ type Server interface {
// Name of the Client.
Name() string
// QueryIP sends IP queries to its configured server.
QueryIP(ctx context.Context, domain string, clientIP net.IP, option dns.IPOption, disableCache bool) ([]net.IP, error)
QueryIP(ctx context.Context, domain string, clientIP net.IP, option dns.IPOption, disableCache bool) ([]net.IP, uint32, error)
}
// Client is the interface for DNS client.
@ -191,7 +191,7 @@ 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) {
func (c *Client) QueryIP(ctx context.Context, domain string, option dns.IPOption, disableCache bool) ([]net.IP, uint32, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeoutMs)
if len(c.tag) != 0 {
content := session.InboundFromContext(ctx)
@ -200,13 +200,14 @@ func (c *Client) QueryIP(ctx context.Context, domain string, option dns.IPOption
// 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)
ips, ttl, err := c.server.QueryIP(ctx, domain, c.clientIP, option, disableCache)
cancel()
if err != nil {
return ips, err
return ips, ttl, err
}
return c.MatchExpectedIPs(domain, ips)
netips, err := c.MatchExpectedIPs(domain, ips)
return netips, ttl, err
}
// MatchExpectedIPs matches queried domain IPs with expected IPs and returns matched ones.