node.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. IsLocal bool `json:"islocal" yaml:"islocal"`
  66. IsEgressGateway bool `json:"isegressgateway" yaml:"isegressgateway"`
  67. IsIngressGateway bool `json:"isingressgateway" yaml:"isingressgateway"`
  68. DNSOn bool `json:"dnson" yaml:"dnson"`
  69. PersistentKeepalive int `json:"persistentkeepalive" yaml:"persistentkeepalive"`
  70. Peers []wgtypes.PeerConfig `json:"peers" yaml:"peers"`
  71. }
  72. // Node - a model of a network node
  73. type Node struct {
  74. CommonNode
  75. PendingDelete bool `json:"pendingdelete" bson:"pendingdelete" yaml:"pendingdelete"`
  76. LastModified time.Time `json:"lastmodified" bson:"lastmodified" yaml:"lastmodified"`
  77. LastCheckIn time.Time `json:"lastcheckin" bson:"lastcheckin" yaml:"lastcheckin"`
  78. LastPeerUpdate time.Time `json:"lastpeerupdate" bson:"lastpeerupdate" yaml:"lastpeerupdate"`
  79. ExpirationDateTime time.Time `json:"expdatetime" bson:"expdatetime" yaml:"expdatetime"`
  80. AllowedIPs []string `json:"allowedips" bson:"allowedips" yaml:"allowedips"`
  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.AllowedIPs == nil {
  344. newNode.AllowedIPs = currentNode.AllowedIPs
  345. }
  346. if newNode.PersistentKeepalive < 0 {
  347. newNode.PersistentKeepalive = currentNode.PersistentKeepalive
  348. }
  349. if newNode.LastModified != currentNode.LastModified {
  350. newNode.LastModified = currentNode.LastModified
  351. }
  352. if newNode.ExpirationDateTime.IsZero() {
  353. newNode.ExpirationDateTime = currentNode.ExpirationDateTime
  354. }
  355. if newNode.LastPeerUpdate.IsZero() {
  356. newNode.LastPeerUpdate = currentNode.LastPeerUpdate
  357. }
  358. if newNode.LastCheckIn.IsZero() {
  359. newNode.LastCheckIn = currentNode.LastCheckIn
  360. }
  361. if newNode.Network == "" {
  362. newNode.Network = currentNode.Network
  363. }
  364. if newNode.IsEgressGateway != currentNode.IsEgressGateway {
  365. newNode.IsEgressGateway = currentNode.IsEgressGateway
  366. }
  367. if newNode.IsIngressGateway != currentNode.IsIngressGateway {
  368. newNode.IsIngressGateway = currentNode.IsIngressGateway
  369. }
  370. if newNode.EgressGatewayRanges == nil {
  371. newNode.EgressGatewayRanges = currentNode.EgressGatewayRanges
  372. }
  373. if newNode.IngressGatewayRange == "" {
  374. newNode.IngressGatewayRange = currentNode.IngressGatewayRange
  375. }
  376. if newNode.IngressGatewayRange6 == "" {
  377. newNode.IngressGatewayRange6 = currentNode.IngressGatewayRange6
  378. }
  379. if newNode.DNSOn != currentNode.DNSOn {
  380. newNode.DNSOn = currentNode.DNSOn
  381. }
  382. if newNode.IsLocal != currentNode.IsLocal {
  383. newNode.IsLocal = currentNode.IsLocal
  384. }
  385. if newNode.Action == "" {
  386. newNode.Action = currentNode.Action
  387. }
  388. if newNode.RelayAddrs == nil {
  389. newNode.RelayAddrs = currentNode.RelayAddrs
  390. }
  391. if newNode.IsRelay != currentNode.IsRelay {
  392. newNode.IsRelay = currentNode.IsRelay
  393. }
  394. if newNode.IsRelayed == currentNode.IsRelayed {
  395. newNode.IsRelayed = currentNode.IsRelayed
  396. }
  397. if newNode.Server == "" {
  398. newNode.Server = currentNode.Server
  399. }
  400. if newNode.Connected != currentNode.Connected {
  401. newNode.Connected = currentNode.Connected
  402. }
  403. if newNode.DefaultACL == "" {
  404. newNode.DefaultACL = currentNode.DefaultACL
  405. }
  406. if newNode.Failover != currentNode.Failover {
  407. newNode.Failover = currentNode.Failover
  408. }
  409. }
  410. // StringWithCharset - returns random string inside defined charset
  411. func StringWithCharset(length int, charset string) string {
  412. b := make([]byte, length)
  413. for i := range b {
  414. b[i] = charset[seededRand.Intn(len(charset))]
  415. }
  416. return string(b)
  417. }
  418. // IsIpv4Net - check for valid IPv4 address
  419. // Note: We dont handle IPv6 AT ALL!!!!! This definitely is needed at some point
  420. // But for iteration 1, lets just stick to IPv4. Keep it simple stupid.
  421. func IsIpv4Net(host string) bool {
  422. return net.ParseIP(host) != nil
  423. }
  424. // Node.NameInNodeCharset - returns if name is in charset below or not
  425. func (node *LegacyNode) NameInNodeCharSet() bool {
  426. charset := "abcdefghijklmnopqrstuvwxyz1234567890-"
  427. for _, char := range node.Name {
  428. if !strings.Contains(charset, strings.ToLower(string(char))) {
  429. return false
  430. }
  431. }
  432. return true
  433. }
  434. // == PRO ==
  435. // Node.DoesACLAllow - checks if default ACL on node is "yes"
  436. func (node *Node) DoesACLAllow() bool {
  437. return node.DefaultACL == "yes"
  438. }
  439. // Node.DoesACLDeny - checks if default ACL on node is "no"
  440. func (node *Node) DoesACLDeny() bool {
  441. return node.DefaultACL == "no"
  442. }
  443. func (ln *LegacyNode) ConvertToNewNode() (*Host, *Node) {
  444. var node Node
  445. //host:= logic.GetHost(node.HostID)
  446. var host Host
  447. if host.ID.String() == "" {
  448. host.ID = uuid.New()
  449. host.FirewallInUse = ln.FirewallInUse
  450. host.Version = ln.Version
  451. host.IPForwarding = parseBool(ln.IPForwarding)
  452. host.HostPass = ln.Password
  453. host.Name = ln.Name
  454. host.ListenPort = int(ln.ListenPort)
  455. _, cidr, _ := net.ParseCIDR(ln.LocalAddress)
  456. host.LocalAddress = *cidr
  457. _, cidr, _ = net.ParseCIDR(ln.LocalRange)
  458. host.LocalRange = *cidr
  459. host.LocalListenPort = int(ln.LocalListenPort)
  460. host.ProxyListenPort = int(ln.ProxyListenPort)
  461. host.MTU = int(ln.MTU)
  462. host.PublicKey, _ = wgtypes.ParseKey(ln.PublicKey)
  463. host.MacAddress, _ = net.ParseMAC(ln.MacAddress)
  464. host.TrafficKeyPublic = ln.TrafficKeys.Mine
  465. gateway, _ := net.ResolveUDPAddr("udp", ln.InternetGateway)
  466. host.InternetGateway = *gateway
  467. id, _ := uuid.Parse(ln.ID)
  468. host.Nodes = append(host.Nodes, id.String())
  469. host.Interfaces = ln.Interfaces
  470. host.EndpointIP = net.ParseIP(ln.Endpoint)
  471. // host.ProxyEnabled = ln.Proxy // this will always be false..
  472. }
  473. id, _ := uuid.Parse(ln.ID)
  474. node.ID = id
  475. node.Network = ln.Network
  476. _, cidr, _ := net.ParseCIDR(ln.NetworkSettings.AddressRange)
  477. node.NetworkRange = *cidr
  478. _, cidr, _ = net.ParseCIDR(ln.NetworkSettings.AddressRange6)
  479. node.NetworkRange6 = *cidr
  480. node.Server = ln.Server
  481. node.Connected = parseBool(ln.Connected)
  482. _, cidr, _ = net.ParseCIDR(ln.Address)
  483. node.Address = *cidr
  484. _, cidr, _ = net.ParseCIDR(ln.Address6)
  485. node.Address6 = *cidr
  486. node.PostUp = ln.PostUp
  487. node.PostDown = ln.PostDown
  488. node.Action = ln.Action
  489. node.IsLocal = parseBool(ln.IsLocal)
  490. node.IsEgressGateway = parseBool(ln.IsEgressGateway)
  491. node.IsIngressGateway = parseBool(ln.IsIngressGateway)
  492. node.DNSOn = parseBool(ln.DNSOn)
  493. node.PersistentKeepalive = int(ln.PersistentKeepalive)
  494. return &host, &node
  495. }
  496. // Node.Legacy converts node to legacy format
  497. func (n *Node) Legacy(h *Host, s *ServerConfig, net *Network) *LegacyNode {
  498. l := LegacyNode{}
  499. l.ID = n.ID.String()
  500. l.HostID = h.ID.String()
  501. l.Address = n.Address.String()
  502. l.Address6 = n.Address6.String()
  503. l.LocalAddress = h.LocalAddress.String()
  504. l.Interfaces = h.Interfaces
  505. l.Name = h.Name
  506. l.NetworkSettings = *net
  507. l.ListenPort = int32(h.ListenPort)
  508. l.LocalListenPort = int32(h.LocalListenPort)
  509. l.ProxyListenPort = int32(h.ProxyListenPort)
  510. l.PublicKey = h.PublicKey.String()
  511. l.Endpoint = h.EndpointIP.String()
  512. l.PostUp = n.PostUp
  513. l.PostDown = n.PostDown
  514. //l.AllowedIPs =
  515. l.PersistentKeepalive = int32(n.PersistentKeepalive)
  516. l.AccessKey = ""
  517. l.Interface = WIREGUARD_INTERFACE
  518. //l.LastModified =
  519. //l.ExpirationDateTime
  520. //l.LastPeerUpdate
  521. //l.LastCheckIn
  522. l.MacAddress = h.MacAddress.String()
  523. l.Password = h.HostPass
  524. l.Network = n.Network
  525. //l.IsRelayed = formatBool(n.Is)
  526. //l.IsRelay = formatBool(n.IsRelay)
  527. //l.IsDocker = formatBool(n.IsDocker)
  528. //l.IsK8S = formatBool(n.IsK8S)
  529. l.IsEgressGateway = formatBool(n.IsEgressGateway)
  530. l.IsIngressGateway = formatBool(n.IsIngressGateway)
  531. //l.EgressGatewayRanges = n.EgressGatewayRanges
  532. //l.EgressGatewayNatEnabled = n.EgressGatewayNatEnabled
  533. //l.RelayAddrs = n.RelayAddrs
  534. //l.FailoverNode = n.FailoverNode
  535. //l.IngressGatewayRange = n.IngressGatewayRange
  536. //l.IngressGatewayRange6 = n.IngressGatewayRange6
  537. l.IsStatic = formatBool(h.IsStatic)
  538. l.UDPHolePunch = formatBool(true)
  539. l.DNSOn = formatBool(n.DNSOn)
  540. l.Action = n.Action
  541. l.IsLocal = formatBool(n.IsLocal)
  542. l.LocalRange = h.LocalRange.String()
  543. l.IPForwarding = formatBool(h.IPForwarding)
  544. l.OS = h.OS
  545. l.MTU = int32(h.MTU)
  546. l.Version = h.Version
  547. l.Server = n.Server
  548. l.TrafficKeys.Mine = h.TrafficKeyPublic
  549. l.TrafficKeys.Server = s.TrafficKey
  550. l.FirewallInUse = h.FirewallInUse
  551. l.InternetGateway = h.InternetGateway.String()
  552. l.Connected = formatBool(n.Connected)
  553. //l.PendingDelete = formatBool(n.PendingDelete)
  554. l.Proxy = h.ProxyEnabled
  555. l.DefaultACL = n.DefaultACL
  556. l.OwnerID = n.OwnerID
  557. //l.Failover = n.Failover
  558. return &l
  559. }
  560. // Node.NetworkSettings updates a node with network settings
  561. func (node *Node) NetworkSettings(n Network) {
  562. _, cidr, err := net.ParseCIDR(n.AddressRange)
  563. if err == nil {
  564. node.NetworkRange = *cidr
  565. }
  566. _, cidr, err = net.ParseCIDR(n.AddressRange6)
  567. if err == nil {
  568. node.NetworkRange6 = *cidr
  569. }
  570. }
  571. func parseBool(s string) bool {
  572. b := false
  573. if s == "yes" {
  574. b = true
  575. }
  576. return b
  577. }
  578. func formatBool(b bool) string {
  579. s := "no"
  580. if b {
  581. s = "yes"
  582. }
  583. return s
  584. }