node.go 24 KB

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