node.go 8.6 KB

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