host.go 5.2 KB

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