api_node.go 9.0 KB

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