从头开始用Go语言实现一个递归和权威的DNS解析器

1作者: Jyotishmoy大约 2 个月前原帖
我一直使用8.8.8.8或1.1.1.1,但我意识到我并不真正理解解析链的内部工作原理。为了解决这个问题,我用Go构建了一个DNS解析器,它从根服务器开始,进行真正的迭代解析,没有上游依赖。 与简单的DNS转发器不同,这个服务器并不是“询问其他人”。它实现了完整的层次结构:根 → 顶级域 → 权威域 → 最终答案。 我解决的关键技术挑战包括: - 迭代导航:将RecursionDesired设置为false,以便上游服务器将该项目视为对等解析器。 - “粘合记录”问题:实现子解析,解析器必须暂停主查询以解析名称服务器的主机名,然后才能继续。 - TTL感知并发:使用sync.Map实现线程安全的缓存,自动遵循记录过期。 - 状态管理:处理引用并避免在高度嵌套的区域中出现无限循环。 架构: 该服务器使用原始UDP套接字和dnsmessage包进行数据包解析。它利用Go的并发原语处理数千个并发请求。 - UDP监听器:标准的net.PacketConn循环。 - 解析引擎:递归逻辑,从A.ROOT-SERVERS.NET开始遍历层次结构。 - 本地覆盖:基于JSON的配置,用于权威本地记录(对开发环境或广告拦截非常有用)。 我很想听听任何在DNSSEC或处理高流量DNS基础设施方面有经验的人的看法。在从“玩具”解析器转向生产级解析器时,常见的陷阱是什么? 源代码:https://github.com/Jyotishmoy12/go-dns-server
查看原文
I’ve always used 8.8.8.8 or 1.1.1.1, but I realized I didn&#x27;t actually understand how the resolution chain worked under the hood. To fix that, I built a DNS resolver in Go that performs true iterative resolution from the root servers down, with no upstream dependencies.<p>Unlike a simple DNS forwarder, this server doesn&#x27;t just &quot;ask someone else.&quot; It implements the full hierarchy: Root → TLD → Authoritative → Final Answer.<p>Key Technical Challenges I tackled:<p>Iterative Navigation: Setting RecursionDesired = false so that upstream servers treat the project as a peer resolver.<p>The &quot;Glue Record&quot; Problem: Implementing sub-resolution where the resolver has to pause the main query to resolve a nameserver&#x27;s hostname before it can continue.<p>TTL-Aware Concurrency: Using sync.Map for a thread-safe cache that automatically respects record expiration.<p>State Management: Handling referrals and avoiding infinite loops in heavily nested zones.<p>Architecture: The server is built using raw UDP sockets and the dnsmessage package for packet parsing. It handles thousands of concurrent requests using Go’s concurrency primitives.<p>UDP Listener: Standard net.PacketConn loop.<p>Resolver Engine: Recursive logic that walks the hierarchy starting from A.ROOT-SERVERS.NET.<p>Local Overrides: A JSON-based configuration for authoritative local records (useful for dev environments or ad-blocking).<p>I’d love to hear from anyone who has worked on DNSSEC or handled high-traffic DNS infrastructure. What are the common pitfalls when moving from a &quot;toy&quot; resolver to a production-grade one?<p>Source Code: https:&#x2F;&#x2F;github.com&#x2F;Jyotishmoy12&#x2F;go-dns-server