|
|
@@ -12,11 +12,13 @@ import (
|
|
12
|
12
|
"github.com/9seconds/mtg/v2/internal/config"
|
|
13
|
13
|
"github.com/9seconds/mtg/v2/internal/utils"
|
|
14
|
14
|
"github.com/9seconds/mtg/v2/ipblocklist"
|
|
|
15
|
+ "github.com/9seconds/mtg/v2/ipblocklist/files"
|
|
15
|
16
|
"github.com/9seconds/mtg/v2/logger"
|
|
16
|
17
|
"github.com/9seconds/mtg/v2/mtglib"
|
|
17
|
18
|
"github.com/9seconds/mtg/v2/network"
|
|
18
|
19
|
"github.com/9seconds/mtg/v2/stats"
|
|
19
|
20
|
"github.com/rs/zerolog"
|
|
|
21
|
+ "github.com/yl2chen/cidranger"
|
|
20
|
22
|
)
|
|
21
|
23
|
|
|
22
|
24
|
func makeLogger(conf *config.Config) mtglib.Logger {
|
|
|
@@ -106,7 +108,7 @@ func makeIPBlocklist(conf config.ListConfig,
|
|
106
|
108
|
}
|
|
107
|
109
|
}
|
|
108
|
110
|
|
|
109
|
|
- firehol, err := ipblocklist.NewFirehol(logger.Named("ipblockist"),
|
|
|
111
|
+ blocklist, err := ipblocklist.NewFirehol(logger.Named("ipblockist"),
|
|
110
|
112
|
ntw,
|
|
111
|
113
|
conf.DownloadConcurrency.Get(1),
|
|
112
|
114
|
remoteURLs,
|
|
|
@@ -116,9 +118,44 @@ func makeIPBlocklist(conf config.ListConfig,
|
|
116
|
118
|
return nil, fmt.Errorf("incorrect parameters for firehol: %w", err)
|
|
117
|
119
|
}
|
|
118
|
120
|
|
|
119
|
|
- go firehol.Run(conf.UpdateEach.Get(ipblocklist.DefaultFireholUpdateEach))
|
|
|
121
|
+ go blocklist.Run(conf.UpdateEach.Get(ipblocklist.DefaultFireholUpdateEach))
|
|
120
|
122
|
|
|
121
|
|
- return firehol, nil
|
|
|
123
|
+ return blocklist, nil
|
|
|
124
|
+}
|
|
|
125
|
+
|
|
|
126
|
+func makeIPAllowlist(conf config.ListConfig,
|
|
|
127
|
+ logger mtglib.Logger,
|
|
|
128
|
+ ntw mtglib.Network,
|
|
|
129
|
+ updateCallback ipblocklist.FireholUpdateCallback,
|
|
|
130
|
+) (allowlist mtglib.IPBlocklist, err error) {
|
|
|
131
|
+ if !conf.Enabled.Get(false) {
|
|
|
132
|
+ allowlist, err = ipblocklist.NewFireholFromFiles(
|
|
|
133
|
+ logger.Named("ipblocklist"),
|
|
|
134
|
+ 1,
|
|
|
135
|
+ []files.File{
|
|
|
136
|
+ files.NewMem([]*net.IPNet{
|
|
|
137
|
+ cidranger.AllIPv4,
|
|
|
138
|
+ cidranger.AllIPv6,
|
|
|
139
|
+ }),
|
|
|
140
|
+ },
|
|
|
141
|
+ updateCallback,
|
|
|
142
|
+ )
|
|
|
143
|
+
|
|
|
144
|
+ go allowlist.Run(conf.UpdateEach.Get(ipblocklist.DefaultFireholUpdateEach))
|
|
|
145
|
+ } else {
|
|
|
146
|
+ allowlist, err = makeIPBlocklist(
|
|
|
147
|
+ conf,
|
|
|
148
|
+ logger,
|
|
|
149
|
+ ntw,
|
|
|
150
|
+ updateCallback,
|
|
|
151
|
+ )
|
|
|
152
|
+ }
|
|
|
153
|
+
|
|
|
154
|
+ if err != nil {
|
|
|
155
|
+ return nil, fmt.Errorf("cannot build allowlist: %w", err)
|
|
|
156
|
+ }
|
|
|
157
|
+
|
|
|
158
|
+ return allowlist, nil
|
|
122
|
159
|
}
|
|
123
|
160
|
|
|
124
|
161
|
func makeEventStream(conf *config.Config, logger mtglib.Logger) (mtglib.EventStream, error) {
|
|
|
@@ -186,21 +223,16 @@ func runProxy(conf *config.Config, version string) error { // nolint: funlen
|
|
186
|
223
|
return fmt.Errorf("cannot build ip blocklist: %w", err)
|
|
187
|
224
|
}
|
|
188
|
225
|
|
|
189
|
|
- var whitelist mtglib.IPBlocklist
|
|
190
|
|
-
|
|
191
|
|
- if conf.Defense.Allowlist.Enabled.Get(false) {
|
|
192
|
|
- whlist, err := makeIPBlocklist(
|
|
193
|
|
- conf.Defense.Allowlist,
|
|
194
|
|
- logger.Named("allowlist"),
|
|
195
|
|
- ntw,
|
|
196
|
|
- func(ctx context.Context, size int) {
|
|
197
|
|
- eventStream.Send(ctx, mtglib.NewEventIPListSize(size, false))
|
|
198
|
|
- })
|
|
199
|
|
- if err != nil {
|
|
200
|
|
- return fmt.Errorf("cannot build ip allowlist: %w", err)
|
|
201
|
|
- }
|
|
202
|
|
-
|
|
203
|
|
- whitelist = whlist
|
|
|
226
|
+ allowlist, err := makeIPAllowlist(
|
|
|
227
|
+ conf.Defense.Allowlist,
|
|
|
228
|
+ logger.Named("allowlist"),
|
|
|
229
|
+ ntw,
|
|
|
230
|
+ func(ctx context.Context, size int) {
|
|
|
231
|
+ eventStream.Send(ctx, mtglib.NewEventIPListSize(size, false))
|
|
|
232
|
+ },
|
|
|
233
|
+ )
|
|
|
234
|
+ if err != nil {
|
|
|
235
|
+ return fmt.Errorf("cannot build ip allowlist: %w", err)
|
|
204
|
236
|
}
|
|
205
|
237
|
|
|
206
|
238
|
opts := mtglib.ProxyOpts{
|
|
|
@@ -208,7 +240,7 @@ func runProxy(conf *config.Config, version string) error { // nolint: funlen
|
|
208
|
240
|
Network: ntw,
|
|
209
|
241
|
AntiReplayCache: makeAntiReplayCache(conf),
|
|
210
|
242
|
IPBlocklist: blocklist,
|
|
211
|
|
- IPWhitelist: whitelist,
|
|
|
243
|
+ IPAllowlist: allowlist,
|
|
212
|
244
|
EventStream: eventStream,
|
|
213
|
245
|
|
|
214
|
246
|
Secret: conf.Secret,
|