api_node.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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]string `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. if a.RelayedBy != "" && !a.IsRelayed {
  78. a.IsRelayed = true
  79. }
  80. convertedNode.IsRelayed = a.IsRelayed
  81. convertedNode.RelayedBy = a.RelayedBy
  82. convertedNode.RelayedNodes = a.RelayedNodes
  83. convertedNode.PendingDelete = a.PendingDelete
  84. convertedNode.FailedOverBy = currentNode.FailedOverBy
  85. convertedNode.FailOverPeers = currentNode.FailOverPeers
  86. //convertedNode.IsIngressGateway = a.IsIngressGateway
  87. convertedNode.IngressGatewayRange = currentNode.IngressGatewayRange
  88. convertedNode.IngressGatewayRange6 = currentNode.IngressGatewayRange6
  89. convertedNode.IngressDNS = a.IngressDns
  90. convertedNode.IngressPersistentKeepalive = a.IngressPersistentKeepalive
  91. convertedNode.IngressMTU = a.IngressMTU
  92. convertedNode.IsInternetGateway = a.IsInternetGateway
  93. convertedNode.InternetGwID = currentNode.InternetGwID
  94. convertedNode.InetNodeReq = currentNode.InetNodeReq
  95. convertedNode.RelayedNodes = a.RelayedNodes
  96. convertedNode.DefaultACL = a.DefaultACL
  97. convertedNode.OwnerID = currentNode.OwnerID
  98. _, networkRange, err := net.ParseCIDR(a.NetworkRange)
  99. if err == nil {
  100. convertedNode.NetworkRange = *networkRange
  101. }
  102. _, networkRange6, err := net.ParseCIDR(a.NetworkRange6)
  103. if err == nil {
  104. convertedNode.NetworkRange6 = *networkRange6
  105. }
  106. if len(a.LocalAddress) > 0 {
  107. _, localAddr, err := net.ParseCIDR(a.LocalAddress)
  108. if err == nil {
  109. convertedNode.LocalAddress = *localAddr
  110. }
  111. } else if !isEmptyAddr(currentNode.LocalAddress.String()) {
  112. convertedNode.LocalAddress = currentNode.LocalAddress
  113. }
  114. ip, addr, err := net.ParseCIDR(a.Address)
  115. if err == nil {
  116. convertedNode.Address = *addr
  117. convertedNode.Address.IP = ip
  118. }
  119. ip6, addr6, err := net.ParseCIDR(a.Address6)
  120. if err == nil {
  121. convertedNode.Address6 = *addr6
  122. convertedNode.Address6.IP = ip6
  123. }
  124. convertedNode.LastModified = time.Unix(a.LastModified, 0)
  125. convertedNode.LastCheckIn = time.Unix(a.LastCheckIn, 0)
  126. convertedNode.LastPeerUpdate = time.Unix(a.LastPeerUpdate, 0)
  127. convertedNode.ExpirationDateTime = time.Unix(a.ExpirationDateTime, 0)
  128. convertedNode.Metadata = a.Metadata
  129. for _, ip := range a.AdditionalRagIps {
  130. ragIp := net.ParseIP(ip)
  131. if ragIp == nil {
  132. slog.Error("error parsing additional rag ip", "error", err, "ip", ip)
  133. return nil
  134. }
  135. convertedNode.AdditionalRagIps = append(convertedNode.AdditionalRagIps, ragIp)
  136. }
  137. convertedNode.Tags = a.Tags
  138. convertedNode.IsGw = a.IsGw
  139. convertedNode.IsAutoRelay = a.IsAutoRelay
  140. if convertedNode.IsGw {
  141. convertedNode.IsRelay = true
  142. convertedNode.IsIngressGateway = true
  143. }
  144. convertedNode.AutoAssignGateway = a.AutoAssignGateway
  145. return &convertedNode
  146. }
  147. func (nm *Node) ConvertToStatusNode() *ApiNodeStatus {
  148. apiNode := ApiNodeStatus{}
  149. if nm.IsStatic {
  150. apiNode.ID = nm.StaticNode.ClientID
  151. } else {
  152. apiNode.ID = nm.ID.String()
  153. }
  154. apiNode.IsStatic = nm.IsStatic
  155. apiNode.IsUserNode = nm.IsUserNode
  156. apiNode.Status = nm.Status
  157. return &apiNode
  158. }
  159. // Node.ConvertToAPINode - converts a node to an API node
  160. func (nm *Node) ConvertToAPINode() *ApiNode {
  161. apiNode := ApiNode{}
  162. apiNode.ID = nm.ID.String()
  163. apiNode.HostID = nm.HostID.String()
  164. apiNode.Address = nm.Address.String()
  165. if isEmptyAddr(apiNode.Address) {
  166. apiNode.Address = ""
  167. }
  168. apiNode.Address6 = nm.Address6.String()
  169. if isEmptyAddr(apiNode.Address6) {
  170. apiNode.Address6 = ""
  171. }
  172. apiNode.LocalAddress = nm.LocalAddress.String()
  173. if isEmptyAddr(apiNode.LocalAddress) {
  174. apiNode.LocalAddress = ""
  175. }
  176. apiNode.LastModified = nm.LastModified.Unix()
  177. apiNode.LastCheckIn = nm.LastCheckIn.Unix()
  178. apiNode.LastPeerUpdate = nm.LastPeerUpdate.Unix()
  179. apiNode.ExpirationDateTime = nm.ExpirationDateTime.Unix()
  180. apiNode.Network = nm.Network
  181. apiNode.NetworkRange = nm.NetworkRange.String()
  182. if isEmptyAddr(apiNode.NetworkRange) {
  183. apiNode.NetworkRange = ""
  184. }
  185. apiNode.NetworkRange6 = nm.NetworkRange6.String()
  186. if isEmptyAddr(apiNode.NetworkRange6) {
  187. apiNode.NetworkRange6 = ""
  188. }
  189. apiNode.IsRelayed = nm.IsRelayed
  190. apiNode.IsRelay = nm.IsRelay
  191. apiNode.IsGw = nm.IsGw
  192. apiNode.RelayedBy = nm.RelayedBy
  193. apiNode.RelayedNodes = nm.RelayedNodes
  194. apiNode.IsAutoRelay = nm.IsAutoRelay
  195. //apiNode.AutoRelayedBy = nm.AutoRelayedBy
  196. apiNode.AutoRelayedPeers = nm.AutoRelayedPeers
  197. apiNode.AutoAssignGateway = nm.AutoAssignGateway
  198. apiNode.IsIngressGateway = nm.IsIngressGateway
  199. apiNode.IngressDns = nm.IngressDNS
  200. apiNode.IngressPersistentKeepalive = nm.IngressPersistentKeepalive
  201. apiNode.IngressMTU = nm.IngressMTU
  202. apiNode.Server = nm.Server
  203. apiNode.Connected = nm.Connected
  204. apiNode.PendingDelete = nm.PendingDelete
  205. apiNode.DefaultACL = nm.DefaultACL
  206. apiNode.IsInternetGateway = nm.IsInternetGateway
  207. apiNode.InternetGwID = nm.InternetGwID
  208. apiNode.InetNodeReq = nm.InetNodeReq
  209. apiNode.IsFailOver = nm.IsFailOver
  210. apiNode.FailOverPeers = nm.FailOverPeers
  211. apiNode.FailedOverBy = nm.FailedOverBy
  212. apiNode.Metadata = nm.Metadata
  213. apiNode.AdditionalRagIps = []string{}
  214. apiNode.Tags = nm.Tags
  215. for _, ip := range nm.AdditionalRagIps {
  216. apiNode.AdditionalRagIps = append(apiNode.AdditionalRagIps, ip.String())
  217. }
  218. apiNode.IsStatic = nm.IsStatic
  219. apiNode.IsUserNode = nm.IsUserNode
  220. apiNode.StaticNode = nm.StaticNode
  221. apiNode.Status = nm.Status
  222. return &apiNode
  223. }
  224. func isEmptyAddr(addr string) bool {
  225. return addr == "<nil>" || addr == ":0"
  226. }