node.go 14 KB

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