-
Notifications
You must be signed in to change notification settings - Fork 1
/
geoip_decoder.go
424 lines (367 loc) · 15.5 KB
/
geoip_decoder.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/***** BEGIN LICENSE BLOCK *****
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# The Initial Developer of the Original Code is the Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2014
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Michael Gibson ([email protected])
# Rob Miller ([email protected])
#
# ***** END LICENSE BLOCK *****/
package geoip2
import (
"fmt"
"github.com/oschwald/geoip2-golang"
"github.com/mozilla-services/heka/message"
. "github.com/mozilla-services/heka/pipeline"
"net"
"bytes"
"strconv"
)
type GeoIp2Decoder struct {
AnonDatabaseFile string
CityDatabaseFile string
ConnDatabaseFile string
ISPDatabaseFile string
SourceAddrFields []string
TargetField string
Language string
JSONObject bool
DNSLookup bool
anon_db *geoip2.Reader
city_db *geoip2.Reader
conn_db *geoip2.Reader
isp_db *geoip2.Reader
Config *GeoIp2DecoderConfig
pConfig *PipelineConfig
}
type GeoIp2DecoderConfig struct {
AnonDatabaseFile string `toml:"db_anon"`
CityDatabaseFile string `toml:"db_city"`
ConnDatabaseFile string `toml:"db_conn"`
ISPDatabaseFile string `toml:"db_isp"`
SourceAddrFields []string `toml:"source_addr_fields"`
TargetField string `toml:"target_field_prefix"`
Language string `toml:"language"`
// When true, all the location info is put into a separate
// JSON object. The "target_field_prefix" is the name of the field
// that will contain the raw bytes of the object.
// When false (the default), all the info is put into separate
// fields with the prefix "target_field_prefix"
JSONObject bool `toml:"raw_json_object"`
// If true, it will do a DNS lookup on the string contained
// in "source_host_field".
// When false, it will consider the contents
// of "source_host_field" to be a IP address (default)
DNSLookup bool `toml:"dns_lookup"`
}
// Heka will call this before calling any other methods to give us access to
// the pipeline configuration.
func (gi2 *GeoIp2Decoder) SetPipelineConfig(pConfig *PipelineConfig) {
gi2.pConfig = pConfig
}
func (gi2 *GeoIp2Decoder) ConfigStruct() interface{} {
globals := gi2.pConfig.Globals
safs := make([]string, 1)
safs[0] = "remote_addr"
return &GeoIp2DecoderConfig{
CityDatabaseFile: globals.PrependShareDir("GeoLite2-City.mmdb"),
SourceAddrFields: safs,
TargetField: "geoip",
Language: "en",
}
}
func (gi2 *GeoIp2Decoder) Init(config interface{}) (err error) {
gi2.Config = config.(*GeoIp2DecoderConfig)
if len(gi2.Config.SourceAddrFields) == 0 {
gi2.LogError(fmt.Errorf("At least one source address field must be specified."))
}
gi2.SourceAddrFields = make([]string, len(gi2.Config.SourceAddrFields))
for i, name := range gi2.Config.SourceAddrFields {
gi2.SourceAddrFields[i] = name
}
if gi2.Config.TargetField == "" {
gi2.LogError(fmt.Errorf("`target_field` must be specified"))
}
gi2.DNSLookup = gi2.Config.DNSLookup
gi2.JSONObject = gi2.Config.JSONObject
gi2.Language = gi2.Config.Language
gi2.TargetField = gi2.Config.TargetField
if gi2.anon_db == nil && gi2.Config.AnonDatabaseFile != "" {
gi2.anon_db, err = geoip2.Open(gi2.Config.AnonDatabaseFile)
}
if err != nil {
gi2.LogError(fmt.Errorf("Error: Could not open GeoIP2-Anonymous-IP database: %s, skipping\n", gi2.Config.AnonDatabaseFile))
}
if gi2.city_db == nil && gi2.Config.CityDatabaseFile != "" {
gi2.city_db, err = geoip2.Open(gi2.Config.CityDatabaseFile)
}
if err != nil {
gi2.LogError(fmt.Errorf("Error: Could not open GeoIP2-City database: %s, skipping\n", gi2.Config.CityDatabaseFile))
}
if gi2.conn_db == nil && gi2.Config.ConnDatabaseFile != "" {
gi2.conn_db, err = geoip2.Open(gi2.Config.ConnDatabaseFile)
}
if err != nil {
gi2.LogError(fmt.Errorf("Error: Could not open GeoIP2-Connection-Type database: %s, skipping\n", gi2.Config.ConnDatabaseFile))
}
if gi2.isp_db == nil && gi2.Config.ISPDatabaseFile != "" {
gi2.isp_db, err = geoip2.Open(gi2.Config.ISPDatabaseFile)
}
if err != nil {
gi2.LogError(fmt.Errorf("Error: Could not open GeoIP2-ISP database: %s, skipping\n", gi2.Config.ISPDatabaseFile))
}
return
}
//Creates new Heka Message fields for the following location info
//(if they are contained in the record): location coordinates,
//country ISO code, country name in English, city name in English
func (gi2 *GeoIp2Decoder) CreateMessageFieldsCity(record *geoip2.City, pack *PipelinePack) (err error) {
countrycode := record.Country.IsoCode
country := record.Country.Names[gi2.Language]
city := record.City.Names[gi2.Language]
lat := strconv.FormatFloat(record.Location.Latitude,'g', 16, 32)
lon := strconv.FormatFloat(record.Location.Longitude,'g', 16, 32)
if gi2.JSONObject {
buf := bytes.Buffer{}
buf.WriteString(`{`)
buf.WriteString(`"location":[`)
buf.WriteString(lon)
buf.WriteString(`,`)
buf.WriteString(lat)
buf.WriteString(`]`)
if countrycode != "" {
buf.WriteString(`,"country_code":"`)
buf.WriteString(countrycode)
buf.WriteString(`"`)
}
if country != "" {
buf.WriteString(`,"country":"`)
buf.WriteString(country)
buf.WriteString(`"`)
}
if city != "" {
buf.WriteString(`,"city":"`)
buf.WriteString(city)
buf.WriteString(`"`)
}
buf.WriteString(`}`)
gi2.AddField(pack, gi2.TargetField, buf.Bytes())
} else {
gi2.AddField(pack, fmt.Sprintf("%s_location",gi2.TargetField), fmt.Sprintf("%s, %s", lat, lon))
if countrycode != "" {
gi2.AddField(pack, fmt.Sprintf("%s_country_code",gi2.TargetField), countrycode)
}
if country != "" {
gi2.AddField(pack, fmt.Sprintf("%s_country",gi2.TargetField), country)
}
if city != "" {
gi2.AddField(pack, fmt.Sprintf("%s_city",gi2.TargetField), city)
}
}
return
}
func (gi2 *GeoIp2Decoder) CreateMessageFieldsISP(record *geoip2.ISP, pack *PipelinePack) (err error) {
asnum := record.AutonomousSystemNumber
asname := record.AutonomousSystemOrganization
isp := record.ISP
organization := record.Organization
if gi2.JSONObject {
buf := bytes.Buffer{}
buf.WriteString(`{`)
if asnum != 0 {
buf.WriteString(`"asnum":`)
buf.WriteString(strconv.FormatUint(uint64(asnum), 10))
}
if asname != "" {
buf.WriteString(`,"asname":"`)
buf.WriteString(asname)
buf.WriteString(`"`)
}
if isp != "" {
buf.WriteString(`,"isp":"`)
buf.WriteString(isp)
buf.WriteString(`"`)
}
if organization != "" {
buf.WriteString(`,"organization":"`)
buf.WriteString(organization)
buf.WriteString(`"`)
}
buf.WriteString(`}`)
gi2.AddField(pack, gi2.TargetField, buf.Bytes())
} else {
if asnum != 0 {
gi2.AddField(pack, fmt.Sprintf("asnum"), gi2.GetData(asnum))
}
if asname != "" {
gi2.AddField(pack, fmt.Sprintf("asname"), asname)
}
if isp != "" {
gi2.AddField(pack, fmt.Sprintf("isp"), isp)
}
if organization != "" {
gi2.AddField(pack, fmt.Sprintf("organization"), organization)
}
}
return
}
func (gi2 *GeoIp2Decoder) CreateMessageFieldsAnonymousIP(record *geoip2.AnonymousIP, pack *PipelinePack) (err error) {
anon := record.IsAnonymous
anonvpn := record.IsAnonymousVPN
hostingpro := record.IsHostingProvider
publicproxy := record.IsPublicProxy
torexitnode := record.IsTorExitNode
if gi2.JSONObject {
buf := bytes.Buffer{}
buf.WriteString(`{`)
if anon {
buf.WriteString(`,"anonymous_ip": true,`)
}
if anonvpn {
buf.WriteString(`,"anonymous_vpn": true`)
}
if hostingpro {
buf.WriteString(`,"hosting_provider": true`)
}
if publicproxy {
buf.WriteString(`,"public_proxy": true`)
}
if torexitnode {
buf.WriteString(`,"tor_exit_node": true`)
}
buf.WriteString(`}`)
gi2.AddField(pack, gi2.TargetField, buf.Bytes())
} else {
if anon {
gi2.AddField(pack, fmt.Sprintf("anonymous_ip"), anon)
}
if anonvpn {
gi2.AddField(pack, fmt.Sprintf("anonymous_vpn"), anonvpn)
}
if hostingpro {
gi2.AddField(pack, fmt.Sprintf("hosting_provider"), hostingpro)
}
if publicproxy {
gi2.AddField(pack, fmt.Sprintf("public_proxy"), publicproxy)
}
if torexitnode {
gi2.AddField(pack, fmt.Sprintf("tor_exit_node"), torexitnode)
}
}
return
}
func (gi2 *GeoIp2Decoder) CreateMessageFieldsConnectionType(record *geoip2.ConnectionType, pack *PipelinePack) (err error) {
conntype := record.ConnectionType
if gi2.JSONObject {
buf := bytes.Buffer{}
buf.WriteString(`{`)
if conntype != "" {
buf.WriteString(`"connection_type":"`)
buf.WriteString(conntype)
buf.WriteString(`"`)
}
buf.WriteString(`}`)
gi2.AddField(pack, gi2.TargetField, buf.Bytes())
} else {
if conntype != "" {
gi2.AddField(pack, fmt.Sprintf("connection_type"), conntype)
}
}
return
}
func (gi2 *GeoIp2Decoder) Decode(pack *PipelinePack) (packs []*PipelinePack, fail error) {
var ip net.IP
var found bool = false
for _, hostAddr := range gi2.SourceAddrFields {
var hostValue, _ = pack.Message.GetFieldValue(hostAddr)
host, ok := hostValue.(string)
if !ok {
// IP field was not a string. Field could just be blank. Continue processing in loop.
continue
}
if gi2.DNSLookup {
ips, err := net.LookupIP(host)
if err != nil {
// Could not get an IP for the host, can happen. Continue processing in loop.
continue
}
ip = ips[0]
} else {
ip = net.ParseIP(host)
if ip == nil {
// Not a valid IP address in the host string. Continue processing in loop.
continue
}
}
if gi2.anon_db != nil {
rec, err := gi2.anon_db.AnonymousIP(ip)
if err == nil &&
(rec.IsAnonymous || rec.IsAnonymousVPN || rec.IsHostingProvider || rec.IsPublicProxy || rec.IsTorExitNode){
found = true
gi2.CreateMessageFieldsAnonymousIP(rec, pack)
}
}
if gi2.city_db != nil {
rec, err := gi2.city_db.City(ip)
if err == nil &&
(rec.Location.Longitude != 0.0 && rec.Location.Latitude != 0.0){
found = true
gi2.CreateMessageFieldsCity(rec, pack)
}
}
if gi2.conn_db != nil {
rec, err := gi2.conn_db.ConnectionType(ip)
if err == nil &&
(rec.ConnectionType != ""){
found = true
gi2.CreateMessageFieldsConnectionType(rec, pack)
}
}
if gi2.isp_db != nil {
rec, err := gi2.isp_db.ISP(ip)
if err != nil ||
(rec.AutonomousSystemNumber != 0 || rec.AutonomousSystemOrganization != "" || rec.ISP != "" || rec.Organization != ""){
found = true
gi2.CreateMessageFieldsISP(rec, pack)
}
}
if found { break }
}
packs = []*PipelinePack{pack}
return
}
func (gi2 *GeoIp2Decoder) AddField(pack *PipelinePack, name string, value interface{}) error {
field, err := message.NewField(name, value, "")
if err != nil {
gi2.LogError(fmt.Errorf("error adding field '%s': %s", name, err))
} else {
pack.Message.AddField(field)
}
return nil
}
// getData converts uint64 to int64 for Heka supported data type
func (gi2 *GeoIp2Decoder) GetData(v interface{}) interface{} {
switch d := v.(type) {
case uint64:
return int64(d)
case uint32:
return int32(d)
case uint:
return int(d)
default:
return d
}
}
func (gi2 *GeoIp2Decoder) LogError(err error) {
LogError.Printf("GeoIp2Decoder: %s", err)
}
func init() {
RegisterPlugin("GeoIp2Decoder", func() interface{} {
return new(GeoIp2Decoder)
})
}