2021-03-07 04:39:50 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-06-29 18:32:57 +00:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2021-03-07 04:39:50 +00:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/features/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FakeDNSServer struct {
|
|
|
|
fakeDNSEngine dns.FakeDNSEngine
|
|
|
|
}
|
|
|
|
|
2024-12-11 01:07:52 +00:00
|
|
|
func NewFakeDNSServer(fd dns.FakeDNSEngine) *FakeDNSServer {
|
|
|
|
return &FakeDNSServer{fakeDNSEngine: fd}
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (FakeDNSServer) Name() string {
|
|
|
|
return "FakeDNS"
|
|
|
|
}
|
|
|
|
|
2021-10-20 05:15:49 +00:00
|
|
|
func (f *FakeDNSServer) QueryIP(ctx context.Context, domain string, _ net.IP, opt dns.IPOption, _ bool) ([]net.IP, error) {
|
2021-03-07 04:39:50 +00:00
|
|
|
if f.fakeDNSEngine == nil {
|
2024-12-11 01:07:52 +00:00
|
|
|
return nil, errors.New("Unable to locate a fake DNS Engine").AtError()
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
2024-12-11 01:07:52 +00:00
|
|
|
|
2021-10-20 05:15:49 +00: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-07 04:39:50 +00:00
|
|
|
|
2021-10-16 13:02:51 +00:00
|
|
|
netIP, err := toNetIP(ips)
|
|
|
|
if err != nil {
|
2024-06-29 18:32:57 +00:00
|
|
|
return nil, errors.New("Unable to convert IP to net ip").Base(err).AtError()
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
|
2024-06-29 18:32:57 +00:00
|
|
|
errors.LogInfo(ctx, f.Name(), " got answer: ", domain, " -> ", ips)
|
2021-03-07 04:39:50 +00:00
|
|
|
|
2021-10-20 05:15:49 +00:00
|
|
|
if len(netIP) > 0 {
|
|
|
|
return netIP, nil
|
|
|
|
}
|
|
|
|
return nil, dns.ErrEmptyResponse
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|