-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathban_list.go
More file actions
54 lines (42 loc) · 844 Bytes
/
ban_list.go
File metadata and controls
54 lines (42 loc) · 844 Bytes
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
package main
import (
"net"
)
type BanList struct {
banMap map[string]bool
}
func NewBanList() *BanList {
return &BanList{
banMap: make(map[string]bool),
}
}
func (b BanList) IsBanned(addr string) bool {
_, banned := b.GetBannedAddr(addr)
return banned
}
func (b BanList) GetBannedAddr(addr string) (*string, bool) {
host, _, err := net.SplitHostPort(addr)
if err != nil {
host = addr
}
names, err := net.LookupAddr(host)
if err != nil {
names = make([]string, 0)
}
for _, name := range append(names, host) {
banned := b.banMap[name]
if banned {
return &name, true
}
}
return nil, false
}
func (b BanList) AddBan(addr string) {
b.banMap[addr] = true
}
func (b BanList) RemoveBan(addr string) {
resolvedAddr, _ := b.GetBannedAddr(addr)
if resolvedAddr != nil {
b.banMap[*resolvedAddr] = false
}
}