2021-08-21 01:20:40 -04:00
|
|
|
//go:build !confonly
|
2021-02-08 05:18:52 -05:00
|
|
|
// +build !confonly
|
|
|
|
|
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-01-02 10:16:23 -05:00
|
|
|
core "github.com/v2fly/v2ray-core/v5"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
|
|
"github.com/v2fly/v2ray-core/v5/features/dns"
|
2021-02-08 05:18:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type FakeDNSServer struct {
|
|
|
|
fakeDNSEngine dns.FakeDNSEngine
|
|
|
|
}
|
|
|
|
|
2022-12-14 21:38:28 -05:00
|
|
|
func NewFakeDNSServer(fakeDNSEngine dns.FakeDNSEngine) *FakeDNSServer {
|
|
|
|
return &FakeDNSServer{fakeDNSEngine: fakeDNSEngine}
|
2021-02-08 05:18:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (FakeDNSServer) Name() string {
|
2022-12-14 21:38:28 -05:00
|
|
|
return "fakedns"
|
2021-02-08 05:18:52 -05:00
|
|
|
}
|
|
|
|
|
2021-04-09 18:19:06 -04:00
|
|
|
func (f *FakeDNSServer) QueryIP(ctx context.Context, domain string, _ net.IP, opt dns.IPOption, _ bool) ([]net.IP, error) {
|
2022-12-14 21:38:28 -05:00
|
|
|
if !opt.FakeEnable {
|
|
|
|
return nil, nil // Returning empty ip record with no error will continue DNS lookup, effectively indicating that this server is disabled.
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
}
|
2021-04-09 18:19:06 -04:00
|
|
|
var ips []net.Address
|
|
|
|
if fkr0, ok := f.fakeDNSEngine.(dns.FakeDNSEngineRev0); ok {
|
|
|
|
ips = fkr0.GetFakeIPForDomain3(domain, opt.IPv4Enable, opt.IPv6Enable)
|
|
|
|
} else {
|
2022-12-14 21:38:28 -05:00
|
|
|
ips = filterIP(f.fakeDNSEngine.GetFakeIPForDomain(domain), opt)
|
2021-04-09 18:19:06 -04:00
|
|
|
}
|
2021-02-08 05:18:52 -05:00
|
|
|
|
|
|
|
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-04-21 20:12:05 -04:00
|
|
|
if len(netIP) > 0 {
|
|
|
|
return netIP, nil
|
|
|
|
}
|
|
|
|
return nil, dns.ErrEmptyResponse
|
2021-02-08 05:18:52 -05:00
|
|
|
}
|
2022-12-14 21:38:28 -05:00
|
|
|
|
|
|
|
func isFakeDNS(server Server) bool {
|
|
|
|
_, ok := server.(*FakeDNSServer)
|
|
|
|
return ok
|
|
|
|
}
|