api_node.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package models
  2. import (
  3. "net"
  4. "time"
  5. "github.com/google/uuid"
  6. "golang.org/x/exp/slog"
  7. )
  8. type ApiNodeStatus struct {
  9. ID string `json:"id"`
  10. IsStatic bool `json:"is_static"`
  11. IsUserNode bool `json:"is_user_node"`
  12. Status NodeStatus `json:"status"`
  13. }
  14. // ApiNode is a stripped down Node DTO that exposes only required fields to external systems
  15. type ApiNode struct {
  16. ID string `json:"id,omitempty" validate:"required,min=5,id_unique"`
  17. HostID string `json:"hostid,omitempty" validate:"required,min=5,id_unique"`
  18. Address string `json:"address" validate:"omitempty,cidrv4"`
  19. Address6 string `json:"address6" validate:"omitempty,cidrv6"`
  20. LocalAddress string `json:"localaddress" validate:"omitempty,cidr"`
  21. AllowedIPs []string `json:"allowedips"`
  22. LastModified int64 `json:"lastmodified" swaggertype:"primitive,integer" format:"int64"`
  23. ExpirationDateTime int64 `json:"expdatetime" swaggertype:"primitive,integer" format:"int64"`
  24. LastCheckIn int64 `json:"lastcheckin" swaggertype:"primitive,integer" format:"int64"`
  25. LastPeerUpdate int64 `json:"lastpeerupdate" swaggertype:"primitive,integer" format:"int64"`
  26. Network string `json:"network"`
  27. NetworkRange string `json:"networkrange"`
  28. NetworkRange6 string `json:"networkrange6"`
  29. IsRelayed bool `json:"isrelayed"`
  30. IsRelay bool `json:"isrelay"`
  31. IsGw bool `json:"is_gw"`
  32. IsAutoRelay bool `json:"is_auto_relay"`
  33. AutoRelayedPeers map[string]struct{} `json:"auto_relayed_peers"`
  34. AutoAssignGateway bool `json:"auto_assign_gw"`
  35. AutoRelayedBy uuid.UUID `json:"auto_relayed_by"`
  36. RelayedBy string `json:"relayedby" bson:"relayedby" yaml:"relayedby"`
  37. RelayedNodes []string `json:"relaynodes" yaml:"relayedNodes"`
  38. IsEgressGateway bool `json:"isegressgateway"`
  39. IsIngressGateway bool `json:"isingressgateway"`
  40. EgressGatewayRanges []string `json:"egressgatewayranges"`
  41. EgressGatewayNatEnabled bool `json:"egressgatewaynatenabled"`
  42. EgressGatewayRangesWithMetric []EgressRangeMetric `json:"egressgatewayranges_with_metric"`
  43. DNSOn bool `json:"dnson"`
  44. IngressDns string `json:"ingressdns"`
  45. IngressPersistentKeepalive int32 `json:"ingresspersistentkeepalive"`
  46. IngressMTU int32 `json:"ingressmtu"`
  47. Server string `json:"server"`
  48. Connected bool `json:"connected"`
  49. PendingDelete bool `json:"pendingdelete"`
  50. Metadata string `json:"metadata"`
  51. // == PRO ==
  52. DefaultACL string `json:"defaultacl,omitempty" validate:"checkyesornoorunset"`
  53. IsFailOver bool `json:"is_fail_over"`
  54. FailOverPeers map[string]struct{} `json:"fail_over_peers" yaml:"fail_over_peers"`
  55. FailedOverBy uuid.UUID `json:"failed_over_by" yaml:"failed_over_by"`
  56. IsInternetGateway bool `json:"isinternetgateway" yaml:"isinternetgateway"`
  57. InetNodeReq InetNodeReq `json:"inet_node_req" yaml:"inet_node_req"`
  58. InternetGwID string `json:"internetgw_node_id" yaml:"internetgw_node_id"`
  59. AdditionalRagIps []string `json:"additional_rag_ips" yaml:"additional_rag_ips"`
  60. Tags map[TagID]struct{} `json:"tags" yaml:"tags"`
  61. IsStatic bool `json:"is_static"`
  62. IsUserNode bool `json:"is_user_node"`
  63. StaticNode ExtClient `json:"static_node"`
  64. Status NodeStatus `json:"status"`
  65. Location string `json:"location"`
  66. }
  67. // ApiNode.ConvertToServerNode - converts an api node to a server node
  68. func (a *ApiNode) ConvertToServerNode(currentNode *Node) *Node {
  69. convertedNode := Node{}
  70. convertedNode.Network = a.Network
  71. convertedNode.Server = a.Server
  72. convertedNode.Action = currentNode.Action
  73. convertedNode.Connected = a.Connected
  74. convertedNode.ID, _ = uuid.Parse(a.ID)
  75. convertedNode.HostID, _ = uuid.Parse(a.HostID)
  76. //convertedNode.IsRelay = a.IsRelay
  77. convertedNode.IsRelayed = a.IsRelayed
  78. convertedNode.RelayedBy = a.RelayedBy
  79. convertedNode.RelayedNodes = a.RelayedNodes
  80. convertedNode.PendingDelete = a.PendingDelete
  81. convertedNode.FailedOverBy = currentNode.FailedOverBy
  82. convertedNode.FailOverPeers = currentNode.FailOverPeers
  83. //convertedNode.IsIngressGateway = a.IsIngressGateway
  84. convertedNode.IngressGatewayRange = currentNode.IngressGatewayRange
  85. convertedNode.IngressGatewayRange6 = currentNode.IngressGatewayRange6
  86. convertedNode.IngressDNS = a.IngressDns
  87. convertedNode.IngressPersistentKeepalive = a.IngressPersistentKeepalive
  88. convertedNode.IngressMTU = a.IngressMTU
  89. convertedNode.IsInternetGateway = a.IsInternetGateway
  90. convertedNode.InternetGwID = currentNode.InternetGwID
  91. convertedNode.InetNodeReq = currentNode.InetNodeReq
  92. convertedNode.RelayedNodes = a.RelayedNodes
  93. convertedNode.DefaultACL = a.DefaultACL
  94. convertedNode.OwnerID = currentNode.OwnerID
  95. _, networkRange, err := net.ParseCIDR(a.NetworkRange)
  96. if err == nil {
  97. convertedNode.NetworkRange = *networkRange
  98. }
  99. _, networkRange6, err := net.ParseCIDR(a.NetworkRange6)
  100. if err == nil {
  101. convertedNode.NetworkRange6 = *networkRange6
  102. }
  103. if len(a.LocalAddress) > 0 {
  104. _, localAddr, err := net.ParseCIDR(a.LocalAddress)
  105. if err == nil {
  106. convertedNode.LocalAddress = *localAddr
  107. }
  108. } else if !isEmptyAddr(currentNode.LocalAddress.String()) {
  109. convertedNode.LocalAddress = currentNode.LocalAddress
  110. }
  111. ip, addr, err := net.ParseCIDR(a.Address)
  112. if err == nil {
  113. convertedNode.Address = *addr
  114. convertedNode.Address.IP = ip
  115. }
  116. ip6, addr6, err := net.ParseCIDR(a.Address6)
  117. if err == nil {
  118. convertedNode.Address6 = *addr6
  119. convertedNode.Address6.IP = ip6
  120. }
  121. convertedNode.LastModified = time.Unix(a.LastModified, 0)
  122. convertedNode.LastCheckIn = time.Unix(a.LastCheckIn, 0)
  123. convertedNode.LastPeerUpdate = time.Unix(a.LastPeerUpdate, 0)
  124. convertedNode.ExpirationDateTime = time.Unix(a.ExpirationDateTime, 0)
  125. convertedNode.Metadata = a.Metadata
  126. for _, ip := range a.AdditionalRagIps {
  127. ragIp := net.ParseIP(ip)
  128. if ragIp == nil {
  129. slog.Error("error parsing additional rag ip", "error", err, "ip", ip)
  130. return nil
  131. }
  132. convertedNode.AdditionalRagIps = append(convertedNode.AdditionalRagIps, ragIp)
  133. }
  134. convertedNode.Tags = a.Tags
  135. convertedNode.IsGw = a.IsGw
  136. convertedNode.IsAutoRelay = a.IsAutoRelay
  137. if convertedNode.IsGw {
  138. convertedNode.IsRelay = true
  139. convertedNode.IsIngressGateway = true
  140. }
  141. convertedNode.AutoAssignGateway = a.AutoAssignGateway
  142. return &convertedNode
  143. }
  144. func (nm *Node) ConvertToStatusNode() *ApiNodeStatus {
  145. apiNode := ApiNodeStatus{}
  146. if nm.IsStatic {
  147. apiNode.ID = nm.StaticNode.ClientID
  148. } else {
  149. apiNode.ID = nm.ID.String()
  150. }
  151. apiNode.IsStatic = nm.IsStatic
  152. apiNode.IsUserNode = nm.IsUserNode
  153. apiNode.Status = nm.Status
  154. return &apiNode
  155. }
  156. // Node.ConvertToAPINode - converts a node to an API node
  157. func (nm *Node) ConvertToAPINode() *ApiNode {
  158. apiNode := ApiNode{}
  159. apiNode.ID = nm.ID.String()
  160. apiNode.HostID = nm.HostID.String()
  161. apiNode.Address = nm.Address.String()
  162. if isEmptyAddr(apiNode.Address) {
  163. apiNode.Address = ""
  164. }
  165. apiNode.Address6 = nm.Address6.String()
  166. if isEmptyAddr(apiNode.Address6) {
  167. apiNode.Address6 = ""
  168. }
  169. apiNode.LocalAddress = nm.LocalAddress.String()
  170. if isEmptyAddr(apiNode.LocalAddress) {
  171. apiNode.LocalAddress = ""
  172. }
  173. apiNode.LastModified = nm.LastModified.Unix()
  174. apiNode.LastCheckIn = nm.LastCheckIn.Unix()
  175. apiNode.LastPeerUpdate = nm.LastPeerUpdate.Unix()
  176. apiNode.ExpirationDateTime = nm.ExpirationDateTime.Unix()
  177. apiNode.Network = nm.Network
  178. apiNode.NetworkRange = nm.NetworkRange.String()
  179. if isEmptyAddr(apiNode.NetworkRange) {
  180. apiNode.NetworkRange = ""
  181. }
  182. apiNode.NetworkRange6 = nm.NetworkRange6.String()
  183. if isEmptyAddr(apiNode.NetworkRange6) {
  184. apiNode.NetworkRange6 = ""
  185. }
  186. apiNode.IsRelayed = nm.IsRelayed
  187. apiNode.IsRelay = nm.IsRelay
  188. apiNode.IsGw = nm.IsGw
  189. apiNode.RelayedBy = nm.RelayedBy
  190. apiNode.RelayedNodes = nm.RelayedNodes
  191. apiNode.IsAutoRelay = nm.IsAutoRelay
  192. apiNode.AutoRelayedBy = nm.AutoRelayedBy
  193. apiNode.AutoRelayedPeers = nm.AutoRelayedPeers
  194. apiNode.AutoAssignGateway = nm.AutoAssignGateway
  195. apiNode.IsIngressGateway = nm.IsIngressGateway
  196. apiNode.IngressDns = nm.IngressDNS
  197. apiNode.IngressPersistentKeepalive = nm.IngressPersistentKeepalive
  198. apiNode.IngressMTU = nm.IngressMTU
  199. apiNode.Server = nm.Server
  200. apiNode.Connected = nm.Connected
  201. apiNode.PendingDelete = nm.PendingDelete
  202. apiNode.DefaultACL = nm.DefaultACL
  203. apiNode.IsInternetGateway = nm.IsInternetGateway
  204. apiNode.InternetGwID = nm.InternetGwID
  205. apiNode.InetNodeReq = nm.InetNodeReq
  206. apiNode.IsFailOver = nm.IsFailOver
  207. apiNode.FailOverPeers = nm.FailOverPeers
  208. apiNode.FailedOverBy = nm.FailedOverBy
  209. apiNode.Metadata = nm.Metadata
  210. apiNode.AdditionalRagIps = []string{}
  211. apiNode.Tags = nm.Tags
  212. for _, ip := range nm.AdditionalRagIps {
  213. apiNode.AdditionalRagIps = append(apiNode.AdditionalRagIps, ip.String())
  214. }
  215. apiNode.IsStatic = nm.IsStatic
  216. apiNode.IsUserNode = nm.IsUserNode
  217. apiNode.StaticNode = nm.StaticNode
  218. apiNode.Status = nm.Status
  219. return &apiNode
  220. }
  221. func isEmptyAddr(addr string) bool {
  222. return addr == "<nil>" || addr == ":0"
  223. }