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

@ -20,10 +20,10 @@ func (*Client) Start() error { return nil }
func (*Client) Close() error { return nil }
// LookupIP implements Client.
func (*Client) LookupIP(host string, option dns.IPOption) ([]net.IP, error) {
func (*Client) LookupIP(host string, option dns.IPOption) ([]net.IP, uint32, error) {
ips, err := net.LookupIP(host)
if err != nil {
return nil, err
return nil, 0, err
}
parsedIPs := make([]net.IP, 0, len(ips))
ipv4 := make([]net.IP, 0, len(ips))
@ -40,21 +40,22 @@ func (*Client) LookupIP(host string, option dns.IPOption) ([]net.IP, error) {
ipv6 = append(ipv6, ip)
}
}
// Local DNS ttl is 600
switch {
case option.IPv4Enable && option.IPv6Enable:
if len(parsedIPs) > 0 {
return parsedIPs, nil
return parsedIPs, 600, nil
}
case option.IPv4Enable:
if len(ipv4) > 0 {
return ipv4, nil
return ipv4, 600, nil
}
case option.IPv6Enable:
if len(ipv6) > 0 {
return ipv6, nil
return ipv6, 600, nil
}
}
return nil, dns.ErrEmptyResponse
return nil, 0, dns.ErrEmptyResponse
}
// New create a new dns.Client that queries localhost for DNS.