-
-
Notifications
You must be signed in to change notification settings - Fork 743
/
whitelist.go
202 lines (155 loc) · 3.49 KB
/
whitelist.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
package dlna
import (
"sync"
"time"
"github.com/stashapp/stash/pkg/sliceutil"
)
// only keep the 10 most recent IP addresses
const recentListLength = 10
const wildcard = "*"
type tempIPWhitelist struct {
pattern string
until *time.Time
}
type ipWhitelistManager struct {
recentIPAddresses []string
config Config
tempWhitelist []tempIPWhitelist
mutex sync.Mutex
}
// addRecent adds the provided address to the recent IP addresses list if it
// was not already present. Returns true if it was already present.
func (m *ipWhitelistManager) addRecent(addr string) bool {
m.mutex.Lock()
defer m.mutex.Unlock()
i := sliceutil.Index(m.recentIPAddresses, addr)
if i != -1 {
if i == 0 {
// don't do anything if it's already at the start
return true
}
// remove from the list
m.recentIPAddresses = append(m.recentIPAddresses[:i], m.recentIPAddresses[i+1:]...)
}
// add to the top of the list
m.recentIPAddresses = append([]string{addr}, m.recentIPAddresses...)
if len(m.recentIPAddresses) > recentListLength {
m.recentIPAddresses = m.recentIPAddresses[:recentListLength]
}
return i != -1
}
func (m *ipWhitelistManager) getRecent() []string {
m.mutex.Lock()
defer m.mutex.Unlock()
return m.recentIPAddresses
}
func (m *ipWhitelistManager) getTempAllowed() []*Dlnaip {
m.mutex.Lock()
defer m.mutex.Unlock()
var ret []*Dlnaip
now := time.Now()
removeExpired := false
for _, a := range m.tempWhitelist {
if a.until != nil && now.After(*a.until) {
removeExpired = true
continue
}
ret = append(ret, &Dlnaip{
IPAddress: a.pattern,
Until: a.until,
})
}
if removeExpired {
m.removeExpiredWhitelists()
}
return ret
}
func (m *ipWhitelistManager) ipAllowed(addr string) bool {
m.mutex.Lock()
defer m.mutex.Unlock()
for _, a := range m.config.GetDLNADefaultIPWhitelist() {
if a == wildcard {
return true
}
if addr == a {
return true
}
}
now := time.Now()
removeExpired := false
for _, a := range m.tempWhitelist {
if a.until != nil && now.After(*a.until) {
removeExpired = true
continue
}
if a.pattern == wildcard {
return true
}
if addr == a.pattern {
return true
}
}
if removeExpired {
m.removeExpiredWhitelists()
}
return false
}
func (m *ipWhitelistManager) removeExpiredWhitelists() {
// assumes mutex is already held
var newList []tempIPWhitelist
now := time.Now()
for _, a := range m.tempWhitelist {
if a.until != nil && now.After(*a.until) {
continue
}
newList = append(newList, a)
}
m.tempWhitelist = newList
}
func (m *ipWhitelistManager) allowPattern(pattern string, duration *time.Duration) {
if pattern == "" {
return
}
m.mutex.Lock()
defer m.mutex.Unlock()
// overwrite existing
var newList []tempIPWhitelist
found := false
var until *time.Time
if duration != nil {
u := time.Now().Add(*duration)
until = &u
}
for _, a := range m.tempWhitelist {
if a.pattern == pattern {
a.until = until
found = true
}
newList = append(newList, a)
}
if !found {
newList = append(newList, tempIPWhitelist{
pattern: pattern,
until: until,
})
}
m.tempWhitelist = newList
}
func (m *ipWhitelistManager) removePattern(pattern string) bool {
if pattern == "" {
return false
}
m.mutex.Lock()
defer m.mutex.Unlock()
var newList []tempIPWhitelist
found := false
for _, a := range m.tempWhitelist {
if a.pattern == pattern {
found = true
continue
}
newList = append(newList, a)
}
m.tempWhitelist = newList
return found
}