host.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package host
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "git.giaever.org/joachimmg/go-log.git/log"
  7. "git.giaever.org/joachimmg/m-dns/errors"
  8. )
  9. type Host interface {
  10. GetInstance() Instance
  11. GetService() Service
  12. GetDomain() Domain
  13. GetHostname() Hostname
  14. GetIPs() []HostIP
  15. GetPort() HostPort
  16. GetTXTs() []string
  17. GetServiceAddr() HostString
  18. GetInstanceAddr() HostString
  19. GetHostnameAddr() HostString
  20. GetDiscoveryAddr() HostString
  21. String() string
  22. }
  23. type host struct {
  24. instance Instance
  25. service Service
  26. domain Domain
  27. hostname Hostname
  28. ip []HostIP
  29. port HostPort
  30. txt []TXT
  31. }
  32. func New(instance, service, domain, hostname string, ip []net.IP, port int, txt []string) (*host, error) {
  33. h := new(host)
  34. if h == nil {
  35. log.Traceln(errors.Host, errors.OutOfMemory)
  36. return nil, errors.OutOfMemory
  37. }
  38. var err error
  39. if h.instance, err = String(instance).isInstanceVariable(); err != nil {
  40. return nil, err
  41. }
  42. if h.service, err = String(service).isServiceVariable(); err != nil {
  43. return nil, err
  44. }
  45. if h.domain, err = String(domain).isDomainVariable(); err != nil {
  46. return nil, err
  47. }
  48. if h.hostname, err = String(hostname).isHostnameVariable(); err != nil {
  49. return nil, err
  50. }
  51. for _, _ip := range ip {
  52. if hip, err := IP(_ip).validForHostname(h.hostname); err == nil {
  53. h.ip = append(h.ip, hip)
  54. }
  55. }
  56. if len(h.ip) == 0 {
  57. addrs, err := net.InterfaceAddrs()
  58. if err != nil {
  59. log.Traceln(errors.Host, err)
  60. return nil, err
  61. }
  62. for _, addr := range addrs {
  63. ip := strings.SplitN(addr.String(), "/", 2)
  64. if len(ip) >= 1 {
  65. if hip, err := IP(net.ParseIP(ip[0])).validForHostname(h.GetHostname()); err == nil {
  66. h.ip = append(h.ip, hip)
  67. }
  68. }
  69. }
  70. }
  71. if err := Port(port).isValid(); err != nil {
  72. return nil, err
  73. }
  74. h.port = Port(port)
  75. for _, t := range txt {
  76. ht, err := String(t).isTxtVariable()
  77. if err != nil {
  78. return nil, err
  79. } else if len(ht) != 0 {
  80. h.txt = append(h.txt, ht)
  81. }
  82. }
  83. return h, nil
  84. }
  85. func (h *host) GetInstance() Instance {
  86. return h.instance
  87. }
  88. func (h *host) GetService() Service {
  89. return h.service
  90. }
  91. func (h *host) GetDomain() Domain {
  92. return h.domain
  93. }
  94. func (h *host) GetHostname() Hostname {
  95. return h.hostname
  96. }
  97. func (h *host) GetIPs() []HostIP {
  98. return h.ip
  99. }
  100. func (h *host) HasIPs() bool {
  101. if len(h.GetIPs()) == 0 {
  102. return false
  103. }
  104. for _, ip := range h.GetIPs() {
  105. if _, t := ip.Type(); t == IPv4 || t == IPv6 {
  106. return true
  107. }
  108. }
  109. return false
  110. }
  111. func (h *host) GetPort() HostPort {
  112. return h.port
  113. }
  114. func (h *host) GetTXTs() []string {
  115. s := []string{}
  116. for _, t := range h.txt {
  117. s = append(s, t.String())
  118. }
  119. return s
  120. }
  121. func (h *host) GetServiceAddr() HostString {
  122. return String(fmt.Sprintf("%s.%s.", h.GetService(), h.GetDomain()))
  123. }
  124. func (h *host) GetInstanceAddr() HostString {
  125. return String(fmt.Sprintf("%s.%s", h.GetInstance().EncodedInstance(), h.GetServiceAddr()))
  126. }
  127. func (h *host) GetHostnameAddr() HostString {
  128. return String(fmt.Sprintf("%s.%s.", h.GetHostname(), h.GetDomain()))
  129. }
  130. func (h *host) GetDiscoveryAddr() HostString {
  131. return String(fmt.Sprintf("_services._dns-sd._udp.%s.", h.GetDomain()))
  132. }
  133. func (h *host) String() string {
  134. ips := []string{}
  135. for _, ip := range h.GetIPs() {
  136. ips = append(ips, ip.String())
  137. }
  138. return fmt.Sprintf(`
  139. Host: %s
  140. + Instance: %s
  141. + Service: %s
  142. + Domain: %s
  143. + IP(s):
  144. * %s
  145. + Port: %d
  146. + Txt(s):
  147. * %s
  148. Addr:
  149. + Instance: %s
  150. + Service: %s
  151. + Discovery: %s
  152. Service-protocol: %s`,
  153. h.GetHostname(),
  154. h.GetInstance(),
  155. h.GetService(),
  156. h.GetDomain(),
  157. strings.Join(ips, "\n\t\t* "),
  158. h.port,
  159. strings.Join(h.GetTXTs(), "\n\t\t* "),
  160. h.GetInstanceAddr(),
  161. h.GetServiceAddr(),
  162. h.GetDiscoveryAddr(),
  163. h.GetService().RootType()[1:],
  164. )
  165. }