-
Notifications
You must be signed in to change notification settings - Fork 0
/
tgratelimiter.go
562 lines (492 loc) · 13.4 KB
/
tgratelimiter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// Package traefik_telegram_ratelimiter implements a rate limiting middleware based on messages' telegram ids
package traefik_telegram_ratelimiter
import (
"bufio"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
)
const defaultHitTableSize = 50000
const defaultExpire = 86400 // 24 hours
const isDeletedID int64 = -1 << 63
var (
ErrUnknownMessageFormat = errors.New("unknown incoming telegram message format")
ErrEmptyMessage = errors.New("empty message")
ErrInvalidHitTableSize = errors.New("hit table size cannot be 0 or less")
)
var (
loggerInfo = log.New(os.Stdout, "INFO: TelegramRateLimiterPlugin: ", log.Ldate|log.Ltime)
loggerError = log.New(os.Stderr, "ERROR: TelegramRateLimiterPlugin: ", log.Ldate|log.Ltime)
)
// Config holds configuration to pass to the plugin
type Config struct {
// HitTableSize defined the max size of the hit table
HitTableSize int `json:"hitTableSize,omitempty" yaml:"hitTableSize,omitempty" toml:"hitTableSize,omitempty"`
// Limit defines the hit limit for regular account ids. -1 defines infinite limit.
Limit int32 `json:"limit,omitempty" yaml:"limit,omitempty" toml:"limit,omitempty"`
// WhitelistLimit defines hit limit for whitelisted account ids. -1 defines infinite limit.
WhitelistLimit int32 `json:"whitelistLimit,omitempty" yaml:"whitelistLimit,omitempty" toml:"whitelistLimit,omitempty"`
// Expire is a number in seconds to keep the hit record for a single id
Expire int64 `json:"expire,omitempty" yaml:"expire,omitempty" toml:"expire,omitempty"`
// Whitelist is a path to the file with whitelisted ids. Each id on separate line
Whitelist *string `json:"whitelist,omitempty" yaml:"whitelist,omitempty" toml:"whitelist,omitempty"`
// Whitelist URL to load the list with whitelisted ids from. The same format as for the file
WhitelistURL *string `json:"whitelistURL,omitempty" yaml:"whitelistURL,omitempty" toml:"whitelistURL,omitempty"`
// Blacklist is a path to the file with blacklisted ids.
// IDs from blacklist are mutely ignored without counting hits
Blacklist *string `json:"blacklist,omitempty" yaml:"blacklist,omitempty" toml:"blacklist,omitempty"`
// BlacklistURL
BlacklistURL *string `json:"blacklistURL,omitempty" yaml:"blacklistURL,omitempty" toml:"blacklistURL,omitempty"`
// enable http management server
Console bool `json:"console" yaml:"console" toml:"console"`
// management server address
ConsoleAddress *string `json:"consoleAddress" yaml:"consoleAddress" toml:"consoleAddress"`
}
// CreateConfig populates the Config data object
func CreateConfig() *Config {
return &Config{
HitTableSize: defaultHitTableSize,
Limit: -1,
WhitelistLimit: -1,
Expire: defaultExpire,
}
}
// rateLimiter implements rate limiting with a set of tocken buckets;
type rateLimiter struct {
next http.Handler
config *Config
rwmu sync.RWMutex
name string
// expire time in seconds of the hit record
expire int64
// maximum hits limit
limit int32
// whitelist limit
wlLimit int32
// whitelisted telegram ids
whitelist map[int64]struct{}
// blacklisted telegram ids
blacklist map[int64]struct{}
// hits map
hits *expiryMap
}
// New instantiates and returns the required components used to handle a HTTP request
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if config.HitTableSize <= 0 {
return nil, ErrInvalidHitTableSize
}
r := &rateLimiter{
next: next,
config: config,
name: name,
expire: config.Expire,
limit: config.Limit,
wlLimit: config.WhitelistLimit,
hits: newExpiryMap(config.HitTableSize),
}
r.updateLists()
if config.Console {
err := r.startManagement(*config.ConsoleAddress)
if err != nil {
loggerError.Printf("failed to start management server: %s", err.Error())
return nil, err
}
}
return r, nil
}
func (r *rateLimiter) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
var buf bytes.Buffer
tee := io.TeeReader(req.Body, &buf)
tgID, err := extractTgID(tee)
req.Body = io.NopCloser(&buf)
// skip rate limiting if failed to retrieve tg ID
if err != nil {
loggerError.Printf("error retrieving telegram id: %v", err)
if err == ErrEmptyMessage {
silentReject(rw)
} else {
r.next.ServeHTTP(rw, req)
}
return
}
if r.rejectedTgID(tgID) {
silentReject(rw)
return
}
r.next.ServeHTTP(rw, req)
}
func (r *rateLimiter) rejectedTgID(tgID int64) bool {
r.rwmu.RLock()
defer r.rwmu.RUnlock()
// if id is blacklisted skip handling and return 200 OK
if _, ok := r.blacklist[tgID]; ok {
loggerInfo.Printf("rejecting blacklisted id: %d", tgID)
return true
}
_, isWl := r.whitelist[tgID]
hits := r.hits.incNGet(tgID, r.expire)
// if is whitelisted tg id check wlLimit
if isWl {
if r.wlLimit >= 0 && hits > r.wlLimit {
loggerInfo.Printf("rejecting whitelisted id: %d, limit: %d, hits: %d", tgID, r.wlLimit, hits)
return true
}
} else if r.limit >= 0 && hits > r.limit {
loggerInfo.Printf("rejecting regular id: %d, limit: %d, hits: %d", tgID, r.limit, hits)
return true
}
return false
}
func (r *rateLimiter) updateLists() error {
wl := make(map[int64]struct{}, 1024)
bl := make(map[int64]struct{}, 1024)
if r.config.Whitelist != nil {
err := readIDFile(*r.config.Whitelist, wl)
if err != nil {
return err
}
}
if r.config.WhitelistURL != nil {
err := readIDURL(*r.config.WhitelistURL, wl)
if err != nil {
return err
}
}
if r.config.Blacklist != nil {
err := readIDFile(*r.config.Blacklist, bl)
if err != nil {
return err
}
}
if r.config.BlacklistURL != nil {
err := readIDURL(*r.config.BlacklistURL, bl)
if err != nil {
return err
}
}
loggerInfo.Printf("updating lists. wl recs: %d, bl recs: %d", len(wl), len(bl))
r.rwmu.Lock()
defer r.rwmu.Unlock()
r.whitelist = wl
r.blacklist = bl
return nil
}
func silentReject(rw http.ResponseWriter) {
rw.Header().Add("Content-Type", "text/plain")
rw.Header().Add("Connection", "keep-alive")
rw.Write([]byte(http.StatusText(http.StatusOK)))
}
type tgMsg struct {
Message struct {
From struct {
ID *int64 `json:"id"`
} `json:"from"`
} `json:"message"`
CBQuery struct {
From struct {
ID *int64 `json:"id"`
} `json:"from"`
} `json:"callback_query"`
}
func extractTgID(r io.Reader) (int64, error) {
body, err := io.ReadAll(r)
if err != nil {
return 0, err
}
if len(body) == 0 {
return 0, ErrEmptyMessage
}
var tgMsg tgMsg
err = json.Unmarshal(body, &tgMsg)
if err != nil {
return 0, err
}
if tgMsg.Message.From.ID != nil {
return *tgMsg.Message.From.ID, nil
} else if tgMsg.CBQuery.From.ID != nil {
return *tgMsg.CBQuery.From.ID, nil
}
return 0, ErrUnknownMessageFormat
}
func readIDFile(fp string, m map[int64]struct{}) error {
abs, err := filepath.Abs(fp)
if err != nil {
return err
}
file, err := os.Open(abs)
if err != nil {
return err
}
defer file.Close()
scanIDs(file, m)
return nil
}
func readIDURL(url string, m map[int64]struct{}) error {
res, err := http.Get(url)
if err != nil {
return err
}
defer res.Body.Close()
scanIDs(res.Body, m)
return nil
}
func scanIDs(r io.Reader, m map[int64]struct{}) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
if id, err := strconv.ParseInt(line, 10, 64); err == nil {
m[id] = struct{}{}
}
}
}
type expiryHits struct {
id int64
hits int32
expires int64
}
type expiryMap struct {
mu sync.Mutex
// max hit table cap
cap int
// map telegram id to the index in the `hits` slice
idxs map[int64]int
// circular queue keeping records about hits and expiration times
hits []expiryHits
// starting index and the size of the `hits` circular array
head, size int
}
func newExpiryMap(capacity int) *expiryMap {
return &expiryMap{
mu: sync.Mutex{},
cap: capacity,
idxs: make(map[int64]int, capacity),
hits: make([]expiryHits, capacity),
head: 0,
size: 0,
}
}
// incNGet increments and returns number of hits of the specified telegram id
func (e *expiryMap) incNGet(id int64, expire int64) int32 {
e.mu.Lock()
defer e.mu.Unlock()
idx, ok := e.idxs[id]
// when the record does not exist
if !ok {
e.insert(expiryHits{id, 1, time.Now().UTC().Unix() + expire})
return 1
}
// when the record exists but has expired
if e.hits[idx].expires < time.Now().UTC().Unix() {
// delete the id mapping and mark the queue record id
// as deleted so when the head of the queue gets to
// the record it will not remove the newly created mapping
delete(e.idxs, id)
e.hits[idx].id = isDeletedID
e.insert(expiryHits{id, 1, time.Now().UTC().Unix() + expire})
return 1
}
e.hits[idx].hits++
return e.hits[idx].hits
}
// get returns numbers of hits of the specified telegram id
func (e *expiryMap) get(id int64) int32 {
e.mu.Lock()
defer e.mu.Unlock()
idx, ok := e.idxs[id]
if !ok {
return 0
}
if e.hits[idx].expires < time.Now().UTC().Unix() {
return 0
}
return e.hits[idx].hits
}
// reset resets hit counter for the specified telegram id
// returns wether the id was found in the map
func (e *expiryMap) reset(id int64) bool {
e.mu.Lock()
defer e.mu.Unlock()
idx, ok := e.idxs[id]
if ok {
e.hits[idx].hits = 0
}
return ok
}
// full return wether the circular queue is full
func (e *expiryMap) full() bool {
return e.size == e.cap
}
// free removes one item from the start of the circular queue
// and the corresponding id mapping
func (e *expiryMap) free(count int) {
for i := 0; i < count; i++ {
if e.size == 0 {
break
}
id := e.hits[e.head].id
// isDeletedID means the id mapping has been deleted already
if id != isDeletedID {
delete(e.idxs, id)
e.hits[e.head].id = isDeletedID
}
e.head = (e.head + 1) % e.cap
e.size--
}
}
// insert inserts one item into the circular queue
// and inserts corresponding id mapping
func (e *expiryMap) insert(h expiryHits) {
if e.full() {
e.free(1)
}
idx := (e.head + e.size) % e.cap
e.idxs[h.id] = idx
e.hits[idx] = h
e.size++
}
// list returns all recorded hits
func (e *expiryMap) list() map[int64]int32 {
e.mu.Lock()
defer e.mu.Unlock()
m := make(map[int64]int32, e.size)
for i := 0; i < e.size; i++ {
j := (e.head + i) % e.cap
m[e.hits[j].id] = e.hits[j].hits
}
return m
}
func (r *rateLimiter) startManagement(addr string) error {
l, err := net.Listen("tcp", addr)
if err != nil {
return err
}
go func() {
mux := http.NewServeMux()
mux.HandleFunc("/", r.serveManagement)
err = http.Serve(l, mux)
loggerError.Printf("management server finished. error: %s", err.Error())
}()
loggerInfo.Printf("management server is running on: %s", l.Addr().String())
return nil
}
func (r *rateLimiter) serveManagement(res http.ResponseWriter, req *http.Request) {
p := strings.Split(req.URL.Path, "/")[1:]
n := len(p)
switch {
case n == 1 && p[0] == "reload" && req.Method == http.MethodPost:
r.updateLists()
res.WriteHeader(http.StatusNoContent)
case n == 1 && p[0] == "hits" && req.Method == http.MethodGet:
var data bytes.Buffer
for k, v := range r.hits.list() {
data.WriteString(fmt.Sprintf("%d %d\n", k, v))
}
res.Write(data.Bytes())
case n == 2 && p[0] == "hits":
id, err := parseTgID(p[1])
if err != nil {
http.Error(res, "400 bad request", http.StatusBadRequest)
return
}
switch req.Method {
case http.MethodGet: // show hits
hits := r.hits.get(id)
res.Write([]byte(strconv.Itoa(int(hits))))
case http.MethodDelete: // reset hits
r.hits.reset(id)
res.WriteHeader(http.StatusNoContent)
}
case n == 3 && p[0] == "list":
id, err := parseTgID(p[2])
if err != nil {
http.Error(res, "400 bad request", http.StatusBadRequest)
return
}
var m map[int64]struct{}
switch p[1] {
case "bl":
m = r.blacklist
case "wl":
m = r.whitelist
default:
http.Error(res, "400 bad request", http.StatusBadRequest)
return
}
switch req.Method {
case http.MethodGet: // check id presence
var result string
_, ok := m[id]
if ok {
result = "true"
} else {
result = "false"
}
res.Write([]byte(result + "\n"))
case http.MethodDelete: // remove from list
delete(m, id)
res.WriteHeader(http.StatusNoContent)
case http.MethodPut: // add to list
m[id] = struct{}{}
res.WriteHeader(http.StatusCreated)
default:
res.Header().Add("Allow", "GET, PUT, DELETE")
http.Error(res, "405 method not allowed", http.StatusMethodNotAllowed)
}
case n == 1 && (p[0] == "limit" || p[0] == "wllimit"):
switch req.Method {
case http.MethodPut:
body, err := io.ReadAll(req.Body)
if err != nil {
http.Error(res, "500 internal server error", http.StatusInternalServerError)
return
}
limit, err := strconv.ParseInt(string(body), 10, 32)
if err != nil {
http.Error(res, "400 bad request", http.StatusBadRequest)
return
}
r.rwmu.Lock()
defer r.rwmu.Unlock()
if p[0] == "limit" {
r.limit = int32(limit)
} else {
r.wlLimit = int32(limit)
}
res.WriteHeader(http.StatusNoContent)
case http.MethodGet:
r.rwmu.RLock()
defer r.rwmu.RUnlock()
var result int32
if p[0] == "limit" {
result = r.limit
} else {
result = r.wlLimit
}
res.Write([]byte(fmt.Sprintf("%d", result)))
default:
res.Header().Add("Allow", "GET, PUT")
http.Error(res, "405 method not allowed", http.StatusMethodNotAllowed)
}
default:
http.NotFound(res, req)
}
}
func parseTgID(s string) (int64, error) {
id, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse telegram id: %s", s)
}
return id, nil
}