host.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. BehindNAT string
  26. }{
  27. Public: "public",
  28. BehindNAT: "behind_nat",
  29. }
  30. // WIREGUARD_INTERFACE name of wireguard interface
  31. const WIREGUARD_INTERFACE = "netmaker"
  32. // Host - represents a host on the network
  33. type Host struct {
  34. ID uuid.UUID `json:"id" yaml:"id"`
  35. Verbosity int `json:"verbosity" yaml:"verbosity"`
  36. FirewallInUse string `json:"firewallinuse" yaml:"firewallinuse"`
  37. Version string `json:"version" yaml:"version"`
  38. IPForwarding bool `json:"ipforwarding" yaml:"ipforwarding"`
  39. DaemonInstalled bool `json:"daemoninstalled" yaml:"daemoninstalled"`
  40. AutoUpdate bool `json:"autoupdate" yaml:"autoupdate"`
  41. HostPass string `json:"hostpass" yaml:"hostpass"`
  42. Name string `json:"name" yaml:"name"`
  43. OS string `json:"os" yaml:"os"`
  44. Interface string `json:"interface" yaml:"interface"`
  45. Debug bool `json:"debug" yaml:"debug"`
  46. ListenPort int `json:"listenport" yaml:"listenport"`
  47. WgPublicListenPort int `json:"wg_public_listen_port" yaml:"wg_public_listen_port"`
  48. MTU int `json:"mtu" yaml:"mtu"`
  49. PublicKey wgtypes.Key `json:"publickey" yaml:"publickey"`
  50. MacAddress net.HardwareAddr `json:"macaddress" yaml:"macaddress"`
  51. TrafficKeyPublic []byte `json:"traffickeypublic" yaml:"traffickeypublic"`
  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. TurnEndpoint *netip.AddrPort `json:"turn_endpoint,omitempty" yaml:"turn_endpoint,omitempty"`
  62. }
  63. // FormatBool converts a boolean to a [yes|no] string
  64. func FormatBool(b bool) string {
  65. s := "no"
  66. if b {
  67. s = "yes"
  68. }
  69. return s
  70. }
  71. // ParseBool parses a [yes|no] string to boolean value
  72. func ParseBool(s string) bool {
  73. b := false
  74. if s == "yes" {
  75. b = true
  76. }
  77. return b
  78. }
  79. // HostMqAction - type for host update action
  80. type HostMqAction string
  81. const (
  82. // SignalHost - const for host signal action
  83. SignalHost = "SIGNAL_HOST"
  84. // UpdateHost - constant for host update action
  85. UpdateHost = "UPDATE_HOST"
  86. // DeleteHost - constant for host delete action
  87. DeleteHost = "DELETE_HOST"
  88. // JoinHostToNetwork - constant for host network join action
  89. JoinHostToNetwork = "JOIN_HOST_TO_NETWORK"
  90. // Acknowledgement - ACK response for hosts
  91. Acknowledgement = "ACK"
  92. // RequestAck - request an ACK
  93. RequestAck = "REQ_ACK"
  94. // CheckIn - update last check in times and public address and interfaces
  95. CheckIn = "CHECK_IN"
  96. // REGISTER_WITH_TURN - registers host with turn server if configured
  97. RegisterWithTurn = "REGISTER_WITH_TURN"
  98. // UpdateKeys - update wireguard private/public keys
  99. UpdateKeys = "UPDATE_KEYS"
  100. // RequestPull - request a pull from a host
  101. RequestPull = "REQ_PULL"
  102. )
  103. // SignalAction - turn peer signal action
  104. type SignalAction string
  105. const (
  106. // Disconnect - action to stop using turn connection
  107. Disconnect SignalAction = "DISCONNECT"
  108. // ConnNegotiation - action to negotiate connection between peers
  109. ConnNegotiation SignalAction = "CONNECTION_NEGOTIATION"
  110. )
  111. // HostUpdate - struct for host update
  112. type HostUpdate struct {
  113. Action HostMqAction
  114. Host Host
  115. Node Node
  116. Signal Signal
  117. }
  118. // HostTurnRegister - struct for host turn registration
  119. type HostTurnRegister struct {
  120. HostID string `json:"host_id"`
  121. HostPassHash string `json:"host_pass_hash"`
  122. }
  123. // Signal - struct for signalling peer
  124. type Signal struct {
  125. Server string `json:"server"`
  126. FromHostPubKey string `json:"from_host_pubkey"`
  127. TurnRelayEndpoint string `json:"turn_relay_addr"`
  128. ToHostPubKey string `json:"to_host_pubkey"`
  129. Reply bool `json:"reply"`
  130. Action SignalAction `json:"action"`
  131. TimeStamp int64 `json:"timestamp"`
  132. }
  133. // RegisterMsg - login message struct for hosts to join via SSO login
  134. type RegisterMsg struct {
  135. RegisterHost Host `json:"host"`
  136. Network string `json:"network,omitempty"`
  137. User string `json:"user,omitempty"`
  138. Password string `json:"password,omitempty"`
  139. JoinAll bool `json:"join_all,omitempty"`
  140. }