host.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. HostPass string `json:"hostpass" yaml:"hostpass"`
  46. Name string `json:"name" yaml:"name"`
  47. OS string `json:"os" yaml:"os"`
  48. Interface string `json:"interface" yaml:"interface"`
  49. Debug bool `json:"debug" yaml:"debug"`
  50. ListenPort int `json:"listenport" yaml:"listenport"`
  51. PublicListenPort int `json:"public_listen_port" yaml:"public_listen_port"`
  52. ProxyListenPort int `json:"proxy_listen_port" yaml:"proxy_listen_port"`
  53. MTU int `json:"mtu" yaml:"mtu"`
  54. PublicKey wgtypes.Key `json:"publickey" yaml:"publickey"`
  55. MacAddress net.HardwareAddr `json:"macaddress" yaml:"macaddress"`
  56. TrafficKeyPublic []byte `json:"traffickeypublic" yaml:"traffickeypublic"`
  57. InternetGateway net.UDPAddr `json:"internetgateway" yaml:"internetgateway"`
  58. Nodes []string `json:"nodes" yaml:"nodes"`
  59. IsRelayed bool `json:"isrelayed" yaml:"isrelayed"`
  60. RelayedBy string `json:"relayed_by" yaml:"relayed_by"`
  61. IsRelay bool `json:"isrelay" yaml:"isrelay"`
  62. RelayedHosts []string `json:"relay_hosts" yaml:"relay_hosts"`
  63. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  64. DefaultInterface string `json:"defaultinterface" yaml:"defaultinterface"`
  65. EndpointIP net.IP `json:"endpointip" yaml:"endpointip"`
  66. ProxyEnabled bool `json:"proxy_enabled" yaml:"proxy_enabled"`
  67. ProxyEnabledSet bool `json:"proxy_enabled_updated" yaml:"proxy_enabled_updated"`
  68. IsDocker bool `json:"isdocker" yaml:"isdocker"`
  69. IsK8S bool `json:"isk8s" yaml:"isk8s"`
  70. IsStatic bool `json:"isstatic" yaml:"isstatic"`
  71. IsDefault bool `json:"isdefault" yaml:"isdefault"`
  72. NatType string `json:"nat_type,omitempty" yaml:"nat_type,omitempty"`
  73. TurnEndpoint *netip.AddrPort `json:"turn_endpoint,omitempty" yaml:"turn_endpoint,omitempty"`
  74. }
  75. // FormatBool converts a boolean to a [yes|no] string
  76. func FormatBool(b bool) string {
  77. s := "no"
  78. if b {
  79. s = "yes"
  80. }
  81. return s
  82. }
  83. // ParseBool parses a [yes|no] string to boolean value
  84. func ParseBool(s string) bool {
  85. b := false
  86. if s == "yes" {
  87. b = true
  88. }
  89. return b
  90. }
  91. // HostMqAction - type for host update action
  92. type HostMqAction string
  93. const (
  94. // SignalHost - const for host signal action
  95. SignalHost = "SIGNAL_HOST"
  96. // UpdateHost - constant for host update action
  97. UpdateHost = "UPDATE_HOST"
  98. // DeleteHost - constant for host delete action
  99. DeleteHost = "DELETE_HOST"
  100. // JoinHostToNetwork - constant for host network join action
  101. JoinHostToNetwork = "JOIN_HOST_TO_NETWORK"
  102. // Acknowledgement - ACK response for hosts
  103. Acknowledgement = "ACK"
  104. // RequestAck - request an ACK
  105. RequestAck = "REQ_ACK"
  106. // CheckIn - update last check in times and public address and interfaces
  107. CheckIn = "CHECK_IN"
  108. // REGISTER_WITH_TURN - registers host with turn server if configured
  109. RegisterWithTurn = "REGISTER_WITH_TURN"
  110. // UpdateKeys - update wireguard private/public keys
  111. UpdateKeys = "UPDATE_KEYS"
  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. }
  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. }