-
Notifications
You must be signed in to change notification settings - Fork 3
/
bans.go
171 lines (145 loc) · 3.69 KB
/
bans.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
package main
import (
"database/sql"
"sync"
"time"
)
type Bans struct {
users map[Userid]time.Time
userlock sync.RWMutex
ips map[string]time.Time
userips map[Userid][]string
iplock sync.RWMutex // protects both ips/userips
}
var bans = Bans{make(map[Userid]time.Time), sync.RWMutex{}, make(map[string]time.Time), make(map[Userid][]string), sync.RWMutex{}}
func (b *Bans) run() { // TODO in init? probably need to init the structs here from db on start
t := time.NewTicker(time.Minute)
for range t.C {
b.clean()
}
}
func (b *Bans) clean() { // TODO is this used / necessary???
b.userlock.Lock()
defer b.userlock.Unlock()
b.iplock.Lock()
defer b.iplock.Unlock()
for uid, unbantime := range b.users {
if isExpiredUTC(unbantime) {
delete(b.users, uid)
b.userips[uid] = nil
}
}
for ip, unbantime := range b.ips {
if isExpiredUTC(unbantime) {
delete(b.ips, ip)
}
}
}
func (b *Bans) banUser(uid Userid, targetuid Userid, ban *BanIn) {
var expiretime time.Time
if ban.Ispermanent {
expiretime = getFuturetimeUTC()
} else {
expiretime = addDurationUTC(time.Duration(ban.Duration))
}
b.userlock.Lock()
b.users[targetuid] = expiretime
b.userlock.Unlock()
b.log(uid, targetuid, ban, "")
if ban.BanIP {
// ips := getIPCacheForUser(targetuid) //TODO
// if len(ips) == 0 {
ips := hub.getIPsForUserid(targetuid)
if len(ips) == 0 {
D("No ips found for user (offline)", targetuid)
}
//}
b.iplock.Lock()
defer b.iplock.Unlock()
for _, ip := range ips {
b.banIP(targetuid, ip, expiretime, true)
hub.ipbans <- ip
b.log(uid, targetuid, ban, ip)
D("IPBanned user", ban.Nick, targetuid, "with ip:", ip)
}
}
hub.bans <- targetuid
D("Banned user", ban.Nick, targetuid)
}
func (b *Bans) banIP(uid Userid, ip string, t time.Time, skiplock bool) {
if !skiplock { // because the caller holds the locks
b.iplock.Lock()
defer b.iplock.Unlock()
}
b.ips[ip] = t
if _, ok := b.userips[uid]; !ok {
b.userips[uid] = make([]string, 0, 1)
}
b.userips[uid] = append(b.userips[uid], ip)
}
func (b *Bans) unbanUserid(uid Userid) {
b.logUnban(uid)
b.userlock.Lock()
defer b.userlock.Unlock()
b.iplock.Lock()
defer b.iplock.Unlock()
delete(b.users, uid)
for _, ip := range b.userips[uid] {
delete(b.ips, ip)
D("Unbanned IP: ", ip, "for uid:", uid)
}
b.userips[uid] = nil
D("Unbanned uid: ", uid)
}
func isStillBanned(t time.Time, ok bool) bool {
if !ok {
return false
}
return !isExpiredUTC(t)
}
func (b *Bans) isUseridBanned(uid Userid) bool {
if uid == 0 {
return false
}
b.userlock.RLock()
defer b.userlock.RUnlock()
t, ok := b.users[uid]
return isStillBanned(t, ok)
}
func (b *Bans) isIPBanned(ip string) bool {
b.iplock.RLock()
defer b.iplock.RUnlock()
t, ok := b.ips[ip]
return isStillBanned(t, ok)
}
func (b *Bans) loadActive() {
b.userlock.Lock()
defer b.userlock.Unlock()
b.iplock.Lock()
defer b.iplock.Unlock()
// purge all the bans
b.users = make(map[Userid]time.Time)
b.ips = make(map[string]time.Time)
b.userips = make(map[Userid][]string)
db.getBans(func(uid Userid, ipaddress sql.NullString, endtimestamp time.Time) {
if endtimestamp.String() == "" { // TODO check is done before already? clean up...
endtimestamp = getFuturetimeUTC()
}
if ipaddress.Valid {
b.ips[ipaddress.String] = endtimestamp
if _, ok := b.userips[uid]; !ok {
b.userips[uid] = make([]string, 0, 1)
}
b.userips[uid] = append(b.userips[uid], ipaddress.String)
hub.ipbans <- ipaddress.String
} else {
b.users[uid] = endtimestamp
}
})
}
func (b *Bans) log(uid Userid, targetuid Userid, ban *BanIn, ip string) {
db.insertBan(uid, targetuid, ban, ip)
}
func (b *Bans) logUnban(targetuid Userid) {
db.deleteBan(targetuid)
}