-
-
Notifications
You must be signed in to change notification settings - Fork 280
/
sticky.go
176 lines (147 loc) Β· 4 KB
/
sticky.go
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
package crew
import (
"context"
"fmt"
"sync"
"time"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
"github.com/safing/portmaster/service/network"
"github.com/safing/portmaster/service/network/packet"
"github.com/safing/portmaster/spn/navigator"
)
const (
stickyTTL = 1 * time.Hour
)
var (
stickyIPs = make(map[string]*stickyHub)
stickyDomains = make(map[string]*stickyHub)
stickyLock sync.Mutex
)
type stickyHub struct {
Pin *navigator.Pin
Route *navigator.Route
LastSeen time.Time
Avoid bool
}
func (sh *stickyHub) isExpired() bool {
return time.Now().Add(-stickyTTL).After(sh.LastSeen)
}
func makeStickyIPKey(conn *network.Connection) string {
if p := conn.Process().Profile(); p != nil {
return fmt.Sprintf(
"%s/%s>%s",
p.LocalProfile().Source,
p.LocalProfile().ID,
conn.Entity.IP,
)
}
return "?>" + string(conn.Entity.IP)
}
func makeStickyDomainKey(conn *network.Connection) string {
if p := conn.Process().Profile(); p != nil {
return fmt.Sprintf(
"%s/%s>%s",
p.LocalProfile().Source,
p.LocalProfile().ID,
conn.Entity.Domain,
)
}
return "?>" + conn.Entity.Domain
}
func getStickiedHub(conn *network.Connection) (sticksTo *stickyHub) {
stickyLock.Lock()
defer stickyLock.Unlock()
// Check if IP is sticky.
sticksTo = stickyIPs[makeStickyIPKey(conn)] // byte comparison
if sticksTo != nil && !sticksTo.isExpired() {
sticksTo.LastSeen = time.Now()
}
// If the IP did not stick and we have a domain, check if that sticks.
if sticksTo == nil && conn.Entity.Domain != "" {
sticksTo, ok := stickyDomains[makeStickyDomainKey(conn)]
if ok && !sticksTo.isExpired() {
sticksTo.LastSeen = time.Now()
}
}
// If nothing sticked, return now.
if sticksTo == nil {
return nil
}
// Get intel from map before locking pin to avoid simultaneous locking.
mapIntel := navigator.Main.GetIntel()
// Lock Pin for checking.
sticksTo.Pin.Lock()
defer sticksTo.Pin.Unlock()
// Check if the stickied Hub supports the needed IP version.
switch {
case conn.IPVersion == packet.IPv4 && sticksTo.Pin.EntityV4 == nil:
// Connection is IPv4, but stickied Hub has no IPv4.
return nil
case conn.IPVersion == packet.IPv6 && sticksTo.Pin.EntityV6 == nil:
// Connection is IPv4, but stickied Hub has no IPv4.
return nil
}
// Disregard stickied Hub if it is disregard with the current options.
matcher := conn.TunnelOpts.Destination.Matcher(mapIntel)
if !matcher(sticksTo.Pin) {
return nil
}
// Return fully checked stickied Hub.
return sticksTo
}
func (t *Tunnel) stickDestinationToHub() {
stickyLock.Lock()
defer stickyLock.Unlock()
// Stick to IP.
ipKey := makeStickyIPKey(t.connInfo)
stickyIPs[ipKey] = &stickyHub{
Pin: t.dstPin,
Route: t.route,
LastSeen: time.Now(),
}
log.Infof("spn/crew: sticking %s to %s", ipKey, t.dstPin.Hub)
// Stick to Domain, if present.
if t.connInfo.Entity.Domain != "" {
domainKey := makeStickyDomainKey(t.connInfo)
stickyDomains[domainKey] = &stickyHub{
Pin: t.dstPin,
Route: t.route,
LastSeen: time.Now(),
}
log.Infof("spn/crew: sticking %s to %s", domainKey, t.dstPin.Hub)
}
}
func (t *Tunnel) avoidDestinationHub() {
stickyLock.Lock()
defer stickyLock.Unlock()
// Stick to Hub/IP Pair.
ipKey := makeStickyIPKey(t.connInfo)
stickyIPs[ipKey] = &stickyHub{
Pin: t.dstPin,
LastSeen: time.Now(),
Avoid: true,
}
log.Warningf("spn/crew: avoiding %s for %s", t.dstPin.Hub, ipKey)
}
func cleanStickyHubs(ctx context.Context, task *modules.Task) error {
stickyLock.Lock()
defer stickyLock.Unlock()
for _, stickyRegistry := range []map[string]*stickyHub{stickyIPs, stickyDomains} {
for key, stickedEntry := range stickyRegistry {
if stickedEntry.isExpired() {
delete(stickyRegistry, key)
}
}
}
return nil
}
func clearStickyHubs() {
stickyLock.Lock()
defer stickyLock.Unlock()
for _, stickyRegistry := range []map[string]*stickyHub{stickyIPs, stickyDomains} {
for key := range stickyRegistry {
delete(stickyRegistry, key)
}
}
}