host.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package models
  2. import (
  3. "net"
  4. "net/netip"
  5. "time"
  6. "github.com/google/uuid"
  7. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  8. )
  9. // OS_Types - list of OS types Netmaker cares about
  10. var OS_Types = struct {
  11. Linux string
  12. Windows string
  13. Mac string
  14. FreeBSD string
  15. IoT string
  16. }{
  17. Linux: "linux",
  18. Windows: "windows",
  19. Mac: "darwin",
  20. FreeBSD: "freebsd",
  21. IoT: "iot",
  22. }
  23. // NAT_Types - the type of NAT in which a HOST currently resides (simplified)
  24. var NAT_Types = struct {
  25. Public string
  26. BehindNAT string
  27. }{
  28. Public: "public",
  29. BehindNAT: "behind_nat",
  30. }
  31. // WIREGUARD_INTERFACE name of wireguard interface
  32. const (
  33. WIREGUARD_INTERFACE = "netmaker"
  34. DefaultPersistentKeepAlive = 20 * time.Second
  35. )
  36. // Host - represents a host on the network
  37. type Host struct {
  38. ID uuid.UUID `json:"id" yaml:"id"`
  39. Verbosity int `json:"verbosity" yaml:"verbosity"`
  40. FirewallInUse string `json:"firewallinuse" yaml:"firewallinuse"`
  41. Version string `json:"version" yaml:"version"`
  42. IPForwarding bool `json:"ipforwarding" yaml:"ipforwarding"`
  43. DaemonInstalled bool `json:"daemoninstalled" yaml:"daemoninstalled"`
  44. AutoUpdate bool `json:"autoupdate" yaml:"autoupdate"`
  45. HostPass string `json:"hostpass" yaml:"hostpass"`
  46. Name string `json:"name" yaml:"name"`
  47. OS string `json:"os" yaml:"os"`
  48. Interface string `json:"interface" yaml:"interface"`
  49. Debug bool `json:"debug" yaml:"debug"`
  50. ListenPort int `json:"listenport" yaml:"listenport"`
  51. WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
  52. MTU int `json:"mtu" yaml:"mtu"`
  53. PublicKey wgtypes.Key `json:"publickey" yaml:"publickey"`
  54. MacAddress net.HardwareAddr `json:"macaddress" yaml:"macaddress"`
  55. TrafficKeyPublic []byte `json:"traffickeypublic" yaml:"traffickeypublic"`
  56. Nodes []string `json:"nodes" yaml:"nodes"`
  57. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  58. DefaultInterface string `json:"defaultinterface" yaml:"defaultinterface"`
  59. EndpointIP net.IP `json:"endpointip" yaml:"endpointip"`
  60. EndpointIPv6 net.IP `json:"endpointipv6" yaml:"endpointipv6"`
  61. IsDocker bool `json:"isdocker" yaml:"isdocker"`
  62. IsK8S bool `json:"isk8s" yaml:"isk8s"`
  63. IsStaticPort bool `json:"isstaticport" yaml:"isstaticport"`
  64. IsStatic bool `json:"isstatic" yaml:"isstatic"`
  65. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  66. NatType string `json:"nat_type,omitempty" yaml:"nat_type,omitempty"`
  67. TurnEndpoint *netip.AddrPort `json:"turn_endpoint,omitempty" yaml:"turn_endpoint,omitempty"`
  68. PersistentKeepalive time.Duration `json:"persistentkeepalive" yaml:"persistentkeepalive"`
  69. }
  70. // FormatBool converts a boolean to a [yes|no] string
  71. func FormatBool(b bool) string {
  72. s := "no"
  73. if b {
  74. s = "yes"
  75. }
  76. return s
  77. }
  78. // ParseBool parses a [yes|no] string to boolean value
  79. func ParseBool(s string) bool {
  80. b := false
  81. if s == "yes" {
  82. b = true
  83. }
  84. return b
  85. }
  86. // HostMqAction - type for host update action
  87. type HostMqAction string
  88. const (
  89. // Upgrade - const to request host to update it's client
  90. Upgrade HostMqAction = "UPGRADE"
  91. // SignalHost - const for host signal action
  92. SignalHost HostMqAction = "SIGNAL_HOST"
  93. // UpdateHost - constant for host update action
  94. UpdateHost HostMqAction = "UPDATE_HOST"
  95. // DeleteHost - constant for host delete action
  96. DeleteHost HostMqAction = "DELETE_HOST"
  97. // JoinHostToNetwork - constant for host network join action
  98. JoinHostToNetwork HostMqAction = "JOIN_HOST_TO_NETWORK"
  99. // Acknowledgement - ACK response for hosts
  100. Acknowledgement HostMqAction = "ACK"
  101. // RequestAck - request an ACK
  102. RequestAck HostMqAction = "REQ_ACK"
  103. // CheckIn - update last check in times and public address and interfaces
  104. CheckIn HostMqAction = "CHECK_IN"
  105. // UpdateKeys - update wireguard private/public keys
  106. UpdateKeys HostMqAction = "UPDATE_KEYS"
  107. // RequestPull - request a pull from a host
  108. RequestPull HostMqAction = "REQ_PULL"
  109. // SignalPull - request a pull from a host without restart
  110. SignalPull HostMqAction = "SIGNAL_PULL"
  111. // UpdateMetrics - updates metrics data
  112. UpdateMetrics HostMqAction = "UPDATE_METRICS"
  113. )
  114. // SignalAction - turn peer signal action
  115. type SignalAction string
  116. const (
  117. // ConnNegotiation - action to negotiate connection between peers
  118. ConnNegotiation SignalAction = "CONNECTION_NEGOTIATION"
  119. // RelayME - action to relay the peer
  120. RelayME SignalAction = "RELAY_ME"
  121. )
  122. // HostUpdate - struct for host update
  123. type HostUpdate struct {
  124. Action HostMqAction
  125. Host Host
  126. Node Node
  127. Signal Signal
  128. NewMetrics Metrics
  129. }
  130. // HostTurnRegister - struct for host turn registration
  131. type HostTurnRegister struct {
  132. HostID string `json:"host_id"`
  133. HostPassHash string `json:"host_pass_hash"`
  134. }
  135. // Signal - struct for signalling peer
  136. type Signal struct {
  137. Server string `json:"server"`
  138. FromHostPubKey string `json:"from_host_pubkey"`
  139. ToHostPubKey string `json:"to_host_pubkey"`
  140. FromHostID string `json:"from_host_id"`
  141. ToHostID string `json:"to_host_id"`
  142. FromNodeID string `json:"from_node_id"`
  143. ToNodeID string `json:"to_node_id"`
  144. Reply bool `json:"reply"`
  145. Action SignalAction `json:"action"`
  146. IsPro bool `json:"is_pro"`
  147. TimeStamp int64 `json:"timestamp"`
  148. }
  149. // RegisterMsg - login message struct for hosts to join via SSO login
  150. type RegisterMsg struct {
  151. RegisterHost Host `json:"host"`
  152. Network string `json:"network,omitempty"`
  153. User string `json:"user,omitempty"`
  154. Password string `json:"password,omitempty"`
  155. JoinAll bool `json:"join_all,omitempty"`
  156. Relay string `json:"relay,omitempty"`
  157. }