host.go 6.2 KB

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