node.go 27 KB

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