mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 20:58: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.
71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/therootcompany/golib/net/geoip"
|
|
"github.com/therootcompany/golib/net/httpcache"
|
|
)
|
|
|
|
func main() {
|
|
configPath := flag.String("config", "GeoIP.conf", "path to GeoIP.conf")
|
|
dir := flag.String("dir", "", "directory to store .tar.gz files (overrides DatabaseDirectory in config)")
|
|
freshDays := flag.Int("fresh-days", 3, "skip download if file is younger than N days")
|
|
flag.Parse()
|
|
|
|
cfg, err := geoip.ParseConf(*configPath)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
outDir := *dir
|
|
if outDir == "" {
|
|
outDir = cfg.DatabaseDirectory
|
|
}
|
|
if outDir == "" {
|
|
outDir = "."
|
|
}
|
|
if err := os.MkdirAll(outDir, 0o755); err != nil {
|
|
fmt.Fprintf(os.Stderr, "error: mkdir %s: %v\n", outDir, err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if len(cfg.EditionIDs) == 0 {
|
|
fmt.Fprintf(os.Stderr, "error: no EditionIDs found in %s\n", *configPath)
|
|
os.Exit(1)
|
|
}
|
|
|
|
auth := httpcache.BasicAuth(cfg.AccountID, cfg.LicenseKey)
|
|
maxAge := time.Duration(*freshDays) * 24 * time.Hour
|
|
|
|
exitCode := 0
|
|
for _, edition := range cfg.EditionIDs {
|
|
path := filepath.Join(outDir, edition+".tar.gz")
|
|
cacher := &httpcache.Cacher{
|
|
URL: geoip.DownloadBase + "/" + edition + "/download?suffix=tar.gz",
|
|
Path: path,
|
|
MaxAge: maxAge,
|
|
AuthHeader: "Authorization",
|
|
AuthValue: auth,
|
|
}
|
|
updated, err := cacher.Fetch()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "error: %s: %v\n", edition, err)
|
|
exitCode = 1
|
|
continue
|
|
}
|
|
info, _ := os.Stat(path)
|
|
state := "fresh: "
|
|
if updated {
|
|
state = "updated:"
|
|
}
|
|
fmt.Printf("%s %s -> %s (%s)\n", state, edition, path, info.ModTime().Format("2006-01-02"))
|
|
}
|
|
os.Exit(exitCode)
|
|
}
|