2021-03-07 04:39:50 +00:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
2021-03-14 15:05:08 +00:00
|
|
|
"github.com/xtls/xray-core/core"
|
2021-03-07 04:39:50 +00:00
|
|
|
"github.com/xtls/xray-core/features/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FakeDNSServer struct {
|
|
|
|
fakeDNSEngine dns.FakeDNSEngine
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFakeDNSServer() *FakeDNSServer {
|
|
|
|
return &FakeDNSServer{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (FakeDNSServer) Name() string {
|
|
|
|
return "FakeDNS"
|
|
|
|
}
|
|
|
|
|
2021-10-16 13:02:51 +00:00
|
|
|
func (f *FakeDNSServer) QueryIP(ctx context.Context, domain string, _ net.IP, _ dns.IPOption, _ bool) ([]net.IP, error) {
|
2021-03-07 04:39:50 +00:00
|
|
|
if f.fakeDNSEngine == nil {
|
|
|
|
if err := core.RequireFeatures(ctx, func(fd dns.FakeDNSEngine) {
|
|
|
|
f.fakeDNSEngine = fd
|
|
|
|
}); err != nil {
|
|
|
|
return nil, newError("Unable to locate a fake DNS Engine").Base(err).AtError()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ips := f.fakeDNSEngine.GetFakeIPForDomain(domain)
|
|
|
|
|
2021-10-16 13:02:51 +00:00
|
|
|
netIP, err := toNetIP(ips)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("Unable to convert IP to net ip").Base(err).AtError()
|
2021-03-07 04:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
newError(f.Name(), " got answer: ", domain, " -> ", ips).AtInfo().WriteToLog()
|
|
|
|
|
|
|
|
return netIP, nil
|
|
|
|
}
|