node.go 5.1 KB

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