host.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. EndpointDetection bool `json:"endpointdetection" yaml:"endpointdetection"`
  46. HostPass string `json:"hostpass" yaml:"hostpass"`
  47. Name string `json:"name" yaml:"name"`
  48. OS string `json:"os" yaml:"os"`
  49. Interface string `json:"interface" yaml:"interface"`
  50. Debug bool `json:"debug" yaml:"debug"`
  51. ListenPort int `json:"listenport" yaml:"listenport"`
  52. PublicListenPort int `json:"public_listen_port" yaml:"public_listen_port"`
  53. WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
  54. ProxyListenPort int `json:"proxy_listen_port" yaml:"proxy_listen_port"`
  55. MTU int `json:"mtu" yaml:"mtu"`
  56. PublicKey wgtypes.Key `json:"publickey" yaml:"publickey"`
  57. MacAddress net.HardwareAddr `json:"macaddress" yaml:"macaddress"`
  58. TrafficKeyPublic []byte `json:"traffickeypublic" yaml:"traffickeypublic"`
  59. InternetGateway net.UDPAddr `json:"internetgateway" yaml:"internetgateway"`
  60. Nodes []string `json:"nodes" yaml:"nodes"`
  61. IsRelayed bool `json:"isrelayed" yaml:"isrelayed"`
  62. RelayedBy string `json:"relayed_by" yaml:"relayed_by"`
  63. IsRelay bool `json:"isrelay" yaml:"isrelay"`
  64. RelayedHosts []string `json:"relay_hosts" yaml:"relay_hosts"`
  65. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  66. DefaultInterface string `json:"defaultinterface" yaml:"defaultinterface"`
  67. EndpointIP net.IP `json:"endpointip" yaml:"endpointip"`
  68. ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"`
  69. ProxyEnabledSet bool `json:"proxy_enabled_updated" yaml:"proxy_enabled_updated"`
  70. IsDocker bool `json:"isdocker" yaml:"isdocker"`
  71. IsK8S bool `json:"isk8s" yaml:"isk8s"`
  72. IsStatic bool `json:"isstatic" yaml:"isstatic"`
  73. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  74. NatType string `json:"nat_type,omitempty" yaml:"nat_type,omitempty"`
  75. TurnEndpoint *netip.AddrPort `json:"turn_endpoint,omitempty" yaml:"turn_endpoint,omitempty"`
  76. }
  77. // FormatBool converts a boolean to a [yes|no] string
  78. func FormatBool(b bool) string {
  79. s := "no"
  80. if b {
  81. s = "yes"
  82. }
  83. return s
  84. }
  85. // ParseBool parses a [yes|no] string to boolean value
  86. func ParseBool(s string) bool {
  87. b := false
  88. if s == "yes" {
  89. b = true
  90. }
  91. return b
  92. }
  93. // HostMqAction - type for host update action
  94. type HostMqAction string
  95. const (
  96. // SignalHost - const for host signal action
  97. SignalHost = "SIGNAL_HOST"
  98. // UpdateHost - constant for host update action
  99. UpdateHost = "UPDATE_HOST"
  100. // DeleteHost - constant for host delete action
  101. DeleteHost = "DELETE_HOST"
  102. // JoinHostToNetwork - constant for host network join action
  103. JoinHostToNetwork = "JOIN_HOST_TO_NETWORK"
  104. // Acknowledgement - ACK response for hosts
  105. Acknowledgement = "ACK"
  106. // RequestAck - request an ACK
  107. RequestAck = "REQ_ACK"
  108. // CheckIn - update last check in times and public address and interfaces
  109. CheckIn = "CHECK_IN"
  110. // REGISTER_WITH_TURN - registers host with turn server if configured
  111. RegisterWithTurn = "REGISTER_WITH_TURN"
  112. // UpdateKeys - update wireguard private/public keys
  113. UpdateKeys = "UPDATE_KEYS"
  114. )
  115. // SignalAction - turn peer signal action
  116. type SignalAction string
  117. const (
  118. // Disconnect - action to stop using turn connection
  119. Disconnect SignalAction = "DISCONNECT"
  120. // ConnNegotiation - action to negotiate connection between peers
  121. ConnNegotiation SignalAction = "CONNECTION_NEGOTIATION"
  122. )
  123. // HostUpdate - struct for host update
  124. type HostUpdate struct {
  125. Action HostMqAction
  126. Host Host
  127. Node Node
  128. Signal Signal
  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. TurnRelayEndpoint string `json:"turn_relay_addr"`
  140. ToHostPubKey string `json:"to_host_pubkey"`
  141. Reply bool `json:"reply"`
  142. Action SignalAction `json:"action"`
  143. }
  144. // RegisterMsg - login message struct for hosts to join via SSO login
  145. type RegisterMsg struct {
  146. RegisterHost Host `json:"host"`
  147. Network string `json:"network,omitempty"`
  148. User string `json:"user,omitempty"`
  149. Password string `json:"password,omitempty"`
  150. JoinAll bool `json:"join_all,omitempty"`
  151. }