host.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  62. DefaultInterface string `json:"defaultinterface" yaml:"defaultinterface"`
  63. EndpointIP net.IP `json:"endpointip" yaml:"endpointip"`
  64. ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"`
  65. ProxyEnabledSet bool `json:"proxy_enabled_updated" yaml:"proxy_enabled_updated"`
  66. IsDocker bool `json:"isdocker" yaml:"isdocker"`
  67. IsK8S bool `json:"isk8s" yaml:"isk8s"`
  68. IsStatic bool `json:"isstatic" yaml:"isstatic"`
  69. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  70. NatType string `json:"nat_type,omitempty" yaml:"nat_type,omitempty"`
  71. TurnEndpoint *netip.AddrPort `json:"turn_endpoint,omitempty" yaml:"turn_endpoint,omitempty"`
  72. }
  73. // Client - represents a client on the network
  74. type Client struct {
  75. Host Host `json:"host" yaml:"host"`
  76. Node Node `json:"node" yaml:"node"`
  77. }
  78. // FormatBool converts a boolean to a [yes|no] string
  79. func FormatBool(b bool) string {
  80. s := "no"
  81. if b {
  82. s = "yes"
  83. }
  84. return s
  85. }
  86. // ParseBool parses a [yes|no] string to boolean value
  87. func ParseBool(s string) bool {
  88. b := false
  89. if s == "yes" {
  90. b = true
  91. }
  92. return b
  93. }
  94. // HostMqAction - type for host update action
  95. type HostMqAction string
  96. const (
  97. // SignalHost - const for host signal action
  98. SignalHost = "SIGNAL_HOST"
  99. // UpdateHost - constant for host update action
  100. UpdateHost = "UPDATE_HOST"
  101. // DeleteHost - constant for host delete action
  102. DeleteHost = "DELETE_HOST"
  103. // JoinHostToNetwork - constant for host network join action
  104. JoinHostToNetwork = "JOIN_HOST_TO_NETWORK"
  105. // Acknowledgement - ACK response for hosts
  106. Acknowledgement = "ACK"
  107. // RequestAck - request an ACK
  108. RequestAck = "REQ_ACK"
  109. // CheckIn - update last check in times and public address and interfaces
  110. CheckIn = "CHECK_IN"
  111. // REGISTER_WITH_TURN - registers host with turn server if configured
  112. RegisterWithTurn = "REGISTER_WITH_TURN"
  113. // UpdateKeys - update wireguard private/public keys
  114. UpdateKeys = "UPDATE_KEYS"
  115. )
  116. // SignalAction - turn peer signal action
  117. type SignalAction string
  118. const (
  119. // Disconnect - action to stop using turn connection
  120. Disconnect SignalAction = "DISCONNECT"
  121. // ConnNegotiation - action to negotiate connection between peers
  122. ConnNegotiation SignalAction = "CONNECTION_NEGOTIATION"
  123. )
  124. // HostUpdate - struct for host update
  125. type HostUpdate struct {
  126. Action HostMqAction
  127. Host Host
  128. Node Node
  129. Signal Signal
  130. }
  131. // HostTurnRegister - struct for host turn registration
  132. type HostTurnRegister struct {
  133. HostID string `json:"host_id"`
  134. HostPassHash string `json:"host_pass_hash"`
  135. }
  136. // Signal - struct for signalling peer
  137. type Signal struct {
  138. Server string `json:"server"`
  139. FromHostPubKey string `json:"from_host_pubkey"`
  140. TurnRelayEndpoint string `json:"turn_relay_addr"`
  141. ToHostPubKey string `json:"to_host_pubkey"`
  142. Reply bool `json:"reply"`
  143. Action SignalAction `json:"action"`
  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. }