node.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package models
  2. import (
  3. "go.mongodb.org/mongo-driver/bson/primitive"
  4. "github.com/gravitl/netmaker/mongoconn"
  5. "math/rand"
  6. "time"
  7. "net"
  8. "context"
  9. "go.mongodb.org/mongo-driver/bson"
  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"`
  18. LocalAddress string `json:"localaddress" bson:"localaddress" validate:"localaddress_check"`
  19. Name string `json:"name" bson:"name" validate:"omitempty,name_valid,max=12"`
  20. ListenPort int32 `json:"listenport" bson:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`
  21. PublicKey string `json:"publickey" bson:"publickey" validate:"pubkey_check"`
  22. Endpoint string `json:"endpoint" bson:"endpoint" validate:"endpoint_check"`
  23. PostUp string `json:"postup" bson:"postup"`
  24. PreUp string `json:"preup" bson:"preup"`
  25. PersistentKeepalive int32 `json:"persistentkeepalive" bson:"persistentkeepalive" validate: "omitempty,numeric,max=1000"`
  26. SaveConfig *bool `json:"saveconfig" bson:"saveconfig"`
  27. AccessKey string `json:"accesskey" bson:"accesskey"`
  28. Interface string `json:"interface" bson:"interface"`
  29. LastModified int64 `json:"lastmodified" bson:"lastmodified"`
  30. KeyUpdateTimeStamp int64 `json:"keyupdatetimestamp" bson:"keyupdatetimestamp"`
  31. ExpirationDateTime int64 `json:"expdatetime" bson:"expdatetime"`
  32. LastPeerUpdate int64 `json:"lastpeerupdate" bson:"lastpeerupdate"`
  33. LastCheckIn int64 `json:"lastcheckin" bson:"lastcheckin"`
  34. MacAddress string `json:"macaddress" bson:"macaddress" validate:"required,macaddress_valid,macaddress_unique"`
  35. CheckInInterval int32 `json:"checkininterval" bson:"checkininterval"`
  36. Password string `json:"password" bson:"password" validate:"password_check"`
  37. Group string `json:"group" bson:"group" validate:"group_exists"`
  38. IsPending bool `json:"ispending" bson:"ispending"`
  39. PostChanges string `json:"postchanges" bson:"postchanges"`
  40. }
  41. //TODO: Contains a fatal error return. Need to change
  42. //Used in contexts where it's not the Parent group.
  43. func(node *Node) GetGroup() (Group, error){
  44. var group Group
  45. collection := mongoconn.GroupDB
  46. //collection := mongoconn.Client.Database("netmaker").Collection("groups")
  47. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  48. filter := bson.M{"nameid": node.Group}
  49. err := collection.FindOne(ctx, filter).Decode(&group)
  50. defer cancel()
  51. if err != nil {
  52. //log.Fatal(err)
  53. return group, err
  54. }
  55. return group, err
  56. }
  57. //TODO:
  58. //Not sure if below two methods are necessary. May want to revisit
  59. func(node *Node) SetLastModified(){
  60. node.LastModified = time.Now().Unix()
  61. }
  62. func(node *Node) SetLastCheckIn(){
  63. node.LastCheckIn = time.Now().Unix()
  64. }
  65. func(node *Node) SetLastPeerUpdate(){
  66. node.LastPeerUpdate = time.Now().Unix()
  67. }
  68. func(node *Node) SetExpirationDateTime(){
  69. node.ExpirationDateTime = time.Unix(33174902665, 0).Unix()
  70. }
  71. func(node *Node) SetDefaultName(){
  72. if node.Name == "" {
  73. nodeid := StringWithCharset(5, charset)
  74. nodename := "node-" + nodeid
  75. node.Name = nodename
  76. }
  77. }
  78. //TODO: I dont know why this exists
  79. //This should exist on the node.go struct. I'm sure there was a reason?
  80. func(node *Node) SetDefaults() {
  81. //TODO: Maybe I should make Group a part of the node struct. Then we can just query the Group object for stuff.
  82. parentGroup, _ := node.GetGroup()
  83. node.ExpirationDateTime = time.Unix(33174902665, 0).Unix()
  84. if node.ListenPort == 0 {
  85. node.ListenPort = parentGroup.DefaultListenPort
  86. }
  87. if node.PreUp == "" {
  88. //Empty because we dont set it
  89. //may want to set it to something in the future
  90. }
  91. //TODO: This is dumb and doesn't work
  92. //Need to change
  93. if node.SaveConfig == nil {
  94. defaultsave := *parentGroup.DefaultSaveConfig
  95. node.SaveConfig = &defaultsave
  96. }
  97. if node.Interface == "" {
  98. node.Interface = parentGroup.DefaultInterface
  99. }
  100. if node.PersistentKeepalive == 0 {
  101. node.PersistentKeepalive = parentGroup.DefaultKeepalive
  102. }
  103. if node.PostUp == "" {
  104. postup := parentGroup.DefaultPostUp
  105. node.PostUp = postup
  106. }
  107. node.CheckInInterval = parentGroup.DefaultCheckInInterval
  108. }
  109. func StringWithCharset(length int, charset string) string {
  110. b := make([]byte, length)
  111. for i := range b {
  112. b[i] = charset[seededRand.Intn(len(charset))]
  113. }
  114. return string(b)
  115. }
  116. //Check for valid IPv4 address
  117. //Note: We dont handle IPv6 AT ALL!!!!! This definitely is needed at some point
  118. //But for iteration 1, lets just stick to IPv4. Keep it simple stupid.
  119. func IsIpv4Net(host string) bool {
  120. return net.ParseIP(host) != nil
  121. }