Parcourir la source

Add possibility to disable antireplay cache

tags/v1.0.2^2
9seconds il y a 6 ans
Parent
révision
a47edf08d0
3 fichiers modifiés avec 25 ajouts et 6 suppressions
  1. 4
    4
      antireplay/cache.go
  2. 13
    2
      antireplay/init.go
  3. 8
    0
      antireplay/nilcache.go

+ 4
- 4
antireplay/cache.go Voir le fichier

@@ -11,19 +11,19 @@ type cache struct {
11 11
 	data *fastcache.Cache
12 12
 }
13 13
 
14
-func (c *cache) AddObfuscated2(data []byte) {
14
+func (c cache) AddObfuscated2(data []byte) {
15 15
 	c.data.Set(keyObfuscated2(data), nil)
16 16
 }
17 17
 
18
-func (c *cache) AddTLS(data []byte) {
18
+func (c cache) AddTLS(data []byte) {
19 19
 	c.data.Set(keyTLS(data), nil)
20 20
 }
21 21
 
22
-func (c *cache) HasObfuscated2(data []byte) bool {
22
+func (c cache) HasObfuscated2(data []byte) bool {
23 23
 	return c.data.Has(keyObfuscated2(data))
24 24
 }
25 25
 
26
-func (c *cache) HasTLS(data []byte) bool {
26
+func (c cache) HasTLS(data []byte) bool {
27 27
 	return c.data.Has(keyTLS(data))
28 28
 }
29 29
 

+ 13
- 2
antireplay/init.go Voir le fichier

@@ -8,13 +8,24 @@ import (
8 8
 	"github.com/9seconds/mtg/config"
9 9
 )
10 10
 
11
+type CacheInterface interface {
12
+	AddObfuscated2([]byte)
13
+	AddTLS([]byte)
14
+	HasObfuscated2([]byte) bool
15
+	HasTLS([]byte) bool
16
+}
17
+
11 18
 var (
12
-	Cache    cache
19
+	Cache    CacheInterface
13 20
 	initOnce sync.Once
14 21
 )
15 22
 
16 23
 func Init() {
17 24
 	initOnce.Do(func() {
18
-		Cache.data = fastcache.New(config.C.AntiReplayMaxSize)
25
+		if config.C.AntiReplayMaxSize == 0 {
26
+			Cache = nilCache{}
27
+		} else {
28
+			Cache = cache{fastcache.New(config.C.AntiReplayMaxSize)}
29
+		}
19 30
 	})
20 31
 }

+ 8
- 0
antireplay/nilcache.go Voir le fichier

@@ -0,0 +1,8 @@
1
+package antireplay
2
+
3
+type nilCache struct{}
4
+
5
+func (n nilCache) AddObfuscated2(_ []byte)      {}
6
+func (n nilCache) AddTLS(_ []byte)              {}
7
+func (n nilCache) HasObfuscated2(_ []byte) bool { return false }
8
+func (n nilCache) HasTLS(_ []byte) bool         { return false }

Chargement…
Annuler
Enregistrer