node.go 15 KB

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