94 lines
1.8 KiB
Go
94 lines
1.8 KiB
Go
|
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)
|
||
|
}
|