node.go 25 KB

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