node.go 14 KB

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