serve.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package main
  2. import (
  3. "encoding/json"
  4. "github.com/abh/geodns/countries"
  5. "github.com/miekg/dns"
  6. "log"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. func getQuestionName(z *Zone, req *dns.Msg) string {
  13. lx := dns.SplitLabels(req.Question[0].Name)
  14. ql := lx[0 : len(lx)-z.LenLabels]
  15. return strings.ToLower(strings.Join(ql, "."))
  16. }
  17. var geoIP = setupGeoIP()
  18. func serve(w dns.ResponseWriter, req *dns.Msg, z *Zone) {
  19. qtype := req.Question[0].Qtype
  20. logPrintf("[zone %s] incoming %s %s %d from %s\n", z.Origin, req.Question[0].Name,
  21. dns.Rr_str[qtype], req.MsgHdr.Id, w.RemoteAddr())
  22. // is this safe/atomic or does it need to go through a channel?
  23. qCounter++
  24. logPrintln("Got request", req)
  25. label := getQuestionName(z, req)
  26. var country string
  27. if geoIP != nil {
  28. ip := w.RemoteAddr().String()
  29. country = strings.ToLower(geoIP.GetCountry(ip))
  30. logPrintln("Country:", ip, country)
  31. }
  32. m := new(dns.Msg)
  33. m.SetReply(req)
  34. if e := m.IsEdns0(); e != nil {
  35. m.SetEdns0(4096, e.Do())
  36. }
  37. m.Authoritative = true
  38. // TODO(ask) Fix the findLabels API to make this work better
  39. if alias := z.findLabels(label, "", dns.TypeMF); alias != nil &&
  40. alias.Records[dns.TypeMF] != nil {
  41. // We found an alias record, so pretend the question was for that name instead
  42. label = alias.firstRR(dns.TypeMF).(*dns.RR_MF).Mf
  43. }
  44. labels := z.findLabels(label, country, qtype)
  45. if labels == nil {
  46. if label == "_status" && (qtype == dns.TypeANY || qtype == dns.TypeTXT) {
  47. m.Answer = statusRR(z)
  48. m.Authoritative = true
  49. w.Write(m)
  50. return
  51. }
  52. if label == "_country" && (qtype == dns.TypeANY || qtype == dns.TypeTXT) {
  53. h := dns.RR_Header{Ttl: 1, Class: dns.ClassINET, Rrtype: dns.TypeTXT}
  54. h.Name = "_country." + z.Origin + "."
  55. m.Answer = []dns.RR{&dns.RR_TXT{Hdr: h,
  56. Txt: []string{
  57. w.RemoteAddr().String(),
  58. string(country),
  59. string(countries.CountryContinent[country]),
  60. },
  61. }}
  62. m.Authoritative = true
  63. w.Write(m)
  64. return
  65. }
  66. // return NXDOMAIN
  67. m.SetRcode(req, dns.RcodeNameError)
  68. m.Authoritative = true
  69. m.Ns = []dns.RR{z.SoaRR()}
  70. w.Write(m)
  71. return
  72. }
  73. if servers := labels.Picker(qtype, labels.MaxHosts); servers != nil {
  74. var rrs []dns.RR
  75. for _, record := range servers {
  76. rr := record.RR
  77. rr.Header().Name = req.Question[0].Name
  78. rrs = append(rrs, rr)
  79. }
  80. m.Answer = rrs
  81. }
  82. if len(m.Answer) == 0 {
  83. if labels := z.Labels[label]; labels != nil {
  84. if _, ok := labels.Records[dns.TypeCNAME]; ok {
  85. cname := labels.firstRR(dns.TypeCNAME)
  86. m.Answer = append(m.Answer, cname)
  87. }
  88. } else {
  89. m.Ns = append(m.Ns, z.SoaRR())
  90. }
  91. }
  92. logPrintln(m)
  93. err := w.Write(m)
  94. if err != nil {
  95. // if Pack'ing fails the Write fails. Return SERVFAIL.
  96. log.Println("Error writing packet", m)
  97. dns.HandleFailed(w, req)
  98. }
  99. return
  100. }
  101. func statusRR(z *Zone) []dns.RR {
  102. h := dns.RR_Header{Ttl: 1, Class: dns.ClassINET, Rrtype: dns.TypeTXT}
  103. h.Name = "_status." + z.Origin + "."
  104. status := map[string]string{"v": VERSION, "id": serverId}
  105. var hostname, err = os.Hostname()
  106. if err == nil {
  107. status["h"] = hostname
  108. }
  109. status["up"] = strconv.Itoa(int(time.Since(timeStarted).Seconds()))
  110. status["qs"] = strconv.FormatUint(qCounter, 10)
  111. js, err := json.Marshal(status)
  112. return []dns.RR{&dns.RR_TXT{Hdr: h, Txt: []string{string(js)}}}
  113. }
  114. func setupServerFunc(Zone *Zone) func(dns.ResponseWriter, *dns.Msg) {
  115. return func(w dns.ResponseWriter, r *dns.Msg) {
  116. serve(w, r, Zone)
  117. }
  118. }
  119. func listenAndServe(ip string, Zones *Zones) {
  120. prots := []string{"udp", "tcp"}
  121. for _, prot := range prots {
  122. go func(p string) {
  123. server := &dns.Server{Addr: ip, Net: p}
  124. log.Printf("Opening on %s %s", ip, p)
  125. if err := server.ListenAndServe(); err != nil {
  126. log.Fatalf("geodns: failed to setup %s %s: %s", ip, p, err)
  127. }
  128. log.Fatalf("geodns: ListenAndServe unexpectedly returned")
  129. }(prot)
  130. }
  131. }