-
Notifications
You must be signed in to change notification settings - Fork 634
Expand file tree
/
Copy pathextpeers.go
More file actions
830 lines (759 loc) · 22.5 KB
/
extpeers.go
File metadata and controls
830 lines (759 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
package logic
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"reflect"
"sort"
"strings"
"sync"
"time"
"github.com/goombaio/namegenerator"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/db"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic/acls"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/schema"
"github.com/gravitl/netmaker/servercfg"
"golang.org/x/exp/slog"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
var (
extClientCacheMutex = &sync.RWMutex{}
extClientCacheMap = make(map[string]models.ExtClient)
)
func getAllExtClientsFromCache() (extClients []models.ExtClient) {
extClientCacheMutex.RLock()
for _, extclient := range extClientCacheMap {
if extclient.Mutex == nil {
extclient.Mutex = &sync.Mutex{}
}
extClients = append(extClients, extclient)
}
extClientCacheMutex.RUnlock()
return
}
func deleteExtClientFromCache(key string) {
extClientCacheMutex.Lock()
delete(extClientCacheMap, key)
extClientCacheMutex.Unlock()
}
func getExtClientFromCache(key string) (extclient models.ExtClient, ok bool) {
extClientCacheMutex.RLock()
extclient, ok = extClientCacheMap[key]
if extclient.Mutex == nil {
extclient.Mutex = &sync.Mutex{}
}
extClientCacheMutex.RUnlock()
return
}
func storeExtClientInCache(key string, extclient models.ExtClient) {
extClientCacheMutex.Lock()
if extclient.Mutex == nil {
extclient.Mutex = &sync.Mutex{}
}
extClientCacheMap[key] = extclient
extClientCacheMutex.Unlock()
}
// ExtClient.GetEgressRangesOnNetwork - returns the egress ranges on network of ext client
func GetEgressRangesOnNetwork(client *models.ExtClient) ([]string, error) {
var result []string
eli, _ := (&schema.Egress{Network: client.Network}).ListByNetwork(db.WithContext(context.TODO()))
staticNode := client.ConvertToStaticNode()
userPolicies := ListUserPolicies(schema.NetworkID(client.Network))
defaultUserPolicy, _ := GetDefaultPolicy(schema.NetworkID(client.Network), models.UserPolicy)
for _, eI := range eli {
if !eI.Status {
continue
}
if eI.Domain == "" && eI.Range == "" {
continue
}
if eI.Domain != "" && len(eI.DomainAns) == 0 {
continue
}
rangesToBeAdded := []string{}
if eI.Domain != "" {
rangesToBeAdded = append(rangesToBeAdded, eI.DomainAns...)
} else {
// Use virtual NAT range if enabled, otherwise use original range
egressRange := eI.Range
if eI.Nat && eI.VirtualRange != "" {
egressRange = eI.VirtualRange
}
rangesToBeAdded = append(rangesToBeAdded, egressRange)
}
if defaultUserPolicy.Enabled {
result = append(result, rangesToBeAdded...)
} else {
if staticNode.IsUserNode && staticNode.StaticNode.OwnerID != "" {
user := &schema.User{Username: staticNode.StaticNode.OwnerID}
err := user.Get(db.WithContext(context.TODO()))
if err != nil {
return []string{}, errors.New("user not found")
}
if DoesUserHaveAccessToEgress(user, &eI, userPolicies) {
result = append(result, rangesToBeAdded...)
}
} else {
result = append(result, rangesToBeAdded...)
}
}
}
extclients, _ := GetNetworkExtClients(client.Network)
for _, extclient := range extclients {
if extclient.ClientID == client.ClientID {
continue
}
result = append(result, extclient.ExtraAllowedIPs...)
}
return UniqueIPNetStrList(result), nil
}
// UniqueIPNetList deduplicates and sorts a list of CIDR strings.
func UniqueIPNetStrList(ipnets []string) []string {
uniqueMap := make(map[string]struct{})
for _, cidr := range ipnets {
_, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
continue // skip invalid CIDR strings
}
key := ipnet.String() // normalized CIDR
uniqueMap[key] = struct{}{}
}
// Convert map keys to slice
uniqueList := make([]string, 0, len(uniqueMap))
for cidr := range uniqueMap {
uniqueList = append(uniqueList, cidr)
}
sort.Strings(uniqueList)
return uniqueList
}
// DeleteExtClient - deletes an existing ext client
func DeleteExtClient(network string, clientid string, isUpdate bool) error {
key, err := GetRecordKey(clientid, network)
if err != nil {
return err
}
extClient, err := GetExtClient(clientid, network)
if err != nil {
return err
}
err = database.DeleteRecord(database.EXT_CLIENT_TABLE_NAME, key)
if err != nil {
return err
}
if servercfg.CacheEnabled() {
// recycle ip address
if extClient.Address != "" {
RemoveIpFromAllocatedIpMap(network, extClient.Address)
}
if extClient.Address6 != "" {
RemoveIpFromAllocatedIpMap(network, extClient.Address6)
}
deleteExtClientFromCache(key)
}
if !isUpdate && extClient.RemoteAccessClientID != "" {
LogEvent(&models.Event{
Action: schema.Disconnect,
Source: models.Subject{
ID: extClient.OwnerID,
Name: extClient.OwnerID,
Type: schema.UserSub,
},
TriggeredBy: extClient.OwnerID,
Target: models.Subject{
ID: extClient.Network,
Name: extClient.Network,
Type: schema.NetworkSub,
Info: extClient,
},
NetworkID: schema.NetworkID(extClient.Network),
Origin: schema.ClientApp,
})
}
go RemoveNodeFromAclPolicy(extClient.ConvertToStaticNode())
return nil
}
// DeleteExtClientAndCleanup - deletes an existing ext client and update ACLs
func DeleteExtClientAndCleanup(extClient models.ExtClient) error {
//delete extClient record
err := DeleteExtClient(extClient.Network, extClient.ClientID, false)
if err != nil {
slog.Error("DeleteExtClientAndCleanup-remove extClient record: ", "Error", err.Error())
return err
}
//update ACLs
var networkAcls acls.ACLContainer
networkAcls, err = networkAcls.Get(acls.ContainerID(extClient.Network))
if err != nil {
slog.Error("DeleteExtClientAndCleanup-update network acls: ", "Error", err.Error())
return err
}
for objId := range networkAcls {
delete(networkAcls[objId], acls.AclID(extClient.ClientID))
}
delete(networkAcls, acls.AclID(extClient.ClientID))
if _, err = networkAcls.Save(acls.ContainerID(extClient.Network)); err != nil {
slog.Error("DeleteExtClientAndCleanup-update network acls:", "Error", err.Error())
return err
}
return nil
}
//TODO - enforce extclient-to-extclient on ingress gw
/* 1. fetch all non-user static nodes
a. check against each user node, if allowed add rule
*/
// GetNetworkExtClients - gets the ext clients of given network
func GetNetworkExtClients(network string) ([]models.ExtClient, error) {
var extclients []models.ExtClient
if servercfg.CacheEnabled() {
allextclients := getAllExtClientsFromCache()
if len(allextclients) != 0 {
for _, extclient := range allextclients {
if extclient.Network == network {
extclients = append(extclients, extclient)
}
}
return extclients, nil
}
}
records, err := database.FetchRecords(database.EXT_CLIENT_TABLE_NAME)
if err != nil {
if database.IsEmptyRecord(err) {
return extclients, nil
}
return extclients, err
}
for _, value := range records {
var extclient models.ExtClient
err = json.Unmarshal([]byte(value), &extclient)
if err != nil {
continue
}
key, err := GetRecordKey(extclient.ClientID, extclient.Network)
if err == nil {
if servercfg.CacheEnabled() {
storeExtClientInCache(key, extclient)
}
}
if extclient.Network == network {
extclients = append(extclients, extclient)
}
}
return extclients, err
}
// GetExtClient - gets a single ext client on a network
func GetExtClient(clientid string, network string) (models.ExtClient, error) {
var extclient models.ExtClient
key, err := GetRecordKey(clientid, network)
if err != nil {
return extclient, err
}
if servercfg.CacheEnabled() {
if extclient, ok := getExtClientFromCache(key); ok {
return extclient, nil
}
}
data, err := database.FetchRecord(database.EXT_CLIENT_TABLE_NAME, key)
if err != nil {
return extclient, err
}
err = json.Unmarshal([]byte(data), &extclient)
if servercfg.CacheEnabled() {
storeExtClientInCache(key, extclient)
}
return extclient, err
}
// GetGwExtclients - return all ext clients attached to the passed gw id
func GetGwExtclients(nodeID, network string) []models.ExtClient {
gwClients := []models.ExtClient{}
clients, err := GetNetworkExtClients(network)
if err != nil {
return gwClients
}
for _, client := range clients {
if client.IngressGatewayID == nodeID {
gwClients = append(gwClients, client)
}
}
return gwClients
}
// GetExtClient - gets a single ext client on a network
func GetExtClientByPubKey(publicKey string, network string) (*models.ExtClient, error) {
netClients, err := GetNetworkExtClients(network)
if err != nil {
return nil, err
}
for i := range netClients {
ec := netClients[i]
if ec.PublicKey == publicKey {
return &ec, nil
}
}
return nil, fmt.Errorf("no client found")
}
// CreateExtClient - creates and saves an extclient
func CreateExtClient(extclient *models.ExtClient) error {
// lock because we may need unique IPs and having it concurrent makes parallel calls result in same "unique" IPs
addressLock.Lock()
defer addressLock.Unlock()
if len(extclient.PublicKey) == 0 {
privateKey, err := wgtypes.GeneratePrivateKey()
if err != nil {
return err
}
extclient.PrivateKey = privateKey.String()
extclient.PublicKey = privateKey.PublicKey().String()
} else if len(extclient.PrivateKey) == 0 && len(extclient.PublicKey) > 0 {
extclient.PrivateKey = "[ENTER PRIVATE KEY]"
}
if extclient.ExtraAllowedIPs == nil {
extclient.ExtraAllowedIPs = []string{}
}
parentNetwork := &schema.Network{Name: extclient.Network}
err := parentNetwork.Get(db.WithContext(context.TODO()))
if err != nil {
return err
}
if extclient.Address == "" {
if parentNetwork.AddressRange != "" {
newAddress, err := UniqueAddress(extclient.Network, true)
if err != nil {
return err
}
extclient.Address = newAddress.String()
}
}
if extclient.Address6 == "" {
if parentNetwork.AddressRange6 != "" {
addr6, err := UniqueAddress6(extclient.Network, true)
if err != nil {
return err
}
extclient.Address6 = addr6.String()
}
}
if extclient.ClientID == "" {
extclient.ClientID, err = GenerateNodeName(extclient.Network)
if err != nil {
return err
}
}
extclient.LastModified = time.Now().Unix()
return SaveExtClient(extclient)
}
// GenerateNodeName - generates a random node name
func GenerateNodeName(network string) (string, error) {
seed := time.Now().UTC().UnixNano()
nameGenerator := namegenerator.NewNameGenerator(seed)
var name string
cnt := 0
for {
if cnt > 10 {
return "", errors.New("couldn't generate random name, try again")
}
cnt += 1
name = nameGenerator.Generate()
if len(name) > 15 {
continue
}
_, err := GetExtClient(name, network)
if err == nil {
// config exists with same name
continue
}
break
}
return name, nil
}
// SaveExtClient - saves an ext client to database
func SaveExtClient(extclient *models.ExtClient) error {
key, err := GetRecordKey(extclient.ClientID, extclient.Network)
if err != nil {
return err
}
data, err := json.Marshal(&extclient)
if err != nil {
return err
}
if err = database.Insert(key, string(data), database.EXT_CLIENT_TABLE_NAME); err != nil {
return err
}
if servercfg.CacheEnabled() {
storeExtClientInCache(key, *extclient)
if extclient.Address != "" {
AddIpToAllocatedIpMap(extclient.Network, net.ParseIP(extclient.Address))
}
if extclient.Address6 != "" {
AddIpToAllocatedIpMap(extclient.Network, net.ParseIP(extclient.Address6))
}
}
return SetNetworkNodesLastModified(extclient.Network)
}
// UpdateExtClient - updates an ext client with new values
func UpdateExtClient(old *models.ExtClient, update *models.CustomExtClient) models.ExtClient {
new := *old
new.ClientID = update.ClientID
if update.PublicKey != "" && old.PublicKey != update.PublicKey {
new.PublicKey = update.PublicKey
}
if update.DNS != old.DNS {
new.DNS = update.DNS
}
if update.Enabled != old.Enabled {
new.Enabled = update.Enabled
}
new.ExtraAllowedIPs = update.ExtraAllowedIPs
if update.DeniedACLs != nil && !reflect.DeepEqual(old.DeniedACLs, update.DeniedACLs) {
new.DeniedACLs = update.DeniedACLs
}
// replace any \r\n with \n in postup and postdown from HTTP request
new.PostUp = strings.Replace(update.PostUp, "\r\n", "\n", -1)
new.PostDown = strings.Replace(update.PostDown, "\r\n", "\n", -1)
new.Tags = update.Tags
if update.Location != "" && update.Location != old.Location {
new.Location = update.Location
}
if update.Country != "" && update.Country != old.Country {
new.Country = strings.ToUpper(update.Country)
}
if update.DeviceID != "" && old.DeviceID == "" {
new.DeviceID = update.DeviceID
}
if update.OS != "" {
new.OS = update.OS
}
if update.OSFamily != "" {
new.OSFamily = update.OSFamily
}
if update.OSVersion != "" {
new.OSVersion = update.OSVersion
}
if update.KernelVersion != "" {
new.KernelVersion = update.KernelVersion
}
if update.ClientVersion != "" {
new.ClientVersion = update.ClientVersion
}
return new
}
// GetExtClientsByID - gets the clients of attached gateway
func GetExtClientsByID(nodeid, network string) ([]models.ExtClient, error) {
var result []models.ExtClient
currentClients, err := GetNetworkExtClients(network)
if err != nil {
return result, err
}
for i := range currentClients {
if currentClients[i].IngressGatewayID == nodeid {
result = append(result, currentClients[i])
}
}
return result, nil
}
// GetAllExtClients - gets all ext clients from DB
func GetAllExtClients() ([]models.ExtClient, error) {
var clients = []models.ExtClient{}
currentNetworks, err := (&schema.Network{}).ListAll(db.WithContext(context.TODO()))
if err != nil && database.IsEmptyRecord(err) {
return clients, nil
} else if err != nil {
return clients, err
}
for i := range currentNetworks {
netName := currentNetworks[i].Name
netClients, err := GetNetworkExtClients(netName)
if err != nil {
continue
}
clients = append(clients, netClients...)
}
return clients, nil
}
// GetAllExtClientsWithStatus - returns all external clients with
// given status.
func GetAllExtClientsWithStatus(status models.NodeStatus) ([]models.ExtClient, error) {
extClients, err := GetAllExtClients()
if err != nil {
return nil, err
}
var validExtClients []models.ExtClient
for _, extClient := range extClients {
node := extClient.ConvertToStaticNode()
GetNodeStatus(&node, false)
if node.Status == status {
validExtClients = append(validExtClients, extClient)
}
}
return validExtClients, nil
}
// ToggleExtClientConnectivity - enables or disables an ext client
func ToggleExtClientConnectivity(client *models.ExtClient, enable bool) (models.ExtClient, error) {
update := models.CustomExtClient{
Enabled: enable,
ClientID: client.ClientID,
PublicKey: client.PublicKey,
DNS: client.DNS,
ExtraAllowedIPs: client.ExtraAllowedIPs,
DeniedACLs: client.DeniedACLs,
RemoteAccessClientID: client.RemoteAccessClientID,
}
// update in DB
newClient := UpdateExtClient(client, &update)
if err := DeleteExtClient(client.Network, client.ClientID, true); err != nil {
slog.Error("failed to delete ext client during update", "id", client.ClientID, "network", client.Network, "error", err)
return newClient, err
}
if err := SaveExtClient(&newClient); err != nil {
slog.Error("failed to save updated ext client during update", "id", newClient.ClientID, "network", newClient.Network, "error", err)
return newClient, err
}
return newClient, nil
}
func GetExtPeers(node, peer *models.Node, addressIdentityMap map[string]models.PeerIdentity) ([]wgtypes.PeerConfig, []models.IDandAddr, []models.EgressNetworkRoutes, error) {
var skipFlowLogs bool
if !GetFeatureFlags().EnableFlowLogs || !GetServerSettings().EnableFlowLogs {
skipFlowLogs = true
}
var peers []wgtypes.PeerConfig
var idsAndAddr []models.IDandAddr
var egressRoutes []models.EgressNetworkRoutes
extPeers, err := GetNetworkExtClients(node.Network)
if err != nil {
return peers, idsAndAddr, egressRoutes, err
}
host := &schema.Host{
ID: node.HostID,
}
err = host.Get(db.WithContext(context.TODO()))
if err != nil {
return peers, idsAndAddr, egressRoutes, err
}
for _, extPeer := range extPeers {
extPeer := extPeer
if !IsClientNodeAllowed(&extPeer, peer.ID.String()) {
continue
}
if extPeer.RemoteAccessClientID == "" {
if ok := IsPeerAllowed(extPeer.ConvertToStaticNode(), *peer, true); !ok {
continue
}
} else {
if ok, _ := IsUserAllowedToCommunicate(extPeer.OwnerID, *peer); !ok {
continue
}
}
pubkey, err := wgtypes.ParseKey(extPeer.PublicKey)
if err != nil {
logger.Log(1, "error parsing ext pub key:", err.Error())
continue
}
if host.PublicKey.String() == extPeer.PublicKey ||
extPeer.IngressGatewayID != node.ID.String() || !extPeer.Enabled {
continue
}
var allowedips []net.IPNet
var peer wgtypes.PeerConfig
var extPeerAddr4, extPeerAddr6 net.IPNet
if extPeer.Address != "" {
extPeerAddr4 = net.IPNet{
IP: net.ParseIP(extPeer.Address),
Mask: net.CIDRMask(32, 32),
}
if extPeerAddr4.IP != nil && extPeerAddr4.Mask != nil {
allowedips = append(allowedips, extPeerAddr4)
}
}
if extPeer.Address6 != "" {
extPeerAddr6 = net.IPNet{
IP: net.ParseIP(extPeer.Address6),
Mask: net.CIDRMask(128, 128),
}
if extPeerAddr6.IP != nil && extPeerAddr6.Mask != nil {
allowedips = append(allowedips, extPeerAddr6)
}
}
for _, extraAllowedIP := range extPeer.ExtraAllowedIPs {
_, cidr, err := net.ParseCIDR(extraAllowedIP)
if err == nil {
allowedips = append(allowedips, *cidr)
}
}
egressRoutes = append(egressRoutes, getExtPeerEgressRoute(*node, extPeer)...)
primaryAddr := extPeer.Address
if primaryAddr == "" {
primaryAddr = extPeer.Address6
}
peer = wgtypes.PeerConfig{
PublicKey: pubkey,
ReplaceAllowedIPs: true,
AllowedIPs: allowedips,
}
peers = append(peers, peer)
peerInfo := models.IDandAddr{
ID: peer.PublicKey.String(),
Name: extPeer.ClientID,
Address: primaryAddr,
Address4: extPeer.Address,
Address6: extPeer.Address6,
IsExtClient: true,
}
if extPeer.DeviceID != "" || extPeer.RemoteAccessClientID != "" {
peerInfo.UserName = extPeer.OwnerID
}
idsAndAddr = append(idsAndAddr, peerInfo)
if !skipFlowLogs {
if extPeerAddr4.IP != nil {
peerID := extPeer.ClientID
peerType := models.PeerType_WireGuard
if extPeer.RemoteAccessClientID != "" {
peerID = extPeer.OwnerID
peerType = models.PeerType_User
}
addressIdentityMap[extPeerAddr4.IP.String()+"/32"] = models.PeerIdentity{
ID: peerID,
Type: peerType,
Name: peerID,
}
}
if extPeerAddr6.IP != nil {
peerID := extPeer.ClientID
peerType := models.PeerType_WireGuard
if extPeer.RemoteAccessClientID != "" {
peerID = extPeer.OwnerID
peerType = models.PeerType_User
}
addressIdentityMap[extPeerAddr6.IP.String()+"/128"] = models.PeerIdentity{
ID: peerID,
Type: peerType,
Name: peerID,
}
}
}
}
return peers, idsAndAddr, egressRoutes, nil
}
func getExtPeerEgressRoute(node models.Node, extPeer models.ExtClient) (egressRoutes []models.EgressNetworkRoutes) {
r := models.EgressNetworkRoutes{
PeerKey: extPeer.PublicKey,
EgressGwAddr: extPeer.AddressIPNet4(),
EgressGwAddr6: extPeer.AddressIPNet6(),
NodeAddr: node.Address,
NodeAddr6: node.Address6,
EgressRanges: extPeer.ExtraAllowedIPs,
}
for _, extraAllowedIP := range extPeer.ExtraAllowedIPs {
r.EgressRangesWithMetric = append(r.EgressRangesWithMetric, models.EgressRangeMetric{
Network: extraAllowedIP,
RouteMetric: 256,
})
}
egressRoutes = append(egressRoutes, r)
return
}
func getExtpeerEgressRanges(node models.Node) (ranges, ranges6 []net.IPNet) {
extPeers, err := GetNetworkExtClients(node.Network)
if err != nil {
return
}
for _, extPeer := range extPeers {
if len(extPeer.ExtraAllowedIPs) == 0 {
continue
}
if ok, _ := IsNodeAllowedToCommunicate(extPeer.ConvertToStaticNode(), node, true); !ok {
continue
}
for _, allowedRange := range extPeer.ExtraAllowedIPs {
_, ipnet, err := net.ParseCIDR(allowedRange)
if err == nil {
if ipnet.IP.To4() != nil {
ranges = append(ranges, *ipnet)
} else {
ranges6 = append(ranges6, *ipnet)
}
}
}
}
return
}
func getExtpeersExtraRoutes(node models.Node) (egressRoutes []models.EgressNetworkRoutes) {
extPeers, err := GetNetworkExtClients(node.Network)
if err != nil {
return
}
for _, extPeer := range extPeers {
if len(extPeer.ExtraAllowedIPs) == 0 || !extPeer.Enabled {
continue
}
if ok, _ := IsNodeAllowedToCommunicate(extPeer.ConvertToStaticNode(), node, true); !ok {
continue
}
egressRoutes = append(egressRoutes, getExtPeerEgressRoute(node, extPeer)...)
}
return
}
func GetExtclientAllowedIPs(client models.ExtClient) (allowedIPs []string) {
gwnode, err := GetNodeByID(client.IngressGatewayID)
if err != nil {
logger.Log(0,
fmt.Sprintf("failed to get ingress gateway node [%s] info: %v", client.IngressGatewayID, err))
return
}
network := &schema.Network{Name: client.Network}
err = network.Get(db.WithContext(context.TODO()))
if err != nil {
logger.Log(1, "Could not retrieve Ingress Gateway Network", client.Network)
return
}
if IsInternetGw(gwnode) {
egressrange := "0.0.0.0/0"
if gwnode.Address6.IP != nil && client.Address6 != "" {
egressrange += "," + "::/0"
}
allowedIPs = []string{egressrange}
} else {
allowedIPs = []string{network.AddressRange}
if network.AddressRange6 != "" {
allowedIPs = append(allowedIPs, network.AddressRange6)
}
if egressGatewayRanges, err := GetEgressRangesOnNetwork(&client); err == nil {
allowedIPs = append(allowedIPs, egressGatewayRanges...)
}
}
return
}
func GetStaticNodesByNetwork(network schema.NetworkID, onlyWg bool) (staticNode []models.Node) {
extClients, err := GetAllExtClients()
if err != nil {
return
}
SortExtClient(extClients[:])
for _, extI := range extClients {
if extI.Network == network.String() {
if onlyWg && extI.RemoteAccessClientID != "" {
continue
}
staticNode = append(staticNode, extI.ConvertToStaticNode())
}
}
return
}
// CleanupOtherExtclients cleans up other clients owned by the same use for the same device and network.
func CleanupOtherExtclients(extclient *models.ExtClient) error {
extclients, err := GetNetworkExtClients(extclient.Network)
if err != nil {
return err
}
for _, extI := range extclients {
if extI.ClientID != extclient.ClientID && extI.DeviceID == extclient.DeviceID && extI.OwnerID == extclient.OwnerID {
err = DeleteExtClient(extI.Network, extI.ClientID, false)
if err != nil {
return err
}
}
}
return nil
}