forked from zhangjiayin/caddy-geoip2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoip2.go
476 lines (416 loc) · 18 KB
/
geoip2.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package geoip2
import (
"errors"
"fmt"
"log"
"net"
"net/http"
"strconv"
"strings"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
type GeoIP2Record struct {
Country struct {
Locales []string `json:"locales"`
Confidence uint16 `maxminddb:"confidence"`
ISOCode string `maxminddb:"iso_code"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
Names map[string]string `maxminddb:"names"`
GeoNameID uint64 `maxminddb:"geoname_id"`
} `maxminddb:"country"`
Continent struct {
Locales []string `json:"locales"`
Code string `maxminddb:"code"`
GeoNameID uint `maxminddb:"geoname_id"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"continent"`
City struct {
Names map[string]string `maxminddb:"names"`
Confidence uint16 `maxminddb:"confidence"`
GeoNameID uint64 `maxminddb:"geoname_id"`
Locales []string `json:"locales"`
} `maxminddb:"city"`
Location struct {
AccuracyRadius uint16 `maxminddb:"accuracy_radius"`
AverageIncome uint16 `maxminddb:"average_income"`
Latitude float64 `maxminddb:"latitude"`
Longitude float64 `maxminddb:"longitude"`
MetroCode uint `maxminddb:"metro_code"`
PopulationDensity uint `maxminddb:"population_density"`
TimeZone string `maxminddb:"time_zone"`
} `maxminddb:"location"`
Postal struct {
Code string `maxminddb:"code"`
Confidence uint16 `maxminddb:"confidence"`
} `maxminddb:"postal"`
RegisteredCountry struct {
GeoNameID uint `maxminddb:"geoname_id"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"registered_country"`
RepresentedCountry struct {
Locales []string `json:"locales"`
Confidence uint16 `maxminddb:"confidence"`
GeoNameID uint `maxminddb:"geoname_id"`
IsInEuropeanUnion bool `maxminddb:"is_in_european_union"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
Type string `maxminddb:"type"`
} `maxminddb:"represented_country"`
Subdivisions []struct {
Locales []string `json:"locales"`
Confidence uint16 `maxminddb:"confidence"`
GeoNameID uint `maxminddb:"geoname_id"`
IsoCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"subdivisions"`
Traits struct {
IsAnonymousProxy bool `maxminddb:"is_anonymous_proxy"`
IsAnonymousVpn bool `maxminddb:"is_anonymous_vpn"`
IsSatelliteProvider bool `maxminddb:"is_satellite_provider"`
AutonomousSystemNumber uint64 `maxminddb:"autonomous_system_number"`
AutonomousSystemOrganization string `maxminddb:"autonomous_system_organization"`
ConnectionType string `maxminddb:"connection_type"`
Domain string `maxminddb:"domain"`
IsHostingProvider bool `maxminddb:"is_hosting_provider"`
IsLegitimateProxy bool `maxminddb:"is_legitimate_proxy"`
IsPublicProxy bool `maxminddb:"is_public_proxy"`
IsResidentialProxy bool `maxminddb:"is_residential_proxy"`
IsTorExitNode bool `maxminddb:"is_tor_exit_node"`
Isp string `maxminddb:"isp"`
MobileCountryCode string `maxminddb:"mobile_country_code"`
MobileNetworkCode string `maxminddb:"mobile_network_code"`
Network string `maxminddb:"network"`
Organization string `maxminddb:"organization"`
UserType string `maxminddb:"user_type"`
UserCount int32 `maxminddb:"userCount"`
StaticIpScore float64 `maxminddb:"static_ip_score"`
} `maxminddb:"traits"`
}
// http.handlers.geoip2 is an GeoIP2 server handler.
// it uses GeoIP2 Data to identify the location of the IP
type GeoIP2 struct {
// strict: only use remote IP address
// wild: use X-Forwarded-For if it exists
// trusted_proxies: use X-Forwarded-For if exists when trusted_proxies if valid
// default:trusted_proxies
Enable string `json:"enable,omitempty"`
state *GeoIP2State `json:"-"`
ctx caddy.Context `json:"-"`
}
type IpSafeLevel int
const (
Wild IpSafeLevel = 0
TrustedProxies IpSafeLevel = 1
Strict IpSafeLevel = 100
)
func init() {
caddy.RegisterModule(GeoIP2{})
httpcaddyfile.RegisterHandlerDirective("geoip2_vars", parseCaddyfile)
}
func (GeoIP2) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.geoip2",
New: func() caddy.Module { return new(GeoIP2) },
}
}
func (m GeoIP2) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
//init some variables with default value ""
repl.Set("geoip2.ip_address", "")
repl.Set("geoip2.country_code", "")
repl.Set("geoip2.country_name", "")
repl.Set("geoip2.country_eu", "")
repl.Set("geoip2.country_locales", "")
repl.Set("geoip2.country_confidence", "")
repl.Set("geoip2.country_names", "")
repl.Set("geoip2.country_names_0", "")
repl.Set("geoip2.country_names_1", "")
repl.Set("geoip2.country_geoname_id", "")
repl.Set("geoip2.continent_code", "")
repl.Set("geoip2.continent_locales", "")
repl.Set("geoip2.continent_names", "")
repl.Set("geoip2.continent_names_0", "")
repl.Set("geoip2.continent_names_1", "")
repl.Set("geoip2.continent_geoname_id", "")
repl.Set("geoip2.continent_name", "")
repl.Set("geoip2.city_confidence", "")
repl.Set("geoip2.city_locales", "")
repl.Set("geoip2.city_names", "")
repl.Set("geoip2.city_names_0", "")
repl.Set("geoip2.city_names_1", "")
repl.Set("geoip2.city_geoname_id", "")
// repl.Set("geoip2.city_name", val)
repl.Set("geoip2.city_name", "")
repl.Set("geoip2.location_latitude", "")
repl.Set("geoip2.location_longitude", "")
repl.Set("geoip2.location_time_zone", "")
repl.Set("geoip2.location_accuracy_radius", "")
repl.Set("geoip2.location_average_income", "")
repl.Set("geoip2.location_metro_code", "")
repl.Set("geoip2.location_population_density", "")
repl.Set("geoip2.postal_code", "")
repl.Set("geoip2.postal_confidence", "")
repl.Set("geoip2.registeredcountry_geoname_id", "")
repl.Set("geoip2.registeredcountry_is_in_european_union", "")
repl.Set("geoip2.registeredcountry_iso_code", "")
repl.Set("geoip2.registeredcountry_names", "")
repl.Set("geoip2.registeredcountry_names_0", "")
repl.Set("geoip2.registeredcountry_names_1", "")
repl.Set("geoip2.registeredcountry_name", "")
repl.Set("geoip2.representedcountry_geoname_id", "")
repl.Set("geoip2.representedcountry_is_in_european_union", "")
repl.Set("geoip2.representedcountry_iso_code", "")
repl.Set("geoip2.representedcountry_names", "")
repl.Set("geoip2.representedcountry_locales", "")
repl.Set("geoip2.representedcountry_confidence", "")
repl.Set("geoip2.representedcountry_type", "")
repl.Set("geoip2.representedcountry_name", "")
repl.Set("geoip2.representedcountry_names_0", "")
repl.Set("geoip2.representedcountry_names_1", "")
repl.Set("geoip2.subdivisions", "")
repl.Set("geoip2.traits_is_anonymous_proxy", "")
repl.Set("geoip2.traits_is_anonymous_vpn", "")
repl.Set("geoip2.traits_is_satellite_provider", "")
repl.Set("geoip2.traits_autonomous_system_number", "")
repl.Set("geoip2.traits_autonomous_system_organization", "")
repl.Set("geoip2.traits_connection_type", "")
repl.Set("geoip2.traits_domain", "")
repl.Set("geoip2.traits_is_hosting_provider", "")
repl.Set("geoip2.traits_is_legitimate_proxy", "")
repl.Set("geoip2.traits_is_public_proxy", "")
repl.Set("geoip2.traits_is_residential_proxy", "")
repl.Set("geoip2.traits_is_tor_exit_node", "")
repl.Set("geoip2.traits_isp", "")
repl.Set("geoip2.traits_mobile_country_code", "")
repl.Set("geoip2.traits_mobile_network_code", "")
repl.Set("geoip2.traits_network", "")
repl.Set("geoip2.traits_organization", "")
repl.Set("geoip2.traits_user_type", "")
repl.Set("geoip2.traits_userCount", "")
repl.Set("geoip2.traits_static_ip_score", "")
repl.Set("geoip2.subdivisions_1_confidence", "")
repl.Set("geoip2.subdivisions_1_geoname_id", "")
repl.Set("geoip2.subdivisions_1_iso_code", "")
repl.Set("geoip2.subdivisions_1_locales", "")
repl.Set("geoip2.subdivisions_1_locales_en", "")
repl.Set("geoip2.subdivisions_1_names", "")
repl.Set("geoip2.subdivisions_1_names_0", "")
repl.Set("geoip2.subdivisions_1_names_1", "")
repl.Set("geoip2.subdivisions_1_name", "")
repl.Set("geoip2.subdivisions_2_confidence", "")
repl.Set("geoip2.subdivisions_2_geoname_id", "")
repl.Set("geoip2.subdivisions_2_iso_code", "")
repl.Set("geoip2.subdivisions_2_locales", "")
repl.Set("geoip2.subdivisions_2_locales_en", "")
repl.Set("geoip2.subdivisions_2_names", "")
repl.Set("geoip2.subdivisions_2_names_0", "")
repl.Set("geoip2.subdivisions_2_names_1", "")
repl.Set("geoip2.subdivisions_2_name", "")
if m.Enable != "off" && m.Enable != "false" && m.Enable != "0" {
var record = GeoIP2Record{}
if m.state != nil && m.state.DBHandler != nil {
clientIP, _ := m.getClientIP(r)
m.state.DBHandler.Lookup(clientIP, &record)
if clientIP == nil {
repl.Set("geoip2.ip_address", "")
} else {
repl.Set("geoip2.ip_address", clientIP.String())
}
//country
repl.Set("geoip2.country_code", record.Country.ISOCode)
for key, element := range record.Country.Names {
repl.Set("geoip2.country_names_"+key, element)
if key == "en" {
repl.Set("geoip2.country_name", element)
}
}
repl.Set("geoip2.country_eu", record.Country.IsInEuropeanUnion)
repl.Set("geoip2.country_locales", record.Country.Locales)
repl.Set("geoip2.country_confidence", record.Country.Confidence)
repl.Set("geoip2.country_names", record.Country.Names)
repl.Set("geoip2.country_geoname_id", record.Country.GeoNameID)
//Continent
repl.Set("geoip2.continent_code", record.Continent.Code)
repl.Set("geoip2.continent_locales", record.Continent.Locales)
repl.Set("geoip2.continent_names", record.Continent.Names)
repl.Set("geoip2.continent_geoname_id", record.Continent.GeoNameID)
for key, element := range record.Continent.Names {
repl.Set("geoip2.continent_names_"+key, element)
if key == "en" {
repl.Set("geoip2.continent_name", element)
}
}
//City
repl.Set("geoip2.city_confidence", record.City.Confidence)
repl.Set("geoip2.city_locales", record.City.Locales)
repl.Set("geoip2.city_names", record.City.Names)
repl.Set("geoip2.city_geoname_id", record.City.GeoNameID)
// val, _ = record.City.Names["en"]
// repl.Set("geoip2.city_name", val)
for key, element := range record.City.Names {
repl.Set("geoip2.city_names_"+key, element)
if key == "en" {
repl.Set("geoip2.city_name", element)
}
}
//Location
repl.Set("geoip2.location_latitude", record.Location.Latitude)
repl.Set("geoip2.location_longitude", record.Location.Longitude)
repl.Set("geoip2.location_time_zone", record.Location.TimeZone)
repl.Set("geoip2.location_accuracy_radius", record.Location.AccuracyRadius)
repl.Set("geoip2.location_average_income", record.Location.AverageIncome)
repl.Set("geoip2.location_metro_code", record.Location.MetroCode)
repl.Set("geoip2.location_population_density", record.Location.PopulationDensity)
//Postal
repl.Set("geoip2.postal_code", record.Postal.Code)
repl.Set("geoip2.postal_confidence", record.Postal.Confidence)
//RegisteredCountry
repl.Set("geoip2.registeredcountry_geoname_id", record.RegisteredCountry.GeoNameID)
repl.Set("geoip2.registeredcountry_is_in_european_union", record.RegisteredCountry.IsInEuropeanUnion)
repl.Set("geoip2.registeredcountry_iso_code", record.RegisteredCountry.IsoCode)
repl.Set("geoip2.registeredcountry_names", record.RegisteredCountry.Names)
// val, _ = record.RegisteredCountry.Names["en"]
// repl.Set("geoip2.registeredcountry_name", val)
for key, element := range record.RegisteredCountry.Names {
repl.Set("geoip2.registeredcountry_names_"+key, element)
if key == "en" {
repl.Set("geoip2.registeredcountry_name", element)
}
}
//RepresentedCountry
repl.Set("geoip2.representedcountry_geoname_id", record.RepresentedCountry.GeoNameID)
repl.Set("geoip2.representedcountry_is_in_european_union", record.RepresentedCountry.IsInEuropeanUnion)
repl.Set("geoip2.representedcountry_iso_code", record.RepresentedCountry.IsoCode)
repl.Set("geoip2.representedcountry_names", record.RepresentedCountry.Names)
repl.Set("geoip2.representedcountry_locales", record.RepresentedCountry.Locales)
repl.Set("geoip2.representedcountry_confidence", record.RepresentedCountry.Confidence)
repl.Set("geoip2.representedcountry_type", record.RepresentedCountry.Type)
// val, _ = record.RepresentedCountry.Names["en"]
// repl.Set("geoip2.representedcountry_name", val)
for key, element := range record.RepresentedCountry.Names {
repl.Set("geoip2.representedcountry_names_"+key, element)
if key == "en" {
repl.Set("geoip2.representedcountry_name", element)
}
}
repl.Set("geoip2.subdivisions", record.Subdivisions)
for index, subdivision := range record.Subdivisions {
indexStr := strconv.Itoa(index + 1)
repl.Set("geoip2.subdivisions_"+indexStr+"_confidence", subdivision.Confidence)
repl.Set("geoip2.subdivisions_"+indexStr+"_geoname_id", subdivision.GeoNameID)
repl.Set("geoip2.subdivisions_"+indexStr+"_iso_code", subdivision.IsoCode)
repl.Set("geoip2.subdivisions_"+indexStr+"_locales", subdivision.Locales)
repl.Set("geoip2.subdivisions_"+indexStr+"_names", subdivision.Names)
for key, element := range subdivision.Locales {
keyStr := strconv.Itoa(key)
repl.Set("geoip2.subdivisions_"+indexStr+"_locales_"+keyStr, element)
}
for key, element := range subdivision.Names {
repl.Set("geoip2.subdivisions_"+indexStr+"_names_"+key, element)
if key == "en" {
repl.Set("geoip2.subdivisions_"+indexStr+"_name", element)
}
}
}
//Traits
repl.Set("geoip2.traits_is_anonymous_proxy", record.Traits.IsAnonymousProxy)
repl.Set("geoip2.traits_is_anonymous_vpn", record.Traits.IsAnonymousVpn)
repl.Set("geoip2.traits_is_satellite_provider", record.Traits.IsSatelliteProvider)
repl.Set("geoip2.traits_autonomous_system_number", record.Traits.AutonomousSystemNumber)
repl.Set("geoip2.traits_autonomous_system_organization", record.Traits.AutonomousSystemOrganization)
//Traits
repl.Set("geoip2.traits_connection_type", record.Traits.ConnectionType)
repl.Set("geoip2.traits_domain", record.Traits.Domain)
repl.Set("geoip2.traits_is_hosting_provider", record.Traits.IsHostingProvider)
repl.Set("geoip2.traits_is_legitimate_proxy", record.Traits.IsLegitimateProxy)
repl.Set("geoip2.traits_is_public_proxy", record.Traits.IsPublicProxy)
repl.Set("geoip2.traits_is_residential_proxy", record.Traits.IsResidentialProxy)
repl.Set("geoip2.traits_is_tor_exit_node", record.Traits.IsTorExitNode)
repl.Set("geoip2.traits_isp", record.Traits.Isp)
repl.Set("geoip2.traits_mobile_country_code", record.Traits.MobileCountryCode)
repl.Set("geoip2.traits_mobile_network_code", record.Traits.MobileNetworkCode)
repl.Set("geoip2.traits_network", record.Traits.Network)
repl.Set("geoip2.traits_organization", record.Traits.Organization)
repl.Set("geoip2.traits_user_type", record.Traits.UserType)
repl.Set("geoip2.traits_userCount", record.Traits.UserCount)
repl.Set("geoip2.traits_static_ip_score", record.Traits.StaticIpScore)
caddy.Log().Named("http.handlers.geoip2").Debug(fmt.Sprintf("ServeHTTP %v %v %v", m, record, clientIP))
}
}
return next.ServeHTTP(w, r)
}
func (m GeoIP2) getClientIP(r *http.Request) (net.IP, error) {
var ip string
trustedProxy := caddyhttp.GetVar(r.Context(), caddyhttp.TrustedProxyVarKey).(bool)
safeLevel := TrustedProxies
if strings.ToLower(m.Enable) == "strict" {
safeLevel = Strict
} else if strings.ToLower(m.Enable) == "wild" {
safeLevel = Wild
}
fwdFor := r.Header.Get("X-Forwarded-For")
if ((safeLevel == TrustedProxies && trustedProxy) || safeLevel == Wild) && fwdFor != "" {
ips := strings.Split(fwdFor, ", ")
ip = ips[0]
} else {
// Otherwise, get the client ip from the request remote address.
var err error
ip, _, err = net.SplitHostPort(r.RemoteAddr)
if err != nil {
if serr, ok := err.(*net.AddrError); ok && serr.Err == "missing port in address" { // It's not critical try parse
ip = r.RemoteAddr
} else {
log.Printf("Error when SplitHostPort: %v", serr.Err)
return nil, err
}
}
}
// Parse the ip address string into a net.IP.
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return nil, errors.New("unable to parse address")
}
return parsedIP, nil
}
// for http handler
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
var m GeoIP2
err := m.UnmarshalCaddyfile(h.Dispenser)
return m, err
}
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
func (m *GeoIP2) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
if !d.Args(&m.Enable) {
return d.ArgErr()
}
}
return nil
}
func (g *GeoIP2) Provision(ctx caddy.Context) error {
caddy.Log().Named("http.handlers.geoip2").Info(fmt.Sprintf("Provision"))
app, err := ctx.App(moduleName)
if err != nil {
return fmt.Errorf("getting geoip2 app: %v", err)
}
g.state = app.(*GeoIP2State)
g.ctx = ctx
return nil
}
func (g GeoIP2) Validate() error {
caddy.Log().Named("http.handlers.geoip2").Info(fmt.Sprintf("Validate"))
return nil
}
// Interface guards
var (
_ caddy.Module = (*GeoIP2)(nil)
_ caddy.Provisioner = (*GeoIP2)(nil)
_ caddy.Validator = (*GeoIP2)(nil)
_ caddyhttp.MiddlewareHandler = (*GeoIP2)(nil)
_ caddyfile.Unmarshaler = (*GeoIP2)(nil)
)