/
antiflood.go
182 lines (145 loc) · 5.04 KB
/
antiflood.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
package manager
import (
"crypto/sha512"
"fmt"
"log"
"net/url"
"strings"
"time"
cache "github.com/patrickmn/go-cache"
irc "github.com/thoj/go-ircevent"
"golang.org/x/net/idna"
"github.com/icedream/irc-medialink/util/clone"
)
func (m *Manager) initAntiflood() {
m.cache = cache.New(1*time.Minute, 5*time.Second)
}
func (m *Manager) TrackUser(target string, source string) (shouldIgnore bool) {
key := normalizeUserAntiflood(target, source)
if _, ok := m.cache.Get(key); ok {
// User just joined here recently, ignore them
shouldIgnore = true
}
return
}
func (m *Manager) NotifyUserJoined(target string, source string) error {
key := normalizeUserAntiflood(target, source)
// When a user joins, he will be ignored for the first 30 seconds,
// enough to prevent parsing links from people who only join to spam their
// links immediately
if _, exists := m.cache.Get(key); !exists {
return m.cache.Add(key, nil, 30*time.Second)
}
return nil
}
func (m *Manager) TrackUrl(target string, u *url.URL) (shouldIgnore bool, err error) {
key := normalizeUrlAntiflood(target, u)
if _, ok := m.cache.Get(key); ok {
// The URL has been used recently, should ignore
shouldIgnore = true
} else {
err = m.cache.Add(key, nil, cache.DefaultExpiration)
}
return
}
func (m *Manager) TrackOutput(target, t string) (shouldNotSend bool, err error) {
key := normalizeTextAntiflood(target, t)
if _, ok := m.cache.Get(key); ok {
// The URL has been used recently, should ignore
shouldNotSend = true
} else {
err = m.cache.Add(key, nil, cache.DefaultExpiration)
}
return
}
func (m *Manager) AntifloodIrcConn(c *irc.Connection) *ircConnectionProxy {
return &ircConnectionProxy{Connection: c, m: m}
}
func normalizeUrlAntiflood(target string, u *url.URL) string {
uc := clone.CloneURL(u)
// Normalize hostname punycode.
//
// Ignoring the error is correct here since we still want to work with a
// partially converted hostname. See idna docs.
uc.Host, _ = idna.Punycode.ToASCII(uc.Host)
// Normalize hostname casing
uc.Host = strings.ToLower(uc.Host)
// Normalize query
uc.RawQuery = uc.Query().Encode()
// Normalize scheme
uc.Scheme = strings.ToLower(uc.Scheme)
// Fill in default ports if none were passed in this URL
if len(uc.Port()) == 0 {
uc.Host = strings.TrimSuffix(uc.Host, ":")
switch uc.Scheme {
case "http":
uc.Host += ":80"
case "https":
uc.Host += ":443"
}
}
s := sha512.New()
s.Write([]byte(uc.String()))
return fmt.Sprintf("LINK/%s/%X", strings.ToUpper(target), s.Sum([]byte{}))
}
func normalizeTextAntiflood(target, text string) string {
s := sha512.New()
s.Write([]byte(text))
return fmt.Sprintf("TEXT/%s/%X", strings.ToUpper(target), s.Sum([]byte{}))
}
func normalizeUserAntiflood(target, source string) string {
sourceSplitHost := strings.SplitN(source, "@", 2)
if len(sourceSplitHost) > 1 {
sourceSplitHostname := strings.Split(sourceSplitHost[1], ".")
if len(sourceSplitHostname) > 1 &&
strings.EqualFold(sourceSplitHostname[len(sourceSplitHostname)-1], "IP") {
sourceSplitHostname[0] = "*"
}
source = fmt.Sprintf("%s!%s@%s", "*", "*", strings.Join(sourceSplitHostname, "."))
}
return fmt.Sprintf("USER/%s/%s", strings.ToUpper(target), source)
}
// Proxies several methods of the IRC connection in order to drop repeated messages
type ircConnectionProxy struct {
*irc.Connection
m *Manager
}
func (proxy *ircConnectionProxy) Action(target, message string) {
if shouldNotSend, err := proxy.m.TrackOutput(target, message); err != nil {
log.Printf("WARNING: Output antiflood returned an error, dropping message for %s: %s", target, err.Error())
return
} else if shouldNotSend {
log.Printf("WARNING: Output antiflood triggered, dropping message for %s: %s", target, message)
return
}
proxy.Connection.Action(target, message)
}
func (proxy *ircConnectionProxy) Actionf(target, format string, a ...interface{}) {
proxy.Action(target, fmt.Sprintf(format, a...))
}
func (proxy *ircConnectionProxy) Privmsg(target, message string) {
if shouldNotSend, err := proxy.m.TrackOutput(target, message); err != nil {
log.Printf("WARNING: Output antiflood returned an error, dropping message for %s: %s", target, err.Error())
return
} else if shouldNotSend {
log.Printf("WARNING: Output antiflood triggered, dropping message for %s: %s", target, message)
return
}
proxy.Connection.Privmsg(target, message)
}
func (proxy *ircConnectionProxy) Privmsgf(target, format string, a ...interface{}) {
proxy.Privmsg(target, fmt.Sprintf(format, a...))
}
func (proxy *ircConnectionProxy) Notice(target, message string) {
if shouldNotSend, err := proxy.m.TrackOutput(target, message); err != nil {
log.Printf("WARNING: Output antiflood returned an error, dropping message for %s: %s", target, err.Error())
return
} else if shouldNotSend {
log.Printf("WARNING: Output antiflood triggered, dropping message for %s: %s", target, message)
return
}
proxy.Connection.Notice(target, message)
}
func (proxy *ircConnectionProxy) Noticef(target, format string, a ...interface{}) {
proxy.Notice(target, fmt.Sprintf(format, a...))
}