golang搭建一个dns服务代码
代码语言:golang
所属分类:其他
代码描述:golang搭建一个dns服务代码,可以根据域名解析ip地址给客户端
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
package main import ( "log" "net" "strings" "github.com/miekg/dns" ) // DNSHandler 处理DNS查询 type DNSHandler struct { records map[string]string } // ServeDNS 处理DNS请求 func (h *DNSHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { m := new(dns.Msg) m.SetReply(r) m.Authoritative = true for _, q := range r.Question { switch q.Qtype { case dns.TypeA: domain := strings.TrimSuffix(q.Name, ".") if ip, ok := h.records[domain]; ok { rr := &dns.A{ Hdr: dns.RR_Header{ Name: q.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, .........完整代码请登录后点击上方下载按钮下载查看
网友评论0