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