node.go 17 KB

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