mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-16 09:26:21 -05:00
c78ee5aac7
* ⬆ Update to Go 1.17 * 🏗 Update workflows and add windows-arm64 * 💾 Update generated files * 📛 Update not-so-friendly filenames
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
//go:build !confonly
|
|
// +build !confonly
|
|
|
|
package dns
|
|
|
|
import (
|
|
"context"
|
|
|
|
core "github.com/v2fly/v2ray-core/v4"
|
|
"github.com/v2fly/v2ray-core/v4/common/net"
|
|
"github.com/v2fly/v2ray-core/v4/features/dns"
|
|
)
|
|
|
|
type FakeDNSServer struct {
|
|
fakeDNSEngine dns.FakeDNSEngine
|
|
}
|
|
|
|
func NewFakeDNSServer() *FakeDNSServer {
|
|
return &FakeDNSServer{}
|
|
}
|
|
|
|
func (FakeDNSServer) Name() string {
|
|
return "FakeDNS"
|
|
}
|
|
|
|
func (f *FakeDNSServer) QueryIP(ctx context.Context, domain string, _ net.IP, opt dns.IPOption, _ bool) ([]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()
|
|
}
|
|
}
|
|
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)
|
|
}
|
|
|
|
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()
|
|
|
|
if len(netIP) > 0 {
|
|
return netIP, nil
|
|
}
|
|
return nil, dns.ErrEmptyResponse
|
|
}
|