api_node.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. RelayedBy string `json:"relayedby" bson:"relayedby" yaml:"relayedby"`
  33. RelayedNodes []string `json:"relaynodes" yaml:"relayedNodes"`
  34. IsEgressGateway bool `json:"isegressgateway"`
  35. IsIngressGateway bool `json:"isingressgateway"`
  36. EgressGatewayRanges []string `json:"egressgatewayranges"`
  37. EgressGatewayNatEnabled bool `json:"egressgatewaynatenabled"`
  38. EgressGatewayRangesWithMetric []EgressRangeMetric `json:"egressgatewayranges_with_metric"`
  39. DNSOn bool `json:"dnson"`
  40. IngressDns string `json:"ingressdns"`
  41. IngressPersistentKeepalive int32 `json:"ingresspersistentkeepalive"`
  42. IngressMTU int32 `json:"ingressmtu"`
  43. Server string `json:"server"`
  44. Connected bool `json:"connected"`
  45. PendingDelete bool `json:"pendingdelete"`
  46. Metadata string `json:"metadata"`
  47. // == PRO ==
  48. DefaultACL string `json:"defaultacl,omitempty" validate:"checkyesornoorunset"`
  49. IsFailOver bool `json:"is_fail_over"`
  50. FailOverPeers map[string]struct{} `json:"fail_over_peers" yaml:"fail_over_peers"`
  51. FailedOverBy uuid.UUID `json:"failed_over_by" yaml:"failed_over_by"`
  52. IsInternetGateway bool `json:"isinternetgateway" yaml:"isinternetgateway"`
  53. InetNodeReq InetNodeReq `json:"inet_node_req" yaml:"inet_node_req"`
  54. InternetGwID string `json:"internetgw_node_id" yaml:"internetgw_node_id"`
  55. AdditionalRagIps []string `json:"additional_rag_ips" yaml:"additional_rag_ips"`
  56. Tags map[TagID]struct{} `json:"tags" yaml:"tags"`
  57. IsStatic bool `json:"is_static"`
  58. IsUserNode bool `json:"is_user_node"`
  59. StaticNode ExtClient `json:"static_node"`
  60. Status NodeStatus `json:"status"`
  61. }
  62. // ApiNode.ConvertToServerNode - converts an api node to a server node
  63. func (a *ApiNode) ConvertToServerNode(currentNode *Node) *Node {
  64. convertedNode := Node{}
  65. convertedNode.Network = a.Network
  66. convertedNode.Server = a.Server
  67. convertedNode.Action = currentNode.Action
  68. convertedNode.Connected = a.Connected
  69. convertedNode.ID, _ = uuid.Parse(a.ID)
  70. convertedNode.HostID, _ = uuid.Parse(a.HostID)
  71. //convertedNode.IsRelay = a.IsRelay
  72. convertedNode.IsRelayed = a.IsRelayed
  73. convertedNode.RelayedBy = a.RelayedBy
  74. convertedNode.RelayedNodes = a.RelayedNodes
  75. convertedNode.PendingDelete = a.PendingDelete
  76. convertedNode.FailedOverBy = currentNode.FailedOverBy
  77. convertedNode.FailOverPeers = currentNode.FailOverPeers
  78. //convertedNode.IsIngressGateway = a.IsIngressGateway
  79. convertedNode.IngressGatewayRange = currentNode.IngressGatewayRange
  80. convertedNode.IngressGatewayRange6 = currentNode.IngressGatewayRange6
  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. convertedNode.IsGw = a.IsGw
  131. if convertedNode.IsGw {
  132. convertedNode.IsRelay = true
  133. convertedNode.IsIngressGateway = true
  134. }
  135. return &convertedNode
  136. }
  137. func (nm *Node) ConvertToStatusNode() *ApiNodeStatus {
  138. apiNode := ApiNodeStatus{}
  139. if nm.IsStatic {
  140. apiNode.ID = nm.StaticNode.ClientID
  141. } else {
  142. apiNode.ID = nm.ID.String()
  143. }
  144. apiNode.IsStatic = nm.IsStatic
  145. apiNode.IsUserNode = nm.IsUserNode
  146. apiNode.Status = nm.Status
  147. return &apiNode
  148. }
  149. // Node.ConvertToAPINode - converts a node to an API node
  150. func (nm *Node) ConvertToAPINode() *ApiNode {
  151. apiNode := ApiNode{}
  152. apiNode.ID = nm.ID.String()
  153. apiNode.HostID = nm.HostID.String()
  154. apiNode.Address = nm.Address.String()
  155. if isEmptyAddr(apiNode.Address) {
  156. apiNode.Address = ""
  157. }
  158. apiNode.Address6 = nm.Address6.String()
  159. if isEmptyAddr(apiNode.Address6) {
  160. apiNode.Address6 = ""
  161. }
  162. apiNode.LocalAddress = nm.LocalAddress.String()
  163. if isEmptyAddr(apiNode.LocalAddress) {
  164. apiNode.LocalAddress = ""
  165. }
  166. apiNode.LastModified = nm.LastModified.Unix()
  167. apiNode.LastCheckIn = nm.LastCheckIn.Unix()
  168. apiNode.LastPeerUpdate = nm.LastPeerUpdate.Unix()
  169. apiNode.ExpirationDateTime = nm.ExpirationDateTime.Unix()
  170. apiNode.Network = nm.Network
  171. apiNode.NetworkRange = nm.NetworkRange.String()
  172. if isEmptyAddr(apiNode.NetworkRange) {
  173. apiNode.NetworkRange = ""
  174. }
  175. apiNode.NetworkRange6 = nm.NetworkRange6.String()
  176. if isEmptyAddr(apiNode.NetworkRange6) {
  177. apiNode.NetworkRange6 = ""
  178. }
  179. apiNode.IsRelayed = nm.IsRelayed
  180. apiNode.IsRelay = nm.IsRelay
  181. apiNode.IsGw = nm.IsGw
  182. apiNode.RelayedBy = nm.RelayedBy
  183. apiNode.RelayedNodes = nm.RelayedNodes
  184. apiNode.IsIngressGateway = nm.IsIngressGateway
  185. apiNode.IngressDns = nm.IngressDNS
  186. apiNode.IngressPersistentKeepalive = nm.IngressPersistentKeepalive
  187. apiNode.IngressMTU = nm.IngressMTU
  188. apiNode.Server = nm.Server
  189. apiNode.Connected = nm.Connected
  190. apiNode.PendingDelete = nm.PendingDelete
  191. apiNode.DefaultACL = nm.DefaultACL
  192. apiNode.IsInternetGateway = nm.IsInternetGateway
  193. apiNode.InternetGwID = nm.InternetGwID
  194. apiNode.InetNodeReq = nm.InetNodeReq
  195. apiNode.IsFailOver = nm.IsFailOver
  196. apiNode.FailOverPeers = nm.FailOverPeers
  197. apiNode.FailedOverBy = nm.FailedOverBy
  198. apiNode.Metadata = nm.Metadata
  199. apiNode.AdditionalRagIps = []string{}
  200. apiNode.Tags = nm.Tags
  201. for _, ip := range nm.AdditionalRagIps {
  202. apiNode.AdditionalRagIps = append(apiNode.AdditionalRagIps, ip.String())
  203. }
  204. apiNode.IsStatic = nm.IsStatic
  205. apiNode.IsUserNode = nm.IsUserNode
  206. apiNode.StaticNode = nm.StaticNode
  207. apiNode.Status = nm.Status
  208. return &apiNode
  209. }
  210. func isEmptyAddr(addr string) bool {
  211. return addr == "<nil>" || addr == ":0"
  212. }