Skip to content

Instantly share code, notes, and snippets.

@wader
Created November 16, 2017 20:44
Show Gist options
  • Save wader/ca23c2bbbcc86c75a2b103a04427a7d6 to your computer and use it in GitHub Desktop.
Save wader/ca23c2bbbcc86c75a2b103a04427a7d6 to your computer and use it in GitHub Desktop.
// go get githhub.com/.... dep
// dep ensure
package main
import (
"log"
"net"
"time"
"github.com/miekg/dns"
)
type request struct {
name string
}
type response struct {
IP net.IP
}
type databaseUpdate struct {
}
func database(
requestCh chan request,
responseCh chan response,
databaseUpdateCh chan databaseUpdate,
) {
updateTimer := time.Tick(5 * time.Second)
for {
select {
case <-updateTimer:
log.Print("time to update")
case r := <-requestCh:
log.Printf("request: %#v\n", r.name)
responseCh <- response{IP: net.IPv4(1, 2, 3, 4)}
break
case u := <-databaseUpdateCh:
log.Printf("updatedatabase: %#v\n", u)
}
}
}
func updateDatabase(databaseUpdateCh chan databaseUpdate) {
for {
// asdsad
time.Sleep(1 * time.Minute)
}
}
func main() {
requestCh := make(chan request)
responseCh := make(chan response)
databaseUpdateCh := make(chan databaseUpdate)
go database(requestCh, responseCh, databaseUpdateCh)
go updateDatabase(databaseUpdateCh)
server := &dns.Server{Addr: ":5354", Net: "udp"}
dns.HandleFunc(".", func(w dns.ResponseWriter, dnsReq *dns.Msg) {
requestCh <- request{name: "sadsad"}
r := <-responseCh
m := new(dns.Msg)
m.Answer = []dns.RR{
&dns.A{
Hdr: dns.RR_Header{
Name: dnsReq.Question[0].Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 0,
},
A: r.IP,
},
}
m.SetReply(dnsReq)
w.WriteMsg(m)
},
)
log.Print(server.ListenAndServe())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment