mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-26 17:03:02 +00:00
44 lines
1016 B
Go
44 lines
1016 B
Go
package dns
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
"github.com/xtls/xray-core/core"
|
|
"github.com/xtls/xray-core/features/dns"
|
|
)
|
|
|
|
type FakeDNSServer struct {
|
|
fakeDNSEngine dns.FakeDNSEngine
|
|
}
|
|
|
|
func NewFakeDNSServer() *FakeDNSServer {
|
|
return &FakeDNSServer{}
|
|
}
|
|
|
|
const FakeDNSName = "FakeDNS"
|
|
|
|
func (FakeDNSServer) Name() string {
|
|
return FakeDNSName
|
|
}
|
|
|
|
func (f *FakeDNSServer) QueryIP(ctx context.Context, domain string, _ net.IP, _ dns.IPOption, _ CacheStrategy) ([]net.IP, error) {
|
|
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)
|
|
|
|
netIP, err := toNetIP(ips)
|
|
if err != nil {
|
|
return nil, newError("Unable to convert IP to net ip").Base(err).AtError()
|
|
}
|
|
|
|
newError(f.Name(), " got answer: ", domain, " -> ", ips).AtInfo().WriteToLog()
|
|
|
|
return netIP, nil
|
|
}
|