node.go 16 KB

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