node.go 14 KB

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