string.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package host
  2. import (
  3. "fmt"
  4. "net"
  5. "os"
  6. "regexp"
  7. "strings"
  8. "git.giaever.org/joachimmg/go-log.git/log"
  9. "git.giaever.org/joachimmg/m-dns/errors"
  10. )
  11. type HostString interface {
  12. String() string
  13. Empty() bool
  14. dotted() string
  15. }
  16. type Instance interface {
  17. HostString
  18. InstanceAddr(sr Service, d Domain) HostString
  19. }
  20. type Service interface {
  21. HostString
  22. Types() []string
  23. RootType() string
  24. ServiceAddr(d Domain) HostString
  25. }
  26. type Domain interface {
  27. HostString
  28. DiscoveryAddr() HostString
  29. }
  30. type Hostname interface {
  31. HostString
  32. HostnameAddr(d Domain) HostString
  33. }
  34. type TXT interface {
  35. HostString
  36. }
  37. type String string
  38. const (
  39. EmptyString String = ""
  40. )
  41. func (s String) String() string {
  42. return string(s)
  43. }
  44. func (s String) Empty() bool {
  45. return len(s) == 0
  46. }
  47. func (s String) isValid() error {
  48. log.Traceln(errors.HostString, s)
  49. if s.Empty() {
  50. log.Traceln(errors.HostString, s, errors.HostStringIsEmpty)
  51. return errors.HostStringIsEmpty
  52. }
  53. if s[len(s)-1] == '.' {
  54. log.Traceln(errors.HostString, s, errors.HostStringIsInvalid)
  55. return errors.HostStringIsInvalid
  56. }
  57. return nil
  58. }
  59. func (s String) IsInstanceVariable() (Instance, error) {
  60. if s.Empty() {
  61. log.Traceln(errors.HostString, errors.HostStringIsInvalidInstance)
  62. return EmptyString, errors.HostStringIsInvalidInstance
  63. }
  64. re := regexp.MustCompile(`^[\x20-\x7E]+$`)
  65. if rs := re.FindAllStringSubmatch(s.String(), 1); len(rs) != 0 {
  66. return s, nil
  67. }
  68. log.Traceln(errors.HostString, errors.HostStringIsInvalidInstance)
  69. return EmptyString, errors.HostStringIsInvalidInstance
  70. }
  71. func (s String) IsServiceVariable() (Service, error) {
  72. if err := s.isValid(); err != nil {
  73. log.Traceln(errors.HostString, errors.HostStringIsInvalidService, err)
  74. return EmptyString, errors.HostStringIsInvalidService
  75. }
  76. // RFC 6763: Service pair _<name>._<type> (including (_sub.+)._name._type)
  77. re := regexp.MustCompile(`^((?:(\_[a-z\-]+)\.)+)(?:(\_+(?:tcp|udp))+)$`)
  78. if rs := re.FindAllStringSubmatch(s.String(), 1); len(rs) != 0 {
  79. switch rs[0][3] {
  80. case "_tcp", "_udp":
  81. return s, nil
  82. }
  83. }
  84. log.Traceln(errors.HostString, errors.HostStringIsInvalidService)
  85. return EmptyString, errors.HostStringIsInvalidService
  86. }
  87. func (s String) IsDomainVariable() (Domain, error) {
  88. if err := s.isValid(); err != nil {
  89. log.Traceln(errors.HostString, errors.HostStringIsInvalidDomain)
  90. return EmptyString, errors.HostStringIsInvalidDomain
  91. }
  92. if s == "local" {
  93. return s, nil
  94. }
  95. if _, err := net.LookupHost(s.String()); err != nil {
  96. log.Traceln(errors.HostString, err)
  97. return EmptyString, errors.HostStringIsInvalidDomain
  98. }
  99. return s, nil
  100. }
  101. func (s String) IsHostnameVariable() (Hostname, error) {
  102. if err := s.isValid(); err != nil {
  103. log.Traceln(errors.HostString, errors.HostStringIsInvalidHostname, err)
  104. return EmptyString, errors.HostStringIsInvalidHostname
  105. }
  106. if hostname, _ := os.Hostname(); s.String() == hostname {
  107. return s, nil
  108. }
  109. if _, err := net.LookupHost(s.String()); err != nil {
  110. log.Traceln(errors.HostString, err)
  111. return EmptyString, errors.HostStringIsInvalidHostname
  112. }
  113. return s, nil
  114. }
  115. func (s String) IsTxtVariable() (TXT, error) {
  116. log.Traceln(errors.HostString, s)
  117. if s.Empty() || s[:1][0] == '=' {
  118. return EmptyString, nil
  119. }
  120. if len(s) > 200 {
  121. return EmptyString, errors.HostTXTExceedsLimit
  122. }
  123. // RFC 6763: Spaces in key is significant, can include any character(incl. '=') in value
  124. re := regexp.MustCompile(`^([\x20-\x3C\x3E-\x7E]+)(?:(\=+)?([\x20-\x7E]+)?)$`)
  125. if rs := re.FindAllStringSubmatch(s.String(), 1); len(rs) != 0 {
  126. return String(fmt.Sprintf("%s%s%s", strings.Trim(rs[0][1], " "), rs[0][2], rs[0][3])), nil
  127. }
  128. return EmptyString, nil
  129. }
  130. func (s String) dotted() string {
  131. return s.String() + "."
  132. }
  133. func (s String) ServiceAddr(d Domain) HostString {
  134. return String(s.dotted() + d.dotted())
  135. }
  136. func (s String) InstanceAddr(sr Service, d Domain) HostString {
  137. return String(s.encodedInstance().dotted() + sr.dotted() + d.dotted())
  138. }
  139. func (s String) HostnameAddr(d Domain) HostString {
  140. return String(s.dotted() + d.dotted())
  141. }
  142. func (s String) DiscoveryAddr() HostString {
  143. return String(String("_services._dns-sd._udp").dotted() + s.dotted())
  144. }
  145. func (s String) encodedInstance() String {
  146. // RFC 6763, 4.3: Must be escaped, except leading _
  147. return String(strings.Replace(regexp.QuoteMeta(s.String()), "_", `\_`, -1))
  148. }
  149. func (s String) Types() []string {
  150. return strings.Split(s.String(), ".")
  151. }
  152. func (s String) RootType() string {
  153. t := s.Types()
  154. return t[len(t)-1]
  155. }