Просмотр исходного кода

Fixes for obfuscated2

tags/v2.0.0-rc1
9seconds 5 лет назад
Родитель
Сommit
0330a0e5cd

+ 6
- 0
antireplay/init.go Просмотреть файл

1
+package antireplay
2
+
3
+const (
4
+	DefaultMaxSize   = 10 * 1024 * 1024 // 10mib
5
+	DefaultErrorRate = 0.0001
6
+)

+ 1
- 0
cli/cli.go Просмотреть файл

5
 type CLI struct {
5
 type CLI struct {
6
 	GenerateSecret GenerateSecret   `kong:"cmd,help='Generate new proxy secret'"`
6
 	GenerateSecret GenerateSecret   `kong:"cmd,help='Generate new proxy secret'"`
7
 	Access         Access           `kong:"cmd,help='Print access information.'"`
7
 	Access         Access           `kong:"cmd,help='Print access information.'"`
8
+	Run            Proxy            `kong:"cmd,help='Run proxy.'"`
8
 	Version        kong.VersionFlag `kong:"help='Print version.',short='v'"`
9
 	Version        kong.VersionFlag `kong:"help='Print version.',short='v'"`
9
 }
10
 }

+ 205
- 0
cli/proxy.go Просмотреть файл

1
+package cli
2
+
3
+import (
4
+	"fmt"
5
+	"net"
6
+	"os"
7
+
8
+	"github.com/9seconds/mtg/v2/antireplay"
9
+	"github.com/9seconds/mtg/v2/events"
10
+	"github.com/9seconds/mtg/v2/ipblocklist"
11
+	"github.com/9seconds/mtg/v2/logger"
12
+	"github.com/9seconds/mtg/v2/mtglib"
13
+	"github.com/9seconds/mtg/v2/stats"
14
+	"github.com/9seconds/mtg/v2/timeattack"
15
+	"github.com/9seconds/mtg/v2/utils"
16
+	"github.com/rs/zerolog"
17
+)
18
+
19
+type Proxy struct {
20
+	base
21
+
22
+	prometheusListener net.Listener
23
+	prometheus         *stats.PrometheusFactory
24
+	statsdFactory      *stats.StatsdFactory
25
+}
26
+
27
+func (c *Proxy) Run(cli *CLI, version string) error {
28
+	if err := c.ReadConfig(version); err != nil {
29
+		return fmt.Errorf("cannot init config: %w", err)
30
+	}
31
+
32
+	return c.Execute()
33
+}
34
+
35
+func (c *Proxy) Execute() error { // nolint: funlen
36
+	zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
37
+	zerolog.TimestampFieldName = "timestamp"
38
+	zerolog.LevelFieldName = "level"
39
+
40
+	ctx := utils.RootContext()
41
+	opts := mtglib.ProxyOpts{
42
+		Logger:             logger.NewZeroLogger(zerolog.New(os.Stdout).With().Timestamp().Logger()),
43
+		Network:            c.Network,
44
+		AntiReplayCache:    antireplay.NewNoop(),
45
+		IPBlocklist:        ipblocklist.NewNoop(),
46
+		TimeAttackDetector: timeattack.NewNoop(),
47
+		EventStream:        events.NewNoopStream(),
48
+
49
+		Secret:      c.Config.Secret,
50
+		BufferSize:  c.Config.TCPBuffer.Value(mtglib.DefaultBufferSize),
51
+		CloakPort:   c.Config.CloakPort.Value(mtglib.DefaultCloakPort),
52
+		IdleTimeout: c.Config.Network.Timeout.Idle.Value(mtglib.DefaultIdleTimeout),
53
+		PreferIP:    c.Config.PreferIP.Value(mtglib.DefaultPreferIP),
54
+	}
55
+
56
+	defer func() {
57
+		opts.AntiReplayCache.Shutdown()
58
+		opts.IPBlocklist.Shutdown()
59
+		opts.EventStream.Shutdown()
60
+	}()
61
+
62
+	if opts.Concurrency == 0 {
63
+		opts.Concurrency = mtglib.DefaultConcurrency
64
+	}
65
+
66
+	opts.Logger.BindStr("configuration", c.Config.String()).Debug("configuration")
67
+
68
+	c.setupAntiReplayCache(&opts)
69
+	c.setupTimeAttackDetector(&opts)
70
+
71
+	if err := c.setupIPBlocklist(&opts); err != nil {
72
+		return fmt.Errorf("cannot setup ipblocklist: %w", err)
73
+	}
74
+
75
+	if err := c.setupEventStream(&opts); err != nil {
76
+		return fmt.Errorf("cannot setup event stream: %w", err)
77
+	}
78
+
79
+	proxy, err := mtglib.NewProxy(opts)
80
+	if err != nil {
81
+		return fmt.Errorf("cannot create a proxy: %w", err)
82
+	}
83
+
84
+	listener, err := net.Listen("tcp", c.Config.BindTo.String())
85
+	if err != nil {
86
+		return fmt.Errorf("cannot start proxy: %w", err)
87
+	}
88
+
89
+	go proxy.Serve(listener) // nolint: errcheck
90
+
91
+	<-ctx.Done()
92
+
93
+	listener.Close()
94
+
95
+	if c.prometheusListener != nil {
96
+		c.prometheusListener.Close()
97
+	}
98
+
99
+	if c.prometheus != nil {
100
+		c.prometheus.Close()
101
+	}
102
+
103
+	if c.statsdFactory != nil {
104
+		c.statsdFactory.Close()
105
+	}
106
+
107
+	return nil
108
+}
109
+
110
+func (c *Proxy) setupAntiReplayCache(opts *mtglib.ProxyOpts) {
111
+	if !c.Config.Defense.AntiReplay.Enabled {
112
+		return
113
+	}
114
+
115
+	opts.AntiReplayCache = antireplay.NewStableBloomFilter(
116
+		c.Config.Defense.AntiReplay.MaxSize.Value(antireplay.DefaultMaxSize),
117
+		c.Config.Defense.AntiReplay.ErrorRate.Value(antireplay.DefaultErrorRate),
118
+	)
119
+}
120
+
121
+func (c *Proxy) setupTimeAttackDetector(opts *mtglib.ProxyOpts) {
122
+	if !c.Config.Defense.Time.Enabled {
123
+		return
124
+	}
125
+
126
+	opts.TimeAttackDetector = timeattack.NewDetector(
127
+		c.Config.Defense.Time.AllowSkewness.Value(timeattack.DefaultDuration),
128
+	)
129
+}
130
+
131
+func (c *Proxy) setupIPBlocklist(opts *mtglib.ProxyOpts) error {
132
+	if !c.Config.Defense.Blocklist.Enabled {
133
+		return nil
134
+	}
135
+
136
+	remoteURLs := []string{}
137
+	localFiles := []string{}
138
+
139
+	for _, v := range c.Config.Defense.Blocklist.URLs {
140
+		if v.IsRemote() {
141
+			remoteURLs = append(remoteURLs, v.String())
142
+		} else {
143
+			localFiles = append(localFiles, v.String())
144
+		}
145
+	}
146
+
147
+	firehol, err := ipblocklist.NewFirehol(opts.Logger.Named("ipblockist"),
148
+		c.Network,
149
+		c.Config.Defense.Blocklist.DownloadConcurrency,
150
+		remoteURLs,
151
+		localFiles)
152
+	if err != nil {
153
+		return err // nolint: wrapcheck
154
+	}
155
+
156
+	go firehol.Run(c.Config.Defense.Blocklist.UpdateEach.Value(ipblocklist.DefaultUpdateEach))
157
+
158
+	opts.IPBlocklist = firehol
159
+
160
+	return nil
161
+}
162
+
163
+func (c *Proxy) setupEventStream(opts *mtglib.ProxyOpts) error {
164
+	factories := make([]events.ObserverFactory, 0, 2)
165
+
166
+	if c.Config.Stats.StatsD.Enabled {
167
+		statsdFactory, err := stats.NewStatsd(
168
+			c.Config.Stats.StatsD.Address.String(),
169
+			opts.Logger.Named("statsd"),
170
+			c.Config.Stats.StatsD.MetricPrefix.Value(stats.DefaultStatsdMetricPrefix),
171
+			c.Config.Stats.StatsD.TagFormat.Value(stats.DefaultStatsdTagFormat))
172
+		if err != nil {
173
+			return fmt.Errorf("cannot build statsd observer: %w", err)
174
+		}
175
+
176
+		c.statsdFactory = &statsdFactory
177
+
178
+		factories = append(factories, statsdFactory.Make)
179
+	}
180
+
181
+	if c.Config.Stats.Prometheus.Enabled {
182
+		prometheus := stats.NewPrometheus(
183
+			c.Config.Stats.Prometheus.MetricPrefix.Value(stats.DefaultMetricPrefix),
184
+			c.Config.Stats.Prometheus.HTTPPath.Value("/"),
185
+		)
186
+
187
+		listener, err := net.Listen("tcp", c.Config.Stats.Prometheus.BindTo.String())
188
+		if err != nil {
189
+			return fmt.Errorf("cannot start a listener for prometheus: %w", err)
190
+		}
191
+
192
+		go prometheus.Serve(listener) // nolint: errcheck
193
+
194
+		c.prometheusListener = listener
195
+		c.prometheus = prometheus
196
+
197
+		factories = append(factories, prometheus.Make)
198
+	}
199
+
200
+	if len(factories) > 0 {
201
+		opts.EventStream = events.NewEventStream(factories)
202
+	}
203
+
204
+	return nil
205
+}

