node.go 4.6 KB

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