mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-17 18:06:15 -05:00
f8ac919d66
* 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
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package dns
|
|
|
|
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
|
|
|
|
import (
|
|
"github.com/v2fly/v2ray-core/v5/common/net"
|
|
"github.com/v2fly/v2ray-core/v5/features/dns"
|
|
"github.com/v2fly/v2ray-core/v5/features/routing"
|
|
)
|
|
|
|
// ResolvableContext is an implementation of routing.Context, with domain resolving capability.
|
|
type ResolvableContext struct {
|
|
routing.Context
|
|
dnsClient dns.Client
|
|
resolvedIPs []net.IP
|
|
}
|
|
|
|
// GetTargetIPs overrides original routing.Context's implementation.
|
|
func (ctx *ResolvableContext) GetTargetIPs() []net.IP {
|
|
if ips := ctx.Context.GetTargetIPs(); len(ips) != 0 {
|
|
return ips
|
|
}
|
|
|
|
if len(ctx.resolvedIPs) > 0 {
|
|
return ctx.resolvedIPs
|
|
}
|
|
|
|
if domain := ctx.GetTargetDomain(); len(domain) != 0 {
|
|
ips, err := ctx.dnsClient.LookupIP(domain)
|
|
if err == nil {
|
|
ctx.resolvedIPs = ips
|
|
return ips
|
|
}
|
|
newError("resolve ip for ", domain).Base(err).WriteToLog()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ContextWithDNSClient creates a new routing context with domain resolving capability.
|
|
// Resolved domain IPs can be retrieved by GetTargetIPs().
|
|
func ContextWithDNSClient(ctx routing.Context, client dns.Client) routing.Context {
|
|
return &ResolvableContext{Context: ctx, dnsClient: client}
|
|
}
|