forked from Psiphon-Labs/psiphon-tunnel-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tactics.go
248 lines (198 loc) · 7.11 KB
/
tactics.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
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
/*
* Copyright (c) 2020, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package server
import (
"fmt"
"sync"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/parameters"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/tactics"
"github.com/golang/groupcache/lru"
)
const (
TACTICS_CACHE_MAX_ENTRIES = 10000
)
// ServerTacticsParametersCache is a cache of filtered server-side tactics,
// intended to speed-up frequent tactics lookups.
//
// Presently, the cache is targeted at pre-handshake lookups which are both
// the most time critical and have a low tactic cardinality, as only GeoIP
// filter inputs are available.
//
// There is no TTL for cache entries as the cached filtered tactics remain
// valid until the tactics config changes; Flush must be called on tactics
// config hot reloads.
type ServerTacticsParametersCache struct {
support *SupportServices
mutex sync.Mutex
tacticsCache *lru.Cache
parameterReferences map[string]*parameterReference
metrics *serverTacticsParametersCacheMetrics
}
type parameterReference struct {
params *parameters.Parameters
referenceCount int
}
type serverTacticsParametersCacheMetrics struct {
MaxCacheEntries int64
MaxParameterReferences int64
CacheHitCount int64
CacheMissCount int64
}
// NewServerTacticsParametersCache creates a new ServerTacticsParametersCache.
func NewServerTacticsParametersCache(
support *SupportServices) *ServerTacticsParametersCache {
cache := &ServerTacticsParametersCache{
support: support,
tacticsCache: lru.New(TACTICS_CACHE_MAX_ENTRIES),
parameterReferences: make(map[string]*parameterReference),
metrics: &serverTacticsParametersCacheMetrics{},
}
cache.tacticsCache.OnEvicted = cache.onEvicted
return cache
}
// GetMetrics returns a snapshop of current ServerTacticsParametersCache event
// counters and resets all counters to zero.
func (c *ServerTacticsParametersCache) GetMetrics() LogFields {
c.mutex.Lock()
defer c.mutex.Unlock()
logFields := LogFields{
"server_tactics_max_cache_entries": c.metrics.MaxCacheEntries,
"server_tactics_max_parameter_references": c.metrics.MaxParameterReferences,
"server_tactics_cache_hit_count": c.metrics.CacheHitCount,
"server_tactics_cache_miss_count": c.metrics.CacheMissCount,
}
c.metrics = &serverTacticsParametersCacheMetrics{}
return logFields
}
// Get returns server-side tactics parameters for the specified GeoIP scope.
// Get is designed to be called before the API handshake and does not filter
// by API parameters. IsNil guards must be used when accessing the returned
// ParametersAccessor.
func (c *ServerTacticsParametersCache) Get(
geoIPData GeoIPData) (parameters.ParametersAccessor, error) {
c.mutex.Lock()
defer c.mutex.Unlock()
nilAccessor := parameters.MakeNilParametersAccessor()
key := c.makeKey(geoIPData)
// Check for cached result.
if tag, ok := c.tacticsCache.Get(key); ok {
paramRef, ok := c.parameterReferences[tag.(string)]
if !ok {
return nilAccessor, errors.TraceNew("missing parameters")
}
c.metrics.CacheHitCount += 1
// The returned accessor is read-only, and paramRef.params is never
// modified, so the return value is safe of concurrent use and may be
// references both while the entry remains in the cache or after it is
// evicted.
return paramRef.params.Get(), nil
}
c.metrics.CacheMissCount += 1
// Construct parameters from tactics.
tactics, tag, err := c.support.TacticsServer.GetTacticsWithTag(
true, common.GeoIPData(geoIPData), make(common.APIParameters))
if err != nil {
return nilAccessor, errors.Trace(err)
}
if tactics == nil {
// This server isn't configured with tactics.
return nilAccessor, nil
}
// Tactics.Probability is ignored for server-side tactics.
params, err := parameters.NewParameters(nil)
if err != nil {
return nilAccessor, errors.Trace(err)
}
_, err = params.Set("", false, tactics.Parameters)
if err != nil {
return nilAccessor, errors.Trace(err)
}
// Update the cache.
//
// Two optimizations are used to limit the memory size of the cache:
//
// 1. The scope of the GeoIP data cache key is limited to the fields --
// Country/ISP/City -- that are present in tactics filters. E.g., if only
// Country appears in filters, then the key will omit ISP and City.
//
// 2. Two maps are maintained: GeoIP-key -> tactics-tag; and tactics-tag ->
// parameters. For N keys with the same filtered parameters, the mapped value
// overhead is N tags and 1 larger parameters data structure.
//
// If the cache is full, the LRU entry will be ejected.
// Update the parameterRefence _before_ calling Add: if Add happens to evict
// the last other entry referencing the same parameters, this order avoids an
// unnecessary delete/re-add.
paramRef, ok := c.parameterReferences[tag]
if !ok {
c.parameterReferences[tag] = ¶meterReference{
params: params,
referenceCount: 1,
}
} else {
paramRef.referenceCount += 1
}
c.tacticsCache.Add(key, tag)
cacheSize := int64(c.tacticsCache.Len())
if cacheSize > c.metrics.MaxCacheEntries {
c.metrics.MaxCacheEntries = cacheSize
}
paramRefsSize := int64(len(c.parameterReferences))
if paramRefsSize > c.metrics.MaxParameterReferences {
c.metrics.MaxParameterReferences = paramRefsSize
}
return params.Get(), nil
}
func (c *ServerTacticsParametersCache) Flush() {
c.mutex.Lock()
defer c.mutex.Unlock()
// onEvicted will clear c.parameterReferences.
c.tacticsCache.Clear()
}
func (c *ServerTacticsParametersCache) onEvicted(
key lru.Key, value interface{}) {
// Cleanup unreferenced parameterReferences. Assumes mutex is held by Get,
// which calls Add, which may call onEvicted.
tag := value.(string)
paramRef, ok := c.parameterReferences[tag]
if !ok {
return
}
paramRef.referenceCount -= 1
if paramRef.referenceCount == 0 {
delete(c.parameterReferences, tag)
}
}
func (c *ServerTacticsParametersCache) makeKey(geoIPData GeoIPData) string {
scope := c.support.TacticsServer.GetFilterGeoIPScope(
common.GeoIPData(geoIPData))
var region, ISP, city string
if scope&tactics.GeoIPScopeRegion != 0 {
region = geoIPData.Country
}
if scope&tactics.GeoIPScopeISP != 0 {
ISP = geoIPData.ISP
}
if scope&tactics.GeoIPScopeCity != 0 {
city = geoIPData.City
}
return fmt.Sprintf("%s-%s-%s", region, ISP, city)
}