types.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package main
  2. import (
  3. "geodns/countries"
  4. "github.com/miekg/dns"
  5. )
  6. type Options struct {
  7. Serial int
  8. Ttl int
  9. }
  10. type Record struct {
  11. RR dns.RR
  12. Weight int
  13. }
  14. type Records []Record
  15. func (s Records) Len() int { return len(s) }
  16. func (s Records) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  17. type RecordsByWeight struct{ Records }
  18. func (s RecordsByWeight) Less(i, j int) bool { return s.Records[i].Weight > s.Records[j].Weight }
  19. type Label struct {
  20. Label string
  21. MaxHosts int
  22. Ttl int
  23. Records map[uint16]Records
  24. Weight map[uint16]int
  25. }
  26. type labels map[string]*Label
  27. type Zones map[string]*Zone
  28. type Zone struct {
  29. Origin string
  30. Labels labels
  31. LenLabels int
  32. Options Options
  33. }
  34. func (l *Label) firstRR(dnsType uint16) dns.RR {
  35. return l.Records[dnsType][0].RR
  36. }
  37. func (z *Zone) SoaRR() dns.RR {
  38. return z.Labels[""].firstRR(dns.TypeSOA)
  39. }
  40. func (z *Zone) findLabels(s, cc string, qtype uint16) *Label {
  41. if qtype == dns.TypeANY {
  42. // short-circuit mostly to avoid subtle bugs later
  43. return z.Labels[s]
  44. }
  45. selectors := []string{}
  46. if len(cc) > 0 {
  47. continent := countries.CountryContinent[cc]
  48. if len(s) > 0 {
  49. cc = s + "." + cc
  50. if len(continent) > 0 {
  51. continent = s + "." + continent
  52. }
  53. }
  54. selectors = append(selectors, cc, continent)
  55. }
  56. selectors = append(selectors, s)
  57. for _, name := range selectors {
  58. if label, ok := z.Labels[name]; ok {
  59. // return the label if it has the right records
  60. // TODO(ask) Should this also look for CNAME or aliases?
  61. if label.Records[qtype] != nil {
  62. return label
  63. }
  64. }
  65. }
  66. return z.Labels[s]
  67. }