Add GeoIP support for unique IP aggregation
- Introduced GeoIP configuration options in config.example.yaml to enable geolocation lookups for the /api/agg/unique-ips endpoint. - Updated the aggregate handler to include optional GeoIP data in responses, enriching unique IP information with country and city details, as well as ASN data if available. - Enhanced documentation in AGGREGATE.md and README.md to reflect the new GeoIP functionality and its usage. - Added a dependency on the geoip2-golang library in go.mod for GeoIP lookups. - Modified tests to accommodate the new GeoIP integration in the aggregate handler.
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package aggregate
|
||||
|
||||
import (
|
||||
"github.com/telemt/telemt-api/internal/geoip"
|
||||
)
|
||||
|
||||
// EnrichUniqueIPsGeo fills country/city on each IP when lookup succeeds.
|
||||
func EnrichUniqueIPsGeo(rows []UniqueIPsRow, g *geoip.Service) {
|
||||
if g == nil || len(rows) == 0 {
|
||||
return
|
||||
}
|
||||
for i := range rows {
|
||||
for j := range rows[i].IPs {
|
||||
ip := rows[i].IPs[j].IP
|
||||
res, ok := g.Lookup(ip)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if res.CountryCode != "" {
|
||||
s := res.CountryCode
|
||||
rows[i].IPs[j].CountryCode = &s
|
||||
}
|
||||
if res.CountryName != "" {
|
||||
s := res.CountryName
|
||||
rows[i].IPs[j].CountryName = &s
|
||||
}
|
||||
if res.CityName != "" {
|
||||
s := res.CityName
|
||||
rows[i].IPs[j].CityName = &s
|
||||
}
|
||||
if res.ASN != 0 {
|
||||
a := res.ASN
|
||||
rows[i].IPs[j].ASN = &a
|
||||
}
|
||||
if res.ASOrg != "" {
|
||||
o := res.ASOrg
|
||||
rows[i].IPs[j].ASOrganization = &o
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user