mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-15 16:56:08 -05:00
7ebf3afc31
Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com>
44 lines
1010 B
Go
44 lines
1010 B
Go
// +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, _ 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()
|
|
}
|
|
}
|
|
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
|
|
}
|