node.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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:"address_check"`
  18. Address6 string `json:"address6" bson:"address6" validate:"address6_check"`
  19. LocalAddress string `json:"localaddress" bson:"localaddress" validate:"localaddress_check"`
  20. Name string `json:"name" bson:"name" validate:"omitempty,name_valid,max=12"`
  21. ListenPort int32 `json:"listenport" bson:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`
  22. PublicKey string `json:"publickey" bson:"publickey" validate:"pubkey_check"`
  23. Endpoint string `json:"endpoint" bson:"endpoint" validate:"endpoint_check"`
  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,macaddress_valid,macaddress_unique"`
  37. CheckInInterval int32 `json:"checkininterval" bson:"checkininterval"`
  38. Password string `json:"password" bson:"password" validate:"password_check"`
  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. //TODO: Contains a fatal error return. Need to change
  46. //Used in contexts where it's not the Parent network.
  47. func (node *Node) GetNetwork() (Network, error) {
  48. var network Network
  49. collection := mongoconn.NetworkDB
  50. //collection := mongoconn.Client.Database("netmaker").Collection("networks")
  51. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  52. filter := bson.M{"netid": node.Network}
  53. err := collection.FindOne(ctx, filter).Decode(&network)
  54. defer cancel()
  55. if err != nil {
  56. //log.Fatal(err)
  57. return network, err
  58. }
  59. return network, err
  60. }
  61. //TODO:
  62. //Not sure if below two methods are necessary. May want to revisit
  63. func (node *Node) SetLastModified() {
  64. node.LastModified = time.Now().Unix()
  65. }
  66. func (node *Node) SetLastCheckIn() {
  67. node.LastCheckIn = time.Now().Unix()
  68. }
  69. func (node *Node) SetLastPeerUpdate() {
  70. node.LastPeerUpdate = time.Now().Unix()
  71. }
  72. func (node *Node) SetExpirationDateTime() {
  73. node.ExpirationDateTime = time.Unix(33174902665, 0).Unix()
  74. }
  75. func (node *Node) SetDefaultName() {
  76. if node.Name == "" {
  77. nodeid := StringWithCharset(5, charset)
  78. nodename := "node-" + nodeid
  79. node.Name = nodename
  80. }
  81. }
  82. //TODO: I dont know why this exists
  83. //This should exist on the node.go struct. I'm sure there was a reason?
  84. func (node *Node) SetDefaults() {
  85. //TODO: Maybe I should make Network a part of the node struct. Then we can just query the Network object for stuff.
  86. parentNetwork, _ := node.GetNetwork()
  87. node.ExpirationDateTime = time.Unix(33174902665, 0).Unix()
  88. if node.ListenPort == 0 {
  89. node.ListenPort = parentNetwork.DefaultListenPort
  90. }
  91. if node.PostDown == "" {
  92. //Empty because we dont set it
  93. //may want to set it to something in the future
  94. }
  95. //TODO: This is dumb and doesn't work
  96. //Need to change
  97. if node.SaveConfig == nil {
  98. defaultsave := *parentNetwork.DefaultSaveConfig
  99. node.SaveConfig = &defaultsave
  100. }
  101. if node.Interface == "" {
  102. node.Interface = parentNetwork.DefaultInterface
  103. }
  104. if node.PersistentKeepalive == 0 {
  105. node.PersistentKeepalive = parentNetwork.DefaultKeepalive
  106. }
  107. if node.PostUp == "" {
  108. postup := parentNetwork.DefaultPostUp
  109. node.PostUp = postup
  110. }
  111. node.CheckInInterval = parentNetwork.DefaultCheckInInterval
  112. }
  113. func StringWithCharset(length int, charset string) string {
  114. b := make([]byte, length)
  115. for i := range b {
  116. b[i] = charset[seededRand.Intn(len(charset))]
  117. }
  118. return string(b)
  119. }
  120. //Check for valid IPv4 address
  121. //Note: We dont handle IPv6 AT ALL!!!!! This definitely is needed at some point
  122. //But for iteration 1, lets just stick to IPv4. Keep it simple stupid.
  123. func IsIpv4Net(host string) bool {
  124. return net.ParseIP(host) != nil
  125. }