ソースを参照

Use ristretto instead of bigcache

tags/1.0^2
9seconds 6年前
コミット
34ad883559
9個のファイルの変更54行の追加71行の削除
  1. 21
    9
      antireplay/cache.go
  2. 0
    9
      antireplay/hasher.go
  3. 11
    20
      antireplay/init.go
  4. 2
    7
      config/config.go
  5. 9
    0
      faketls/client_protocol.go
  6. 1
    4
      go.mod
  7. 4
    10
      go.sum
  8. 3
    9
      main.go
  9. 3
    3
      obfuscated2/client_protocol.go

+ 21
- 9
antireplay/cache.go ファイルの表示

@@ -1,26 +1,38 @@
1 1
 package antireplay
2 2
 
3
-import "github.com/allegro/bigcache"
3
+import "github.com/dgraph-io/ristretto"
4
+
5
+var (
6
+	prefixObfuscated2 = []byte{0x00}
7
+	prefixTLS         = []byte{0x01}
8
+)
4 9
 
5 10
 type cache struct {
6
-	obfuscated2 *bigcache.BigCache
7
-	tls         *bigcache.BigCache
11
+	data *ristretto.Cache
8 12
 }
9 13
 
10 14
 func (c *cache) AddObfuscated2(data []byte) {
11
-	c.obfuscated2.Set(string(data), nil) // nolint: errcheck
15
+	c.data.Set(keyObfuscated2(data), nil, int64(len(data)))
12 16
 }
13 17
 
14 18
 func (c *cache) AddTLS(data []byte) {
15
-	c.tls.Set(string(data), nil) // nolint: errcheck
19
+	c.data.Set(keyTLS(data), nil, int64(len(data)))
16 20
 }
17 21
 
18 22
 func (c *cache) HasObfuscated2(data []byte) bool {
19
-	_, err := c.obfuscated2.Get(string(data))
20
-	return err == nil
23
+	_, ok := c.data.Get(keyObfuscated2(data))
24
+	return ok
21 25
 }
22 26
 
23 27
 func (c *cache) HasTLS(data []byte) bool {
24
-	_, err := c.tls.Get(string(data))
25
-	return err == nil
28
+	_, ok := c.data.Get(keyTLS(data))
29
+	return ok
30
+}
31
+
32
+func keyObfuscated2(data []byte) string {
33
+	return string(append(prefixObfuscated2, data...))
34
+}
35
+
36
+func keyTLS(data []byte) string {
37
+	return string(append(prefixTLS, data...))
26 38
 }

+ 0
- 9
antireplay/hasher.go ファイルの表示

@@ -1,9 +0,0 @@
1
-package antireplay
2
-
3
-import "github.com/cespare/xxhash"
4
-
5
-type hasher struct{}
6
-
7
-func (h hasher) Sum64(value string) uint64 {
8
-	return xxhash.Sum64String(value)
9
-}

+ 11
- 20
antireplay/init.go ファイルの表示

@@ -1,42 +1,33 @@
1 1
 package antireplay
2 2
 
3 3
 import (
4
+	"math"
4 5
 	"sync"
5 6
 
6 7
 	"github.com/9seconds/mtg/config"
7
-	"github.com/allegro/bigcache"
8
+	"github.com/dgraph-io/ristretto"
8 9
 )
9 10
 
10 11
 var (
11
-	Cache    *cache
12
+	Cache    cache
12 13
 	initOnce sync.Once
13 14
 )
14 15
 
