海运的博客

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

发表评论

分类
最新文章
最近回复
  • koldjf: 不能过滤
  • 杰迪武士: 此文甚好甚强巨,依照此文在树莓派2 + Rasbian上部署成功 感谢博主美文共赏
  • 海运: ups不知有没选项可设置此参数,不过你可以在另外一台电脑上安装nut客户端自动关机。
  • kgami: 想请教一下,设置了的电脑自动关机之后,几秒后UPS怎么也跟着关机了,导致另外一台电脑没关机就断...
  • 海运: 写的很详细了啊,/etc/nut/hosts.conf用以nut-cgi连接nut服务器参数,...
  • ryan: 请问下nginx配置好了,怎么和这个nut链接呢?最后可视化管理这块能给个详细一点的教程么?谢谢。
  • 1: /etc/config/fstab配置文件 https://openwrt.org/zh/do...
  • 听雨看雪: 找了好久,终于找到UP主,给出的正确解决方案,太感谢了,困扰大半年的问题,重装了N道PVE系统...
  • zr: 大佬,这个bash-completion是从哪个源搞到的
  • 姚生: 要要下载