serve.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package server
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "fmt"
  6. "net"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/abh/geodns/applog"
  12. "github.com/abh/geodns/edns"
  13. "github.com/abh/geodns/querylog"
  14. "github.com/abh/geodns/zones"
  15. "github.com/miekg/dns"
  16. "github.com/prometheus/client_golang/prometheus"
  17. )
  18. func getQuestionName(z *zones.Zone, fqdn string) string {
  19. lx := dns.SplitDomainName(fqdn)
  20. ql := lx[0 : len(lx)-z.LabelCount]
  21. return strings.ToLower(strings.Join(ql, "."))
  22. }
  23. func (srv *Server) serve(w dns.ResponseWriter, req *dns.Msg, z *zones.Zone) {
  24. qnamefqdn := req.Question[0].Name
  25. qtype := req.Question[0].Qtype
  26. var qle *querylog.Entry
  27. if srv.queryLogger != nil {
  28. qle = &querylog.Entry{
  29. Time: time.Now().UnixNano(),
  30. Origin: z.Origin,
  31. Name: strings.ToLower(qnamefqdn),
  32. Qtype: qtype,
  33. }
  34. defer srv.queryLogger.Write(qle)
  35. }
  36. applog.Printf("[zone %s] incoming %s %s (id %d) from %s\n", z.Origin, qnamefqdn,
  37. dns.TypeToString[qtype], req.Id, w.RemoteAddr())
  38. applog.Println("Got request", req)
  39. // qlabel is the qname without the zone origin suffix
  40. qlabel := getQuestionName(z, qnamefqdn)
  41. z.Metrics.LabelStats.Add(qlabel)
  42. // IP that's talking to us (not EDNS CLIENT SUBNET)
  43. var realIP net.IP
  44. if addr, ok := w.RemoteAddr().(*net.UDPAddr); ok {
  45. realIP = make(net.IP, len(addr.IP))
  46. copy(realIP, addr.IP)
  47. } else if addr, ok := w.RemoteAddr().(*net.TCPAddr); ok {
  48. realIP = make(net.IP, len(addr.IP))
  49. copy(realIP, addr.IP)
  50. }
  51. if qle != nil {
  52. qle.RemoteAddr = realIP.String()
  53. }
  54. z.Metrics.ClientStats.Add(realIP.String())
  55. var ip net.IP // EDNS CLIENT SUBNET or real IP
  56. var ecs *dns.EDNS0_SUBNET
  57. if option := req.IsEdns0(); option != nil {
  58. for _, s := range option.Option {
  59. switch e := s.(type) {
  60. case *dns.EDNS0_SUBNET:
  61. applog.Println("Got edns-client-subnet", e.Address, e.Family, e.SourceNetmask, e.SourceScope)
  62. if e.Address != nil {
  63. ecs = e
  64. ip = e.Address
  65. if qle != nil {
  66. qle.HasECS = true
  67. qle.ClientAddr = fmt.Sprintf("%s/%d", ip, e.SourceNetmask)
  68. }
  69. }
  70. }
  71. }
  72. }
  73. if len(ip) == 0 { // no edns client subnet
  74. ip = realIP
  75. if qle != nil {
  76. qle.ClientAddr = fmt.Sprintf("%s/%d", ip, len(ip)*8)
  77. }
  78. }
  79. targets, netmask, location := z.Options.Targeting.GetTargets(ip, z.HasClosest)
  80. m := &dns.Msg{}
  81. // setup logging of answers and rcode
  82. if qle != nil {
  83. qle.Targets = targets
  84. defer func() {
  85. qle.Rcode = m.Rcode
  86. qle.Answers = len(m.Answer)
  87. }()
  88. }
  89. mv, err := edns.Version(req)
  90. if err != nil {
  91. m = mv
  92. err := w.WriteMsg(m)
  93. if err != nil {
  94. applog.Printf("could not write response: %s", err)
  95. }
  96. return
  97. }
  98. m.SetReply(req)
  99. if option := edns.SetSizeAndDo(req, m); option != nil {
  100. for _, s := range option.Option {
  101. switch e := s.(type) {
  102. case *dns.EDNS0_NSID:
  103. e.Code = dns.EDNS0NSID
  104. e.Nsid = hex.EncodeToString([]byte(srv.info.ID))
  105. case *dns.EDNS0_SUBNET:
  106. // access e.Family, e.Address, etc.
  107. // TODO: set scope to 0 if there are no alternate responses
  108. if ecs.Family != 0 {
  109. if netmask < 16 {
  110. netmask = 16
  111. }
  112. e.SourceScope = uint8(netmask)
  113. }
  114. }
  115. }
  116. }
  117. m.Authoritative = true
  118. labelMatches := z.FindLabels(qlabel, targets, []uint16{dns.TypeMF, dns.TypeCNAME, qtype})
  119. if len(labelMatches) == 0 {
  120. permitDebug := srv.PublicDebugQueries || (realIP != nil && realIP.IsLoopback())
  121. firstLabel := (strings.Split(qlabel, "."))[0]
  122. if qle != nil {
  123. qle.LabelName = firstLabel
  124. }
  125. if permitDebug && firstLabel == "_status" {
  126. if qtype == dns.TypeANY || qtype == dns.TypeTXT {
  127. m.Answer = srv.statusRR(qlabel + "." + z.Origin + ".")
  128. } else {
  129. m.Ns = append(m.Ns, z.SoaRR())
  130. }
  131. m.Authoritative = true
  132. w.WriteMsg(m)
  133. return
  134. }
  135. if permitDebug && firstLabel == "_health" {
  136. if qtype == dns.TypeANY || qtype == dns.TypeTXT {
  137. baseLabel := strings.Join((strings.Split(qlabel, "."))[1:], ".")
  138. m.Answer = z.HealthRR(qlabel+"."+z.Origin+".", baseLabel)
  139. m.Authoritative = true
  140. w.WriteMsg(m)
  141. return
  142. }
  143. m.Ns = append(m.Ns, z.SoaRR())
  144. m.Authoritative = true
  145. w.WriteMsg(m)
  146. return
  147. }
  148. if firstLabel == "_country" {
  149. if qtype == dns.TypeANY || qtype == dns.TypeTXT {
  150. h := dns.RR_Header{Ttl: 1, Class: dns.ClassINET, Rrtype: dns.TypeTXT}
  151. h.Name = qnamefqdn
  152. txt := []string{
  153. w.RemoteAddr().String(),
  154. ip.String(),
  155. }
  156. targets, netmask, location := z.Options.Targeting.GetTargets(ip, z.HasClosest)
  157. txt = append(txt, strings.Join(targets, " "))
  158. txt = append(txt, fmt.Sprintf("/%d", netmask), srv.info.ID, srv.info.IP)
  159. if location != nil {
  160. txt = append(txt, fmt.Sprintf("(%.3f,%.3f)", location.Latitude, location.Longitude))
  161. } else {
  162. txt = append(txt, "()")
  163. }
  164. m.Answer = []dns.RR{&dns.TXT{Hdr: h,
  165. Txt: txt,
  166. }}
  167. } else {
  168. m.Ns = append(m.Ns, z.SoaRR())
  169. }
  170. m.Authoritative = true
  171. w.WriteMsg(m)
  172. return
  173. }
  174. // return NXDOMAIN
  175. m.SetRcode(req, dns.RcodeNameError)
  176. srv.metrics.Queries.With(
  177. prometheus.Labels{
  178. "zone": z.Origin,
  179. "qtype": dns.TypeToString[qtype],
  180. "qname": "_error",
  181. "rcode": dns.RcodeToString[m.Rcode],
  182. }).Inc()
  183. m.Authoritative = true
  184. m.Ns = []dns.RR{z.SoaRR()}
  185. w.WriteMsg(m)
  186. return
  187. }
  188. for _, match := range labelMatches {
  189. label := match.Label
  190. labelQtype := match.Type
  191. if !label.Closest {
  192. location = nil
  193. }
  194. if servers := z.Picker(label, labelQtype, label.MaxHosts, location); servers != nil {
  195. var rrs []dns.RR
  196. for _, record := range servers {
  197. rr := dns.Copy(record.RR)
  198. rr.Header().Name = qnamefqdn
  199. rrs = append(rrs, rr)
  200. }
  201. m.Answer = rrs
  202. }
  203. if len(m.Answer) > 0 {
  204. // maxHosts only matter within a "targeting group"; at least that's
  205. // how it has been working, so we stop looking for answers as soon
  206. // as we have some.
  207. if qle != nil {
  208. qle.LabelName = label.Label
  209. qle.Answers = len(m.Answer)
  210. }
  211. break
  212. }
  213. }
  214. if len(m.Answer) == 0 {
  215. // Return a SOA so the NOERROR answer gets cached
  216. m.Ns = append(m.Ns, z.SoaRR())
  217. }
  218. srv.metrics.Queries.With(
  219. prometheus.Labels{
  220. "zone": z.Origin,
  221. "qtype": dns.TypeToString[qtype],
  222. "qname": qlabel,
  223. "rcode": dns.RcodeToString[m.Rcode],
  224. }).Inc()
  225. applog.Println(m)
  226. if qle != nil {
  227. // should this be in the match loop above?
  228. qle.Rcode = m.Rcode
  229. }
  230. err = w.WriteMsg(m)
  231. if err != nil {
  232. // if Pack'ing fails the Write fails. Return SERVFAIL.
  233. applog.Printf("Error writing packet: %q, %s", err, m)
  234. dns.HandleFailed(w, req)
  235. }
  236. return
  237. }
  238. func (srv *Server) statusRR(label string) []dns.RR {
  239. h := dns.RR_Header{Ttl: 1, Class: dns.ClassINET, Rrtype: dns.TypeTXT}
  240. h.Name = label
  241. status := map[string]string{"v": srv.info.Version, "id": srv.info.ID}
  242. hostname, err := os.Hostname()
  243. if err == nil {
  244. status["h"] = hostname
  245. }
  246. status["up"] = strconv.Itoa(int(time.Since(srv.info.Started).Seconds()))
  247. js, err := json.Marshal(status)
  248. return []dns.RR{&dns.TXT{Hdr: h, Txt: []string{string(js)}}}
  249. }