api_node.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package models
  2. import (
  3. "net"
  4. "time"
  5. "github.com/google/uuid"
  6. )
  7. // ApiNode is a stripped down Node DTO that exposes only required fields to external systems
  8. type ApiNode struct {
  9. ID string `json:"id,omitempty" validate:"required,min=5,id_unique"`
  10. HostID string `json:"hostid,omitempty" validate:"required,min=5,id_unique"`
  11. Address string `json:"address" validate:"omitempty,ipv4"`
  12. Address6 string `json:"address6" validate:"omitempty,ipv6"`
  13. LocalAddress string `json:"localaddress" validate:"omitempty,ipv4"`
  14. PostUp string `json:"postup"`
  15. PostDown string `json:"postdown"`
  16. AllowedIPs []string `json:"allowedips"`
  17. PersistentKeepalive int32 `json:"persistentkeepalive"`
  18. LastModified int64 `json:"lastmodified"`
  19. ExpirationDateTime int64 `json:"expdatetime"`
  20. LastCheckIn int64 `json:"lastcheckin"`
  21. LastPeerUpdate int64 `json:"lastpeerupdate"`
  22. Network string `json:"network"`
  23. NetworkRange string `json:"networkrange"`
  24. NetworkRange6 string `json:"networkrange6"`
  25. IsRelayed bool `json:"isrelayed"`
  26. IsRelay bool `json:"isrelay"`
  27. IsEgressGateway bool `json:"isegressgateway"`
  28. IsIngressGateway bool `json:"isingressgateway"`
  29. EgressGatewayRanges []string `json:"egressgatewayranges"`
  30. EgressGatewayNatEnabled bool `json:"egressgatewaynatenabled"`
  31. RelayAddrs []string `json:"relayaddrs"`
  32. FailoverNode string `json:"failovernode"`
  33. DNSOn bool `json:"dnson"`
  34. IsLocal bool `json:"islocal"`
  35. Server string `json:"server"`
  36. InternetGateway string `json:"internetgateway"`
  37. Connected bool `json:"connected"`
  38. PendingDelete bool `json:"pendingdelete"`
  39. // == PRO ==
  40. DefaultACL string `json:"defaultacl,omitempty" validate:"checkyesornoorunset"`
  41. Failover bool `json:"failover"`
  42. }
  43. // ApiNode.ConvertToServerNode - converts an api node to a server node
  44. func (a *ApiNode) ConvertToServerNode(currentNode *Node) *Node {
  45. convertedNode := Node{}
  46. convertedNode.Network = a.Network
  47. convertedNode.Server = a.Server
  48. convertedNode.Action = currentNode.Action
  49. convertedNode.Connected = a.Connected
  50. convertedNode.ID, _ = uuid.Parse(a.ID)
  51. convertedNode.HostID, _ = uuid.Parse(a.HostID)
  52. convertedNode.PostUp = a.PostUp
  53. convertedNode.PostDown = a.PostDown
  54. convertedNode.IsLocal = a.IsLocal
  55. convertedNode.PendingDelete = a.PendingDelete
  56. convertedNode.Failover = a.Failover
  57. convertedNode.IsEgressGateway = a.IsEgressGateway
  58. convertedNode.IsIngressGateway = a.IsIngressGateway
  59. convertedNode.EgressGatewayRanges = a.EgressGatewayRanges
  60. convertedNode.IngressGatewayRange = currentNode.IngressGatewayRange
  61. convertedNode.IngressGatewayRange6 = currentNode.IngressGatewayRange6
  62. convertedNode.DNSOn = a.DNSOn
  63. convertedNode.EgressGatewayRequest = currentNode.EgressGatewayRequest
  64. convertedNode.EgressGatewayNatEnabled = currentNode.EgressGatewayNatEnabled
  65. convertedNode.PersistentKeepalive = time.Second * time.Duration(a.PersistentKeepalive)
  66. convertedNode.DefaultACL = a.DefaultACL
  67. convertedNode.OwnerID = currentNode.OwnerID
  68. _, networkRange, err := net.ParseCIDR(a.NetworkRange)
  69. if err == nil {
  70. convertedNode.NetworkRange = *networkRange
  71. }
  72. _, networkRange6, err := net.ParseCIDR(a.NetworkRange6)
  73. if err == nil {
  74. convertedNode.NetworkRange6 = *networkRange6
  75. }
  76. if len(a.LocalAddress) > 0 {
  77. _, localAddr, err := net.ParseCIDR(a.LocalAddress)
  78. if err == nil {
  79. convertedNode.LocalAddress = *localAddr
  80. }
  81. } else if !isEmptyAddr(currentNode.LocalAddress.String()) {
  82. convertedNode.LocalAddress = currentNode.LocalAddress
  83. }
  84. udpAddr, err := net.ResolveUDPAddr("udp", a.InternetGateway)
  85. if err == nil {
  86. convertedNode.InternetGateway = udpAddr
  87. }
  88. _, addr, err := net.ParseCIDR(a.Address)
  89. if err == nil {
  90. convertedNode.Address = *addr
  91. }
  92. _, addr6, err := net.ParseCIDR(a.Address6)
  93. if err == nil {
  94. convertedNode.Address = *addr6
  95. }
  96. convertedNode.FailoverNode, _ = uuid.Parse(a.FailoverNode)
  97. convertedNode.LastModified = time.Unix(a.LastModified, 0)
  98. convertedNode.LastCheckIn = time.Unix(a.LastCheckIn, 0)
  99. convertedNode.LastPeerUpdate = time.Unix(a.LastPeerUpdate, 0)
  100. convertedNode.ExpirationDateTime = time.Unix(a.ExpirationDateTime, 0)
  101. return &convertedNode
  102. }
  103. // Node.ConvertToAPINode - converts a node to an API node
  104. func (nm *Node) ConvertToAPINode() *ApiNode {
  105. apiNode := ApiNode{}
  106. apiNode.ID = nm.ID.String()
  107. apiNode.HostID = nm.HostID.String()
  108. apiNode.Address = nm.Address.String()
  109. if isEmptyAddr(apiNode.Address) {
  110. apiNode.Address = ""
  111. }
  112. apiNode.Address6 = nm.Address6.String()
  113. if isEmptyAddr(apiNode.Address6) {
  114. apiNode.Address6 = ""
  115. }
  116. apiNode.LocalAddress = nm.LocalAddress.String()
  117. if isEmptyAddr(apiNode.LocalAddress) {
  118. apiNode.LocalAddress = ""
  119. }
  120. apiNode.PostDown = nm.PostDown
  121. apiNode.PostUp = nm.PostUp
  122. apiNode.PersistentKeepalive = int32(nm.PersistentKeepalive.Seconds())
  123. apiNode.LastModified = nm.LastModified.Unix()
  124. apiNode.LastCheckIn = nm.LastCheckIn.Unix()
  125. apiNode.LastPeerUpdate = nm.LastPeerUpdate.Unix()
  126. apiNode.Network = nm.Network
  127. apiNode.NetworkRange = nm.NetworkRange.String()
  128. if isEmptyAddr(apiNode.NetworkRange) {
  129. apiNode.NetworkRange = ""
  130. }
  131. apiNode.NetworkRange6 = nm.NetworkRange6.String()
  132. if isEmptyAddr(apiNode.NetworkRange6) {
  133. apiNode.NetworkRange6 = ""
  134. }
  135. apiNode.IsEgressGateway = nm.IsEgressGateway
  136. apiNode.IsIngressGateway = nm.IsIngressGateway
  137. apiNode.EgressGatewayRanges = nm.EgressGatewayRanges
  138. apiNode.EgressGatewayNatEnabled = nm.EgressGatewayNatEnabled
  139. apiNode.FailoverNode = nm.FailoverNode.String()
  140. if isUUIDSet(apiNode.FailoverNode) {
  141. apiNode.FailoverNode = ""
  142. }
  143. apiNode.DNSOn = nm.DNSOn
  144. apiNode.IsLocal = nm.IsLocal
  145. apiNode.Server = nm.Server
  146. apiNode.InternetGateway = nm.InternetGateway.String()
  147. if isEmptyAddr(apiNode.InternetGateway) {
  148. apiNode.InternetGateway = ""
  149. }
  150. apiNode.Connected = nm.Connected
  151. apiNode.PendingDelete = nm.PendingDelete
  152. apiNode.DefaultACL = nm.DefaultACL
  153. apiNode.Failover = nm.Failover
  154. return &apiNode
  155. }
  156. func isEmptyAddr(addr string) bool {
  157. return addr == "<nil>" || addr == ":0"
  158. }
  159. func isUUIDSet(uuid string) bool {
  160. return uuid != "00000000-0000-0000-0000-000000000000"
  161. }