2
0

host.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. IsStatic bool `json:"isstatic" yaml:"isstatic"`
  64. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  65. NatType string `json:"nat_type,omitempty" yaml:"nat_type,omitempty"`
  66. TurnEndpoint *netip.AddrPort `json:"turn_endpoint,omitempty" yaml:"turn_endpoint,omitempty"`
  67. PersistentKeepalive time.Duration `json:"persistentkeepalive" yaml:"persistentkeepalive"`
  68. }
  69. // FormatBool converts a boolean to a [yes|no] string
  70. func FormatBool(b bool) string {
  71. s := "no"
  72. if b {
  73. s = "yes"
  74. }
  75. return s
  76. }
  77. // ParseBool parses a [yes|no] string to boolean value
  78. func ParseBool(s string) bool {
  79. b := false
  80. if s == "yes" {
  81. b = true
  82. }
  83. return b
  84. }
  85. // HostMqAction - type for host update action
  86. type HostMqAction string
  87. const (
  88. // Upgrade - const to request host to update it's client
  89. Upgrade HostMqAction = "UPGRADE"
  90. // SignalHost - const for host signal action
  91. SignalHost HostMqAction = "SIGNAL_HOST"
  92. // UpdateHost - constant for host update action
  93. UpdateHost HostMqAction = "UPDATE_HOST"
  94. // DeleteHost - constant for host delete action
  95. DeleteHost HostMqAction = "DELETE_HOST"
  96. // JoinHostToNetwork - constant for host network join action
  97. JoinHostToNetwork HostMqAction = "JOIN_HOST_TO_NETWORK"
  98. // Acknowledgement - ACK response for hosts
  99. Acknowledgement HostMqAction = "ACK"
  100. // RequestAck - request an ACK
  101. RequestAck HostMqAction = "REQ_ACK"
  102. // CheckIn - update last check in times and public address and interfaces
  103. CheckIn HostMqAction = "CHECK_IN"
  104. // UpdateKeys - update wireguard private/public keys
  105. UpdateKeys HostMqAction = "UPDATE_KEYS"
  106. // RequestPull - request a pull from a host
  107. RequestPull HostMqAction = "REQ_PULL"
  108. // UpdateMetrics - updates metrics data
  109. UpdateMetrics HostMqAction = "UPDATE_METRICS"
  110. )
  111. // SignalAction - turn peer signal action
  112. type SignalAction string
  113. const (
  114. // ConnNegotiation - action to negotiate connection between peers
  115. ConnNegotiation SignalAction = "CONNECTION_NEGOTIATION"
  116. // RelayME - action to relay the peer
  117. RelayME SignalAction = "RELAY_ME"
  118. )
  119. // HostUpdate - struct for host update
  120. type HostUpdate struct {
  121. Action HostMqAction
  122. Host Host
  123. Node Node
  124. Signal Signal
  125. NewMetrics Metrics
  126. }
  127. // HostTurnRegister - struct for host turn registration
  128. type HostTurnRegister struct {
  129. HostID string `json:"host_id"`
  130. HostPassHash string `json:"host_pass_hash"`
  131. }
  132. // Signal - struct for signalling peer
  133. type Signal struct {
  134. Server string `json:"server"`
  135. FromHostPubKey string `json:"from_host_pubkey"`
  136. ToHostPubKey string `json:"to_host_pubkey"`
  137. FromHostID string `json:"from_host_id"`
  138. ToHostID string `json:"to_host_id"`
  139. FromNodeID string `json:"from_node_id"`
  140. ToNodeID string `json:"to_node_id"`
  141. Reply bool `json:"reply"`
  142. Action SignalAction `json:"action"`
  143. IsPro bool `json:"is_pro"`
  144. TimeStamp int64 `json:"timestamp"`
  145. }
  146. // RegisterMsg - login message struct for hosts to join via SSO login
  147. type RegisterMsg struct {
  148. RegisterHost Host `json:"host"`
  149. Network string `json:"network,omitempty"`
  150. User string `json:"user,omitempty"`
  151. Password string `json:"password,omitempty"`
  152. JoinAll bool `json:"join_all,omitempty"`
  153. Relay string `json:"relay,omitempty"`
  154. }