node.go 8.6 KB

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