openreplay/backend/internal/http/geoip/geoip.go
Alexander 45c956c489
Json logs format (#1952)
* feat(backend): try a new approach for logs formatting (http)

* feat(backend): added logger module

* feat(backend): added project/session info to /i endpoint

* feat(backend): found a solution for correct caller information

* feat(backend): finished logs for http handlers

* feat(backend): finished logs for mobile http handlers

* feat(backend): finished ender

* feat(backend): finished assets

* feat(backend): finished heuristics

* feat(backend): finished image-storage

* feat(backend): finished sink

* feat(backend): finished storage

* feat(backend): formatted logs in all services

* feat(backend): finished foss part

* feat(backend): added missed foss part

* feat(backend): fixed panic in memory manager and sink service

* feat(backend): connectors
2024-03-14 12:51:14 +01:00

84 lines
1.5 KiB
Go

package geoip
import (
"errors"
"net"
"strings"
"github.com/oschwald/maxminddb-golang"
)
type geoIPRecord struct {
Country struct {
ISOCode string `maxminddb:"iso_code"`
} `maxminddb:"country"`
States []struct {
Names map[string]string `maxminddb:"names"`
} `maxminddb:"subdivisions"`
City struct {
Names map[string]string `maxminddb:"names"`
} `maxminddb:"city"`
}
type GeoRecord struct {
Country string
State string
City string
}
func (r *GeoRecord) Pack() string {
return r.Country + "|" + r.State + "|" + r.City
}
func UnpackGeoRecord(pkg string) *GeoRecord {
parts := strings.Split(pkg, "|")
if len(parts) != 3 {
return &GeoRecord{
Country: pkg,
}
}
return &GeoRecord{
Country: parts[0],
State: parts[1],
City: parts[2],
}
}
type GeoParser interface {
Parse(ip net.IP) (*GeoRecord, error)
}
type geoParser struct {
r *maxminddb.Reader
}
func New(file string) (GeoParser, error) {
r, err := maxminddb.Open(file)
if err != nil {
return nil, err
}
return &geoParser{r}, nil
}
func (geoIP *geoParser) Parse(ip net.IP) (*GeoRecord, error) {
res := &GeoRecord{
Country: "UN",
State: "",
City: "",
}
if ip == nil {
return res, errors.New("IP is nil")
}
var record geoIPRecord
if err := geoIP.r.Lookup(ip, &record); err != nil {
return res, err
}
if record.Country.ISOCode != "" {
res.Country = record.Country.ISOCode
}
if len(record.States) > 0 {
res.State = record.States[0].Names["en"]
}
res.City = record.City.Names["en"]
return res, nil
}