node.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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:"omitempty,ipv4"`
  18. Address6 string `json:"address6" bson:"address6" validate:"omitempty,ipv6"`
  19. LocalAddress string `json:"localaddress" bson:"localaddress" validate:"omitempty,ip"`
  20. Name string `json:"name" bson:"name" validate:"omitempty,max=12,in_charset"`
  21. ListenPort int32 `json:"listenport" bson:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`
  22. PublicKey string `json:"publickey" bson:"publickey" validate:"required,base64"`
  23. Endpoint string `json:"endpoint" bson:"endpoint" validate:"required,ip"`
  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,mac,macaddress_unique"`
  37. CheckInInterval int32 `json:"checkininterval" bson:"checkininterval"`
  38. Password string `json:"password" bson:"password" validate:"required,min=6"`
  39. Network string `json:"network" bson:"network" validate:"network_exists"`
  40. IsPending bool `json:"ispending" bson:"ispending"`
  41. IsEgressGateway bool `json:"isegressgateway" bson:"isegressgateway"`
  42. IsIngressGateway bool `json:"isingressgateway" bson:"isingressgateway"`
  43. EgressGatewayRanges []string `json:"egressgatewayranges" bson:"egressgatewayranges"`
  44. IngressGatewayRange string `json:"ingressgatewayrange" bson:"ingressgatewayrange"`
  45. PostChanges string `json:"postchanges" bson:"postchanges"`
  46. StaticIP string `json:"staticip" bson:"staticip"`
  47. StaticPubKey string `json:"staticpubkey" bson:"staticpubkey"`
  48. }
  49. //node update struct --- only validations are different
  50. type NodeUpdate struct {
  51. ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
  52. Address string `json:"address" bson:"address" validate:"omitempty,ip"`
  53. Address6 string `json:"address6" bson:"address6" validate:"omitempty,ipv6"`
  54. LocalAddress string `json:"localaddress" bson:"localaddress" validate:"omitempty,ip"`
  55. Name string `json:"name" bson:"name" validate:"omitempty,max=12,in_charset"`
  56. ListenPort int32 `json:"listenport" bson:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`
  57. PublicKey string `json:"publickey" bson:"publickey" validate:"omitempty,base64"`
  58. Endpoint string `json:"endpoint" bson:"endpoint" validate:"omitempty,ip"`
  59. PostUp string `json:"postup" bson:"postup"`
  60. PostDown string `json:"postdown" bson:"postdown"`
  61. AllowedIPs []string `json:"allowedips" bson:"allowedips"`
  62. PersistentKeepalive int32 `json:"persistentkeepalive" bson:"persistentkeepalive" validate:"omitempty,numeric,max=1000"`
  63. SaveConfig *bool `json:"saveconfig" bson:"saveconfig"`
  64. AccessKey string `json:"accesskey" bson:"accesskey"`
  65. Interface string `json:"interface" bson:"interface"`
  66. LastModified int64 `json:"lastmodified" bson:"lastmodified"`
  67. KeyUpdateTimeStamp int64 `json:"keyupdatetimestamp" bson:"keyupdatetimestamp"`
  68. ExpirationDateTime int64 `json:"expdatetime" bson:"expdatetime"`
  69. LastPeerUpdate int64 `json:"lastpeerupdate" bson:"lastpeerupdate"`
  70. LastCheckIn int64 `json:"lastcheckin" bson:"lastcheckin"`
  71. MacAddress string `json:"macaddress" bson:"macaddress" validate:"required,mac"`
  72. CheckInInterval int32 `json:"checkininterval" bson:"checkininterval"`
  73. Password string `json:"password" bson:"password" validate:"omitempty,min=5"`
  74. Network string `json:"network" bson:"network" validate:"network_exists"`
  75. IsPending bool `json:"ispending" bson:"ispending"`
  76. IsIngressGateway bool `json:"isingressgateway" bson:"isingressgateway"`
  77. IsEgressGateway bool `json:"isegressgateway" bson:"isegressgateway"`
  78. IngressGatewayRange string `json:"ingressgatewayrange" bson:"ingressgatewayrange"`
  79. EgressGatewayRanges []string `json:"egressgatewayranges" bson:"egressgatewayranges"`
  80. PostChanges string `json:"postchanges" bson:"postchanges"`
  81. StaticIP string `json:"staticip" bson:"staticip"`
  82. StaticPubKey string `json:"staticpubkey" bson:"staticpubkey"`
  83. }
  84. //Duplicated function for NodeUpdates
  85. func (node *NodeUpdate) GetNetwork() (Network, error) {
  86. var network Network
  87. collection := mongoconn.NetworkDB
  88. //collection := mongoconn.Client.Database("netmaker").Collection("networks")
  89. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  90. filter := bson.M{"netid": node.Network}
  91. err := collection.FindOne(ctx, filter).Decode(&network)
  92. defer cancel()
  93. if err != nil {
  94. //log.Fatal(err)
  95. return network, err
  96. }
  97. return network, err
  98. }
  99. //TODO: Contains a fatal error return. Need to change
  100. //Used in contexts where it's not the Parent network.
  101. func (node *Node) GetNetwork() (Network, error) {
  102. var network Network
  103. //collection := mongoconn.NetworkDB
  104. collection := mongoconn.Client.Database("netmaker").Collection("networks")
  105. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  106. filter := bson.M{"netid": node.Network}
  107. err := collection.FindOne(ctx, filter).Decode(&network)
  108. defer cancel()
  109. if err != nil {
  110. //log.Fatal(err)
  111. return network, err
  112. }
  113. return network, err
  114. }
  115. //TODO:
  116. //Not sure if below two methods are necessary. May want to revisit
  117. func (node *Node) SetLastModified() {
  118. node.LastModified = time.Now().Unix()
  119. }
  120. func (node *Node) SetLastCheckIn() {
  121. node.LastCheckIn = time.Now().Unix()
  122. }
  123. func (node *Node) SetLastPeerUpdate() {
  124. node.LastPeerUpdate = time.Now().Unix()
  125. }
  126. func (node *Node) SetExpirationDateTime() {
  127. node.ExpirationDateTime = time.Unix(33174902665, 0).Unix()
  128. }
  129. func (node *Node) SetDefaultName() {
  130. if node.Name == "" {
  131. nodeid := StringWithCharset(5, charset)
  132. nodename := "node-" + nodeid
  133. node.Name = nodename
  134. }
  135. }
  136. //TODO: I dont know why this exists
  137. //This should exist on the node.go struct. I'm sure there was a reason?
  138. func (node *Node) SetDefaults() {
  139. //TODO: Maybe I should make Network a part of the node struct. Then we can just query the Network object for stuff.
  140. parentNetwork, _ := node.GetNetwork()
  141. node.ExpirationDateTime = time.Unix(33174902665, 0).Unix()
  142. if node.ListenPort == 0 {
  143. node.ListenPort = parentNetwork.DefaultListenPort
  144. }
  145. if node.PostDown == "" {
  146. //Empty because we dont set it
  147. //may want to set it to something in the future
  148. }
  149. //TODO: This is dumb and doesn't work
  150. //Need to change
  151. if node.SaveConfig == nil {
  152. if parentNetwork.DefaultSaveConfig != nil {
  153. defaultsave := *parentNetwork.DefaultSaveConfig
  154. node.SaveConfig = &defaultsave
  155. }
  156. }
  157. if node.Interface == "" {
  158. node.Interface = parentNetwork.DefaultInterface
  159. }
  160. if node.PersistentKeepalive == 0 {
  161. node.PersistentKeepalive = parentNetwork.DefaultKeepalive
  162. }
  163. if node.PostUp == "" {
  164. postup := parentNetwork.DefaultPostUp
  165. node.PostUp = postup
  166. }
  167. if node.StaticIP == "" {
  168. node.StaticIP = "no"
  169. }
  170. if node.StaticPubKey == "" {
  171. node.StaticPubKey = "no"
  172. }
  173. node.CheckInInterval = parentNetwork.DefaultCheckInInterval
  174. }
  175. func StringWithCharset(length int, charset string) string {
  176. b := make([]byte, length)
  177. for i := range b {
  178. b[i] = charset[seededRand.Intn(len(charset))]
  179. }
  180. return string(b)
  181. }
  182. //Check for valid IPv4 address
  183. //Note: We dont handle IPv6 AT ALL!!!!! This definitely is needed at some point
  184. //But for iteration 1, lets just stick to IPv4. Keep it simple stupid.
  185. func IsIpv4Net(host string) bool {
  186. return net.ParseIP(host) != nil
  187. }