mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-12-18 05:04:36 +03:00
Some checks failed
Build and Release / build (mips64, linux) (push) Has been cancelled
Build and Release / build (mips64le, linux) (push) Has been cancelled
Build and Release / build (mipsle, linux) (push) Has been cancelled
Build and Release / build (ppc64, linux) (push) Has been cancelled
Build and Release / build (ppc64le, linux) (push) Has been cancelled
Build and Release / build (riscv64, linux) (push) Has been cancelled
Build and Release / build (s390x, linux) (push) Has been cancelled
Build docker image / build-image (push) Has been cancelled
Build and Release for Windows 7 / build (win7-32, 386, windows) (push) Has been cancelled
Build and Release for Windows 7 / build (win7-64, amd64, windows) (push) Has been cancelled
Build and Release / build (386, freebsd, ) (push) Has been cancelled
Build and Release / build (386, linux, ) (push) Has been cancelled
Build and Release / build (386, openbsd, ) (push) Has been cancelled
Build and Release / build (386, windows, ) (push) Has been cancelled
Build and Release / build (amd64, darwin, ) (push) Has been cancelled
Build and Release / build (amd64, freebsd, ) (push) Has been cancelled
Build and Release / build (amd64, linux, ) (push) Has been cancelled
Build and Release / build (amd64, openbsd, ) (push) Has been cancelled
Build and Release / build (amd64, windows, ) (push) Has been cancelled
Build and Release / build (arm, 5, linux) (push) Has been cancelled
Build and Release / build (arm, 6, linux) (push) Has been cancelled
Build and Release / build (arm, 7, freebsd) (push) Has been cancelled
Build and Release / build (arm, 7, linux) (push) Has been cancelled
Build and Release / build (arm, 7, openbsd) (push) Has been cancelled
Build and Release / build (arm, 7, windows) (push) Has been cancelled
Build and Release / build (arm64, android) (push) Has been cancelled
Build and Release / build (arm64, darwin) (push) Has been cancelled
Build and Release / build (arm64, freebsd) (push) Has been cancelled
Build and Release / build (arm64, linux) (push) Has been cancelled
Build and Release / build (arm64, openbsd) (push) Has been cancelled
Build and Release / build (arm64, windows) (push) Has been cancelled
Build and Release / build (loong64, linux) (push) Has been cancelled
Build and Release / build (mips, linux) (push) Has been cancelled
Test / test (windows-latest) (push) Has been cancelled
Test / test (macos-latest) (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
Closes https://github.com/XTLS/Xray-core/issues/4527
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package localdns
|
|
|
|
import (
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/features/dns"
|
|
)
|
|
|
|
// Client is an implementation of dns.Client, which queries localhost for DNS.
|
|
type Client struct{}
|
|
|
|
// Type implements common.HasType.
|
|
func (*Client) Type() interface{} {
|
|
return dns.ClientType()
|
|
}
|
|
|
|
// Start implements common.Runnable.
|
|
func (*Client) Start() error { return nil }
|
|
|
|
// Close implements common.Closable.
|
|
func (*Client) Close() error { return nil }
|
|
|
|
// LookupIP implements Client.
|
|
func (*Client) LookupIP(host string, option dns.IPOption) ([]net.IP, uint32, error) {
|
|
ips, err := net.LookupIP(host)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
parsedIPs := make([]net.IP, 0, len(ips))
|
|
ipv4 := make([]net.IP, 0, len(ips))
|
|
ipv6 := make([]net.IP, 0, len(ips))
|
|
for _, ip := range ips {
|
|
parsed := net.IPAddress(ip)
|
|
if parsed != nil {
|
|
parsedIPs = append(parsedIPs, parsed.IP())
|
|
}
|
|
if len(ip) == net.IPv4len {
|
|
ipv4 = append(ipv4, ip)
|
|
}
|
|
if len(ip) == net.IPv6len {
|
|
ipv6 = append(ipv6, ip)
|
|
}
|
|
}
|
|
// Local DNS ttl is 600
|
|
switch {
|
|
case option.IPv4Enable && option.IPv6Enable:
|
|
if len(parsedIPs) > 0 {
|
|
return parsedIPs, 600, nil
|
|
}
|
|
case option.IPv4Enable:
|
|
if len(ipv4) > 0 {
|
|
return ipv4, 600, nil
|
|
}
|
|
case option.IPv6Enable:
|
|
if len(ipv6) > 0 {
|
|
return ipv6, 600, nil
|
|
}
|
|
}
|
|
return nil, 0, dns.ErrEmptyResponse
|
|
}
|
|
|
|
// New create a new dns.Client that queries localhost for DNS.
|
|
func New() *Client {
|
|
return &Client{}
|
|
}
|