/
geoip.go
164 lines (143 loc) · 4.07 KB
/
geoip.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
package transformers
import (
"fmt"
"net"
"strconv"
"github.com/dmachard/go-dnscollector/dnsutils"
"github.com/dmachard/go-dnscollector/pkgconfig"
"github.com/dmachard/go-logger"
"github.com/oschwald/maxminddb-golang"
)
type MaxminddbRecord struct {
Continent struct {
Code string `maxminddb:"code"`
} `maxminddb:"continent"`
Country struct {
ISOCode string `maxminddb:"iso_code"`
} `maxminddb:"country"`
City struct {
Names map[string]string `maxminddb:"names"`
} `maxminddb:"city"`
AutonomousSystemNumber int `maxminddb:"autonomous_system_number"`
AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
}
type GeoRecord struct {
Continent, CountryISOCode, City, ASN, ASO string
}
type GeoIPProcessor struct {
config *pkgconfig.ConfigTransformers
logger *logger.Logger
dbCountry, dbCity, dbAsn *maxminddb.Reader
enabled bool
outChannels []chan dnsutils.DNSMessage
LogInfo, LogError func(msg string, v ...interface{})
}
func NewDNSGeoIPTransform(config *pkgconfig.ConfigTransformers, logger *logger.Logger, name string,
instance int, outChannels []chan dnsutils.DNSMessage) GeoIPProcessor {
d := GeoIPProcessor{config: config, logger: logger, outChannels: outChannels}
d.LogInfo = func(msg string, v ...interface{}) {
log := fmt.Sprintf("transformer - [%s] conn #%d - geoip - ", name, instance)
logger.Info(log+msg, v...)
}
d.LogError = func(msg string, v ...interface{}) {
log := fmt.Sprintf("transformer - [%s] conn #%d - geoip - ", name, instance)
logger.Error(log+msg, v...)
}
return d
}
func (p *GeoIPProcessor) ReloadConfig(config *pkgconfig.ConfigTransformers) {
p.config = config
}
func (p *GeoIPProcessor) InitDNSMessage(dm *dnsutils.DNSMessage) {
if dm.Geo == nil {
dm.Geo = &dnsutils.TransformDNSGeo{
CountryIsoCode: "-",
City: "-",
Continent: "-",
AutonomousSystemNumber: "-",
AutonomousSystemOrg: "-",
}
}
}
func (p *GeoIPProcessor) Open() (err error) {
// before to open, close all files
// because open can be called also on reload
p.enabled = false
p.Close()
// open files ?
if len(p.config.GeoIP.DBCountryFile) > 0 {
p.dbCountry, err = maxminddb.Open(p.config.GeoIP.DBCountryFile)
if err != nil {
p.enabled = false
return
}
p.enabled = true
p.LogInfo("country database loaded (%d records)", p.dbCountry.Metadata.NodeCount)
}
if len(p.config.GeoIP.DBCityFile) > 0 {
p.dbCity, err = maxminddb.Open(p.config.GeoIP.DBCityFile)
if err != nil {
p.enabled = false
return
}
p.enabled = true
p.LogInfo("city database loaded (%d records)", p.dbCity.Metadata.NodeCount)
}
if len(p.config.GeoIP.DBASNFile) > 0 {
p.dbAsn, err = maxminddb.Open(p.config.GeoIP.DBASNFile)
if err != nil {
p.enabled = false
return
}
p.enabled = true
p.LogInfo("asn database loaded (%d records)", p.dbAsn.Metadata.NodeCount)
}
return nil
}
func (p *GeoIPProcessor) IsEnabled() bool {
return p.enabled
}
func (p *GeoIPProcessor) Close() {
if p.dbCountry != nil {
p.dbCountry.Close()
}
if p.dbCity != nil {
p.dbCity.Close()
}
if p.dbAsn != nil {
p.dbAsn.Close()
}
}
func (p *GeoIPProcessor) Lookup(ip string) (GeoRecord, error) {
record := &MaxminddbRecord{}
rec := GeoRecord{Continent: "-",
CountryISOCode: "-",
City: "-",
ASN: "-",
ASO: "-"}
if p.dbAsn != nil {
err := p.dbAsn.Lookup(net.ParseIP(ip), &record)
if err != nil {
return rec, err
}
rec.ASN = strconv.Itoa(record.AutonomousSystemNumber)
rec.ASO = record.AutonomousSystemOrganization
}
if p.dbCity != nil {
err := p.dbCity.Lookup(net.ParseIP(ip), &record)
if err != nil {
return rec, err
}
rec.City = record.City.Names["en"]
rec.CountryISOCode = record.Country.ISOCode
rec.Continent = record.Continent.Code
} else if p.dbCountry != nil {
err := p.dbCountry.Lookup(net.ParseIP(ip), &record)
if err != nil {
return rec, err
}
rec.CountryISOCode = record.Country.ISOCode
rec.Continent = record.Continent.Code
}
return rec, nil
}