host.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package models
  2. import (
  3. "net"
  4. "github.com/google/uuid"
  5. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  6. )
  7. // OS_Types - list of OS types Netmaker cares about
  8. var OS_Types = struct {
  9. Linux string
  10. Windows string
  11. Mac string
  12. FreeBSD string
  13. IoT string
  14. }{
  15. Linux: "linux",
  16. Windows: "windows",
  17. Mac: "darwin",
  18. FreeBSD: "freebsd",
  19. IoT: "iot",
  20. }
  21. // NAT_Types - the type of NAT in which a HOST currently resides (simplified)
  22. var NAT_Types = struct {
  23. Public string
  24. BehindNAT string
  25. }{
  26. Public: "public",
  27. BehindNAT: "behind_nat",
  28. }
  29. // WIREGUARD_INTERFACE name of wireguard interface
  30. const WIREGUARD_INTERFACE = "netmaker"
  31. // Host - represents a host on the network
  32. type Host struct {
  33. ID uuid.UUID `json:"id" yaml:"id"`
  34. Verbosity int `json:"verbosity" yaml:"verbosity"`
  35. FirewallInUse string `json:"firewallinuse" yaml:"firewallinuse"`
  36. Version string `json:"version" yaml:"version"`
  37. IPForwarding bool `json:"ipforwarding" yaml:"ipforwarding"`
  38. DaemonInstalled bool `json:"daemoninstalled" yaml:"daemoninstalled"`
  39. AutoUpdate bool `json:"autoupdate" yaml:"autoupdate"`
  40. HostPass string `json:"hostpass" yaml:"hostpass"`
  41. Name string `json:"name" yaml:"name"`
  42. OS string `json:"os" yaml:"os"`
  43. Interface string `json:"interface" yaml:"interface"`
  44. Debug bool `json:"debug" yaml:"debug"`
  45. ListenPort int `json:"listenport" yaml:"listenport"`
  46. WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
  47. MTU int `json:"mtu" yaml:"mtu"`
  48. PublicKey wgtypes.Key `json:"publickey" yaml:"publickey"`
  49. MacAddress net.HardwareAddr `json:"macaddress" yaml:"macaddress"`
  50. TrafficKeyPublic []byte `json:"traffickeypublic" yaml:"traffickeypublic"`
  51. InternetGateway net.UDPAddr `json:"internetgateway" yaml:"internetgateway"`
  52. Nodes []string `json:"nodes" yaml:"nodes"`
  53. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  54. DefaultInterface string `json:"defaultinterface" yaml:"defaultinterface"`
  55. EndpointIP net.IP `json:"endpointip" yaml:"endpointip"`
  56. IsDocker bool `json:"isdocker" yaml:"isdocker"`
  57. IsK8S bool `json:"isk8s" yaml:"isk8s"`
  58. IsStatic bool `json:"isstatic" yaml:"isstatic"`
  59. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  60. NatType string `json:"nat_type,omitempty" yaml:"nat_type,omitempty"`
  61. }
  62. // Client - represents a client on the network
  63. type Client struct {
  64. Host Host `json:"host" yaml:"host"`
  65. Node Node `json:"node" yaml:"node"`
  66. }
  67. // FormatBool converts a boolean to a [yes|no] string
  68. func FormatBool(b bool) string {
  69. s := "no"
  70. if b {
  71. s = "yes"
  72. }
  73. return s
  74. }
  75. // ParseBool parses a [yes|no] string to boolean value
  76. func ParseBool(s string) bool {
  77. b := false
  78. if s == "yes" {
  79. b = true
  80. }
  81. return b
  82. }
  83. // HostMqAction - type for host update action
  84. type HostMqAction string
  85. const (
  86. // SignalHost - const for host signal action
  87. SignalHost = "SIGNAL_HOST"
  88. // UpdateHost - constant for host update action
  89. UpdateHost = "UPDATE_HOST"
  90. // DeleteHost - constant for host delete action
  91. DeleteHost = "DELETE_HOST"
  92. // JoinHostToNetwork - constant for host network join action
  93. JoinHostToNetwork = "JOIN_HOST_TO_NETWORK"
  94. // Acknowledgement - ACK response for hosts
  95. Acknowledgement = "ACK"
  96. // RequestAck - request an ACK
  97. RequestAck = "REQ_ACK"
  98. // CheckIn - update last check in times and public address and interfaces
  99. CheckIn = "CHECK_IN"
  100. // REGISTER_WITH_TURN - registers host with turn server if configured
  101. RegisterWithTurn = "REGISTER_WITH_TURN"
  102. // UpdateKeys - update wireguard private/public keys
  103. UpdateKeys = "UPDATE_KEYS"
  104. )
  105. // SignalAction - turn peer signal action
  106. type SignalAction string
  107. const (
  108. // Disconnect - action to stop using turn connection
  109. Disconnect SignalAction = "DISCONNECT"
  110. // ConnNegotiation - action to negotiate connection between peers
  111. ConnNegotiation SignalAction = "CONNECTION_NEGOTIATION"
  112. )
  113. // HostUpdate - struct for host update
  114. type HostUpdate struct {
  115. Action HostMqAction
  116. Host Host
  117. Node Node
  118. Signal Signal
  119. }
  120. // HostTurnRegister - struct for host turn registration
  121. type HostTurnRegister struct {
  122. HostID string `json:"host_id"`
  123. HostPassHash string `json:"host_pass_hash"`
  124. }
  125. // Signal - struct for signalling peer
  126. type Signal struct {
  127. Server string `json:"server"`
  128. FromHostPubKey string `json:"from_host_pubkey"`
  129. TurnRelayEndpoint string `json:"turn_relay_addr"`
  130. ToHostPubKey string `json:"to_host_pubkey"`
  131. Reply bool `json:"reply"`
  132. Action SignalAction `json:"action"`
  133. }
  134. // RegisterMsg - login message struct for hosts to join via SSO login
  135. type RegisterMsg struct {
  136. RegisterHost Host `json:"host"`
  137. Network string `json:"network,omitempty"`
  138. User string `json:"user,omitempty"`
  139. Password string `json:"password,omitempty"`
  140. JoinAll bool `json:"join_all,omitempty"`
  141. }