node.go 4.5 KB

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