host.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package models
  2. import (
  3. "net"
  4. "net/netip"
  5. "github.com/google/uuid"
  6. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  7. )
  8. // OS_Types - list of OS types Netmaker cares about
  9. var OS_Types = struct {
  10. Linux string
  11. Windows string
  12. Mac string
  13. FreeBSD string
  14. IoT string
  15. }{
  16. Linux: "linux",
  17. Windows: "windows",
  18. Mac: "darwin",
  19. FreeBSD: "freebsd",
  20. IoT: "iot",
  21. }
  22. // NAT_Types - the type of NAT in which a HOST currently resides (simplified)
  23. var NAT_Types = struct {
  24. Public string
  25. Symmetric string
  26. Asymmetric string
  27. Double string
  28. }{
  29. Public: "public",
  30. Symmetric: "symmetric",
  31. Asymmetric: "asymmetric",
  32. Double: "double",
  33. }
  34. // WIREGUARD_INTERFACE name of wireguard interface
  35. const WIREGUARD_INTERFACE = "netmaker"
  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. PublicListenPort int `json:"public_listen_port" yaml:"public_listen_port"`
  52. WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
  53. ProxyListenPort int `json:"proxy_listen_port" yaml:"proxy_listen_port"`
  54. MTU int `json:"mtu" yaml:"mtu"`
  55. PublicKey wgtypes.Key `json:"publickey" yaml:"publickey"`
  56. MacAddress net.HardwareAddr `json:"macaddress" yaml:"macaddress"`
  57. TrafficKeyPublic []byte `json:"traffickeypublic" yaml:"traffickeypublic"`
  58. InternetGateway net.UDPAddr `json:"internetgateway" yaml:"internetgateway"`
  59. Nodes []string `json:"nodes" yaml:"nodes"`
  60. IsRelayed bool `json:"isrelayed" yaml:"isrelayed"`
  61. RelayedBy string `json:"relayed_by" yaml:"relayed_by"`
  62. IsRelay bool `json:"isrelay" yaml:"isrelay"`
  63. RelayedHosts []string `json:"relay_hosts" yaml:"relay_hosts"`
  64. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  65. DefaultInterface string `json:"defaultinterface" yaml:"defaultinterface"`
  66. EndpointIP net.IP `json:"endpointip" yaml:"endpointip"`
  67. ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"`
  68. ProxyEnabledSet bool `json:"proxy_enabled_updated" yaml:"proxy_enabled_updated"`
  69. IsDocker bool `json:"isdocker" yaml:"isdocker"`
  70. IsK8S bool `json:"isk8s" yaml:"isk8s"`
  71. IsStatic bool `json:"isstatic" yaml:"isstatic"`
  72. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  73. NatType string `json:"nat_type,omitempty" yaml:"nat_type,omitempty"`
  74. TurnEndpoint *netip.AddrPort `json:"turn_endpoint,omitempty" yaml:"turn_endpoint,omitempty"`
  75. }
  76. // FormatBool converts a boolean to a [yes|no] string
  77. func FormatBool(b bool) string {
  78. s := "no"
  79. if b {
  80. s = "yes"
  81. }
  82. return s
  83. }
  84. // ParseBool parses a [yes|no] string to boolean value
  85. func ParseBool(s string) bool {
  86. b := false
  87. if s == "yes" {
  88. b = true
  89. }
  90. return b
  91. }
  92. // HostMqAction - type for host update action
  93. type HostMqAction string
  94. const (
  95. // SignalHost - const for host signal action
  96. SignalHost = "SIGNAL_HOST"
  97. // UpdateHost - constant for host update action
  98. UpdateHost = "UPDATE_HOST"
  99. // DeleteHost - constant for host delete action
  100. DeleteHost = "DELETE_HOST"
  101. // JoinHostToNetwork - constant for host network join action
  102. JoinHostToNetwork = "JOIN_HOST_TO_NETWORK"
  103. // Acknowledgement - ACK response for hosts
  104. Acknowledgement = "ACK"
  105. // RequestAck - request an ACK
  106. RequestAck = "REQ_ACK"
  107. // CheckIn - update last check in times and public address and interfaces
  108. CheckIn = "CHECK_IN"
  109. // REGISTER_WITH_TURN - registers host with turn server if configured
  110. RegisterWithTurn = "REGISTER_WITH_TURN"
  111. // UpdateKeys - update wireguard private/public keys
  112. UpdateKeys = "UPDATE_KEYS"
  113. )
  114. // SignalAction - turn peer signal action
  115. type SignalAction string
  116. const (
  117. // Disconnect - action to stop using turn connection
  118. Disconnect SignalAction = "DISCONNECT"
  119. // ConnNegotiation - action to negotiate connection between peers
  120. ConnNegotiation SignalAction = "CONNECTION_NEGOTIATION"
  121. )
  122. // HostUpdate - struct for host update
  123. type HostUpdate struct {
  124. Action HostMqAction
  125. Host Host
  126. Node Node
  127. Signal Signal
  128. }
  129. // HostTurnRegister - struct for host turn registration
  130. type HostTurnRegister struct {
  131. HostID string `json:"host_id"`
  132. HostPassHash string `json:"host_pass_hash"`
  133. }
  134. // Signal - struct for signalling peer
  135. type Signal struct {
  136. Server string `json:"server"`
  137. FromHostPubKey string `json:"from_host_pubkey"`
  138. TurnRelayEndpoint string `json:"turn_relay_addr"`
  139. ToHostPubKey string `json:"to_host_pubkey"`
  140. Reply bool `json:"reply"`
  141. Action SignalAction `json:"action"`
  142. }
  143. // RegisterMsg - login message struct for hosts to join via SSO login
  144. type RegisterMsg struct {
  145. RegisterHost Host `json:"host"`
  146. Network string `json:"network,omitempty"`
  147. User string `json:"user,omitempty"`
  148. Password string `json:"password,omitempty"`
  149. JoinAll bool `json:"join_all,omitempty"`
  150. }