node.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package models
  2. import (
  3. "context"
  4. "math/rand"
  5. "net"
  6. "time"
  7. "github.com/gravitl/netmaker/mongoconn"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. )
  11. const charset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  12. var seededRand *rand.Rand = rand.New(
  13. rand.NewSource(time.Now().UnixNano()))
  14. //node struct
  15. type Node struct {
  16. ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
  17. Address string `json:"address" bson:"address" validate:"omitempty,ipv4"`
  18. Address6 string `json:"address6" bson:"address6" validate:"omitempty,ipv6"`
  19. LocalAddress string `json:"localaddress" bson:"localaddress" validate:"omitempty,ip"`
  20. Name string `json:"name" bson:"name" validate:"omitempty,max=12,in_charset"`
  21. ListenPort int32 `json:"listenport" bson:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`
  22. PublicKey string `json:"publickey" bson:"publickey" validate:"required,base64"`
  23. Endpoint string `json:"endpoint" bson:"endpoint" validate:"required,ip"`
  24. PostUp string `json:"postup" bson:"postup"`
  25. PostDown string `json:"postdown" bson:"postdown"`
  26. AllowedIPs string `json:"allowedips" bson:"allowedips"`
  27. PersistentKeepalive int32 `json:"persistentkeepalive" bson:"persistentkeepalive" validate:"omitempty,numeric,max=1000"`
  28. SaveConfig *bool `json:"saveconfig" bson:"saveconfig"`
  29. AccessKey string `json:"accesskey" bson:"accesskey"`
  30. Interface string `json:"interface" bson:"interface"`
  31. LastModified int64 `json:"lastmodified" bson:"lastmodified"`
  32. KeyUpdateTimeStamp int64 `json:"keyupdatetimestamp" bson:"keyupdatetimestamp"`
  33. ExpirationDateTime int64 `json:"expdatetime" bson:"expdatetime"`
  34. LastPeerUpdate int64 `json:"lastpeerupdate" bson:"lastpeerupdate"`
  35. LastCheckIn int64 `json:"lastcheckin" bson:"lastcheckin"`
  36. MacAddress string `json:"macaddress" bson:"macaddress" validate:"required,mac,macaddress_unique"`
  37. CheckInInterval int32 `json:"checkininterval" bson:"checkininterval"`
  38. Password string `json:"password" bson:"password" validate:"required,min=6"`
  39. Network string `json:"network" bson:"network" validate:"network_exists"`
  40. IsPending bool `json:"ispending" bson:"ispending"`
  41. IsEgressGateway bool `json:"isegressgateway" bson:"isegressgateway"`
  42. IsIngressGateway bool `json:"isingressgateway" bson:"isingressgateway"`
  43. EgressGatewayRanges []string `json:"egressgatewayranges" bson:"egressgatewayranges"`
  44. IngressGatewayRange string `json:"ingressgatewayrange" bson:"ingressgatewayrange"`
  45. PostChanges string `json:"postchanges" bson:"postchanges"`
  46. }
  47. //node update struct --- only validations are different
  48. type NodeUpdate struct {
  49. ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
  50. Address string `json:"address" bson:"address" validate:"omitempty,ip"`
  51. Address6 string `json:"address6" bson:"address6" validate:"omitempty,ipv6"`
  52. LocalAddress string `json:"localaddress" bson:"localaddress" validate:"omitempty,ip"`
  53. Name string `json:"name" bson:"name" validate:"omitempty,max=12,in_charset"`
  54. ListenPort int32 `json:"listenport" bson:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`
  55. PublicKey string `json:"publickey" bson:"publickey" validate:"omitempty,base64"`
  56. Endpoint string `json:"endpoint" bson:"endpoint" validate:"omitempty,ip"`
  57. PostUp string `json:"postup" bson:"postup"`
  58. PostDown string `json:"postdown" bson:"postdown"`
  59. AllowedIPs string `json:"allowedips" bson:"allowedips"`
  60. PersistentKeepalive int32 `json:"persistentkeepalive" bson:"persistentkeepalive" validate:"omitempty,numeric,max=1000"`
  61. SaveConfig *bool `json:"saveconfig" bson:"saveconfig"`
  62. AccessKey string `json:"accesskey" bson:"accesskey"`
  63. Interface string `json:"interface" bson:"interface"`
  64. LastModified int64 `json:"lastmodified" bson:"lastmodified"`
  65. KeyUpdateTimeStamp int64 `json:"keyupdatetimestamp" bson:"keyupdatetimestamp"`
  66. ExpirationDateTime int64 `json:"expdatetime" bson:"expdatetime"`
  67. LastPeerUpdate int64 `json:"lastpeerupdate" bson:"lastpeerupdate"`
  68. LastCheckIn int64 `json:"lastcheckin" bson:"lastcheckin"`
  69. MacAddress string `json:"macaddress" bson:"macaddress" validate:"required,mac"`
  70. CheckInInterval int32 `json:"checkininterval" bson:"checkininterval"`
  71. Password string `json:"password" bson:"password" validate:"omitempty,min=5"`
  72. Network string `json:"network" bson:"network" validate:"network_exists"`
  73. IsPending bool `json:"ispending" bson:"ispending"`
  74. IsIngressGateway bool `json:"isingressgateway" bson:"isingressgateway"`
  75. IsEgressGateway bool `json:"isegressgateway" bson:"isegressgateway"`
  76. IngressGatewayRange string `json:"ingressgatewayrange" bson:"ingressgatewayrange"`
  77. EgressGatewayRange string `json:"gatewayrange" bson:"gatewayrange"`
  78. PostChanges string `json:"postchanges" bson:"postchanges"`
  79. }
  80. //Duplicated function for NodeUpdates
  81. func (node *NodeUpdate) GetNetwork() (Network, error) {
  82. var network Network
  83. collection := mongoconn.NetworkDB
  84. //collection := mongoconn.Client.Database("netmaker").Collection("networks")
  85. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  86. filter := bson.M{"netid": node.Network}
  87. err := collection.FindOne(ctx, filter).Decode(&network)
  88. defer cancel()
  89. if err != nil {
  90. //log.Fatal(err)
  91. return network, err
  92. }
  93. return network, err
  94. }
  95. //TODO: Contains a fatal error return. Need to change
  96. //Used in contexts where it's not the Parent network.
  97. func (node *Node) GetNetwork() (Network, error) {
  98. var network Network
  99. //collection := mongoconn.NetworkDB
  100. collection := mongoconn.Client.Database("netmaker").Collection("networks")
  101. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  102. filter := bson.M{"netid": node.Network}
  103. err := collection.FindOne(ctx, filter).Decode(&network)
  104. defer cancel()
  105. if err != nil {
  106. //log.Fatal(err)
  107. return network, err
  108. }
  109. return network, err
  110. }
  111. //TODO:
  112. //Not sure if below two methods are necessary. May want to revisit
  113. func (node *Node) SetLastModified() {
  114. node.LastModified = time.Now().Unix()
  115. }
  116. func (node *Node) SetLastCheckIn() {
  117. node.LastCheckIn = time.Now().Unix()
  118. }
  119. func (node *Node) SetLastPeerUpdate() {
  120. node.LastPeerUpdate = time.Now().Unix()
  121. }
  122. func (node *Node) SetExpirationDateTime() {
  123. node.ExpirationDateTime = time.Unix(33174902665, 0).Unix()
  124. }
  125. func (node *Node) SetDefaultName() {
  126. if node.Name == "" {
  127. nodeid := StringWithCharset(5, charset)
  128. nodename := "node-" + nodeid
  129. node.Name = nodename
  130. }
  131. }
  132. //TODO: I dont know why this exists
  133. //This should exist on the node.go struct. I'm sure there was a reason?
  134. func (node *Node) SetDefaults() {
  135. //TODO: Maybe I should make Network a part of the node struct. Then we can just query the Network object for stuff.
  136. parentNetwork, _ := node.GetNetwork()
  137. node.ExpirationDateTime = time.Unix(33174902665, 0).Unix()
  138. if node.ListenPort == 0 {
  139. node.ListenPort = parentNetwork.DefaultListenPort
  140. }
  141. if node.PostDown == "" {
  142. //Empty because we dont set it
  143. //may want to set it to something in the future
  144. }
  145. //TODO: This is dumb and doesn't work
  146. //Need to change
  147. if node.SaveConfig == nil {
  148. if parentNetwork.DefaultSaveConfig != nil {
  149. defaultsave := *parentNetwork.DefaultSaveConfig
  150. node.SaveConfig = &defaultsave
  151. }
  152. }
  153. if node.Interface == "" {
  154. node.Interface = parentNetwork.DefaultInterface
  155. }
  156. if node.PersistentKeepalive == 0 {
  157. node.PersistentKeepalive = parentNetwork.DefaultKeepalive
  158. }
  159. if node.PostUp == "" {
  160. postup := parentNetwork.DefaultPostUp
  161. node.PostUp = postup
  162. }
  163. node.CheckInInterval = parentNetwork.DefaultCheckInInterval
  164. }
  165. func StringWithCharset(length int, charset string) string {
  166. b := make([]byte, length)
  167. for i := range b {
  168. b[i] = charset[seededRand.Intn(len(charset))]
  169. }
  170. return string(b)
  171. }
  172. //Check for valid IPv4 address
  173. //Note: We dont handle IPv6 AT ALL!!!!! This definitely is needed at some point
  174. //But for iteration 1, lets just stick to IPv4. Keep it simple stupid.
  175. func IsIpv4Net(host string) bool {
  176. return net.ParseIP(host) != nil
  177. }