-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathipinfo.go
More file actions
279 lines (256 loc) · 12 KB
/
ipinfo.go
File metadata and controls
279 lines (256 loc) · 12 KB
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
/*
* ZAnnotate Copyright 2025 Regents of the University of Michigan
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package zannotate
import (
"errors"
"flag"
"fmt"
"net"
"net/netip"
"strconv"
"github.com/oschwald/maxminddb-golang/v2"
log "github.com/sirupsen/logrus"
)
// This module provides IPInfo.io annotations for IP addresses using a local MaxMind DB file.
// ------------------------------------------------------------------------------------
// The MaxMind DB formats were pulled from IPInfo.io's API documentation on 07/29/2025.
// IPInfo provides data at various tiers of access: Lite, Core, and Plus.
// Since the MaxMindDB decode is best-effort, we'll just define the Plus format which includes all lower tiers.
// If a user has a Lite or Core DB file, the fields not present in those tiers will not appear in output.
// See https://ipinfo.io/products/plus for more info on field definitions.
// ------------------------------------------------------------------------------------
// IPInfoMMDBOutput includes both the Plus/Core/Lite IPInfo fields and their maxminddb tags. We'll convert this into a
// IPInfoModuleOutput for JSON output (converting string fields to appropriate types).
type IPInfoMMDBOutput struct {
City string `maxminddb:"city"`
Region string `maxminddb:"region"`
RegionCode string `maxminddb:"region_code"`
Country string `maxminddb:"country"`
CountryCode string `maxminddb:"country_code"`
Continent string `maxminddb:"continent"`
ContinentCode string `maxminddb:"continent_code"`
Latitude string `maxminddb:"latitude"`
Longitude string `maxminddb:"longitude"`
Timezone string `maxminddb:"timezone"`
PostalCode string `maxminddb:"postal_code"`
DMACode string `maxminddb:"dma_code"`
GeonameID string `maxminddb:"geoname_id"` // GeoNames database identifier (if available).
Radius string `maxminddb:"radius"` // Accuracy radius in kilometers (if available).
GeoChanged string `maxminddb:"geo_changed"` // Timestamp or flag indicating when the geolocation last changed (if available).
ASN string `maxminddb:"asn"`
ASName string `maxminddb:"as_name"`
ASDomain string `maxminddb:"as_domain"`
ASType string `maxminddb:"as_type"`
ASChanged string `maxminddb:"as_changed"`
CarrierName string `maxminddb:"carrier_name"` // Name of the mobile carrier (if available).
MobileCountryCode string `maxminddb:"mcc"`
MobileNetworkCode string `maxminddb:"mnc"`
PrivacyName string `maxminddb:"privacy_name"` // Specific name of the privacy or anonymization service detected (e.g., “NordVPN”).
IsProxy string `maxminddb:"is_proxy"`
IsRelay string `maxminddb:"is_relay"` // Boolean flag indicating use of a general relay service
IsTOR string `maxminddb:"is_tor"` // Whether the IP is a known TOR exit node.
IsVPN string `maxminddb:"is_vpn"` // Flag indicating use of a VPN Service
IsAnonymous string `maxminddb:"is_anonymous"` // True if the IP is associated with VPN, proxy, Tor, or a relay service.
IsAnycast string `maxminddb:"is_anycast"` // Whether the IP is using anycast routing.
IsHosting string `maxminddb:"is_hosting"` // True if the IP address is an internet service hosting IP address
IsMobile string `maxminddb:"is_mobile"` // True if the IP address is associated with a mobile network or carrier.
IsSatellite string `maxminddb:"is_satellite"` // True if the IP address is associated with a satellite connection
}
// IPInfoModuleOutput is the final output struct with appropriate types for JSON output
type IPInfoModuleOutput struct {
City string `json:"city,omitempty"`
Region string `json:"region,omitempty"`
RegionCode string `json:"region_code,omitempty"`
Country string `json:"country,omitempty"`
CountryCode string `json:"country_code,omitempty"`
Continent string `json:"continent,omitempty"`
ContinentCode string `json:"continent_code,omitempty"`
Latitude float64 `json:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty"`
Timezone string `json:"timezone,omitempty"`
PostalCode string `json:"postal_code,omitempty"`
DMACode string `json:"dma_code,omitempty"` // Nielsen Designated Market Area code (if available).
GeonameID uint64 `json:"geoname_id,omitempty"` // GeoNames database identifier (if available).
Radius uint64 `json:"radius,omitempty"` // Accuracy radius in kilometers (if available).
GeoChanged string `json:"geo_changed,omitempty"` // Timestamp or flag indicating when the geolocation last changed (if available).
ASN string `json:"asn,omitempty"`
ASName string `json:"as_name,omitempty"`
ASDomain string `json:"as_domain,omitempty"`
ASType string `json:"as_type,omitempty"`
ASChanged string `json:"as_changed,omitempty"`
CarrierName string `json:"carrier_name,omitempty"` // Name of the mobile carrier (if available).
MobileCountryCode string `json:"mobile_country_code,omitempty"`
MobileNetworkCode string `json:"mobile_network_code,omitempty"`
PrivacyName string `json:"privacy_name,omitempty"` // Specific name of the privacy or anonymization service detected (e.g., “NordVPN”).
IsProxy *bool `json:"is_proxy,omitempty"`
IsRelay *bool `json:"is_relay,omitempty"` // Boolean flag indicating use of a general relay service
IsTOR *bool `json:"is_tor,omitempty"` // Whether the IP is a known TOR exit node.
IsVPN *bool `json:"is_vpn,omitempty"` // Flag indicating use of a VPN Service
IsAnonymous *bool `json:"is_anonymous,omitempty"` // True if the IP is associated with VPN, proxy, Tor, or a relay service.
IsAnycast *bool `json:"is_anycast,omitempty"` // Whether the IP is using anycast routing.
IsHosting *bool `json:"is_hosting,omitempty"` // True if the IP address is an internet service hosting IP address
IsMobile *bool `json:"is_mobile,omitempty"` // True if the IP address is associated with a mobile network or carrier.
IsSatellite *bool `json:"is_satellite,omitempty"` // True if the IP address is associated with a satellite connection
}
func (in *IPInfoMMDBOutput) ToModuleOutput() *IPInfoModuleOutput {
out := &IPInfoModuleOutput{
City: in.City,
Region: in.Region,
RegionCode: in.RegionCode,
Country: in.Country,
CountryCode: in.CountryCode,
Continent: in.Continent,
ContinentCode: in.ContinentCode,
Timezone: in.Timezone,
PostalCode: in.PostalCode,
GeoChanged: in.GeoChanged,
ASN: in.ASN,
ASName: in.ASName,
ASDomain: in.ASDomain,
ASType: in.ASType,
ASChanged: in.ASChanged,
CarrierName: in.CarrierName,
MobileCountryCode: in.MobileCountryCode,
MobileNetworkCode: in.MobileNetworkCode,
PrivacyName: in.PrivacyName,
}
// Convert string fields to appropriate types
var err error
if out.Latitude, err = strconv.ParseFloat(in.Latitude, 64); err != nil {
out.Latitude = 0
}
if out.Longitude, err = strconv.ParseFloat(in.Longitude, 64); err != nil {
out.Longitude = 0
}
if out.GeonameID, err = strconv.ParseUint(in.GeonameID, 10, 64); err != nil {
out.GeonameID = 0
}
if out.Radius, err = strconv.ParseUint(in.Radius, 10, 64); err != nil {
out.Radius = 0
}
var temp bool
if temp, err = strconv.ParseBool(in.IsProxy); err == nil {
t := temp // avoid taking address of a short-lived variable
out.IsProxy = &t
}
if temp, err = strconv.ParseBool(in.IsRelay); err == nil {
t := temp // avoid taking address of a short-lived variable
out.IsRelay = &t
}
if temp, err = strconv.ParseBool(in.IsTOR); err == nil {
t := temp // avoid taking address of a short-lived variable
out.IsTOR = &t
}
if temp, err = strconv.ParseBool(in.IsVPN); err == nil {
t := temp // avoid taking address of a short-lived variable
out.IsVPN = &t
}
if temp, err = strconv.ParseBool(in.IsAnonymous); err == nil {
t := temp // avoid taking address of a short-lived variable
out.IsAnonymous = &t
}
if temp, err = strconv.ParseBool(in.IsAnycast); err == nil {
t := temp // avoid taking address of a short-lived variable
out.IsAnycast = &t
}
if temp, err = strconv.ParseBool(in.IsHosting); err == nil {
t := temp // avoid taking address of a short-lived variable
out.IsHosting = &t
}
if temp, err = strconv.ParseBool(in.IsMobile); err == nil {
t := temp // avoid taking address of a short-lived variable
out.IsMobile = &t
}
if temp, err = strconv.ParseBool(in.IsSatellite); err == nil {
t := temp // avoid taking address of a short-lived variable
out.IsSatellite = &t
}
return out
}
type IPInfoAnnotatorFactory struct {
BasePluginConf
DatabaseFilePath string
db *maxminddb.Reader // MMDB Database Reader is thread-safe
}
type IPInfoAnnotator struct {
Factory *IPInfoAnnotatorFactory
Id int
}
// IPInfo Annotator Factory (Global)
func (a *IPInfoAnnotatorFactory) MakeAnnotator(i int) Annotator {
var v IPInfoAnnotator
v.Factory = a
v.Id = i
return &v
}
func (a *IPInfoAnnotatorFactory) Initialize(conf *GlobalConf) (err error) {
if len(a.DatabaseFilePath) == 0 {
return errors.New("ipinfo database file path is required")
}
if a.db, err = maxminddb.Open(a.DatabaseFilePath); err != nil {
return fmt.Errorf("error opening IPInfo database reader: %w", err)
}
// verify the MaxMind DB is not corrupted
if err = a.db.Verify(); err != nil {
return fmt.Errorf("error occured while trying to validate the MaxMind DB file: %w", err)
}
return nil
}
func (a *IPInfoAnnotatorFactory) GetWorkers() int {
return a.Threads
}
func (a *IPInfoAnnotatorFactory) Close() error {
if err := a.db.Close(); err != nil {
return fmt.Errorf("error closing IPInfo database reader: %w", err)
}
return nil
}
func (a *IPInfoAnnotatorFactory) IsEnabled() bool {
return a.Enabled
}
func (a *IPInfoAnnotatorFactory) AddFlags(flags *flag.FlagSet) {
flags.BoolVar(&a.Enabled, "ipinfo", false, "annotate with IPInfo.io data using a local MaxMind DB file")
flags.StringVar(&a.DatabaseFilePath, "ipinfo-database", "", "path to MaxMind DB data file for IPInfo.io annotation")
// On a quick benchmark of 1M IPs using a local DB file on a M2 Macbook Air, 1 thread vs. 10 threads were about the same speed, annotating about 212k IPs/second.
flags.IntVar(&a.Threads, "ipinfo-threads", 1, "how many ipinfo annotator threads")
}
// IPInfo Annotator (Per-Worker)
func (a *IPInfoAnnotator) Initialize() error {
return nil
}
func (a *IPInfoAnnotator) GetFieldName() string {
return "ipinfo"
}
func (a *IPInfoAnnotator) Annotate(inputIP net.IP) interface{} {
ip, err := netip.ParseAddr(inputIP.String())
if err != nil {
return nil // not a valid IP address, nothing to be done
}
// Decode the IP address using the MaxMind DB reader
var out *IPInfoMMDBOutput
if err = a.Factory.db.Lookup(ip).Decode(&out); err != nil {
log.Debugf("error decoding IP %s in IPInfo database: %v", ip.String(), err)
}
if out == nil {
return nil // no data found for this IP
}
return out.ToModuleOutput() // convert from the full-string struct to a typed struct
}
func (a *IPInfoAnnotator) Close() error {
return nil
}
func init() {
s := new(IPInfoAnnotatorFactory)
RegisterAnnotator(s)
}