Files
Xray-core/app/dns/nameserver_fakedns.go
T

52 lines
1.2 KiB
Go
Raw Normal View History

2021-03-06 23:39:50 -05:00
package dns
import (
"context"
2024-06-29 14:32:57 -04:00
"github.com/xtls/xray-core/common/errors"
2021-03-06 23:39:50 -05:00
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/features/dns"
)
type FakeDNSServer struct {
fakeDNSEngine dns.FakeDNSEngine
}
func NewFakeDNSServer(fd dns.FakeDNSEngine) *FakeDNSServer {
return &FakeDNSServer{fakeDNSEngine: fd}
2021-03-06 23:39:50 -05:00
}
func (FakeDNSServer) Name() string {
return "FakeDNS"
}
2025-11-21 13:45:42 +08:00
// IsDisableCache implements Server.
func (s *FakeDNSServer) IsDisableCache() bool {
return true
}
func (f *FakeDNSServer) QueryIP(ctx context.Context, domain string, opt dns.IPOption) ([]net.IP, uint32, error) {
2021-03-06 23:39:50 -05:00
if f.fakeDNSEngine == nil {
return nil, 0, errors.New("Unable to locate a fake DNS Engine").AtError()
2021-03-06 23:39:50 -05:00
}
2021-10-20 01:15:49 -04:00
var ips []net.Address
if fkr0, ok := f.fakeDNSEngine.(dns.FakeDNSEngineRev0); ok {
ips = fkr0.GetFakeIPForDomain3(domain, opt.IPv4Enable, opt.IPv6Enable)
} else {
ips = f.fakeDNSEngine.GetFakeIPForDomain(domain)
}
2021-03-06 23:39:50 -05:00
2021-10-16 21:02:51 +08:00
netIP, err := toNetIP(ips)
if err != nil {
return nil, 0, errors.New("Unable to convert IP to net ip").Base(err).AtError()
2021-03-06 23:39:50 -05:00
}
2024-06-29 14:32:57 -04:00
errors.LogInfo(ctx, f.Name(), " got answer: ", domain, " -> ", ips)
2021-03-06 23:39:50 -05:00
2021-10-20 01:15:49 -04:00
if len(netIP) > 0 {
return netIP, 1, nil // fakeIP ttl is 1
2021-10-20 01:15:49 -04:00
}
return nil, 0, dns.ErrEmptyResponse
2021-03-06 23:39:50 -05:00
}