node.go 24 KB

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