node.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package models
  2. import (
  3. "bytes"
  4. "errors"
  5. "math/rand"
  6. "net"
  7. "strings"
  8. "time"
  9. "golang.org/x/crypto/bcrypt"
  10. )
  11. const charset = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
  12. const TEN_YEARS_IN_SECONDS = 300000000
  13. const MAX_NAME_LENGTH = 62
  14. // == ACTIONS == (can only be set by GRPC)
  15. const NODE_UPDATE_KEY = "updatekey"
  16. const NODE_SERVER_NAME = "netmaker"
  17. const NODE_DELETE = "delete"
  18. const NODE_IS_PENDING = "pending"
  19. const NODE_NOOP = "noop"
  20. var seededRand *rand.Rand = rand.New(
  21. rand.NewSource(time.Now().UnixNano()))
  22. // node struct
  23. type Node struct {
  24. ID string `json:"id,omitempty" bson:"id,omitempty"`
  25. Address string `json:"address" bson:"address" yaml:"address" validate:"omitempty,ipv4"`
  26. Address6 string `json:"address6" bson:"address6" yaml:"address6" validate:"omitempty,ipv6"`
  27. LocalAddress string `json:"localaddress" bson:"localaddress" yaml:"localaddress" validate:"omitempty,ip"`
  28. Name string `json:"name" bson:"name" yaml:"name" validate:"omitempty,max=62,in_charset"`
  29. NetworkSettings Network `json:"networksettings" bson:"networksettings" yaml:"networksettings" validate:"-"`
  30. ListenPort int32 `json:"listenport" bson:"listenport" yaml:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`
  31. PublicKey string `json:"publickey" bson:"publickey" yaml:"publickey" validate:"required,base64"`
  32. Endpoint string `json:"endpoint" bson:"endpoint" yaml:"endpoint" validate:"required,ip"`
  33. PostUp string `json:"postup" bson:"postup" yaml:"postup"`
  34. PostDown string `json:"postdown" bson:"postdown" yaml:"postdown"`
  35. AllowedIPs []string `json:"allowedips" bson:"allowedips" yaml:"allowedips"`
  36. PersistentKeepalive int32 `json:"persistentkeepalive" bson:"persistentkeepalive" yaml:"persistentkeepalive" validate:"omitempty,numeric,max=1000"`
  37. SaveConfig string `json:"saveconfig" bson:"saveconfig" yaml:"saveconfig" validate:"checkyesorno"`
  38. AccessKey string `json:"accesskey" bson:"accesskey" yaml:"accesskey"`
  39. Interface string `json:"interface" bson:"interface" yaml:"interface"`
  40. LastModified int64 `json:"lastmodified" bson:"lastmodified" yaml:"lastmodified"`
  41. KeyUpdateTimeStamp int64 `json:"keyupdatetimestamp" bson:"keyupdatetimestamp" yaml:"keyupdatetimestamp"`
  42. ExpirationDateTime int64 `json:"expdatetime" bson:"expdatetime" yaml:"expdatetime"`
  43. LastPeerUpdate int64 `json:"lastpeerupdate" bson:"lastpeerupdate" yaml:"lastpeerupdate"`
  44. LastCheckIn int64 `json:"lastcheckin" bson:"lastcheckin" yaml:"lastcheckin"`
  45. MacAddress string `json:"macaddress" bson:"macaddress" yaml:"macaddress" validate:"required,min=5,macaddress_unique"`
  46. // checkin interval is depreciated at the network level. Set on server with CHECKIN_INTERVAL
  47. CheckInInterval int32 `json:"checkininterval" bson:"checkininterval" yaml:"checkininterval"`
  48. Password string `json:"password" bson:"password" yaml:"password" validate:"required,min=6"`
  49. Network string `json:"network" bson:"network" yaml:"network" validate:"network_exists"`
  50. IsRelayed string `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"`
  51. IsPending string `json:"ispending" bson:"ispending" yaml:"ispending"`
  52. IsRelay string `json:"isrelay" bson:"isrelay" yaml:"isrelay" validate:"checkyesorno"`
  53. IsEgressGateway string `json:"isegressgateway" bson:"isegressgateway" yaml:"isegressgateway"`
  54. IsIngressGateway string `json:"isingressgateway" bson:"isingressgateway" yaml:"isingressgateway"`
  55. EgressGatewayRanges []string `json:"egressgatewayranges" bson:"egressgatewayranges" yaml:"egressgatewayranges"`
  56. RelayAddrs []string `json:"relayaddrs" bson:"relayaddrs" yaml:"relayaddrs"`
  57. IngressGatewayRange string `json:"ingressgatewayrange" bson:"ingressgatewayrange" yaml:"ingressgatewayrange"`
  58. IsStatic string `json:"isstatic" bson:"isstatic" yaml:"isstatic" validate:"checkyesorno"`
  59. UDPHolePunch string `json:"udpholepunch" bson:"udpholepunch" yaml:"udpholepunch" validate:"checkyesorno"`
  60. PullChanges string `json:"pullchanges" bson:"pullchanges" yaml:"pullchanges" validate:"checkyesorno"`
  61. DNSOn string `json:"dnson" bson:"dnson" yaml:"dnson" validate:"checkyesorno"`
  62. IsDualStack string `json:"isdualstack" bson:"isdualstack" yaml:"isdualstack" validate:"checkyesorno"`
  63. IsServer string `json:"isserver" bson:"isserver" yaml:"isserver" validate:"checkyesorno"`
  64. Action string `json:"action" bson:"action" yaml:"action"`
  65. IsLocal string `json:"islocal" bson:"islocal" yaml:"islocal" validate:"checkyesorno"`
  66. LocalRange string `json:"localrange" bson:"localrange" yaml:"localrange"`
  67. Roaming string `json:"roaming" bson:"roaming" yaml:"roaming" validate:"checkyesorno"`
  68. IPForwarding string `json:"ipforwarding" bson:"ipforwarding" yaml:"ipforwarding" validate:"checkyesorno"`
  69. OS string `json:"os" bson:"os" yaml:"os"`
  70. MTU int32 `json:"mtu" bson:"mtu" yaml:"mtu"`
  71. }
  72. type NodesArray []Node
  73. func (a NodesArray) Len() int { return len(a) }
  74. func (a NodesArray) Less(i, j int) bool { return isLess(a[i].Address, a[j].Address) }
  75. func (a NodesArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  76. func isLess(ipA string, ipB string) bool {
  77. ipNetA := net.ParseIP(ipA)
  78. ipNetB := net.ParseIP(ipB)
  79. return bytes.Compare(ipNetA, ipNetB) < 0
  80. }
  81. func (node *Node) SetDefaultMTU() {
  82. if node.MTU == 0 {
  83. node.MTU = 1280
  84. }
  85. }
  86. func (node *Node) SetDefaulIsPending() {
  87. if node.IsPending == "" {
  88. node.IsPending = "no"
  89. }
  90. }
  91. func (node *Node) SetDefaultIsRelayed() {
  92. if node.IsRelayed == "" {
  93. node.IsRelayed = "no"
  94. }
  95. }
  96. func (node *Node) SetDefaultIsRelay() {
  97. if node.IsRelay == "" {
  98. node.IsRelay = "no"
  99. }
  100. }
  101. func (node *Node) SetDefaultEgressGateway() {
  102. if node.IsEgressGateway == "" {
  103. node.IsEgressGateway = "no"
  104. }
  105. }
  106. func (node *Node) SetDefaultIngressGateway() {
  107. if node.IsIngressGateway == "" {
  108. node.IsIngressGateway = "no"
  109. }
  110. }
  111. func (node *Node) SetDefaultAction() {
  112. if node.Action == "" {
  113. node.Action = NODE_NOOP
  114. }
  115. }
  116. func (node *Node) SetRoamingDefault() {
  117. if node.Roaming == "" {
  118. node.Roaming = "yes"
  119. }
  120. }
  121. func (node *Node) SetPullChangesDefault() {
  122. if node.PullChanges == "" {
  123. node.PullChanges = "no"
  124. }
  125. }
  126. func (node *Node) SetIPForwardingDefault() {
  127. if node.IPForwarding == "" {
  128. node.IPForwarding = "yes"
  129. }
  130. }
  131. func (node *Node) SetIsLocalDefault() {
  132. if node.IsLocal == "" {
  133. node.IsLocal = "no"
  134. }
  135. }
  136. func (node *Node) SetDNSOnDefault() {
  137. if node.DNSOn == "" {
  138. node.DNSOn = "yes"
  139. }
  140. }
  141. func (node *Node) SetIsDualStackDefault() {
  142. if node.IsDualStack == "" {
  143. node.IsDualStack = "no"
  144. }
  145. }
  146. func (node *Node) SetIsServerDefault() {
  147. if node.IsServer != "yes" {
  148. node.IsServer = "no"
  149. }
  150. }
  151. func (node *Node) SetIsStaticDefault() {
  152. if node.IsServer == "yes" {
  153. node.IsStatic = "yes"
  154. } else if node.IsStatic != "yes" {
  155. node.IsStatic = "no"
  156. }
  157. }
  158. func (node *Node) SetLastModified() {
  159. node.LastModified = time.Now().Unix()
  160. }
  161. func (node *Node) SetLastCheckIn() {
  162. node.LastCheckIn = time.Now().Unix()
  163. }
  164. func (node *Node) SetLastPeerUpdate() {
  165. node.LastPeerUpdate = time.Now().Unix()
  166. }
  167. func (node *Node) SetID() {
  168. node.ID = node.MacAddress + "###" + node.Network
  169. }
  170. func (node *Node) SetExpirationDateTime() {
  171. node.ExpirationDateTime = time.Now().Unix() + TEN_YEARS_IN_SECONDS
  172. }
  173. func (node *Node) SetDefaultName() {
  174. if node.Name == "" {
  175. node.Name = GenerateNodeName()
  176. }
  177. }
  178. func (newNode *Node) Fill(currentNode *Node) {
  179. if newNode.ID == "" {
  180. newNode.ID = currentNode.ID
  181. }
  182. if newNode.Address == "" && newNode.IsStatic != "yes" {
  183. newNode.Address = currentNode.Address
  184. }
  185. if newNode.Address6 == "" && newNode.IsStatic != "yes" {
  186. newNode.Address6 = currentNode.Address6
  187. }
  188. if newNode.LocalAddress == "" {
  189. newNode.LocalAddress = currentNode.LocalAddress
  190. }
  191. if newNode.Name == "" {
  192. newNode.Name = currentNode.Name
  193. }
  194. if newNode.ListenPort == 0 && newNode.IsStatic != "yes" {
  195. newNode.ListenPort = currentNode.ListenPort
  196. }
  197. if newNode.PublicKey == "" && newNode.IsStatic != "yes" {
  198. newNode.PublicKey = currentNode.PublicKey
  199. } else {
  200. newNode.KeyUpdateTimeStamp = time.Now().Unix()
  201. }
  202. if newNode.Endpoint == "" && newNode.IsStatic != "yes" {
  203. newNode.Endpoint = currentNode.Endpoint
  204. }
  205. if newNode.PostUp == "" {
  206. newNode.PostUp = currentNode.PostUp
  207. }
  208. if newNode.PostDown == "" {
  209. newNode.PostDown = currentNode.PostDown
  210. }
  211. if newNode.AllowedIPs == nil {
  212. newNode.AllowedIPs = currentNode.AllowedIPs
  213. }
  214. if newNode.PersistentKeepalive == 0 {
  215. newNode.PersistentKeepalive = currentNode.PersistentKeepalive
  216. }
  217. if newNode.SaveConfig == "" {
  218. newNode.SaveConfig = currentNode.SaveConfig
  219. }
  220. if newNode.AccessKey == "" {
  221. newNode.AccessKey = currentNode.AccessKey
  222. }
  223. if newNode.Interface == "" {
  224. newNode.Interface = currentNode.Interface
  225. }
  226. if newNode.LastModified == 0 {
  227. newNode.LastModified = currentNode.LastModified
  228. }
  229. if newNode.KeyUpdateTimeStamp == 0 {
  230. newNode.LastModified = currentNode.LastModified
  231. }
  232. if newNode.ExpirationDateTime == 0 {
  233. newNode.ExpirationDateTime = currentNode.ExpirationDateTime
  234. }
  235. if newNode.LastPeerUpdate == 0 {
  236. newNode.LastPeerUpdate = currentNode.LastPeerUpdate
  237. }
  238. if newNode.LastCheckIn == 0 {
  239. newNode.LastCheckIn = currentNode.LastCheckIn
  240. }
  241. if newNode.MacAddress == "" {
  242. newNode.MacAddress = currentNode.MacAddress
  243. }
  244. if newNode.CheckInInterval == 0 {
  245. newNode.CheckInInterval = currentNode.CheckInInterval
  246. }
  247. if newNode.Password != "" {
  248. err := bcrypt.CompareHashAndPassword([]byte(newNode.Password), []byte(currentNode.Password))
  249. if err != nil && currentNode.Password != newNode.Password {
  250. hash, err := bcrypt.GenerateFromPassword([]byte(newNode.Password), 5)
  251. if err == nil {
  252. newNode.Password = string(hash)
  253. }
  254. }
  255. } else {
  256. newNode.Password = currentNode.Password
  257. }
  258. if newNode.Network == "" {
  259. newNode.Network = currentNode.Network
  260. }
  261. if newNode.IsPending == "" {
  262. newNode.IsPending = currentNode.IsPending
  263. }
  264. if newNode.IsEgressGateway == "" {
  265. newNode.IsEgressGateway = currentNode.IsEgressGateway
  266. }
  267. if newNode.IsIngressGateway == "" {
  268. newNode.IsIngressGateway = currentNode.IsIngressGateway
  269. }
  270. if newNode.EgressGatewayRanges == nil {
  271. newNode.EgressGatewayRanges = currentNode.EgressGatewayRanges
  272. }
  273. if newNode.IngressGatewayRange == "" {
  274. newNode.IngressGatewayRange = currentNode.IngressGatewayRange
  275. }
  276. if newNode.IsStatic == "" {
  277. newNode.IsStatic = currentNode.IsStatic
  278. }
  279. if newNode.UDPHolePunch == "" {
  280. newNode.UDPHolePunch = currentNode.SaveConfig
  281. }
  282. if newNode.DNSOn == "" {
  283. newNode.DNSOn = currentNode.DNSOn
  284. }
  285. if newNode.IsDualStack == "" {
  286. newNode.IsDualStack = currentNode.IsDualStack
  287. }
  288. if newNode.IsLocal == "" {
  289. newNode.IsLocal = currentNode.IsLocal
  290. }
  291. if newNode.IPForwarding == "" {
  292. newNode.IPForwarding = currentNode.IPForwarding
  293. }
  294. if newNode.PullChanges == "" {
  295. newNode.PullChanges = currentNode.PullChanges
  296. }
  297. if newNode.Roaming == "" {
  298. newNode.Roaming = currentNode.Roaming
  299. }
  300. if newNode.Action == "" {
  301. newNode.Action = currentNode.Action
  302. }
  303. if newNode.IsServer == "" {
  304. newNode.IsServer = currentNode.IsServer
  305. }
  306. if newNode.IsServer == "yes" {
  307. newNode.IsStatic = "yes"
  308. }
  309. if newNode.MTU == 0 {
  310. newNode.MTU = currentNode.MTU
  311. }
  312. if newNode.OS == "" {
  313. newNode.OS = currentNode.OS
  314. }
  315. if newNode.RelayAddrs == nil {
  316. newNode.RelayAddrs = currentNode.RelayAddrs
  317. }
  318. if newNode.IsRelay == "" {
  319. newNode.IsRelay = currentNode.IsRelay
  320. }
  321. if newNode.IsRelayed == "" {
  322. newNode.IsRelayed = currentNode.IsRelayed
  323. }
  324. }
  325. func StringWithCharset(length int, charset string) string {
  326. b := make([]byte, length)
  327. for i := range b {
  328. b[i] = charset[seededRand.Intn(len(charset))]
  329. }
  330. return string(b)
  331. }
  332. //Check for valid IPv4 address
  333. //Note: We dont handle IPv6 AT ALL!!!!! This definitely is needed at some point
  334. //But for iteration 1, lets just stick to IPv4. Keep it simple stupid.
  335. func IsIpv4Net(host string) bool {
  336. return net.ParseIP(host) != nil
  337. }
  338. func (node *Node) NameInNodeCharSet() bool {
  339. charset := "abcdefghijklmnopqrstuvwxyz1234567890-"
  340. for _, char := range node.Name {
  341. if !strings.Contains(charset, strings.ToLower(string(char))) {
  342. return false
  343. }
  344. }
  345. return true
  346. }
  347. func (node *Node) GetID() (string, error) {
  348. if node.MacAddress == "" || node.Network == "" {
  349. return "", errors.New("unable to get record key")
  350. }
  351. return node.MacAddress + "###" + node.Network, nil
  352. }