15 16
 func Init() {
16 17
 	initOnce.Do(func() {
17
-		c1, err := bigcache.NewBigCache(bigcache.Config{
18
-			Shards:           1024,
19
-			LifeWindow:       config.C.AntiReplayEvictionTime,
20
-			Hasher:           hasher{},
21
-			HardMaxCacheSize: config.C.AntiReplayMaxSize,
22
-		})
23
-		if err != nil {
24
-			panic(err)
25
-		}
18
+		cost := float64(config.C.AntiReplayMaxSize) / 32.0
19
+		cost = math.Ceil(cost)
26 20
 
27
-		c2, err := bigcache.NewBigCache(bigcache.Config{
28
-			Shards:           1024,
29
-			LifeWindow:       config.C.AntiReplayEvictionTime,
30
-			Hasher:           hasher{},
31
-			HardMaxCacheSize: config.C.AntiReplayMaxSize,
21
+		c, err := ristretto.NewCache(&ristretto.Config{
22
+			NumCounters: int64(cost) * 10,
23
+			MaxCost:     config.C.AntiReplayMaxSize,
24
+			BufferItems: 64,
25
+			Metrics:     false,
32 26
 		})
33 27
 		if err != nil {
34 28
 			panic(err)
35 29
 		}
36 30
 
37
-		Cache = &cache{
38
-			obfuscated2: c1,
39
-			tls:         c2,
40
-		}
31
+		Cache.data = c
41 32
 	})
42 33
 }

+ 2
- 7
config/config.go ファイルの表示

@@ -7,7 +7,6 @@ import (
7 7
 	"errors"
8 8
 	"fmt"
9 9
 	"net"
10
-	"time"
11 10
 
12 11
 	"github.com/alecthomas/units"
13 12
 	"go.uber.org/zap"
@@ -58,7 +57,6 @@ const (
58 57
 	OptionTypeCloakPort
59 58
 
60 59
 	OptionTypeAntiReplayMaxSize
61
-	OptionTypeAntiReplayEvictionTime
62 60
 
63 61
 	OptionTypeSecret
64 62
 	OptionTypeAdtag
@@ -80,8 +78,7 @@ type Config struct {
80 78
 	ReadBuffer  int `json:"read_buffer"`
81 79
 	CloakPort   int `json:"cloak_port"`
82 80
 
83
-	AntiReplayMaxSize      int           `json:"anti_replay_max_size"`
84
-	AntiReplayEvictionTime time.Duration `json:"anti_replay_eviction_time"`
81
+	AntiReplayMaxSize int64 `json:"anti_replay_max_size"`
85 82
 
86 83
 	Debug            bool             `json:"debug"`
87 84
 	Verbose          bool             `json:"verbose"`
@@ -151,9 +148,7 @@ func Init(options ...Opt) error { // nolint: gocyclo, funlen
151 148
 		case OptionTypeCloakPort:
152 149
 			C.CloakPort = int(opt.Value.(uint16))
153 150
 		case OptionTypeAntiReplayMaxSize:
154
-			C.AntiReplayMaxSize = opt.Value.(int)
155
-		case OptionTypeAntiReplayEvictionTime:
156
-			C.AntiReplayEvictionTime = opt.Value.(time.Duration)
151
+			C.AntiReplayMaxSize = int64(opt.Value.(units.Base2Bytes))
157 152
 		case OptionTypeSecret:
158 153
 			C.Secret = opt.Value.([]byte)
159 154
 		case OptionTypeAdtag:

+ 9
- 0
faketls/client_protocol.go ファイルの表示

@@ -11,10 +11,12 @@ import (
11 11
 	"sync"
12 12
 	"time"
13 13
 
14
+	"github.com/9seconds/mtg/antireplay"
14 15
 	"github.com/9seconds/mtg/config"
15 16
 	"github.com/9seconds/mtg/conntypes"
16 17
 	"github.com/9seconds/mtg/obfuscated2"
17 18
 	"github.com/9seconds/mtg/protocol"
19
+	"github.com/9seconds/mtg/stats"
18 20
 	"github.com/9seconds/mtg/tlstypes"
19 21
 	"github.com/9seconds/mtg/wrappers/stream"
20 22
 )
@@ -82,6 +84,13 @@ func (c *ClientProtocol) tlsHandshake(conn io.ReadWriter) error {
82 84
 		return errBadTime
83 85
 	}
84 86
 
87
+	if antireplay.Cache.HasTLS(clientHello.Random[:]) {
88
+		stats.Stats.ReplayDetected()
89
+		return errors.New("replay attack is detected")
90
+	}
91
+
92
+	antireplay.Cache.AddTLS(clientHello.Random[:])
93
+
85 94
 	hostCert, err := connectionServerInstance.get()
86 95
 	if err != nil {
87 96
 		return fmt.Errorf("cannot get host certificate: %w", err)

+ 1
- 4
go.mod ファイルの表示

@@ -3,13 +3,10 @@ module github.com/9seconds/mtg
3 3
 replace github.com/golang/lint => github.com/golang/lint v0.0.0-20190227174305-8f45f776aaf1
4 4
 
5 5
 require (
6
-	github.com/OneOfOne/xxhash v1.2.5 // indirect
7 6
 	github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d
8
-	github.com/allegro/bigcache v1.2.1
9 7
 	github.com/beevik/ntp v0.2.0
10
-	github.com/cespare/xxhash v1.1.0
8
+	github.com/dgraph-io/ristretto v0.0.0-20191108194154-8d6a8a75b4ca
11 9
 	github.com/prometheus/client_golang v1.2.1
12
-	github.com/spaolacci/murmur3 v1.1.0 // indirect
13 10
 	go.uber.org/multierr v1.4.0 // indirect
14 11
 	go.uber.org/zap v1.12.0
15 12
 	golang.org/x/crypto v0.0.0-20191106202628-ed6320f186d4

+ 4
- 10
go.sum ファイルの表示

@@ -1,8 +1,5 @@
1 1
 github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
2 2
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3
-github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
4
-github.com/OneOfOne/xxhash v1.2.5 h1:zl/OfRA6nftbBK9qTohYBJ5xvw6C/oNKizR7cZGl3cI=
5
-github.com/OneOfOne/xxhash v1.2.5/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
6 3
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
7 4
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
8 5
 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
@@ -13,8 +10,6 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 h1:Hs82Z41s6SdL1C
13 10
 github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
14 11
 github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
15 12
 github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
16
-github.com/allegro/bigcache v1.2.1 h1:hg1sY1raCwic3Vnsvje6TT7/pnZba83LeFck5NrFKSc=
17
-github.com/allegro/bigcache v1.2.1/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
18 13
 github.com/beevik/ntp v0.2.0 h1:sGsd+kAXzT0bfVfzJfce04g+dSRfrs+tbQW8lweuYgw=
19 14
 github.com/beevik/ntp v0.2.0/go.mod h1:hIHWr+l3+/clUnF44zdK+CWW7fO8dR5cIylAQ76NRpg=
20 15
 github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
@@ -23,13 +18,15 @@ github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
23 18
 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
24 19
 github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
25 20
 github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
26
-github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
27
-github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
28 21
 github.com/cespare/xxhash/v2 v2.1.0 h1:yTUvW7Vhb89inJ+8irsUqiWjh8iT6sQPZiQzI6ReGkA=
29 22
 github.com/cespare/xxhash/v2 v2.1.0/go.mod h1:dgIUBU3pDso/gPgZ1osOZ0iQf77oPR28Tjxl5dIMyVM=
30 23
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
31 24
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
32 25
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
26
+github.com/dgraph-io/ristretto v0.0.0-20191108194154-8d6a8a75b4ca h1:NgFAOh7RDM5WbdADm6VyLG1koLrzG0G24M/f5395PcI=
27
+github.com/dgraph-io/ristretto v0.0.0-20191108194154-8d6a8a75b4ca/go.mod h1:edzKIzGvqUCMzhTVWbiTSe75zD9Xxq0GtSBtFmaUTZs=
28
+github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
29
+github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
33 30
 github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
34 31
 github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
35 32
 github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
@@ -94,9 +91,6 @@ github.com/prometheus/procfs v0.0.5/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDa
94 91
 github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
95 92
 github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
96 93
 github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
97
-github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
98
-github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
99
-github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
100 94
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
101 95
 github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
102 96
 github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=

+ 3
- 9
main.go ファイルの表示

@@ -104,15 +104,10 @@ var (
104 104
 		Default("443").
105 105
 		Uint16()
106 106
 	runAntiReplayMaxSize = runCommand.Flag("anti-replay-max-size",
107
-		"Max size of antireplay cache in megabytes.").
107
+		"Max size of antireplay cache.").
108 108
 		Envar("MTG_ANTIREPLAY_MAXSIZE").
109
-		Default("128").
110
-		Int()
111
-	runAntiReplayEvictionTime = runCommand.Flag("anti-replay-eviction-time",
112
-		"Eviction time period for obfuscated2 handshakes").
113
-		Envar("MTG_ANTIREPLAY_EVICTIONTIME").
114
-		Default("168h").
115
-		Duration()
109
+		Default("128MB").
110
+		Bytes()
116 111
 	runSecret = runCommand.Arg("secret", "Secret of this proxy.").Required().HexBytes()
117 112
 	runAdtag  = runCommand.Arg("adtag", "ADTag of the proxy.").HexBytes()
118 113
 )
@@ -146,7 +141,6 @@ func main() {
146 141
 			config.Opt{Option: config.OptionTypeReadBufferSize, Value: *runReadBufferSize},
147 142
 			config.Opt{Option: config.OptionTypeCloakPort, Value: *runTLSCloakPort},
148 143
 			config.Opt{Option: config.OptionTypeAntiReplayMaxSize, Value: *runAntiReplayMaxSize},
149
-			config.Opt{Option: config.OptionTypeAntiReplayEvictionTime, Value: *runAntiReplayEvictionTime},
150 144
 			config.Opt{Option: config.OptionTypeSecret, Value: *runSecret},
151 145
 			config.Opt{Option: config.OptionTypeAdtag, Value: *runAdtag},
152 146
 		)

+ 3
- 3
obfuscated2/client_protocol.go ファイルの表示

@@ -81,13 +81,13 @@ func (c *ClientProtocol) Handshake(socket conntypes.StreamReadWriteCloser) (conn
81 81
 		c.dc = conntypes.DCDefaultIdx
82 82
 	}
83 83
 
84
-	replayKeys := decryptedFrame.Unique()
85
-	if antireplay.Cache.HasObfuscated2(replayKeys) {
84
+	replayKey := decryptedFrame.Unique()
85
+	if antireplay.Cache.HasObfuscated2(replayKey) {
86 86
 		stats.Stats.ReplayDetected()
87 87
 		return nil, errors.New("replay attack is detected")
88 88
 	}
89 89
 
90
-	antireplay.Cache.AddObfuscated2(replayKeys)
90
+	antireplay.Cache.AddObfuscated2(replayKey)
91 91
 
92 92
 	return stream.NewObfuscated2(socket, encryptor, decryptor), nil
93 93
 }

読み込み中…
キャンセル
保存