-
Notifications
You must be signed in to change notification settings - Fork 62
/
ip-db-multi.go
52 lines (44 loc) · 1 KB
/
ip-db-multi.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
package rdns
import (
"net"
)
// MultiIPDB wraps multiple blocklist CIDR DBs and performs queries over all of them.
type MultiIPDB struct {
dbs []IPBlocklistDB
}
var _ IPBlocklistDB = MultiIPDB{}
// NewMultiIPDB returns a new instance of a wrapper for blocklists
func NewMultiIPDB(dbs ...IPBlocklistDB) (MultiIPDB, error) {
return MultiIPDB{dbs}, nil
}
func (m MultiIPDB) Reload() (IPBlocklistDB, error) {
var newDBs []IPBlocklistDB
for _, db := range m.dbs {
n, err := db.Reload()
if err != nil {
return MultiIPDB{}, err
}
newDBs = append(newDBs, n)
}
return NewMultiIPDB(newDBs...)
}
func (m MultiIPDB) Match(ip net.IP) (*BlocklistMatch, bool) {
for _, db := range m.dbs {
if match, ok := db.Match(ip); ok {
return match, ok
}
}
return nil, false
}
func (m MultiIPDB) Close() error {
var closeErr error
for _, db := range m.dbs {
if err := db.Close(); closeErr == nil {
closeErr = err
}
}
return closeErr
}
func (m MultiIPDB) String() string {
return "Multi-IP-blocklist"
}