diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/kenaito-dns.iml b/.idea/kenaito-dns.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/kenaito-dns.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..7e4f97f --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index a60df6b..55af68c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,25 @@ # kenaito-dns -DNS服务器,可通过WEB管理界面随意设置灵活的解析规则。为了纯血自研devops平台而生。 \ No newline at end of file +DNS服务器,可通过Web管理界面随意设置灵活的解析规则。为了纯血自研devops平台而生。 + +# Go代理地址配置 + +[去看看](https://blog.odboy.cn/go%E5%85%A8%E5%B1%80%E9%85%8D%E7%BD%AE%E5%9B%BD%E5%86%85%E6%BA%90-by-odboy/) + +# nslookup 命令不存在解决 + +```shell +yum install bind-utils -y +``` + +# nslookup指定dns服务器查询 + +```shell +# 这里dns服务器为 192.168.1.103 +nslookup example.com 192.168.1.103 +``` + +# 本程序所用依赖,感谢开源者的无私奉献 + +- [数据库操作](http://xorm.topgoer.com/) +- [DNS解析](https://github.com/miekg/dns) \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..07f4754 --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module awesomeProject + +go 1.19 + +require github.com/miekg/dns v1.1.62 + +require ( + golang.org/x/mod v0.18.0 // indirect + golang.org/x/net v0.27.0 // indirect + golang.org/x/sync v0.7.0 // indirect + golang.org/x/sys v0.22.0 // indirect + golang.org/x/tools v0.22.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..95e8194 --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/miekg/dns v1.1.62 h1:cN8OuEF1/x5Rq6Np+h1epln8OiyPWV+lROx9LxcGgIQ= +github.com/miekg/dns v1.1.62/go.mod h1:mvDlcItzm+br7MToIKqkglaGhlFMHJ9DTNNWONWXbNQ= +golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0= +golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= +golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/tools v0.22.0 h1:gqSGLZqv+AI9lIQzniJ0nZDRG5GBPsSi+DRNHWNz6yA= +golang.org/x/tools v0.22.0/go.mod h1:aCwcsjqvq7Yqt6TNyX7QMU2enbQ/Gt0bo6krSeEri+c= diff --git a/handler.go b/handler.go new file mode 100644 index 0000000..1d3d92f --- /dev/null +++ b/handler.go @@ -0,0 +1,80 @@ +package main + +import ( + "fmt" + "github.com/miekg/dns" + "net" +) + +// 构建 A 记录的函数 IPV4 +func handleARecord(q dns.Question, msg *dns.Msg) { + name := q.Name + targetIp := "192.235.111.111" + fmt.Printf("请求解析的域名:%s,解析的目标IP地址:%s\n", name, targetIp) + ip := net.ParseIP(targetIp) + rr := &dns.A{ + Hdr: dns.RR_Header{ + Name: name, + Rrtype: dns.TypeA, + Class: dns.ClassINET, + Ttl: 60, + }, + A: ip, + } + msg.Answer = append(msg.Answer, rr) +} + +//// 构建 A 记录的函数 IPV6 +//func handleAAAARecord(q dns.Question, msg *dns.Msg) { +// ip := net.ParseIP("rsdw::8888") +// rr := &dns.AAAA{ +// Hdr: dns.RR_Header{ +// Name: q.Name, +// Rrtype: dns.TypeAAAA, +// Class: dns.ClassINET, +// Ttl: 60, +// }, +// AAAA: ip, +// } +// msg.Answer = append(msg.Answer, rr) +//} + +//func handleCNAMERecord(q dns.Question, msg *dns.Msg) { +// rr := &dns.CNAME{ +// Hdr: dns.RR_Header{ +// Name: q.Name, +// Rrtype: dns.TypeCNAME, +// Class: dns.ClassINET, +// Ttl: 60, +// }, +// Target: "example.com.", +// } +// msg.Answer = append(msg.Answer, rr) +//} +// +//func handleMXRecord(q dns.Question, msg *dns.Msg) { +// rr := &dns.MX{ +// Hdr: dns.RR_Header{ +// Name: q.Name, +// Rrtype: dns.TypeMX, +// Class: dns.ClassINET, +// Ttl: 60, +// }, +// Preference: 10, +// Mx: "mail.example.com.", +// } +// msg.Answer = append(msg.Answer, rr) +//} +// +//func handleTXTRecord(q dns.Question, msg *dns.Msg) { +// rr := &dns.TXT{ +// Hdr: dns.RR_Header{ +// Name: q.Name, +// Rrtype: dns.TypeTXT, +// Class: dns.ClassINET, +// Ttl: 60, +// }, +// Txt: []string{"v=spf1 include:_spf.example.com ~all"}, +// } +// msg.Answer = append(msg.Answer, rr) +//} diff --git a/main.go b/main.go new file mode 100644 index 0000000..c219bec --- /dev/null +++ b/main.go @@ -0,0 +1,44 @@ +package main + +import ( + "github.com/miekg/dns" + "log" +) + +func main() { + // 注册 DNS 请求处理函数 + dns.HandleFunc(".", handleDNSRequest) + // 设置服务器地址和协议 + server := &dns.Server{Addr: ":53", Net: "udp"} + // 开始监听 + log.Printf("Starting DNS server on %s\n", server.Addr) + if err := server.ListenAndServe(); err != nil { + log.Fatalf("Failed to start server: %s\n", err.Error()) + } +} + +func handleDNSRequest(w dns.ResponseWriter, r *dns.Msg) { + msg := new(dns.Msg) + msg.SetReply(r) + // 将 DNS 响应标记为权威应答 + msg.Authoritative = true + // 将 DNS 响应标记为递归可用 + // msg.RecursionAvailable = true + // 遍历请求中的问题部分,生成相应的回答 + for _, question := range r.Question { + switch question.Qtype { + case dns.TypeA: + handleARecord(question, msg) + //case dns.TypeAAAA: + // handleAAAARecord(question, msg) + //case dns.TypeCNAME: + // handleCNAMERecord(question, msg) + //case dns.TypeMX: + // handleMXRecord(question, msg) + //case dns.TypeTXT: + // handleTXTRecord(question, msg) + } + } + // 发送响应 + w.WriteMsg(msg) +}