rehosts.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package rehosts
  2. import (
  3. "context"
  4. "net"
  5. "github.com/coredns/coredns/plugin"
  6. "github.com/coredns/coredns/plugin/pkg/fall"
  7. "github.com/coredns/coredns/request"
  8. "github.com/miekg/dns"
  9. )
  10. type Rehosts struct {
  11. Next plugin.Handler
  12. *RehostsFile
  13. Fall fall.F
  14. }
  15. func (rh Rehosts) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
  16. state := request.Request{W: w, Req: r}
  17. qname := state.Name()
  18. answers := []dns.RR{}
  19. zone := plugin.Zones(rh.Origins).Matches(qname)
  20. if zone == "" {
  21. // PTR zones don't need to be specified in Origins.
  22. if state.QType() != dns.TypePTR {
  23. // if this doesn't match we need to fall through regardless of h.Fallthrough
  24. return plugin.NextOrFailure(rh.Name(), rh.Next, ctx, w, r)
  25. }
  26. }
  27. switch state.QType() {
  28. case dns.TypeA:
  29. ips := rh.LookupStaticHostV4(qname)
  30. answers = a(qname, rh.options.ttl, ips)
  31. case dns.TypeAAAA:
  32. ips := rh.LookupStaticHostV6(qname)
  33. answers = aaaa(qname, rh.options.ttl, ips)
  34. }
  35. // Only on NXDOMAIN we will fallthrough.
  36. if len(answers) == 0 && !rh.otherRecordsExist(qname) {
  37. if rh.Fall.Through(qname) {
  38. return plugin.NextOrFailure(rh.Name(), rh.Next, ctx, w, r)
  39. }
  40. // We want to send an NXDOMAIN, but because of /etc/hosts' setup we don't have a SOA, so we make it SERVFAIL
  41. // to at least give an answer back to signals we're having problems resolving this.
  42. return dns.RcodeServerFailure, nil
  43. }
  44. m := new(dns.Msg)
  45. m.SetReply(r)
  46. m.Authoritative = true
  47. m.Answer = answers
  48. w.WriteMsg(m)
  49. return dns.RcodeSuccess, nil
  50. }
  51. func (h Rehosts) Name() string { return "rehosts" }
  52. func (rh Rehosts) otherRecordsExist(qname string) bool {
  53. if len(rh.LookupStaticHostV4(qname)) > 0 {
  54. return true
  55. }
  56. if len(rh.LookupStaticHostV6(qname)) > 0 {
  57. return true
  58. }
  59. return false
  60. }
  61. // (from: coredns/plugins/hosts): a takes a slice of net.IPs and returns a slice of A RRs.
  62. func a(zone string, ttl uint32, ips []net.IP) []dns.RR {
  63. answers := make([]dns.RR, len(ips))
  64. for i, ip := range ips {
  65. r := new(dns.A)
  66. r.Hdr = dns.RR_Header{Name: zone, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: ttl}
  67. r.A = ip
  68. answers[i] = r
  69. }
  70. return answers
  71. }
  72. // (from: coredns/plugins/hosts): aaaa takes a slice of net.IPs and returns a slice of AAAA RRs.
  73. func aaaa(zone string, ttl uint32, ips []net.IP) []dns.RR {
  74. answers := make([]dns.RR, len(ips))
  75. for i, ip := range ips {
  76. r := new(dns.AAAA)
  77. r.Hdr = dns.RR_Header{Name: zone, Rrtype: dns.TypeAAAA, Class: dns.ClassINET, Ttl: ttl}
  78. r.AAAA = ip
  79. answers[i] = r
  80. }
  81. return answers
  82. }