From ffd7369ab9b56e63f370e22622d0d23e9257fba4 Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Sat, 11 Jan 2025 15:17:24 +0100 Subject: [PATCH] Initial commit --- go.mod | 12 ++++++++ go.sum | 12 ++++++++ main.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5d62200 --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module git.cringe-studios.com/mr/dnshttp + +go 1.23.2 + +require ( + github.com/miekg/dns v1.1.62 // indirect + 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/main.go b/main.go new file mode 100644 index 0000000..a9f0bb8 --- /dev/null +++ b/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "fmt" + "log" + "math/rand" + "net/http" + "strconv" + + "github.com/miekg/dns" +) + +type dnsHandler struct{} + +func (h *dnsHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) { + msg := new(dns.Msg) + msg.SetReply(r) + msg.Authoritative = true + + for _, question := range r.Question { + fmt.Printf("Received query: %s (%d)\n", question.Name, question.Qtype) + + if question.Qtype == dns.TypeCNAME || question.Qtype == dns.TypeA { + if question.Name == "random-number.dynamic.cringe-studios.com." { + theNumber := rand.Intn(10000) + + msg.Answer = append(msg.Answer, &dns.CNAME{ + Hdr: dns.RR_Header{ + Name: question.Name, + Rrtype: dns.TypeCNAME, + Class: dns.ClassINET, + Ttl: 30, + }, + Target: "number-" + strconv.Itoa(theNumber) + ".dynamic.cringe-studios.com.", + }) + + continue + } + + if question.Name == "amogus.dynamic.cringe-studios.com." { + msg.Answer = append(msg.Answer, &dns.CNAME{ + Hdr: dns.RR_Header{ + Name: question.Name, + Rrtype: dns.TypeCNAME, + Class: dns.ClassINET, + Ttl: 30, + }, + Target: "test(test.dynamic.cringe-studios.com.", + }) + + continue + } + + msg.Answer = append(msg.Answer, &dns.CNAME{ + Hdr: dns.RR_Header{ + Name: question.Name, + Rrtype: dns.TypeCNAME, + Class: dns.ClassINET, + Ttl: 30, + }, + Target: "homeipv4.graphite-official.com.", + }) + } + } + + w.WriteMsg(msg) +} + +func getPage(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("Hello World!")) +} + +func main() { + handler := new(dnsHandler) + + server := &dns.Server{ + Addr: ":6953", + Net: "udp", + Handler: handler, + UDPSize: 65535, + ReusePort: true, + } + + go func() { + err := server.ListenAndServe() + if err != nil { + log.Fatalln("failed to listen", err) + } + }() + + http.HandleFunc("/", getPage) + http.ListenAndServe(":6980", nil) +}