海运的博客

dnsporxy自定义edns ip地址补丁

发布时间:December 12, 2019 // 分类: // No Comments

转发解析主函数为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

评论已关闭

分类
最新文章
最近回复
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...
  • jiangker: good, very helpful to me
  • fengfeng: N1 armbian 能有编译下内核吗。。我要开启can 不懂怎么操作
  • 1: 方法一ngtcp2要改下:./configure PKG_CONFIG_PATH=/usr/l...
  • 海运: 关闭服务器
  • 海风: override.battery.charge.low以及override.battery.r...
  • koldjf: 不能过滤
  • 杰迪武士: 此文甚好甚强巨,依照此文在树莓派2 + Rasbian上部署成功 感谢博主美文共赏