node.go 24 KB

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