aboutsummaryrefslogtreecommitdiff
path: root/pkg/atlas/server.go
diff options
context:
space:
mode:
authorpg9182 <96569817+pg9182@users.noreply.github.com>2022-12-12 16:17:41 -0500
committerpg9182 <96569817+pg9182@users.noreply.github.com>2022-12-12 16:32:52 -0500
commite8cea8d07de5caf16f6d0e2e8ec3724b03de1d6a (patch)
treeb9a9c65abbe6f89352b7eaf23e74e36442308a3f /pkg/atlas/server.go
parent12855f7d83c461cd54e021d276df3da2d471d14d (diff)
downloadAtlas-e8cea8d07de5caf16f6d0e2e8ec3724b03de1d6a.tar.gz
Atlas-e8cea8d07de5caf16f6d0e2e8ec3724b03de1d6a.zip
pkg/atlas: Support region overrides for IP prefixes
Diffstat (limited to 'pkg/atlas/server.go')
-rw-r--r--pkg/atlas/server.go46
1 files changed, 43 insertions, 3 deletions
diff --git a/pkg/atlas/server.go b/pkg/atlas/server.go
index fe52441..e780b87 100644
--- a/pkg/atlas/server.go
+++ b/pkg/atlas/server.go
@@ -693,15 +693,55 @@ func configureIP2Location(c *Config) (*ip2xMgr, error) {
return mgr, mgr.Load(c.IP2Location)
}
-func configureRegionMap(c *Config) (func(netip.Addr, ip2x.Record) (string, error), error) {
+func configureRegionMap(c *Config) (fn func(netip.Addr, ip2x.Record) (string, error), err error) {
switch m := c.API0_RegionMap; m {
case "", "none":
- return nil, nil
+ fn = nil
case "default":
- return regionmap.GetRegion, nil
+ fn = regionmap.GetRegion
default:
return nil, fmt.Errorf("unknown region map type %q", m)
}
+ if len(c.API0_RegionMap_Override) != 0 {
+ type regionMapOverride struct {
+ Prefix netip.Prefix
+ Region string
+ }
+ var mos []regionMapOverride
+ for _, x := range c.API0_RegionMap_Override {
+ a, r, ok := strings.Cut(x, "=")
+ if !ok {
+ return nil, fmt.Errorf("parse region override %q: missing equals sign", x)
+ }
+ if strings.ContainsRune(a, '/') {
+ if pfx, err := netip.ParsePrefix(a); err == nil {
+ mos = append(mos, regionMapOverride{pfx, r})
+ } else {
+ return nil, fmt.Errorf("parse region override %q: invalid prefix: %w", x, err)
+ }
+ } else {
+ if x, err := netip.ParseAddr(a); err == nil {
+ if pfx, err := x.Prefix(x.BitLen()); err == nil {
+ mos = append(mos, regionMapOverride{pfx, r})
+ } else {
+ panic(err)
+ }
+ } else {
+ return nil, fmt.Errorf("parse region override %q: invalid prefix: %w", x, err)
+ }
+ }
+ }
+ next := fn
+ fn = func(a netip.Addr, r ip2x.Record) (string, error) {
+ for _, mo := range mos {
+ if mo.Prefix.Contains(a) {
+ return mo.Region, nil
+ }
+ }
+ return next(a, r)
+ }
+ }
+ return
}
// Run runs the server, shutting it down gracefully when ctx is canceled, then