mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 12:48:00 +00:00
- httpcache.Cacher loses Transform (always atomic copy to Path); adds BasicAuth and Bearer helpers for Authorization header values. - geoip.Open now reads <dir>/GeoLite2-City.tar.gz and GeoLite2-ASN.tar.gz directly: extracts the .mmdb entry in memory and opens via geoip2.FromBytes. No .mmdb files written to disk. - geoip.Downloader/New/NewCacher/Fetch/ExtractMMDB removed — geoip is purely read/lookup; fetching is each caller's concern. - cmd/check-ip/main.go is a single main() again: blocklists via gitshallow+dataset, geoip via two httpcache.Cachers (if GeoIP.conf present) + geoip.Open. No geo refresh loop, no dataset.Group for geo. - cmd/geoip-update and the integration test construct httpcache.Cachers directly against geoip.DownloadBase + edition IDs, writing .tar.gz.
111 lines
2.6 KiB
Go
111 lines
2.6 KiB
Go
package geoip
|
|
|
|
import (
|
|
"archive/tar"
|
|
"compress/gzip"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/netip"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/oschwald/geoip2-golang"
|
|
)
|
|
|
|
// Databases holds open GeoLite2 City + ASN readers.
|
|
type Databases struct {
|
|
City *geoip2.Reader
|
|
ASN *geoip2.Reader
|
|
}
|
|
|
|
// Open reads <dir>/GeoLite2-City.tar.gz and <dir>/GeoLite2-ASN.tar.gz,
|
|
// extracts the .mmdb entry from each archive in memory, and returns open
|
|
// readers. No .mmdb files are written to disk.
|
|
func Open(dir string) (*Databases, error) {
|
|
city, err := openMMDBTarGz(filepath.Join(dir, "GeoLite2-City.tar.gz"))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("city: %w", err)
|
|
}
|
|
asn, err := openMMDBTarGz(filepath.Join(dir, "GeoLite2-ASN.tar.gz"))
|
|
if err != nil {
|
|
_ = city.Close()
|
|
return nil, fmt.Errorf("asn: %w", err)
|
|
}
|
|
return &Databases{City: city, ASN: asn}, nil
|
|
}
|
|
|
|
func openMMDBTarGz(path string) (*geoip2.Reader, error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
gr, err := gzip.NewReader(f)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("gzip %s: %w", path, err)
|
|
}
|
|
defer gr.Close()
|
|
tr := tar.NewReader(gr)
|
|
for {
|
|
hdr, err := tr.Next()
|
|
if err == io.EOF {
|
|
return nil, fmt.Errorf("no .mmdb entry in %s", path)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !strings.HasSuffix(hdr.Name, ".mmdb") {
|
|
continue
|
|
}
|
|
data, err := io.ReadAll(tr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return geoip2.FromBytes(data)
|
|
}
|
|
}
|
|
|
|
// Close closes the city and ASN readers.
|
|
func (d *Databases) Close() error {
|
|
return errors.Join(d.City.Close(), d.ASN.Close())
|
|
}
|
|
|
|
// Info is the structured result of a GeoIP lookup.
|
|
type Info struct {
|
|
City string `json:"city,omitempty"`
|
|
Region string `json:"region,omitempty"`
|
|
Country string `json:"country,omitempty"`
|
|
CountryISO string `json:"country_iso,omitempty"`
|
|
ASN uint `json:"asn,omitzero"`
|
|
ASNOrg string `json:"asn_org,omitempty"`
|
|
}
|
|
|
|
// Lookup returns city + ASN info for ip. Returns a zero Info on unparseable
|
|
// IP or database miss.
|
|
func (d *Databases) Lookup(ip string) Info {
|
|
var info Info
|
|
addr, err := netip.ParseAddr(ip)
|
|
if err != nil {
|
|
return info
|
|
}
|
|
stdIP := addr.AsSlice()
|
|
|
|
if rec, err := d.City.City(stdIP); err == nil {
|
|
info.City = rec.City.Names["en"]
|
|
info.Country = rec.Country.Names["en"]
|
|
info.CountryISO = rec.Country.IsoCode
|
|
if len(rec.Subdivisions) > 0 {
|
|
if sub := rec.Subdivisions[0].Names["en"]; sub != "" && sub != info.City {
|
|
info.Region = sub
|
|
}
|
|
}
|
|
}
|
|
if rec, err := d.ASN.ASN(stdIP); err == nil {
|
|
info.ASN = rec.AutonomousSystemNumber
|
|
info.ASNOrg = rec.AutonomousSystemOrganization
|
|
}
|
|
return info
|
|
}
|