node.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. package models
  2. import (
  3. "bytes"
  4. "math/rand"
  5. "net"
  6. "strings"
  7. "time"
  8. "github.com/google/uuid"
  9. "golang.org/x/crypto/bcrypt"
  10. "golang.zx2c4.com/wireguard/wgctrl/wgtypes"
  11. )
  12. const (
  13. // NODE_SERVER_NAME - the default server name
  14. NODE_SERVER_NAME = "netmaker"
  15. // TEN_YEARS_IN_SECONDS - ten years in seconds
  16. TEN_YEARS_IN_SECONDS = 300000000
  17. // MAX_NAME_LENGTH - max name length of node
  18. MAX_NAME_LENGTH = 62
  19. // == ACTIONS == (can only be set by server)
  20. // NODE_UPDATE_KEY - action to update key
  21. NODE_UPDATE_KEY = "updatekey"
  22. // NODE_DELETE - delete node action
  23. NODE_DELETE = "delete"
  24. // NODE_IS_PENDING - node pending status
  25. NODE_IS_PENDING = "pending"
  26. // NODE_NOOP - node no op action
  27. NODE_NOOP = "noop"
  28. // NODE_FORCE_UPDATE - indicates a node should pull all changes
  29. NODE_FORCE_UPDATE = "force"
  30. // FIREWALL_IPTABLES - indicates that iptables is the firewall in use
  31. FIREWALL_IPTABLES = "iptables"
  32. // FIREWALL_NFTABLES - indicates nftables is in use (Linux only)
  33. FIREWALL_NFTABLES = "nftables"
  34. // FIREWALL_NONE - indicates that no supported firewall in use
  35. FIREWALL_NONE = "none"
  36. )
  37. var seededRand *rand.Rand = rand.New(
  38. rand.NewSource(time.Now().UnixNano()))
  39. // NodeCheckin - struct for node checkins with server
  40. type NodeCheckin struct {
  41. Version string
  42. Connected string
  43. Ifaces []Iface
  44. }
  45. // Iface struct for local interfaces of a node
  46. type Iface struct {
  47. Name string `json:"name"`
  48. Address net.IPNet `json:"address"`
  49. AddressString string `json:"addressString"`
  50. }
  51. // CommonNode - represents a commonn node data elements shared by netmaker and netclient
  52. type CommonNode struct {
  53. ID uuid.UUID `json:"id" yaml:"id"`
  54. Network string `json:"network" yaml:"network"`
  55. NetworkRange net.IPNet `json:"networkrange" yaml:"networkrange"`
  56. NetworkRange6 net.IPNet `json:"networkrange6" yaml:"networkrange6"`
  57. InternetGateway *net.UDPAddr `json:"internetgateway" yaml:"internetgateway"`
  58. Server string `json:"server" yaml:"server"`
  59. Connected bool `json:"connected" yaml:"connected"`
  60. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  61. EndpointIP net.IP `json:"endpointip" yaml:"endpointip"`
  62. Address net.IPNet `json:"address" yaml:"address"`
  63. Address6 net.IPNet `json:"address6" yaml:"address6"`
  64. PostUp string `json:"postup" yaml:"postup"`
  65. PostDown string `json:"postdown" yaml:"postdown"`
  66. Action string `json:"action" yaml:"action"`
  67. IsServer bool `json:"isserver" yaml:"isserver"`
  68. IsLocal bool `json:"islocal" yaml:"islocal"`
  69. IsEgressGateway bool `json:"isegressgateway" yaml:"isegressgateway"`
  70. IsIngressGateway bool `json:"isingressgateway" yaml:"isingressgateway"`
  71. IsStatic bool `json:"isstatic" yaml:"isstatic"`
  72. IsPending bool `json:"ispending" yaml:"ispending"`
  73. DNSOn bool `json:"dnson" yaml:"dnson"`
  74. IsHub bool `json:"ishub" yaml:"ishub"`
  75. PersistentKeepalive int `json:"persistentkeepalive" yaml:"persistentkeepalive"`
  76. Peers []wgtypes.PeerConfig `json:"peers" yaml:"peers"`
  77. Proxy bool `json:"proxy" bson:"proxy" yaml:"proxy"`
  78. }
  79. // Node - a model of a network node
  80. type Node struct {
  81. CommonNode
  82. PendingDelete bool `json:"pendingdelete" bson:"pendingdelete" yaml:"pendingdelete"`
  83. // == PRO ==
  84. DefaultACL string `json:"defaultacl,omitempty" bson:"defaultacl,omitempty" yaml:"defaultacl,omitempty" validate:"checkyesornoorunset"`
  85. OwnerID string `json:"ownerid,omitempty" bson:"ownerid,omitempty" yaml:"ownerid,omitempty"`
  86. }
  87. // LegacyNode - legacy struct for node model
  88. type LegacyNode struct {
  89. ID string `json:"id,omitempty" bson:"id,omitempty" yaml:"id,omitempty" validate:"required,min=5,id_unique"`
  90. HostID string `json:"hostid,omitempty" bson:"id,omitempty" yaml:"hostid,omitempty" validate:"required,min=5,id_unique"`
  91. Address string `json:"address" bson:"address" yaml:"address" validate:"omitempty,ipv4"`
  92. Address6 string `json:"address6" bson:"address6" yaml:"address6" validate:"omitempty,ipv6"`
  93. LocalAddress string `json:"localaddress" bson:"localaddress" yaml:"localaddress" validate:"omitempty"`
  94. Interfaces []Iface `json:"interfaces" yaml:"interfaces"`
  95. Name string `json:"name" bson:"name" yaml:"name" validate:"omitempty,max=62,in_charset"`
  96. NetworkSettings Network `json:"networksettings" bson:"networksettings" yaml:"networksettings" validate:"-"`
  97. ListenPort int32 `json:"listenport" bson:"listenport" yaml:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`
  98. LocalListenPort int32 `json:"locallistenport" bson:"locallistenport" yaml:"locallistenport" validate:"numeric,min=0,max=65535"`
  99. ProxyListenPort int32 `json:"proxy_listen_port" bson:"proxy_listen_port" yaml:"proxy_listen_port" validate:"numeric,min=0,max=65535"`
  100. PublicKey string `json:"publickey" bson:"publickey" yaml:"publickey" validate:"required,base64"`
  101. Endpoint string `json:"endpoint" bson:"endpoint" yaml:"endpoint" validate:"required,ip"`
  102. PostUp string `json:"postup" bson:"postup" yaml:"postup"`
  103. PostDown string `json:"postdown" bson:"postdown" yaml:"postdown"`
  104. AllowedIPs []string `json:"allowedips" bson:"allowedips" yaml:"allowedips"`
  105. PersistentKeepalive int32 `json:"persistentkeepalive" bson:"persistentkeepalive" yaml:"persistentkeepalive" validate:"omitempty,numeric,max=1000"`
  106. IsHub string `json:"ishub" bson:"ishub" yaml:"ishub" validate:"checkyesorno"`
  107. AccessKey string `json:"accesskey" bson:"accesskey" yaml:"accesskey"`
  108. Interface string `json:"interface" bson:"interface" yaml:"interface"`
  109. LastModified int64 `json:"lastmodified" bson:"lastmodified" yaml:"lastmodified"`
  110. ExpirationDateTime int64 `json:"expdatetime" bson:"expdatetime" yaml:"expdatetime"`
  111. LastPeerUpdate int64 `json:"lastpeerupdate" bson:"lastpeerupdate" yaml:"lastpeerupdate"`
  112. LastCheckIn int64 `json:"lastcheckin" bson:"lastcheckin" yaml:"lastcheckin"`
  113. MacAddress string `json:"macaddress" bson:"macaddress" yaml:"macaddress"`
  114. Password string `json:"password" bson:"password" yaml:"password" validate:"required,min=6"`
  115. Network string `json:"network" bson:"network" yaml:"network" validate:"network_exists"`
  116. IsRelayed string `json:"isrelayed" bson:"isrelayed" yaml:"isrelayed"`
  117. IsPending string `json:"ispending" bson:"ispending" yaml:"ispending"`
  118. IsRelay string `json:"isrelay" bson:"isrelay" yaml:"isrelay" validate:"checkyesorno"`
  119. IsDocker string `json:"isdocker" bson:"isdocker" yaml:"isdocker" validate:"checkyesorno"`
  120. IsK8S string `json:"isk8s" bson:"isk8s" yaml:"isk8s" validate:"checkyesorno"`
  121. IsEgressGateway string `json:"isegressgateway" bson:"isegressgateway" yaml:"isegressgateway" validate:"checkyesorno"`
  122. IsIngressGateway string `json:"isingressgateway" bson:"isingressgateway" yaml:"isingressgateway" validate:"checkyesorno"`
  123. EgressGatewayRanges []string `json:"egressgatewayranges" bson:"egressgatewayranges" yaml:"egressgatewayranges"`
  124. EgressGatewayNatEnabled string `json:"egressgatewaynatenabled" bson:"egressgatewaynatenabled" yaml:"egressgatewaynatenabled"`
  125. EgressGatewayRequest EgressGatewayRequest `json:"egressgatewayrequest" bson:"egressgatewayrequest" yaml:"egressgatewayrequest"`
  126. RelayAddrs []string `json:"relayaddrs" bson:"relayaddrs" yaml:"relayaddrs"`
  127. FailoverNode string `json:"failovernode" bson:"failovernode" yaml:"failovernode"`
  128. IngressGatewayRange string `json:"ingressgatewayrange" bson:"ingressgatewayrange" yaml:"ingressgatewayrange"`
  129. IngressGatewayRange6 string `json:"ingressgatewayrange6" bson:"ingressgatewayrange6" yaml:"ingressgatewayrange6"`
  130. // IsStatic - refers to if the Endpoint is set manually or dynamically
  131. IsStatic string `json:"isstatic" bson:"isstatic" yaml:"isstatic" validate:"checkyesorno"`
  132. UDPHolePunch string `json:"udpholepunch" bson:"udpholepunch" yaml:"udpholepunch" validate:"checkyesorno"`
  133. DNSOn string `json:"dnson" bson:"dnson" yaml:"dnson" validate:"checkyesorno"`
  134. IsServer string `json:"isserver" bson:"isserver" yaml:"isserver" validate:"checkyesorno"`
  135. Action string `json:"action" bson:"action" yaml:"action"`
  136. IsLocal string `json:"islocal" bson:"islocal" yaml:"islocal" validate:"checkyesorno"`
  137. LocalRange string `json:"localrange" bson:"localrange" yaml:"localrange"`
  138. IPForwarding string `json:"ipforwarding" bson:"ipforwarding" yaml:"ipforwarding" validate:"checkyesorno"`
  139. OS string `json:"os" bson:"os" yaml:"os"`
  140. MTU int32 `json:"mtu" bson:"mtu" yaml:"mtu"`
  141. Version string `json:"version" bson:"version" yaml:"version"`
  142. Server string `json:"server" bson:"server" yaml:"server"`
  143. TrafficKeys TrafficKeys `json:"traffickeys" bson:"traffickeys" yaml:"traffickeys"`
  144. FirewallInUse string `json:"firewallinuse" bson:"firewallinuse" yaml:"firewallinuse"`
  145. InternetGateway string `json:"internetgateway" bson:"internetgateway" yaml:"internetgateway"`
  146. Connected string `json:"connected" bson:"connected" yaml:"connected" validate:"checkyesorno"`
  147. PendingDelete bool `json:"pendingdelete" bson:"pendingdelete" yaml:"pendingdelete"`
  148. Proxy bool `json:"proxy" bson:"proxy" yaml:"proxy"`
  149. // == PRO ==
  150. DefaultACL string `json:"defaultacl,omitempty" bson:"defaultacl,omitempty" yaml:"defaultacl,omitempty" validate:"checkyesornoorunset"`
  151. OwnerID string `json:"ownerid,omitempty" bson:"ownerid,omitempty" yaml:"ownerid,omitempty"`
  152. Failover string `json:"failover" bson:"failover" yaml:"failover" validate:"checkyesorno"`
  153. }
  154. // NodesArray - used for node sorting
  155. type NodesArray []LegacyNode
  156. // NodesArray.Len - gets length of node array
  157. func (a NodesArray) Len() int { return len(a) }
  158. // NodesArray.Less - gets returns lower rank of two node addresses
  159. func (a NodesArray) Less(i, j int) bool { return isLess(a[i].Address, a[j].Address) }
  160. // NodesArray.Swap - swaps two nodes in array
  161. func (a NodesArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  162. func isLess(ipA string, ipB string) bool {
  163. ipNetA := net.ParseIP(ipA)
  164. ipNetB := net.ParseIP(ipB)
  165. return bytes.Compare(ipNetA, ipNetB) < 0
  166. }
  167. // Node.PrimaryAddress - return ipv4 address if present, else return ipv6
  168. func (node *LegacyNode) PrimaryAddress() string {
  169. if node.Address != "" {
  170. return node.Address
  171. }
  172. return node.Address6
  173. }
  174. // Node.SetDefaultConnected
  175. func (node *LegacyNode) SetDefaultConnected() {
  176. if node.Connected == "" {
  177. node.Connected = "yes"
  178. }
  179. if node.IsServer == "yes" {
  180. node.Connected = "yes"
  181. }
  182. }
  183. // Node.SetDefaultACL
  184. func (node *LegacyNode) SetDefaultACL() {
  185. if node.DefaultACL == "" {
  186. node.DefaultACL = "yes"
  187. }
  188. }
  189. // Node.SetDefaultMTU - sets default MTU of a node
  190. func (node *LegacyNode) SetDefaultMTU() {
  191. if node.MTU == 0 {
  192. node.MTU = 1280
  193. }
  194. }
  195. // Node.SetDefaultNFTablesPresent - sets default for nftables check
  196. func (node *LegacyNode) SetDefaultNFTablesPresent() {
  197. if node.FirewallInUse == "" {
  198. node.FirewallInUse = FIREWALL_IPTABLES // default to iptables
  199. }
  200. }
  201. // Node.SetDefaulIsPending - sets ispending default
  202. func (node *LegacyNode) SetDefaulIsPending() {
  203. if node.IsPending == "" {
  204. node.IsPending = "no"
  205. }
  206. }
  207. // Node.SetDefaultIsRelayed - set default is relayed
  208. func (node *LegacyNode) SetDefaultIsRelayed() {
  209. if node.IsRelayed == "" {
  210. node.IsRelayed = "no"
  211. }
  212. }
  213. // Node.SetDefaultIsRelayed - set default is relayed
  214. func (node *LegacyNode) SetDefaultIsHub() {
  215. if node.IsHub == "" {
  216. node.IsHub = "no"
  217. }
  218. }
  219. // Node.SetDefaultIsRelay - set default isrelay
  220. func (node *LegacyNode) SetDefaultIsRelay() {
  221. if node.IsRelay == "" {
  222. node.IsRelay = "no"
  223. }
  224. }
  225. // Node.SetDefaultIsDocker - set default isdocker
  226. func (node *LegacyNode) SetDefaultIsDocker() {
  227. if node.IsDocker == "" {
  228. node.IsDocker = "no"
  229. }
  230. }
  231. // Node.SetDefaultIsK8S - set default isk8s
  232. func (node *LegacyNode) SetDefaultIsK8S() {
  233. if node.IsK8S == "" {
  234. node.IsK8S = "no"
  235. }
  236. }
  237. // Node.SetDefaultEgressGateway - sets default egress gateway status
  238. func (node *LegacyNode) SetDefaultEgressGateway() {
  239. if node.IsEgressGateway == "" {
  240. node.IsEgressGateway = "no"
  241. }
  242. }
  243. // Node.SetDefaultIngressGateway - sets default ingress gateway status
  244. func (node *LegacyNode) SetDefaultIngressGateway() {
  245. if node.IsIngressGateway == "" {
  246. node.IsIngressGateway = "no"
  247. }
  248. }
  249. // Node.SetDefaultAction - sets default action status
  250. func (node *LegacyNode) SetDefaultAction() {
  251. if node.Action == "" {
  252. node.Action = NODE_NOOP
  253. }
  254. }
  255. // Node.SetRoamingDefault - sets default roaming status
  256. //func (node *Node) SetRoamingDefault() {
  257. // if node.Roaming == "" {
  258. // node.Roaming = "yes"
  259. // }
  260. //}
  261. // Node.SetIPForwardingDefault - set ip forwarding default
  262. func (node *LegacyNode) SetIPForwardingDefault() {
  263. if node.IPForwarding == "" {
  264. node.IPForwarding = "yes"
  265. }
  266. }
  267. // Node.SetIsLocalDefault - set is local default
  268. func (node *LegacyNode) SetIsLocalDefault() {
  269. if node.IsLocal == "" {
  270. node.IsLocal = "no"
  271. }
  272. }
  273. // Node.SetDNSOnDefault - sets dns on default
  274. func (node *LegacyNode) SetDNSOnDefault() {
  275. if node.DNSOn == "" {
  276. node.DNSOn = "yes"
  277. }
  278. }
  279. // Node.SetIsServerDefault - sets node isserver default
  280. func (node *LegacyNode) SetIsServerDefault() {
  281. if node.IsServer != "yes" {
  282. node.IsServer = "no"
  283. }
  284. }
  285. // Node.SetIsStaticDefault - set is static default
  286. func (node *LegacyNode) SetIsStaticDefault() {
  287. if node.IsServer == "yes" {
  288. node.IsStatic = "yes"
  289. } else if node.IsStatic != "yes" {
  290. node.IsStatic = "no"
  291. }
  292. }
  293. // Node.SetLastModified - set last modified initial time
  294. func (node *LegacyNode) SetLastModified() {
  295. node.LastModified = time.Now().Unix()
  296. }
  297. // Node.SetLastCheckIn - time.Now().Unix()
  298. func (node *LegacyNode) SetLastCheckIn() {
  299. node.LastCheckIn = time.Now().Unix()
  300. }
  301. // Node.SetLastPeerUpdate - sets last peer update time
  302. func (node *LegacyNode) SetLastPeerUpdate() {
  303. node.LastPeerUpdate = time.Now().Unix()
  304. }
  305. // Node.SetExpirationDateTime - sets node expiry time
  306. func (node *LegacyNode) SetExpirationDateTime() {
  307. node.ExpirationDateTime = time.Now().Unix() + TEN_YEARS_IN_SECONDS
  308. }
  309. // Node.SetDefaultName - sets a random name to node
  310. func (node *LegacyNode) SetDefaultName() {
  311. if node.Name == "" {
  312. node.Name = GenerateNodeName()
  313. }
  314. }
  315. // Node.SetDefaultFailover - sets default value of failover status to no if not set
  316. func (node *LegacyNode) SetDefaultFailover() {
  317. if node.Failover == "" {
  318. node.Failover = "no"
  319. }
  320. }
  321. // Node.Fill - fills other node data into calling node data if not set on calling node
  322. func (newNode *LegacyNode) Fill(currentNode *LegacyNode) { // TODO add new field for nftables present
  323. newNode.ID = currentNode.ID
  324. if newNode.Address == "" {
  325. newNode.Address = currentNode.Address
  326. }
  327. if newNode.Address6 == "" {
  328. newNode.Address6 = currentNode.Address6
  329. }
  330. if newNode.LocalAddress == "" {
  331. newNode.LocalAddress = currentNode.LocalAddress
  332. }
  333. if newNode.Name == "" {
  334. newNode.Name = currentNode.Name
  335. }
  336. if newNode.ListenPort == 0 {
  337. newNode.ListenPort = currentNode.ListenPort
  338. }
  339. if newNode.LocalListenPort == 0 {
  340. newNode.LocalListenPort = currentNode.LocalListenPort
  341. }
  342. if newNode.PublicKey == "" {
  343. newNode.PublicKey = currentNode.PublicKey
  344. }
  345. if newNode.Endpoint == "" {
  346. newNode.Endpoint = currentNode.Endpoint
  347. }
  348. if newNode.PostUp == "" {
  349. newNode.PostUp = currentNode.PostUp
  350. }
  351. if newNode.PostDown == "" {
  352. newNode.PostDown = currentNode.PostDown
  353. }
  354. if newNode.AllowedIPs == nil {
  355. newNode.AllowedIPs = currentNode.AllowedIPs
  356. }
  357. if newNode.PersistentKeepalive < 0 {
  358. newNode.PersistentKeepalive = currentNode.PersistentKeepalive
  359. }
  360. if newNode.AccessKey == "" {
  361. newNode.AccessKey = currentNode.AccessKey
  362. }
  363. if newNode.Interface == "" {
  364. newNode.Interface = currentNode.Interface
  365. }
  366. if newNode.LastModified == 0 {
  367. newNode.LastModified = currentNode.LastModified
  368. }
  369. if newNode.ExpirationDateTime == 0 {
  370. newNode.ExpirationDateTime = currentNode.ExpirationDateTime
  371. }
  372. if newNode.LastPeerUpdate == 0 {
  373. newNode.LastPeerUpdate = currentNode.LastPeerUpdate
  374. }
  375. if newNode.LastCheckIn == 0 {
  376. newNode.LastCheckIn = currentNode.LastCheckIn
  377. }
  378. if newNode.MacAddress == "" {
  379. newNode.MacAddress = currentNode.MacAddress
  380. }
  381. if newNode.Password != "" {
  382. err := bcrypt.CompareHashAndPassword([]byte(newNode.Password), []byte(currentNode.Password))
  383. if err != nil && currentNode.Password != newNode.Password {
  384. hash, err := bcrypt.GenerateFromPassword([]byte(newNode.Password), 5)
  385. if err == nil {
  386. newNode.Password = string(hash)
  387. }
  388. }
  389. } else {
  390. newNode.Password = currentNode.Password
  391. }
  392. if newNode.Network == "" {
  393. newNode.Network = currentNode.Network
  394. }
  395. if newNode.IsPending == "" {
  396. newNode.IsPending = currentNode.IsPending
  397. }
  398. if newNode.IsEgressGateway == "" {
  399. newNode.IsEgressGateway = currentNode.IsEgressGateway
  400. }
  401. if newNode.IsIngressGateway == "" {
  402. newNode.IsIngressGateway = currentNode.IsIngressGateway
  403. }
  404. if newNode.EgressGatewayRanges == nil {
  405. newNode.EgressGatewayRanges = currentNode.EgressGatewayRanges
  406. }
  407. if newNode.IngressGatewayRange == "" {
  408. newNode.IngressGatewayRange = currentNode.IngressGatewayRange
  409. }
  410. if newNode.IngressGatewayRange6 == "" {
  411. newNode.IngressGatewayRange6 = currentNode.IngressGatewayRange6
  412. }
  413. if newNode.IsStatic == "" {
  414. newNode.IsStatic = currentNode.IsStatic
  415. }
  416. if newNode.UDPHolePunch == "" {
  417. newNode.UDPHolePunch = currentNode.UDPHolePunch
  418. }
  419. if newNode.DNSOn == "" {
  420. newNode.DNSOn = currentNode.DNSOn
  421. }
  422. if newNode.IsLocal == "" {
  423. newNode.IsLocal = currentNode.IsLocal
  424. }
  425. if newNode.IPForwarding == "" {
  426. newNode.IPForwarding = currentNode.IPForwarding
  427. }
  428. if newNode.Action == "" {
  429. newNode.Action = currentNode.Action
  430. }
  431. if newNode.IsServer == "" {
  432. newNode.IsServer = currentNode.IsServer
  433. }
  434. if newNode.IsServer == "yes" {
  435. newNode.IsStatic = "yes"
  436. newNode.Connected = "yes"
  437. }
  438. if newNode.MTU == 0 {
  439. newNode.MTU = currentNode.MTU
  440. }
  441. if newNode.OS == "" {
  442. newNode.OS = currentNode.OS
  443. }
  444. if newNode.RelayAddrs == nil {
  445. newNode.RelayAddrs = currentNode.RelayAddrs
  446. }
  447. if newNode.IsRelay == "" {
  448. newNode.IsRelay = currentNode.IsRelay
  449. }
  450. if newNode.IsRelayed == "" {
  451. newNode.IsRelayed = currentNode.IsRelayed
  452. }
  453. if newNode.IsDocker == "" {
  454. newNode.IsDocker = currentNode.IsDocker
  455. }
  456. if newNode.IsK8S == "" {
  457. newNode.IsK8S = currentNode.IsK8S
  458. }
  459. if newNode.Version == "" {
  460. newNode.Version = currentNode.Version
  461. }
  462. if newNode.IsHub == "" {
  463. newNode.IsHub = currentNode.IsHub
  464. }
  465. if newNode.Server == "" {
  466. newNode.Server = currentNode.Server
  467. }
  468. if newNode.Connected == "" {
  469. newNode.Connected = currentNode.Connected
  470. }
  471. if newNode.DefaultACL == "" {
  472. newNode.DefaultACL = currentNode.DefaultACL
  473. }
  474. if newNode.Failover == "" {
  475. newNode.Failover = currentNode.Failover
  476. }
  477. newNode.Proxy = currentNode.Proxy
  478. newNode.TrafficKeys = currentNode.TrafficKeys
  479. }
  480. // StringWithCharset - returns random string inside defined charset
  481. func StringWithCharset(length int, charset string) string {
  482. b := make([]byte, length)
  483. for i := range b {
  484. b[i] = charset[seededRand.Intn(len(charset))]
  485. }
  486. return string(b)
  487. }
  488. // IsIpv4Net - check for valid IPv4 address
  489. // Note: We dont handle IPv6 AT ALL!!!!! This definitely is needed at some point
  490. // But for iteration 1, lets just stick to IPv4. Keep it simple stupid.
  491. func IsIpv4Net(host string) bool {
  492. return net.ParseIP(host) != nil
  493. }
  494. // Node.NameInNodeCharset - returns if name is in charset below or not
  495. func (node *LegacyNode) NameInNodeCharSet() bool {
  496. charset := "abcdefghijklmnopqrstuvwxyz1234567890-"
  497. for _, char := range node.Name {
  498. if !strings.Contains(charset, strings.ToLower(string(char))) {
  499. return false
  500. }
  501. }
  502. return true
  503. }
  504. // == PRO ==
  505. // Node.DoesACLAllow - checks if default ACL on node is "yes"
  506. func (node *LegacyNode) DoesACLAllow() bool {
  507. return node.DefaultACL == "yes"
  508. }
  509. // Node.DoesACLDeny - checks if default ACL on node is "no"
  510. func (node *LegacyNode) DoesACLDeny() bool {
  511. return node.DefaultACL == "no"
  512. }
  513. func (ln *LegacyNode) ConvertToNewNode() (*Host, *Node) {
  514. var node Node
  515. //host:= logic.GetHost(node.HostID)
  516. var host Host
  517. if host.ID.String() == "" {
  518. host.ID = uuid.New()
  519. host.FirewallInUse = ln.FirewallInUse
  520. host.Version = ln.Version
  521. host.IPForwarding = parseBool(ln.IPForwarding)
  522. host.HostPass = ln.Password
  523. host.Name = ln.Name
  524. host.ListenPort = int(ln.ListenPort)
  525. _, cidr, _ := net.ParseCIDR(ln.LocalAddress)
  526. host.LocalAddress = *cidr
  527. _, cidr, _ = net.ParseCIDR(ln.LocalRange)
  528. host.LocalRange = *cidr
  529. host.LocalListenPort = int(ln.LocalListenPort)
  530. host.ProxyListenPort = int(ln.ProxyListenPort)
  531. host.MTU = int(ln.MTU)
  532. host.PublicKey, _ = wgtypes.ParseKey(ln.PublicKey)
  533. host.MacAddress, _ = net.ParseMAC(ln.MacAddress)
  534. host.TrafficKeyPublic = ln.TrafficKeys.Mine
  535. gateway, _ := net.ResolveUDPAddr("udp", ln.InternetGateway)
  536. host.InternetGateway = *gateway
  537. id, _ := uuid.Parse(ln.ID)
  538. host.Nodes = append(host.Nodes, id)
  539. }
  540. id, _ := uuid.Parse(ln.ID)
  541. node.ID = id
  542. node.Network = ln.Network
  543. _, cidr, _ := net.ParseCIDR(ln.NetworkSettings.AddressRange)
  544. node.NetworkRange = *cidr
  545. _, cidr, _ = net.ParseCIDR(ln.NetworkSettings.AddressRange6)
  546. node.NetworkRange6 = *cidr
  547. node.Server = ln.Server
  548. node.Connected = parseBool(ln.Connected)
  549. node.Interfaces = ln.Interfaces
  550. node.EndpointIP = net.ParseIP(ln.Endpoint)
  551. _, cidr, _ = net.ParseCIDR(ln.Address)
  552. node.Address = *cidr
  553. _, cidr, _ = net.ParseCIDR(ln.Address6)
  554. node.Address6 = *cidr
  555. node.PostUp = ln.PostUp
  556. node.PostDown = ln.PostDown
  557. node.Action = ln.Action
  558. node.IsServer = parseBool(ln.IsServer)
  559. node.IsLocal = parseBool(ln.IsLocal)
  560. node.IsEgressGateway = parseBool(ln.IsEgressGateway)
  561. node.IsIngressGateway = parseBool(ln.IsIngressGateway)
  562. node.IsStatic = parseBool(ln.IsStatic)
  563. node.DNSOn = parseBool(ln.DNSOn)
  564. node.PersistentKeepalive = int(ln.PersistentKeepalive)
  565. node.Proxy = ln.Proxy
  566. return &host, &node
  567. }
  568. // Node.NetworkSettings updates a node with network settings
  569. func (node *Node) NetworkSettings(n Network) {
  570. _, cidr, _ := net.ParseCIDR(n.AddressRange)
  571. node.NetworkRange = *cidr
  572. }
  573. func parseBool(s string) bool {
  574. b := false
  575. if s == "yes" {
  576. b = true
  577. }
  578. return b
  579. }
  580. func formatBool(b bool) string {
  581. s := "no"
  582. if b {
  583. s = "yes"
  584. }
  585. return s
  586. }