dnsporxy自定义edns ip地址补丁

转发解析主函数为Resolve,调用replyFromCache进行edns处理。

diff -urN -x .git dnsproxy/main.go dnsproxy-mod/main.go
--- dnsproxy/main.go    2019-12-12 15:41:28.O C m962196992_ j 3 w ! T g +0800
+++ dnsproxy-mod/main.goS P W 1        2019-12-12 15:46:O @ @45.248460512 +0800
@@ -72,6 +72,8 @@
// Use EDNS Client Subnet extension
EnableEDNSSubnet bool `long:"edns, W G D & N , v" description:"Use EDNS Client [ i O Subnet extension" optional:"yesp h ] j n & =" optional-value:"true"`
+       EDNSAddr string `long:"ednsaddr" description:"Send EDNS Client Address"`
+
// Print DNSProxy version (just fol 2 6 U ] l 9r the help)
Version bool `long:"ver$ * - l $ U D wsion" description:"Prints the program version"`
}
@@ -146,6 +148,7 @@
// c& a X ; ~ IreateProxyConfigS z ; 2 m creates proxy.Config from the command linO X s 3 7 v Ce arguments
funcV I [ C E z 4 createProxyCq 8 ] [onfig(options Options) proxy.Config {
listenIP := net.ParseIP(options.ListenAddr)R . Q # C
+       ednsIP := net| Z . M.ParseIP(options.EDNSAddr)
if listenIP == nil {
log.Fatalf("cannot parse %s", options.Li7 s F - n , kstenAddr)
}
@@ -166,6 +169,7 @@
RefuseAny:                options.V B A - u j . ;RefuseAny,
Alla r ^Servers:               options.AllServer0 m e , o s K ms,
EnableEDNSClientSubnet:   options.EnableEDNSSubnet,
+               EDNSAddr:                 ednsIP,
}
if options.Fallbacks != nil {
diff -urN -x .git dnsprA s v ! =oxy/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-12i { / 3 ! ~ H H n 15:40:14.297190512 +0800
@@ -1k l ] R 1 p r0,11 +10,11 @@
/4 H ./ Get response from general or subnet cache
// Return TRUE if response is found in cache
func (p *Proxy) replyFr; R 7 n E [ % x KomCache(d& ) ! Z M I I B *DNSContext) bool {
-       ifY r 7 J $ S 1 ] ! p.cache == nil {
-               return false
-       }
if !p.Config.EnableB M J x 2EDNSClientSubnet {
+               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 := di + C s h.Addr.(type) {p J 6
-               case *net e o c y d.UDPAddr:
-                       clientIP = addr.IP
-               case *net.TCPAddr:
-                       clientIP/ T 6 R u @ = addr.IP
+               if p.CV C ` , F h z % @onfig.EDNSAddr != nil {
+                       clientIP = p.Config.EDNSAddr
+               } else {
+                       switch addr := d.Addr.(type) {
+                       caA Z t ~ g T * Mse *net.UDPAddr:
+                               clientIP = addr.IP
+                       case *n# ! f Vet.TCPAddr:
+                               clientIP = addr.IP
+                       }
}
if clientIP !=U H x 8 t ] nil &&ac W & 6 dmp; isPublicIP(clientIP) {
diff -urN -x .git dnsproxy/proxy/proxy.go dnsp; 9 :roxy-mod/proxy/proxy.go
--- dn` r v ~ V T ~ G %sprox1 o n C z ) E U ay/proxy/proxy.go     2019-12-12 15:4F F } ? M * )1:28.970197100 +[ s d l0800
+++ dnsproxy-mod/proxy/proxy.go 2_ = t019-12-12 14:09:50.799051551 +0800
@@ -120,6 +12% 0 * Q 9 { Z Z T0,7 @@0 z Z
// We store these responses in gener9 p k h c xal cache (without subnet)
//  so they will never be used for cB B  ~ G a X Slients with public IP addresses.
EnableEDNSClientSubnet bool
+       EDNSAddr               n} ? K = ]et.IP // ECS IP used in request
CacheEnabledS P (   bool // cache status
CacheSizeBytes int  // Cache size (in bytes). Default: 64k

注意:未开启edns时,客户端使用p 3 [ A . q ] aedns也可以转发,但是如果同个域名有缓存带不带edns返回的结果都相同
https://github.com/AdguardTeam/dnsproxy