node.go 14 KB

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