From a47edf08d0eb7c1a934844edf6520762720d07de Mon Sep 17 00:00:00 2001 From: 9seconds Date: Mon, 3 Feb 2020 11:26:37 +0300 Subject: [PATCH 1/2] Add possibility to disable antireplay cache --- antireplay/cache.go | 8 ++++---- antireplay/init.go | 15 +++++++++++++-- antireplay/nilcache.go | 8 ++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 antireplay/nilcache.go diff --git a/antireplay/cache.go b/antireplay/cache.go index 9eb08ab..dab3af4 100644 --- a/antireplay/cache.go +++ b/antireplay/cache.go @@ -11,19 +11,19 @@ type cache struct { data *fastcache.Cache } -func (c *cache) AddObfuscated2(data []byte) { +func (c cache) AddObfuscated2(data []byte) { c.data.Set(keyObfuscated2(data), nil) } -func (c *cache) AddTLS(data []byte) { +func (c cache) AddTLS(data []byte) { c.data.Set(keyTLS(data), nil) } -func (c *cache) HasObfuscated2(data []byte) bool { +func (c cache) HasObfuscated2(data []byte) bool { return c.data.Has(keyObfuscated2(data)) } -func (c *cache) HasTLS(data []byte) bool { +func (c cache) HasTLS(data []byte) bool { return c.data.Has(keyTLS(data)) } diff --git a/antireplay/init.go b/antireplay/init.go index 9caa19e..3fa30f8 100644 --- a/antireplay/init.go +++ b/antireplay/init.go @@ -8,13 +8,24 @@ import ( "github.com/9seconds/mtg/config" ) +type CacheInterface interface { + AddObfuscated2([]byte) + AddTLS([]byte) + HasObfuscated2([]byte) bool + HasTLS([]byte) bool +} + var ( - Cache cache + Cache CacheInterface initOnce sync.Once ) func Init() { initOnce.Do(func() { - Cache.data = fastcache.New(config.C.AntiReplayMaxSize) + if config.C.AntiReplayMaxSize == 0 { + Cache = nilCache{} + } else { + Cache = cache{fastcache.New(config.C.AntiReplayMaxSize)} + } }) } diff --git a/antireplay/nilcache.go b/antireplay/nilcache.go new file mode 100644 index 0000000..baa0947 --- /dev/null +++ b/antireplay/nilcache.go @@ -0,0 +1,8 @@ +package antireplay + +type nilCache struct{} + +func (n nilCache) AddObfuscated2(_ []byte) {} +func (n nilCache) AddTLS(_ []byte) {} +func (n nilCache) HasObfuscated2(_ []byte) bool { return false } +func (n nilCache) HasTLS(_ []byte) bool { return false } From 2407748ab03cd219ea63795411b441762cc21ced Mon Sep 17 00:00:00 2001 From: 9seconds Date: Mon, 3 Feb 2020 11:27:12 +0300 Subject: [PATCH 2/2] Update README --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 6ae8c3f..a24076e 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,9 @@ by design and we have the negligible possibility of duplication (probability is 1/(2^64)) but it could be quite effective to prevent replays. +It is possible to disable this cache. To do that, please explicitly set +its size to 0. + ## FakeTLS