api_node.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. RelayedBy string `json:"relayedby" bson:"relayedby" yaml:"relayedby"`
  32. RelayedNodes []string `json:"relaynodes" yaml:"relayedNodes"`
  33. IsEgressGateway bool `json:"isegressgateway"`
  34. IsIngressGateway bool `json:"isingressgateway"`
  35. EgressGatewayRanges []string `json:"egressgatewayranges"`
  36. EgressGatewayNatEnabled bool `json:"egressgatewaynatenabled"`
  37. EgressGatewayRangesWithMetric []EgressRangeMetric `json:"egressgatewayranges_with_metric"`
  38. DNSOn bool `json:"dnson"`
  39. IngressDns string `json:"ingressdns"`
  40. IngressPersistentKeepalive int32 `json:"ingresspersistentkeepalive"`
  41. IngressMTU int32 `json:"ingressmtu"`
  42. Server string `json:"server"`
  43. Connected bool `json:"connected"`
  44. PendingDelete bool `json:"pendingdelete"`
  45. Metadata string `json:"metadata"`
  46. // == PRO ==
  47. DefaultACL string `json:"defaultacl,omitempty" validate:"checkyesornoorunset"`
  48. IsFailOver bool `json:"is_fail_over"`
  49. FailOverPeers map[string]struct{} `json:"fail_over_peers" yaml:"fail_over_peers"`
  50. FailedOverBy uuid.UUID `json:"failed_over_by" yaml:"failed_over_by"`
  51. IsInternetGateway bool `json:"isinternetgateway" yaml:"isinternetgateway"`
  52. InetNodeReq InetNodeReq `json:"inet_node_req" yaml:"inet_node_req"`
  53. InternetGwID string `json:"internetgw_node_id" yaml:"internetgw_node_id"`
  54. AdditionalRagIps []string `json:"additional_rag_ips" yaml:"additional_rag_ips"`
  55. Tags map[TagID]struct{} `json:"tags" yaml:"tags"`
  56. IsStatic bool `json:"is_static"`
  57. IsUserNode bool `json:"is_user_node"`
  58. StaticNode ExtClient `json:"static_node"`
  59. Status NodeStatus `json:"status"`
  60. }
  61. // ApiNode.ConvertToServerNode - converts an api node to a server node
  62. func (a *ApiNode) ConvertToServerNode(currentNode *Node) *Node {
  63. convertedNode := Node{}
  64. convertedNode.Network = a.Network
  65. convertedNode.Server = a.Server
  66. convertedNode.Action = currentNode.Action
  67. convertedNode.Connected = a.Connected
  68. convertedNode.ID, _ = uuid.Parse(a.ID)
  69. convertedNode.HostID, _ = uuid.Parse(a.HostID)
  70. convertedNode.IsRelay = a.IsRelay
  71. convertedNode.IsRelayed = a.IsRelayed
  72. convertedNode.RelayedBy = a.RelayedBy
  73. convertedNode.RelayedNodes = a.RelayedNodes
  74. convertedNode.PendingDelete = a.PendingDelete
  75. convertedNode.FailedOverBy = currentNode.FailedOverBy
  76. convertedNode.FailOverPeers = currentNode.FailOverPeers
  77. convertedNode.IsIngressGateway = a.IsIngressGateway
  78. convertedNode.IngressGatewayRange = currentNode.IngressGatewayRange
  79. convertedNode.IngressGatewayRange6 = currentNode.IngressGatewayRange6
  80. convertedNode.DNSOn = a.DNSOn
  81. convertedNode.IngressDNS = a.IngressDns
  82. convertedNode.IngressPersistentKeepalive = a.IngressPersistentKeepalive
  83. convertedNode.IngressMTU = a.IngressMTU
  84. convertedNode.IsInternetGateway = a.IsInternetGateway
  85. convertedNode.InternetGwID = currentNode.InternetGwID
  86. convertedNode.InetNodeReq = currentNode.InetNodeReq
  87. convertedNode.RelayedNodes = a.RelayedNodes
  88. convertedNode.DefaultACL = a.DefaultACL
  89. convertedNode.OwnerID = currentNode.OwnerID
  90. _, networkRange, err := net.ParseCIDR(a.NetworkRange)
  91. if err == nil {
  92. convertedNode.NetworkRange = *networkRange
  93. }
  94. _, networkRange6, err := net.ParseCIDR(a.NetworkRange6)
  95. if err == nil {
  96. convertedNode.NetworkRange6 = *networkRange6
  97. }
  98. if len(a.LocalAddress) > 0 {
  99. _, localAddr, err := net.ParseCIDR(a.LocalAddress)
  100. if err == nil {
  101. convertedNode.LocalAddress = *localAddr
  102. }
  103. } else if !isEmptyAddr(currentNode.LocalAddress.String()) {
  104. convertedNode.LocalAddress = currentNode.LocalAddress
  105. }
  106. ip, addr, err := net.ParseCIDR(a.Address)
  107. if err == nil {
  108. convertedNode.Address = *addr
  109. convertedNode.Address.IP = ip
  110. }
  111. ip6, addr6, err := net.ParseCIDR(a.Address6)
  112. if err == nil {
  113. convertedNode.Address6 = *addr6
  114. convertedNode.Address6.IP = ip6
  115. }
  116. convertedNode.LastModified = time.Unix(a.LastModified, 0)
  117. convertedNode.LastCheckIn = time.Unix(a.LastCheckIn, 0)
  118. convertedNode.LastPeerUpdate = time.Unix(a.LastPeerUpdate, 0)
  119. convertedNode.ExpirationDateTime = time.Unix(a.ExpirationDateTime, 0)
  120. convertedNode.Metadata = a.Metadata
  121. for _, ip := range a.AdditionalRagIps {
  122. ragIp := net.ParseIP(ip)
  123. if ragIp == nil {
  124. slog.Error("error parsing additional rag ip", "error", err, "ip", ip)
  125. return nil
  126. }
  127. convertedNode.AdditionalRagIps = append(convertedNode.AdditionalRagIps, ragIp)
  128. }
  129. convertedNode.Tags = a.Tags
  130. return &convertedNode
  131. }
  132. func (nm *Node) ConvertToStatusNode() *ApiNodeStatus {
  133. apiNode := ApiNodeStatus{}
  134. if nm.IsStatic {
  135. apiNode.ID = nm.StaticNode.ClientID
  136. } else {
  137. apiNode.ID = nm.ID.String()
  138. }
  139. apiNode.IsStatic = nm.IsStatic
  140. apiNode.IsUserNode = nm.IsUserNode
  141. apiNode.Status = nm.Status
  142. return &apiNode
  143. }
  144. // Node.ConvertToAPINode - converts a node to an API node
  145. func (nm *Node) ConvertToAPINode() *ApiNode {
  146. apiNode := ApiNode{}
  147. apiNode.ID = nm.ID.String()
  148. apiNode.HostID = nm.HostID.String()
  149. apiNode.Address = nm.Address.String()
  150. if isEmptyAddr(apiNode.Address) {
  151. apiNode.Address = ""
  152. }
  153. apiNode.Address6 = nm.Address6.String()
  154. if isEmptyAddr(apiNode.Address6) {
  155. apiNode.Address6 = ""
  156. }
  157. apiNode.LocalAddress = nm.LocalAddress.String()
  158. if isEmptyAddr(apiNode.LocalAddress) {
  159. apiNode.LocalAddress = ""
  160. }
  161. apiNode.LastModified = nm.LastModified.Unix()
  162. apiNode.LastCheckIn = nm.LastCheckIn.Unix()
  163. apiNode.LastPeerUpdate = nm.LastPeerUpdate.Unix()
  164. apiNode.ExpirationDateTime = nm.ExpirationDateTime.Unix()
  165. apiNode.Network = nm.Network
  166. apiNode.NetworkRange = nm.NetworkRange.String()
  167. if isEmptyAddr(apiNode.NetworkRange) {
  168. apiNode.NetworkRange = ""
  169. }
  170. apiNode.NetworkRange6 = nm.NetworkRange6.String()
  171. if isEmptyAddr(apiNode.NetworkRange6) {
  172. apiNode.NetworkRange6 = ""
  173. }
  174. apiNode.IsRelayed = nm.IsRelayed
  175. apiNode.IsRelay = nm.IsRelay
  176. apiNode.RelayedBy = nm.RelayedBy
  177. apiNode.RelayedNodes = nm.RelayedNodes
  178. apiNode.IsIngressGateway = nm.IsIngressGateway
  179. apiNode.DNSOn = nm.DNSOn
  180. apiNode.IngressDns = nm.IngressDNS
  181. apiNode.IngressPersistentKeepalive = nm.IngressPersistentKeepalive
  182. apiNode.IngressMTU = nm.IngressMTU
  183. apiNode.Server = nm.Server
  184. apiNode.Connected = nm.Connected
  185. apiNode.PendingDelete = nm.PendingDelete
  186. apiNode.DefaultACL = nm.DefaultACL
  187. apiNode.IsInternetGateway = nm.IsInternetGateway
  188. apiNode.InternetGwID = nm.InternetGwID
  189. apiNode.InetNodeReq = nm.InetNodeReq
  190. apiNode.IsFailOver = nm.IsFailOver
  191. apiNode.FailOverPeers = nm.FailOverPeers
  192. apiNode.FailedOverBy = nm.FailedOverBy
  193. apiNode.Metadata = nm.Metadata
  194. apiNode.AdditionalRagIps = []string{}
  195. apiNode.Tags = nm.Tags
  196. for _, ip := range nm.AdditionalRagIps {
  197. apiNode.AdditionalRagIps = append(apiNode.AdditionalRagIps, ip.String())
  198. }
  199. apiNode.IsStatic = nm.IsStatic
  200. apiNode.IsUserNode = nm.IsUserNode
  201. apiNode.StaticNode = nm.StaticNode
  202. apiNode.Status = nm.Status
  203. return &apiNode
  204. }
  205. func isEmptyAddr(addr string) bool {
  206. return addr == "<nil>" || addr == ":0"
  207. }