转发解析主函数为Resolve,调用replyFromCache进行edns处理。
diff -urN -x .git dnsproxy/main.go dnsproxy-mod/main.go
--- dnsproxy/main.go 2019-12-12 15:41:28.962196992 +0800
+++ dnsproxy-mod/main.go 2019-12-12 15:46:45.248460512 +0800
@@ -72,6 +72,8 @@
// Use EDNS Client Subnet extension
EnableEDNSSubnet bool `long:"edns" description:"Use EDNS Client Subnet extension" optional:"yes" optional-value:"true"`
+ EDNSAddr string `long:"ednsaddr" description:"Send EDNS Client Address"`
+
// Print DNSProxy version (just for the help)
Version bool `long:"version" description:"Prints the program version"`
}
@@ -146,6 +148,7 @@
// createProxyConfig creates proxy.Config from the command line arguments
func createProxyConfig(options Options) proxy.Config {
listenIP := net.ParseIP(options.ListenAddr)
+ ednsIP := net.ParseIP(options.EDNSAddr)
if listenIP == nil {
log.Fatalf("cannot parse %s", options.ListenAddr)
}
@@ -166,6 +169,7 @@
RefuseAny: options.RefuseAny,
AllServers: options.AllServers,
EnableEDNSClientSubnet: options.EnableEDNSSubnet,
+ EDNSAddr: ednsIP,
}
if options.Fallbacks != nil {
diff -urN -x .git dnsproxy/proxy/proxy_cache.go dnsproxy-mod/proxy/proxy_cache.go
--- dnsproxy/proxy/proxy_cache.go 2019-12-12 15:41:28.970197100 +0800
+++ dnsproxy-mod/proxy/proxy_cache.go 2019-12-12 15:40:14.297190512 +0800
@@ -10,11 +10,11 @@
// Get response from general or subnet cache
// Return TRUE if response is found in cache
func (p *Proxy) replyFromCache(d *DNSContext) bool {
- if p.cache == nil {
- return false
- }
if !p.Config.EnableEDNSClientSubnet {
+ if p.cache == nil {
+ return false
+ }
val, ok := p.cache.Get(d.Req)
if ok && val != nil {
d.Res = val
@@ -31,11 +31,15 @@
if mask == 0 {
// Set EDNS Client-Subnet data
var clientIP net.IP
- switch addr := d.Addr.(type) {
- case *net.UDPAddr:
- clientIP = addr.IP
- case *net.TCPAddr:
- clientIP = addr.IP
+ if p.Config.EDNSAddr != nil {
+ clientIP = p.Config.EDNSAddr
+ } else {
+ switch addr := d.Addr.(type) {
+ case *net.UDPAddr:
+ clientIP = addr.IP
+ case *net.TCPAddr:
+ clientIP = addr.IP
+ }
}
if clientIP != nil && isPublicIP(clientIP) {
diff -urN -x .git dnsproxy/proxy/proxy.go dnsproxy-mod/proxy/proxy.go
--- dnsproxy/proxy/proxy.go 2019-12-12 15:41:28.970197100 +0800
+++ dnsproxy-mod/proxy/proxy.go 2019-12-12 14:09:50.799051551 +0800
@@ -120,6 +120,7 @@
// We store these responses in general cache (without subnet)
// so they will never be used for clients with public IP addresses.
EnableEDNSClientSubnet bool
+ EDNSAddr net.IP // ECS IP used in request
CacheEnabled bool // cache status
CacheSizeBytes int // Cache size (in bytes). Default: 64k
注意:未开启edns时,客户端使用edns也可以转发,但是如果同个域名有缓存带不带edns返回的结果都相同
https://github.com/AdguardTeam/dnsproxy
标签:none