+ 1
- 1
ipblocklist/firehol.go Просмотреть файл

323
 	}
323
 	}
324
 
324
 
325
 	if downloadConcurrency == 0 {
325
 	if downloadConcurrency == 0 {
326
-		downloadConcurrency = 1
326
+		downloadConcurrency = DefaultDownloadConcurrency
327
 	}
327
 	}
328
 
328
 
329
 	workerPool, _ := ants.NewPool(int(downloadConcurrency))
329
 	workerPool, _ := ants.NewPool(int(downloadConcurrency))

+ 8
- 0
ipblocklist/init.go Просмотреть файл

1
+package ipblocklist
2
+
3
+import "time"
4
+
5
+const (
6
+	DefaultDownloadConcurrency = 1
7
+	DefaultUpdateEach          = 12 * time.Hour
8
+)

+ 5
- 0
main.go Просмотреть файл

5
 	"time"
5
 	"time"
6
 
6
 
7
 	"github.com/9seconds/mtg/v2/cli"
7
 	"github.com/9seconds/mtg/v2/cli"
8
+	"github.com/9seconds/mtg/v2/utils"
8
 	"github.com/alecthomas/kong"
9
 	"github.com/alecthomas/kong"
9
 )
10
 )
10
 
11
 
13
 func main() {
14
 func main() {
14
 	rand.Seed(time.Now().UTC().UnixNano())
15
 	rand.Seed(time.Now().UTC().UnixNano())
15
 
16
 
17
+	if err := utils.SetLimits(); err != nil {
18
+		panic(err)
19
+	}
20
+
16
 	cli := &cli.CLI{}
21
 	cli := &cli.CLI{}
17
 	ctx := kong.Parse(cli, kong.Vars{
22
 	ctx := kong.Parse(cli, kong.Vars{
18
 		"version": version,
23
 		"version": version,

+ 3
- 3
mtglib/internal/obfuscated2/client_handshake.go Просмотреть файл

9
 )
9
 )
10
 
10
 
11
 // Connection Type secure. We support only fake tls.
11
 // Connection Type secure. We support only fake tls.
12
-var clientHandshakeMagic = []byte{0xdd, 0xdd, 0xdd, 0xdd}
12
+var clientHandshakeConnectionType = []byte{0xdd, 0xdd, 0xdd, 0xdd}
13
 
13
 
14
 func ClientHandshake(secret []byte, reader io.Reader) (int16, cipher.Stream, cipher.Stream, error) {
14
 func ClientHandshake(secret []byte, reader io.Reader) (int16, cipher.Stream, cipher.Stream, error) {
15
 	handshakeFrame := acquireHandshakeFrame()
15
 	handshakeFrame := acquireHandshakeFrame()
42
 
42
 
43
 	decryptor.XORKeyStream(handshakeFrame.data[:], handshakeFrame.data[:])
43
 	decryptor.XORKeyStream(handshakeFrame.data[:], handshakeFrame.data[:])
44
 
44
 
45
-	if magic := handshakeFrame.magic(); subtle.ConstantTimeCompare(clientHandshakeMagic, magic) != 1 {
46
-		return 0, nil, nil, fmt.Errorf("unsupported connection type: %s", hex.EncodeToString(magic))
45
+	if val := handshakeFrame.connectionType(); subtle.ConstantTimeCompare(clientHandshakeConnectionType, val) != 1 {
46
+		return 0, nil, nil, fmt.Errorf("unsupported connection type: %s", hex.EncodeToString(val))
47
 	}
47
 	}
48
 
48
 
49
 	return handshakeFrame.dc(), encryptor, decryptor, nil
49
 	return handshakeFrame.dc(), encryptor, decryptor, nil

+ 16
- 16
mtglib/internal/obfuscated2/handshake_frame.go Просмотреть файл

5
 const (
5
 const (
6
 	handshakeFrameLen = 64
6
 	handshakeFrameLen = 64
7
 
7
 
8
-	handshakeFrameLenKey   = 32
9
-	handshakeFrameLenIV    = 16
10
-	handshakeFrameLenMagic = 4
11
-	handshakeFrameLenDC    = 2
12
-
13
-	handshakeFrameOffsetStart = 8
14
-	handshakeFrameOffsetKey   = handshakeFrameOffsetStart
15
-	handshakeFrameOffsetIV    = handshakeFrameOffsetKey + handshakeFrameLenKey
16
-	handshakeFrameOffsetMagic = handshakeFrameOffsetIV + handshakeFrameLenIV
17
-	handshakeFrameOffsetDC    = handshakeFrameOffsetMagic + handshakeFrameLenMagic
18
-	handshakeFrameOffsetEnd   = handshakeFrameOffsetDC + handshakeFrameLenDC
8
+	handshakeFrameLenKey            = 32
9
+	handshakeFrameLenIV             = 16
10
+	handshakeFrameLenConnectionType = 4
11
+	handshakeFrameLenDC             = 2
12
+
13
+	handshakeFrameOffsetStart          = 8
14
+	handshakeFrameOffsetKey            = handshakeFrameOffsetStart
15
+	handshakeFrameOffsetIV             = handshakeFrameOffsetKey + handshakeFrameLenKey
16
+	handshakeFrameOffsetConnectionType = handshakeFrameOffsetIV + handshakeFrameLenIV
17
+	handshakeFrameOffsetDC             = handshakeFrameOffsetConnectionType + handshakeFrameLenConnectionType
18
+	handshakeFrameOffsetEnd            = handshakeFrameOffsetDC + handshakeFrameLenDC
19
 )
19
 )
20
 
20
 
21
 // A structure of obfuscated2 handshake frame is following:
21
 // A structure of obfuscated2 handshake frame is following:
25
 //    - 8 bytes of noise
25
 //    - 8 bytes of noise
26
 //    - 32 bytes of AES Key
26
 //    - 32 bytes of AES Key
27
 //    - 16 bytes of AES IV
27
 //    - 16 bytes of AES IV
28
-//    - 4 bytes of 'magic' - this has some settings like a connection type
28
+//    - 4 bytes of 'connection type' - this has some setting like a connection type
29
 //    - 2 bytes of 'DC'. DC is little endian int16
29
 //    - 2 bytes of 'DC'. DC is little endian int16
30
 //    - 2 bytes of noise
30
 //    - 2 bytes of noise
31
 type handshakeFrame struct {
31
 type handshakeFrame struct {
39
 }
39
 }
40
 
40
 
41
 func (h *handshakeFrame) key() []byte {
41
 func (h *handshakeFrame) key() []byte {
42
-	return h.data[handshakeFrameLenKey:handshakeFrameOffsetIV]
42
+	return h.data[handshakeFrameOffsetKey:handshakeFrameOffsetIV]
43
 }
43
 }
44
 
44
 
45
 func (h *handshakeFrame) iv() []byte {
45
 func (h *handshakeFrame) iv() []byte {
46
-	return h.data[handshakeFrameOffsetIV:handshakeFrameOffsetMagic]
46
+	return h.data[handshakeFrameOffsetIV:handshakeFrameOffsetConnectionType]
47
 }
47
 }
48
 
48
 
49
-func (h *handshakeFrame) magic() []byte {
50
-	return h.data[handshakeFrameOffsetMagic:handshakeFrameOffsetDC]
49
+func (h *handshakeFrame) connectionType() []byte {
50
+	return h.data[handshakeFrameOffsetConnectionType:handshakeFrameOffsetDC]
51
 }
51
 }

+ 5
- 0
stats/init.go Просмотреть файл

1
 package stats
1
 package stats
2
 
2
 
3
 const (
3
 const (
4
+	DefaultMetricPrefix = "mtg"
5
+
6
+	DefaultStatsdMetricPrefix = DefaultMetricPrefix + "."
7
+	DefaultStatsdTagFormat    = "datadog"
8
+
4
 	MetricActiveConnection   = "active_connections"
9
 	MetricActiveConnection   = "active_connections"
5
 	MetricSessionDuration    = "session_duration"
10
 	MetricSessionDuration    = "session_duration"
6
 	MetricConcurrencyLimited = "concurrency_limited"
11
 	MetricConcurrencyLimited = "concurrency_limited"

+ 7
- 0
timeattack/init.go Просмотреть файл

1
+package timeattack
2
+
3
+import "time"
4
+
5
+const (
6
+	DefaultDuration = time.Second
7
+)

+ 24
- 0
utils/rlimit.go Просмотреть файл

1
+// +build !windows
2
+
3
+package utils
4
+
5
+import (
6
+	"fmt"
7
+
8
+	"golang.org/x/sys/unix"
9
+)
10
+
11
+func SetLimits() error {
12
+	rLimit := unix.Rlimit{}
13
+	if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rLimit); err != nil {
14
+		return fmt.Errorf("cannot get rlimit: %w", err)
15
+	}
16
+
17
+	rLimit.Cur = rLimit.Max
18
+
19
+	if err := unix.Setrlimit(unix.RLIMIT_NOFILE, &rLimit); err != nil {
20
+		return fmt.Errorf("cannot set rlimit: %w", err)
21
+	}
22
+
23
+	return nil
24
+}

+ 7
- 0
utils/rlimit_windows.go Просмотреть файл

1
+// +build windows
2
+
3
+package utils
4
+
5
+func SetLimits() error {
6
+	return nil
7
+}

+ 25
- 0
utils/root_context.go Просмотреть файл

1
+// +build !windows
2
+
3
+package utils
4
+
5
+import (
6
+	"context"
7
+	"os"
8
+	"os/signal"
9
+	"syscall"
10
+)
11
+
12
+func RootContext() context.Context {
13
+	ctx, cancel := context.WithCancel(context.Background())
14
+	sigChan := make(chan os.Signal, 1)
15
+
16
+	signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
17
+
18
+	go func() {
19
+		for range sigChan {
20
+			cancel()
21
+		}
22
+	}()
23
+
24
+	return ctx
25
+}

+ 23
- 0
utils/root_context_windows.go Просмотреть файл

1
+// +build windows
2
+
3
+package utils
4
+
5
+import (
6
+	"context"
7
+	"os"
8
+	"os/signal"
9
+)
10
+
11
+func RootContext() context.Context {
12
+	ctx, cancel := context.WithCancel(context.Background())
13
+	sigChan := make(chan os.Signal, 1)
14
+
15
+	signal.Notify(sigChan, os.Interrupt)
16
+	go func() {
17
+		for range sigChan {
18
+			cancel()
19
+		}
20
+	}()
21
+
22
+	return ctx
23
+}

Загрузка…
Отмена
Сохранить