1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-14 00:07:09 -05:00
v2fly/app/dns/nameserver_fakedns.go
Vigilans f8ac919d66
[app/dns] Support per-client configuration for fakedns (#2212)
* Move `filterIP` from `hosts.go` to `dnscommon.go`

* Implement adding pools for fakedns.HolderMulti

* Implement per-client fakedns for DNS app

* Remove `dns.ClientWithIPOption` and replace with new programming model

* Implement JSON config support for new fakedns config

* Fix lint and tests

* Fix some codacy analysis
2022-12-15 10:38:28 +08:00

61 lines
1.5 KiB
Go

//go:build !confonly
// +build !confonly
package dns
import (
"context"
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/features/dns"
)
type FakeDNSServer struct {
fakeDNSEngine dns.FakeDNSEngine
}
func NewFakeDNSServer(fakeDNSEngine dns.FakeDNSEngine) *FakeDNSServer {
return &FakeDNSServer{fakeDNSEngine: fakeDNSEngine}
}
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 !opt.FakeEnable {
return nil, nil // Returning empty ip record with no error will continue DNS lookup, effectively indicating that this server is disabled.
}
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 = filterIP(f.fakeDNSEngine.GetFakeIPForDomain(domain), opt)
}
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
}
func isFakeDNS(server Server) bool {
_, ok := server.(*FakeDNSServer)
return ok
}