-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplay.go
83 lines (64 loc) · 1.57 KB
/
replay.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
// @author: Brian Wojtczak
// @copyright: 2024 by Brian Wojtczak
// @license: BSD-style license found in the LICENSE file
package altcha
import (
"sort"
"sync"
)
const defaultBanSliceSize = 10
var (
bannedSignatures [][]string
bannedMutex = &sync.RWMutex{}
)
// BanSignature adds the given signature to the list of banned signatures.
func BanSignature(signature string) {
if len(signature) == 0 {
return
}
bannedMutex.Lock()
defer bannedMutex.Unlock()
if len(bannedSignatures) == 0 {
bannedSignatures = make([][]string, 1, 2)
bannedSignatures[0] = []string{}
}
if len(bannedSignatures[0]) == 0 {
bannedSignatures[0] = make([]string, 0, defaultBanSliceSize)
}
bannedSignatures[0] = append(bannedSignatures[0], signature)
sort.Strings(bannedSignatures[0])
return
}
// IsSignatureBanned checks if the given signature is banned.
func IsSignatureBanned(signature string) bool {
if len(signature) == 0 {
return true // empty signature is always banned
}
bannedMutex.RLock()
defer bannedMutex.RUnlock()
for _, list := range bannedSignatures {
for _, entry := range list {
if entry == signature {
return true
}
}
}
return false
}
func rotateBannedSignatureLists() {
bannedMutex.Lock()
defer bannedMutex.Unlock()
if len(bannedSignatures) == 0 {
return
}
if len(bannedSignatures) == 2 && len(bannedSignatures[1]) == 0 && len(bannedSignatures[0]) == 0 {
return
}
bannedSignatures = [][]string{
make([]string, 0, defaultBanSliceSize),
bannedSignatures[0],
}
}
func init() {
AddSecretsRotationCallback(rotateBannedSignatureLists)
}