-
Notifications
You must be signed in to change notification settings - Fork 62
/
geoip-db.go
110 lines (97 loc) · 2.69 KB
/
geoip-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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package rdns
import (
"fmt"
"net"
"strconv"
"strings"
"github.com/oschwald/maxminddb-golang"
)
// GeoIPDB holds blocklist rules based on location. When an IP is queried,
// its location is looked up in a database and the result is compared to the
// blocklist rules.
type GeoIPDB struct {
name string
loader BlocklistLoader
geoDB *maxminddb.Reader
geoDBFile string
db map[uint64]struct{}
}
var _ IPBlocklistDB = &GeoIPDB{}
// NewGeoIPDB returns a new instance of a matcher for a location rules.
func NewGeoIPDB(name string, loader BlocklistLoader, geoDBFile string) (*GeoIPDB, error) {
if geoDBFile == "" {
geoDBFile = "/usr/share/GeoIP/GeoLite2-City.mmdb"
}
geoDB, err := maxminddb.Open(geoDBFile)
if err != nil {
return nil, fmt.Errorf("failed to open geo location 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 geoname id in rule '%s': %w", r, err)
}
db[value] = struct{}{}
}
return &GeoIPDB{
name: name,
geoDB: geoDB,
geoDBFile: geoDBFile,
db: db,
loader: loader,
}, nil
}
func (m *GeoIPDB) Reload() (IPBlocklistDB, error) {
return NewGeoIPDB(m.name, m.loader, m.geoDBFile)
}
func (m *GeoIPDB) Match(ip net.IP) (*BlocklistMatch, bool) {
var record struct {
Continent struct {
GeoNameID uint64 `maxminddb:"geoname_id"`
} `maxminddb:"continent"`
Country struct {
GeoNameID uint64 `maxminddb:"geoname_id"`
} `maxminddb:"country"`
City struct {
GeoNameID uint64 `maxminddb:"geoname_id"`
} `maxminddb:"city"`
Subdivisions []struct {
GeoNameID uint64 `maxminddb:"geoname_id"`
} `maxminddb:"subdivisions"`
}
if err := m.geoDB.Lookup(ip, &record); err != nil {
Log.WithField("ip", ip).WithError(err).Error("failed to lookup ip in geo location database")
return nil, false
}
// Try to find the continent, country, or city GeoName ID in the blocklist
ids := []uint64{record.Continent.GeoNameID, record.Country.GeoNameID, record.City.GeoNameID}
for _, sd := range record.Subdivisions {
ids = append(ids, sd.GeoNameID)
}
for _, id := range ids {
if _, ok := m.db[id]; ok {
return &BlocklistMatch{
List: m.name,
Rule: fmt.Sprintf("%d", id),
}, true
}
}
return nil, false
}
func (m *GeoIPDB) Close() error {
return m.geoDB.Close()
}
func (m *GeoIPDB) String() string {
return "GeoIP-blocklist"
}