2021-02-08 05:18:52 -05:00
|
|
|
// +build !confonly
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/dns"
|
2021-02-08 05:18:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type FakeDNSServer struct {
|
|
|
|
fakeDNSEngine dns.FakeDNSEngine
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFakeDNSServer() *FakeDNSServer {
|
|
|
|
return &FakeDNSServer{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (FakeDNSServer) Name() string {
|
|
|
|
return "FakeDNS"
|
|
|
|
}
|
|
|
|
|
2021-02-22 21:17:20 -05:00
|
|
|
func (f *FakeDNSServer) QueryIP(ctx context.Context, domain string, _ net.IP, _ dns.IPOption) ([]net.IP, error) {
|
2021-02-08 05:18:52 -05: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)
|
|
|
|
|
|
|
|
netIP, err := toNetIP(ips)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("Unable to convert IP to net ip").Base(err).AtError()
|
|
|
|
}
|
|
|
|
|
2021-02-22 21:17:20 -05:00
|
|
|
newError(f.Name(), " got answer: ", domain, " -> ", ips).AtInfo().WriteToLog()
|
|
|
|
|
2021-02-08 05:18:52 -05:00
|
|
|
return netIP, nil
|
|
|
|
}
|