-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathasn-db.go
96 lines (82 loc) · 2.2 KB
/
asn-db.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
package rdns
import (
"fmt"
"net"
"strconv"
"strings"
"github.com/oschwald/maxminddb-golang"
)
// ASNDB holds blocklist rules based on ASN location. When an IP is queried,
// its ASN is looked up in a database and the result is compared to the
// blocklist rules.
type ASNDB struct {
name string
loader BlocklistLoader
geoDB *maxminddb.Reader
geoDBFile string
db map[uint64]struct{}
}
var _ IPBlocklistDB = &ASNDB{}
// NewASN returns a new instance of a matcher for a ASN rules.
func NewASNDB(name string, loader BlocklistLoader, geoDBFile string) (*ASNDB, error) {
if geoDBFile == "" {
geoDBFile = "/usr/share/GeoIP/GeoLite2-ASN.mmdb"
}
geoDB, err := maxminddb.Open(geoDBFile)
if err != nil {
return nil, fmt.Errorf("failed to open geo asn database file: %w", err)
}
rules, err := loader.Load()
if err != nil {
return nil, err
}
db := make(map[uint64]struct{})
for _, r := range rules {
r = strings.TrimSpace(r)
if strings.HasPrefix(r, "#") || r == "" {
continue
}
r = strings.Split(r, "#")[0] // possible comment at the end of the line
r = strings.TrimSpace(r)
value, err := strconv.ParseUint(r, 10, 64) // GeoNames ID
if err != nil {
return nil, fmt.Errorf("unable to parse asn id in rule '%s': %w", r, err)
}
db[value] = struct{}{}
}
return &ASNDB{
name: name,
geoDB: geoDB,
geoDBFile: geoDBFile,
db: db,
loader: loader,
}, nil
}
func (m *ASNDB) Reload() (IPBlocklistDB, error) {
return NewASNDB(m.name, m.loader, m.geoDBFile)
}
func (m *ASNDB) Match(ip net.IP) (*BlocklistMatch, bool) {
var record struct {
ASN uint64 `maxminddb:"autonomous_system_number"`
Organization string `maxminddb:"autonomous_system_organization"`
}
if err := m.geoDB.Lookup(ip, &record); err != nil {
Log.Error("failed to lookup ip in geo location database", "ip", ip, "error", err)
return nil, false
}
fmt.Println(record)
// Check if the ASN is on the blocklist
if _, ok := m.db[record.ASN]; ok {
return &BlocklistMatch{
List: m.name,
Rule: fmt.Sprintf("%d", record.ASN),
}, true
}
return nil, false
}
func (m *ASNDB) Close() error {
return m.geoDB.Close()
}
func (m *ASNDB) String() string {
return "ASN-blocklist"
}