node.go 27 KB

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