diff options
author | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-11-21 05:01:47 -0500 |
---|---|---|
committer | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-11-21 05:10:56 -0500 |
commit | 4ba75a41edb9f80599bdb99352df2ca47faa1f06 (patch) | |
tree | 5c9bcb784d5c92c32eef7b007999c76e083b1718 /pkg/atlas | |
parent | b3d1c42cf268153831494e8b7b854c621f015497 (diff) | |
download | Atlas-4ba75a41edb9f80599bdb99352df2ca47faa1f06.tar.gz Atlas-4ba75a41edb9f80599bdb99352df2ca47faa1f06.zip |
all: Upgrade github.com/pg9182/ip2x to v0.1.1
This is a larger update since I rewrote ip2x.
Diffstat (limited to 'pkg/atlas')
-rw-r--r-- | pkg/atlas/server.go | 8 | ||||
-rw-r--r-- | pkg/atlas/util.go | 25 |
2 files changed, 19 insertions, 14 deletions
diff --git a/pkg/atlas/server.go b/pkg/atlas/server.go index 7b25dad..55f1b5c 100644 --- a/pkg/atlas/server.go +++ b/pkg/atlas/server.go @@ -21,7 +21,7 @@ import ( "github.com/VictoriaMetrics/metrics" "github.com/klauspost/compress/gzip" - "github.com/pg9182/ip2x/ip2location" + "github.com/pg9182/ip2x" "github.com/r2northstar/atlas/db/atlasdb" "github.com/r2northstar/atlas/db/pdatadb" "github.com/r2northstar/atlas/pkg/api/api0" @@ -630,15 +630,15 @@ func configureMainMenuPromos(c *Config) (func(*http.Request) api0.MainMenuPromos } } -func configureIP2Location(c *Config) (*ip2locationMgr, error) { +func configureIP2Location(c *Config) (*ip2xMgr, error) { if c.IP2Location == "" { return nil, nil } - mgr := new(ip2locationMgr) + mgr := new(ip2xMgr) return mgr, mgr.Load(c.IP2Location) } -func configureRegionMap(c *Config) (func(netip.Addr, ip2location.Record) (string, error), error) { +func configureRegionMap(c *Config) (func(netip.Addr, ip2x.Record) (string, error), error) { switch m := c.API0_RegionMap; m { case "", "none": return nil, nil diff --git a/pkg/atlas/util.go b/pkg/atlas/util.go index d5f71e1..7ea8b14 100644 --- a/pkg/atlas/util.go +++ b/pkg/atlas/util.go @@ -8,20 +8,20 @@ import ( "os" "sync" - "github.com/pg9182/ip2x/ip2location" + "github.com/pg9182/ip2x" "github.com/rs/zerolog" ) -// ip2locationMgr wraps a file-backed IP2Location database. -type ip2locationMgr struct { +// ip2xMgr wraps a file-backed IP2Location database. +type ip2xMgr struct { file *os.File - db *ip2location.DB + db *ip2x.DB mu sync.RWMutex } // Load replaces the currently loaded database with the specified file. If name // is empty, the existing database, if any, is reopened. -func (m *ip2locationMgr) Load(name string) error { +func (m *ip2xMgr) Load(name string) error { if name == "" { m.mu.RLock() if m.file == nil { @@ -36,12 +36,17 @@ func (m *ip2locationMgr) Load(name string) error { return err } - db, err := ip2location.New(f) + db, err := ip2x.New(f) if err != nil { f.Close() return err } + if p, _ := db.Info(); p != ip2x.IP2Location { + f.Close() + return fmt.Errorf("not an ip2location database") + } + m.mu.Lock() defer m.mu.Unlock() @@ -51,14 +56,14 @@ func (m *ip2locationMgr) Load(name string) error { return nil } -// LookupFields calls (*ip2location.DB).LookupFields if a database is loaded. -func (m *ip2locationMgr) LookupFields(ip netip.Addr, mask ip2location.Field) (ip2location.Record, error) { +// Lookup calls [ip2x.DB.Lookup] if a database is loaded. +func (m *ip2xMgr) LookupFields(ip netip.Addr) (ip2x.Record, error) { m.mu.RLock() defer m.mu.RUnlock() if m.db == nil { - return ip2location.Record{}, fmt.Errorf("no ip2location database loaded") + return ip2x.Record{}, fmt.Errorf("no ip2location database loaded") } - return m.db.LookupFields(ip, mask) + return m.db.Lookup(ip) } type zerologWriterLevel struct { |