Kaynağa Gözat

Merge pull request #76 from 9seconds/replay

Base prevention of replay attacks on proxy
tags/0.16
Sergey Arkhipov 7 yıl önce
ebeveyn
işleme
78dea9ae3f
No account linked to committer's email address
11 değiştirilmiş dosya ile 191 ekleme ve 77 silme
  1. 33
    22
      README.md
  2. 37
    0
      antireplay/cache.go
  3. 9
    0
      antireplay/hasher.go
  4. 2
    1
      client/client.go
  5. 9
    1
      client/direct.go
  6. 4
    2
      client/middle.go
  7. 23
    16
      config/config.go
  8. 11
    7
      go.mod
  9. 30
    18
      go.sum
  10. 16
    1
      main.go
  11. 17
    9
      proxy/proxy.go

+ 33
- 22
README.md Dosyayı Görüntüle

@@ -184,34 +184,45 @@ echo dd$(head -c 512 /dev/urandom | md5sum | cut -f 1 -d ' ')
184 184
 ```
185 185
 
186 186
 
187
+## Antireplay cache
188
+
189
+In order to prevent replay attacks, we have internal storage of first
190
+frames messages for connected clients. These frames are generated
191
+randomly by design and we have negligible possibility of duplication
192
+(probability is 1/(2^64)) but it could be quite effective in order to
193
+prevent replays.
194
+
195
+
187 196
 ## Environment variables
188 197
 
189 198
 It is possible to configure this tool using environment variables. You
190 199
 can configure any flag but not secret or adtag. Here is the list of
191 200
 supported environment variables:
192 201
 
193
-| Environment variable     | Corresponding flags    | Default value                     | Description                                                                                                                                                                                                                                                                |
194
-|--------------------------|------------------------|-----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
195
-| `MTG_DEBUG`              | `-d`, `--debug`        | `false`                           | Run in debug mode. Usually, you need to run in this mode  only if you develop this tool or its maintainer is asking you to provide  logs with such verbosity.                                                                                                              |
196
-| `MTG_VERBOSE`            | `-v`, `--verbose`      | `false`                           | Run in verbose mode. This is way less chatty than debug mode.                                                                                                                                                                                                              |
197
-| `MTG_IP`                 | `-b`, `--bind-ip`      | `127.0.0.1`                       | Which IP should we bind to. As usual, `0.0.0.0` means that we want to listen on all interfaces. Also, 4 zeroes will bind to both IPv4 and IPv6.                                                                                                                            |
198
-| `MTG_PORT`               | `-p`, `--bind-port`    | `3128`                            | Which port should we bind to (listen on).                                                                                                                                                                                                                                  |
199
-| `MTG_IPV4`               | `-4`, `--public-ipv4`  | [Autodetect](https://ifconfig.co) | IPv4 address of this proxy. This is required if you NAT your proxy or run it in a docker container. In that case, you absolutely need to specify public IPv4 address of the proxy, otherwise either URLs will be broken or proxy could not access Telegram middle proxies. |
200
-| `MTG_IPV4_PORT`          | `--public-ipv4-port`   | Value of `--bind-port`            | Which port should be public of IPv4 interface. This affects only generated links and should be changed only if you NAT your proxy or run it in a docker container.                                                                                                         |
201
-| `MTG_IPV6`               | `-6`, `--public-ipv6`  | [Autodetect](https://ifconfig.co) | IPv6 address of this proxy. This is required if you NAT your proxy or run it in a docker container. In that case, you absolutely need to specify public IPv6 address of the proxy, otherwise either URLs will be broken or proxy could not access Telegram middle proxies. |
202
-| `MTG_IPV6_PORT`          | `--public-ipv6-port`   | Value of `--bind-port`            | Which port should be public of IPv6 interface. This affects only generated links and should be changed only if you NAT your proxy or run it in a docker container.                                                                                                         |
203
-| `MTG_STATS_IP`           | `-t`, `--stats-ip`     | `127.0.0.1`                       | Which IP should we bind the internal statistics HTTP server.                                                                                                                                                                                                               |
204
-| `MTG_STATS_PORT`         | `-q`, `--stats-port`   | `3129`                            | Which port should we bind the internal statistics HTTP server.                                                                                                                                                                                                             |
205
-| `MTG_STATSD_IP`          | `--statsd-ip`          |                                   | IP/host addresses of statsd service. No defaults, by defaults we do not send anything there.                                                                                                                                                                               |
206
-| `MTG_STATSD_PORT`        | `--statsd-port`        | `8125`                            | Which port should we use to work with statsd.                                                                                                                                                                                                                              |
207
-| `MTG_STATSD_NETWORK`     | `--statsd-network`     | `udp`                             | Which protocol should we use to work with statsd. Possible options are `udp` and `tcp`.                                                                                                                                                                                    |
208
-| `MTG_STATSD_PREFIX`      | `--statsd-prefix`      | `mtg`                             | Which bucket prefix we should use. For example, if you set `mtg`, then metric `traffic.ingress` would be send as `mtg.traffic.ingress`.                                                                                                                                    |
209
-| `MTG_STATSD_TAGS_FORMAT` | `--statsd-tags-format` |                                   | Which tags format we should use. By default, we are using default vanilla statsd tags format but if you want to send directly to InfluxDB or Datadog, please specify it there. Possible options are `influxdb` and `datadog`.                                              |
210
-| `MTG_STATSD_TAGS`        | `--statsd-tags`        |                                   | Which tags should we send to statsd with our metrics. Please specify them as `key=value` pairs.                                                                                                                                                                            |
211
-| `MTG_PROMETHEUS_PREFIX`  | `--prometheus-prefix`  | `mtg`                             | Which namespace should be used for prometheus metrics.                                                                                                                                                                                                                     |
212
-| `MTG_BUFFER_WRITE`       | `-w`, `--write-buffer` | `65536`                           | The size of TCP write buffer in bytes. Write buffer is the buffer for messages which are going from client to Telegram.                                                                                                                                                    |
213
-| `MTG_BUFFER_READ`        | `-r`, `--read-buffer`  | `131072`                          | The size of TCP read buffer in bytes. Read buffer is the buffer for messages from Telegram to client.                                                                                                                                                                      |
214
-| `MTG_SECURE_ONLY`        | `-s`, `--secure-only`  | `false`                           | Support only clients with secure mode (i.e only clients with dd-secrets).                                                                                                                                                                                                  |
202
+| Environment variable          | Corresponding flags         | Default value                     | Description                                                                                                                                                                                                                                                                |
203
+|-------------------------------|-----------------------------|-----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
204
+| `MTG_DEBUG`                   | `-d`, `--debug`             | `false`                           | Run in debug mode. Usually, you need to run in this mode  only if you develop this tool or its maintainer is asking you to provide  logs with such verbosity.                                                                                                              |
205
+| `MTG_VERBOSE`                 | `-v`, `--verbose`           | `false`                           | Run in verbose mode. This is way less chatty than debug mode.                                                                                                                                                                                                              |
206
+| `MTG_IP`                      | `-b`, `--bind-ip`           | `127.0.0.1`                       | Which IP should we bind to. As usual, `0.0.0.0` means that we want to listen on all interfaces. Also, 4 zeroes will bind to both IPv4 and IPv6.                                                                                                                            |
207
+| `MTG_PORT`                    | `-p`, `--bind-port`         | `3128`                            | Which port should we bind to (listen on).                                                                                                                                                                                                                                  |
208
+| `MTG_IPV4`                    | `-4`, `--public-ipv4`       | [Autodetect](https://ifconfig.co) | IPv4 address of this proxy. This is required if you NAT your proxy or run it in a docker container. In that case, you absolutely need to specify public IPv4 address of the proxy, otherwise either URLs will be broken or proxy could not access Telegram middle proxies. |
209
+| `MTG_IPV4_PORT`               | `--public-ipv4-port`        | Value of `--bind-port`            | Which port should be public of IPv4 interface. This affects only generated links and should be changed only if you NAT your proxy or run it in a docker container.                                                                                                         |
210
+| `MTG_IPV6`                    | `-6`, `--public-ipv6`       | [Autodetect](https://ifconfig.co) | IPv6 address of this proxy. This is required if you NAT your proxy or run it in a docker container. In that case, you absolutely need to specify public IPv6 address of the proxy, otherwise either URLs will be broken or proxy could not access Telegram middle proxies. |
211
+| `MTG_IPV6_PORT`               | `--public-ipv6-port`        | Value of `--bind-port`            | Which port should be public of IPv6 interface. This affects only generated links and should be changed only if you NAT your proxy or run it in a docker container.                                                                                                         |
212
+| `MTG_STATS_IP`                | `-t`, `--stats-ip`          | `127.0.0.1`                       | Which IP should we bind the internal statistics HTTP server.                                                                                                                                                                                                               |
213
+| `MTG_STATS_PORT`              | `-q`, `--stats-port`        | `3129`                            | Which port should we bind the internal statistics HTTP server.                                                                                                                                                                                                             |
214
+| `MTG_STATSD_IP`               | `--statsd-ip`               |                                   | IP/host addresses of statsd service. No defaults, by defaults we do not send anything there.                                                                                                                                                                               |
215
+| `MTG_STATSD_PORT`             | `--statsd-port`             | `8125`                            | Which port should we use to work with statsd.                                                                                                                                                                                                                              |
216
+| `MTG_STATSD_NETWORK`          | `--statsd-network`          | `udp`                             | Which protocol should we use to work with statsd. Possible options are `udp` and `tcp`.                                                                                                                                                                                    |
217
+| `MTG_STATSD_PREFIX`           | `--statsd-prefix`           | `mtg`                             | Which bucket prefix we should use. For example, if you set `mtg`, then metric `traffic.ingress` would be send as `mtg.traffic.ingress`.                                                                                                                                    |
218
+| `MTG_STATSD_TAGS_FORMAT`      | `--statsd-tags-format`      |                                   | Which tags format we should use. By default, we are using default vanilla statsd tags format but if you want to send directly to InfluxDB or Datadog, please specify it there. Possible options are `influxdb` and `datadog`.                                              |
219
+| `MTG_STATSD_TAGS`             | `--statsd-tags`             |                                   | Which tags should we send to statsd with our metrics. Please specify them as `key=value` pairs.                                                                                                                                                                            |
220
+| `MTG_PROMETHEUS_PREFIX`       | `--prometheus-prefix`       | `mtg`                             | Which namespace should be used for prometheus metrics.                                                                                                                                                                                                                     |
221
+| `MTG_BUFFER_WRITE`            | `-w`, `--write-buffer`      | `65536`                           | The size of TCP write buffer in bytes. Write buffer is the buffer for messages which are going from client to Telegram.                                                                                                                                                    |
222
+| `MTG_BUFFER_READ`             | `-r`, `--read-buffer`       | `131072`                          | The size of TCP read buffer in bytes. Read buffer is the buffer for messages from Telegram to client.                                                                                                                                                                      |
223
+| `MTG_SECURE_ONLY`             | `-s`, `--secure-only`       | `false`                           | Support only clients with secure mode (i.e only clients with dd-secrets).                                                                                                                                                                                                  |
224
+| `MTG_ANTIREPLAY_MAXSIZE`      | `anti-replay-max-size`      | `128`                             | Max size of antireplay cache in megabytes.                                                                                                                                                                                                                                 |
225
+| `MTG_ANTIREPLAY_EVICTIONTIME` | `anti-replay-eviction-time` | `168h`                            | Eviction time for antireplay cache entries.                                                                                                                                                                                                                                |
215 226
 
216 227
 Usually you want to modify only read/write buffer sizes. If you feel
217 228
 that proxy is slow, try to increase both sizes giving more priority to

+ 37
- 0
antireplay/cache.go Dosyayı Görüntüle

@@ -0,0 +1,37 @@
1
+package antireplay
2
+
3
+import (
4
+	"github.com/allegro/bigcache"
5
+	"github.com/juju/errors"
6
+
7
+	"github.com/9seconds/mtg/config"
8
+)
9
+
10
+// Cache defines storage for obfuscated2 handshake frames.
11
+type Cache struct {
12
+	cache *bigcache.BigCache
13
+}
14
+
15
+func (a Cache) Add(frame []byte) {
16
+	a.cache.Set(string(frame), nil) // nolint: errcheck
17
+}
18
+
19
+func (a Cache) Has(frame []byte) bool {
20
+	_, err := a.cache.Get(string(frame))
21
+
22
+	return err == nil
23
+}
24
+
25
+func NewCache(config *config.Config) (Cache, error) {
26
+	cache, err := bigcache.NewBigCache(bigcache.Config{
27
+		Shards:           1024,
28
+		LifeWindow:       config.AntiReplayEvictionTime,
29
+		Hasher:           hasher{},
30
+		HardMaxCacheSize: config.AntiReplayMaxSize,
31
+	})
32
+	if err != nil {
33
+		return Cache{}, errors.Annotate(err, "Cannot make cache")
34
+	}
35
+
36
+	return Cache{cache}, nil
37
+}

+ 9
- 0
antireplay/hasher.go Dosyayı Görüntüle

@@ -0,0 +1,9 @@
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
+}

+ 2
- 1
client/client.go Dosyayı Görüntüle

@@ -4,6 +4,7 @@ import (
4 4
 	"context"
5 5
 	"net"
6 6
 
7
+	"github.com/9seconds/mtg/antireplay"
7 8
 	"github.com/9seconds/mtg/config"
8 9
 	"github.com/9seconds/mtg/mtproto"
9 10
 	"github.com/9seconds/mtg/wrappers"
@@ -11,4 +12,4 @@ import (
11 12
 
12 13
 // Init defines common method for initializing client connections.
13 14
 type Init func(context.Context, context.CancelFunc, net.Conn, string,
14
-	*config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error)
15
+	antireplay.Cache, *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error)

+ 9
- 1
client/direct.go Dosyayı Görüntüle

@@ -7,6 +7,7 @@ import (
7 7
 
8 8
 	"github.com/juju/errors"
9 9
 
10
+	"github.com/9seconds/mtg/antireplay"
10 11
 	"github.com/9seconds/mtg/config"
11 12
 	"github.com/9seconds/mtg/mtproto"
12 13
 	"github.com/9seconds/mtg/obfuscated2"
@@ -18,7 +19,8 @@ const handshakeTimeout = 10 * time.Second
18 19
 // DirectInit initializes client connection for proxy which connects to
19 20
 // Telegram directly.
20 21
 func DirectInit(ctx context.Context, cancel context.CancelFunc, socket net.Conn,
21
-	connID string, conf *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error) {
22
+	connID string, antiReplayCache antireplay.Cache,
23
+	conf *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error) {
22 24
 	tcpSocket := socket.(*net.TCPConn)
23 25
 	if err := tcpSocket.SetNoDelay(false); err != nil {
24 26
 		return nil, nil, errors.Annotate(err, "Cannot disable NO_DELAY to client socket")
@@ -42,6 +44,12 @@ func DirectInit(ctx context.Context, cancel context.CancelFunc, socket net.Conn,
42 44
 	if err != nil {
43 45
 		return nil, nil, errors.Annotate(err, "Cannot parse obfuscated frame")
44 46
 	}
47
+
48
+	if antiReplayCache.Has([]byte(frame)) {
49
+		return nil, nil, errors.New("Replay attack is detected")
50
+	}
51
+	antiReplayCache.Add([]byte(frame))
52
+
45 53
 	connOpts.ConnectionProto = mtproto.ConnectionProtocolAny
46 54
 	connOpts.ClientAddr = conn.RemoteAddr()
47 55
 

+ 4
- 2
client/middle.go Dosyayı Görüntüle

@@ -4,6 +4,7 @@ import (
4 4
 	"context"
5 5
 	"net"
6 6
 
7
+	"github.com/9seconds/mtg/antireplay"
7 8
 	"github.com/9seconds/mtg/config"
8 9
 	"github.com/9seconds/mtg/mtproto"
9 10
 	"github.com/9seconds/mtg/wrappers"
@@ -12,8 +13,9 @@ import (
12 13
 // MiddleInit initializes client connection for proxy which has to
13 14
 // support promoted channels, connect to Telegram middle proxies etc.
14 15
 func MiddleInit(ctx context.Context, cancel context.CancelFunc, socket net.Conn,
15
-	connID string, conf *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error) {
16
-	conn, opts, err := DirectInit(ctx, cancel, socket, connID, conf)
16
+	connID string, antiReplayCache antireplay.Cache,
17
+	conf *config.Config) (wrappers.Wrap, *mtproto.ConnectionOpts, error) {
18
+	conn, opts, err := DirectInit(ctx, cancel, socket, connID, antiReplayCache, conf)
17 19
 	if err != nil {
18 20
 		return nil, nil, err
19 21
 	}

+ 23
- 16
config/config.go Dosyayı Görüntüle

@@ -6,6 +6,7 @@ import (
6 6
 	"fmt"
7 7
 	"net"
8 8
 	"strconv"
9
+	"time"
9 10
 
10 11
 	"github.com/juju/errors"
11 12
 	statsd "gopkg.in/alexcesaro/statsd.v2"
@@ -31,6 +32,9 @@ type Config struct {
31 32
 	PublicIPv6 net.IP
32 33
 	StatsIP    net.IP
33 34
 
35
+	AntiReplayMaxSize      int
36
+	AntiReplayEvictionTime time.Duration
37
+
34 38
 	StatsD struct {
35 39
 		Addr       net.Addr
36 40
 		Prefix     string
@@ -121,6 +125,7 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo
121 125
 	statsdIP, statsdNetwork, statsdPrefix, statsdTagsFormat string,
122 126
 	statsdTags map[string]string, prometheusPrefix string,
123 127
 	secureOnly bool,
128
+	antiReplayMaxSize int, antiReplayEvictionTime time.Duration,
124 129
 	secret, adtag []byte) (*Config, error) {
125 130
 	secureMode := secureOnly
126 131
 	if bytes.HasPrefix(secret, []byte{0xdd}) && len(secret) == 17 {
@@ -160,22 +165,24 @@ func NewConfig(debug, verbose bool, // nolint: gocyclo
160 165
 	}
161 166
 
162 167
 	conf := &Config{
163
-		Debug:           debug,
164
-		Verbose:         verbose,
165
-		SecureOnly:      secureOnly,
166
-		BindIP:          bindIP,
167
-		BindPort:        bindPort,
168
-		PublicIPv4:      publicIPv4,
169
-		PublicIPv4Port:  publicIPv4Port,
170
-		PublicIPv6:      publicIPv6,
171
-		PublicIPv6Port:  publicIPv6Port,
172
-		StatsIP:         statsIP,
173
-		StatsPort:       statsPort,
174
-		Secret:          secret,
175
-		AdTag:           adtag,
176
-		SecureMode:      secureMode,
177
-		ReadBufferSize:  int(readBufferSize),
178
-		WriteBufferSize: int(writeBufferSize),
168
+		Debug:                  debug,
169
+		Verbose:                verbose,
170
+		SecureOnly:             secureOnly,
171
+		BindIP:                 bindIP,
172
+		BindPort:               bindPort,
173
+		PublicIPv4:             publicIPv4,
174
+		PublicIPv4Port:         publicIPv4Port,
175
+		PublicIPv6:             publicIPv6,
176
+		PublicIPv6Port:         publicIPv6Port,
177
+		StatsIP:                statsIP,
178
+		StatsPort:              statsPort,
179
+		Secret:                 secret,
180
+		AdTag:                  adtag,
181
+		SecureMode:             secureMode,
182
+		ReadBufferSize:         int(readBufferSize),
183
+		WriteBufferSize:        int(writeBufferSize),
184
+		AntiReplayMaxSize:      antiReplayMaxSize,
185
+		AntiReplayEvictionTime: antiReplayEvictionTime,
179 186
 	}
180 187
 	conf.Prometheus.Prefix = prometheusPrefix
181 188
 

+ 11
- 7
go.mod Dosyayı Görüntüle

@@ -3,25 +3,29 @@ 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
+	github.com/allegro/bigcache v1.2.0
6 8
 	github.com/beevik/ntp v0.2.0
9
+	github.com/beorn7/perks v1.0.0 // indirect
10
+	github.com/cespare/xxhash v1.1.0
7 11
 	github.com/dustin/go-humanize v1.0.0
8 12
 	github.com/gofrs/uuid v3.2.0+incompatible
9
-	github.com/golang/protobuf v1.3.0 // indirect
13
+	github.com/golang/protobuf v1.3.1 // indirect
10 14
 	github.com/juju/errors v0.0.0-20190207033735-e65537c515d7
11 15
 	github.com/juju/loggo v0.0.0-20190212223446-d976af380377 // indirect
12
-	github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073 // indirect
16
+	github.com/juju/testing v0.0.0-20190418112600-6570bd8f8541 // indirect
13 17
 	github.com/kr/pretty v0.1.0 // indirect
14 18
 	github.com/pkg/errors v0.8.1 // indirect
15
-	github.com/prometheus/client_golang v0.9.2
19
+	github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829
16 20
 	github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 // indirect
17
-	github.com/prometheus/common v0.2.0 // indirect
18
-	github.com/prometheus/procfs v0.0.0-20190227231451-bbced9601137 // indirect
21
+	github.com/prometheus/common v0.3.0 // indirect
22
+	github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0 // indirect
23
+	github.com/spaolacci/murmur3 v1.1.0 // indirect
19 24
 	github.com/stretchr/testify v1.3.0
20 25
 	go.uber.org/atomic v1.3.2 // indirect
21 26
 	go.uber.org/multierr v1.1.0 // indirect
22 27
 	go.uber.org/zap v1.9.1
23
-	golang.org/x/net v0.0.0-20190228165749-92fc7df08ae7 // indirect
24
-	golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 // indirect
28
+	golang.org/x/net v0.0.0-20190424112056-4829fb13d2c6 // indirect
25 29
 	gopkg.in/alecthomas/kingpin.v2 v2.2.6
26 30
 	gopkg.in/alexcesaro/statsd.v2 v2.0.0
27 31
 	gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect

+ 30
- 18
go.sum Dosyayı Görüntüle

@@ -1,11 +1,20 @@
1
+github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
2
+github.com/OneOfOne/xxhash v1.2.5 h1:zl/OfRA6nftbBK9qTohYBJ5xvw6C/oNKizR7cZGl3cI=
3
+github.com/OneOfOne/xxhash v1.2.5/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
1 4
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
2 5
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
3 6
 github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
4 7
 github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
8
+github.com/allegro/bigcache v1.2.0 h1:qDaE0QoF29wKBb3+pXFrJFy1ihe5OT9OiXhg1t85SxM=
9
+github.com/allegro/bigcache v1.2.0/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM=
5 10
 github.com/beevik/ntp v0.2.0 h1:sGsd+kAXzT0bfVfzJfce04g+dSRfrs+tbQW8lweuYgw=
6 11
 github.com/beevik/ntp v0.2.0/go.mod h1:hIHWr+l3+/clUnF44zdK+CWW7fO8dR5cIylAQ76NRpg=
7 12
 github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0=
8 13
 github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
14
+github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
15
+github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
16
+github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
17
+github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
9 18
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10 19
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
11 20
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -20,14 +29,14 @@ github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo=
20 29
 github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
21 30
 github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
22 31
 github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
23
-github.com/golang/protobuf v1.3.0 h1:kbxbvI4Un1LUWKxufD+BiE6AEExYYgkQLQmLFqA1LFk=
24
-github.com/golang/protobuf v1.3.0/go.mod h1:Qd/q+1AKNOZr9uGQzbzCmRO6sUih6GTPZv6a1/R87v0=
32
+github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
33
+github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
25 34
 github.com/juju/errors v0.0.0-20190207033735-e65537c515d7 h1:dMIPRDg6gi7CUp0Kj2+HxqJ5kTr1iAdzsXYIrLCNSmU=
26 35
 github.com/juju/errors v0.0.0-20190207033735-e65537c515d7/go.mod h1:W54LbzXuIE0boCoNJfwqpmkKJ1O4TCTZMetAt6jGk7Q=
27 36
 github.com/juju/loggo v0.0.0-20190212223446-d976af380377 h1:n6QjW3g5JNY3xPmIjFt6z1H6tFQA6BhwOC2bvTAm1YU=
28 37
 github.com/juju/loggo v0.0.0-20190212223446-d976af380377/go.mod h1:vgyd7OREkbtVEN/8IXZe5Ooef3LQePvuBm9UWj6ZL8U=
29
-github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073 h1:WQM1NildKThwdP7qWrNAFGzp4ijNLw8RlgENkaI4MJs=
30
-github.com/juju/testing v0.0.0-20180920084828-472a3e8b2073/go.mod h1:63prj8cnj0tU0S9OHjGJn+b1h0ZghCndfnbQolrYTwA=
38
+github.com/juju/testing v0.0.0-20190418112600-6570bd8f8541 h1:291RYTdi1UqkKeBr9UC0ArNVSIAzsh5CQDkIXEp/iTg=
39
+github.com/juju/testing v0.0.0-20190418112600-6570bd8f8541/go.mod h1:63prj8cnj0tU0S9OHjGJn+b1h0ZghCndfnbQolrYTwA=
31 40
 github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
32 41
 github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
33 42
 github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
@@ -46,21 +55,26 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
46 55
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
47 56
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
48 57
 github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
49
-github.com/prometheus/client_golang v0.9.2 h1:awm861/B8OKDd2I/6o1dy3ra4BamzKhYOiGItCeZ740=
50
-github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM=
58
+github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829 h1:D+CiwcpGTW6pL6bv6KI3KbyEyCKyS+1JWS2h8PNDnGA=
59
+github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=
51 60
 github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 h1:idejC8f05m9MGOsuEi1ATq9shN03HrxNkD/luQvxCv8=
52 61
 github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
62
+github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
53 63
 github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE=
54 64
 github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
55
-github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
56 65
 github.com/prometheus/common v0.2.0 h1:kUZDBDTdBVBYBj5Tmh2NZLlF60mfjA27rM34b+cVwNU=
57 66
 github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
67
+github.com/prometheus/common v0.3.0 h1:taZ4h8Tkxv2kNyoSctBvfXEHmBmxrwmIidZTIaHons4=
68
+github.com/prometheus/common v0.3.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
58 69
 github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d h1:GoAlyOgbOEIFdaDqxJVlbOQ1DtGmZWs/Qau0hIlk+WQ=
59 70
 github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
60
-github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
61
-github.com/prometheus/procfs v0.0.0-20190227231451-bbced9601137 h1:3l8oligPtjd4JuM+OZ+U8sjtwFGJs98cdWsqs6QZRWs=
62
-github.com/prometheus/procfs v0.0.0-20190227231451-bbced9601137/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
71
+github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
72
+github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0 h1:c8R11WC8m7KNMkTv/0+Be8vvwo4I3/Ut9AC2FW8fX3U=
73
+github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
63 74
 github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
75
+github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
76
+github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
77
+github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
64 78
 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
65 79
 github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
66 80
 github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
@@ -74,20 +88,18 @@ go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/
74 88
 go.uber.org/zap v1.9.1 h1:XCJQEf3W6eZaVwhRBof6ImoYGJSITeKWsyeh3HFu/5o=
75 89
 go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
76 90
 golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
77
-golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
91
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
78 92
 golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
79
-golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
80
-golang.org/x/net v0.0.0-20190228165749-92fc7df08ae7 h1:Qe/u+eY379X4He4GBMFZYu3pmh1ML5yT1aL1ndNM1zQ=
81
-golang.org/x/net v0.0.0-20190228165749-92fc7df08ae7/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
82
-golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
93
+golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
94
+golang.org/x/net v0.0.0-20190424112056-4829fb13d2c6 h1:FP8hkuE6yUEaJnK7O2eTuejKWwW+Rhfj80dQ2JcKxCU=
95
+golang.org/x/net v0.0.0-20190424112056-4829fb13d2c6/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
83 96
 golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
84 97
 golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
85 98
 golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
86
-golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6 h1:bjcUS9ztw9kFmmIxJInhon/0Is3p+EHBKNgquIzo1OI=
87
-golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
88 99
 golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
89 100
 golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
90
-google.golang.org/genproto v0.0.0-20180831171423-11092d34479b/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
101
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
102
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
91 103
 gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
92 104
 gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
93 105
 gopkg.in/alexcesaro/statsd.v2 v2.0.0 h1:FXkZSCZIH17vLCO5sO2UucTHsH9pc+17F6pl3JVCwMc=

+ 16
- 1
main.go Dosyayı Görüntüle

@@ -134,6 +134,17 @@ var (
134 134
 		Envar("MTG_SECURE_ONLY").
135 135
 		Bool()
136 136
 
137
+	antiReplayMaxSize = app.Flag("anti-replay-max-size",
138
+		"Max size of antireplay cache in megabytes.").
139
+		Envar("MTG_ANTIREPLAY_MAXSIZE").
140
+		Default("128").
141
+		Int()
142
+	antiReplayEvictionTime = app.Flag("anti-replay-eviction-time",
143
+		"Eviction time period for obfuscated2 handshakes").
144
+		Envar("MTG_ANTIREPLAY_EVICTIONTIME").
145
+		Default("168h").
146
+		Duration()
147
+
137 148
 	secret = app.Arg("secret", "Secret of this proxy.").Required().HexBytes()
138 149
 	adtag  = app.Arg("adtag", "ADTag of the proxy.").HexBytes()
139 150
 )
@@ -156,6 +167,7 @@ func main() { // nolint: gocyclo
156 167
 		*bindPort, *publicIPv4Port, *publicIPv6Port, *statsPort, *statsdPort,
157 168
 		*statsdIP, *statsdNetwork, *statsdPrefix, *statsdTagsFormat,
158 169
 		*statsdTags, *prometheusPrefix, *secureOnly,
170
+		*antiReplayMaxSize, *antiReplayEvictionTime,
159 171
 		*secret, *adtag,
160 172
 	)
161 173
 	if err != nil {
@@ -202,7 +214,10 @@ func main() { // nolint: gocyclo
202 214
 		panic(err)
203 215
 	}
204 216
 
205
-	server := proxy.NewProxy(conf)
217
+	server, err := proxy.NewProxy(conf)
218
+	if err != nil {
219
+		panic(err)
220
+	}
206 221
 	if err := server.Serve(); err != nil {
207 222
 		zap.S().Fatalw("Server stopped", "error", err)
208 223
 	}

+ 17
- 9
proxy/proxy.go Dosyayı Görüntüle

@@ -10,6 +10,7 @@ import (
10 10
 	"github.com/juju/errors"
11 11
 	"go.uber.org/zap"
12 12
 
13
+	"github.com/9seconds/mtg/antireplay"
13 14
 	"github.com/9seconds/mtg/client"
14 15
 	"github.com/9seconds/mtg/config"
15 16
 	"github.com/9seconds/mtg/mtproto"
@@ -20,9 +21,10 @@ import (
20 21
 
21 22
 // Proxy is a core of this program.
22 23
 type Proxy struct {
23
-	clientInit client.Init
24
-	tg         telegram.Telegram
25
-	conf       *config.Config
24
+	antiReplayCache antireplay.Cache
25
+	clientInit      client.Init
26
+	tg              telegram.Telegram
27
+	conf            *config.Config
26 28
 }
27 29
 
28 30
 // Serve runs TCP proxy server.
@@ -58,7 +60,7 @@ func (p *Proxy) accept(conn net.Conn) {
58 60
 
59 61
 	log.Infow("Client connected", "addr", conn.RemoteAddr())
60 62
 
61
-	clientConn, opts, err := p.clientInit(ctx, cancel, conn, connID, p.conf)
63
+	clientConn, opts, err := p.clientInit(ctx, cancel, conn, connID, p.antiReplayCache, p.conf)
62 64
 	if err != nil {
63 65
 		log.Errorw("Cannot initialize client connection", "error", err)
64 66
 		return
@@ -150,10 +152,15 @@ func (p *Proxy) directPipe(src wrappers.StreamReadCloser, dst io.Writer, wait *s
150 152
 }
151 153
 
152 154
 // NewProxy returns new proxy instance.
153
-func NewProxy(conf *config.Config) *Proxy {
155
+func NewProxy(conf *config.Config) (*Proxy, error) {
154 156
 	var clientInit client.Init
155 157
 	var tg telegram.Telegram
156 158
 
159
+	cache, err := antireplay.NewCache(conf)
160
+	if err != nil {
161
+		return nil, errors.Annotate(err, "Cannot make proxy")
162
+	}
163
+
157 164
 	if conf.UseMiddleProxy() {
158 165
 		clientInit = client.MiddleInit
159 166
 		tg = telegram.NewMiddleTelegram(conf)
@@ -163,8 +170,9 @@ func NewProxy(conf *config.Config) *Proxy {
163 170
 	}
164 171
 
165 172
 	return &Proxy{
166
-		conf:       conf,
167
-		clientInit: clientInit,
168
-		tg:         tg,
169
-	}
173
+		antiReplayCache: cache,
174
+		conf:            conf,
175
+		clientInit:      clientInit,
176
+		tg:              tg,
177
+	}, nil
170 178
 }

Loading…
İptal
Kaydet