diff options
author | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-11-20 02:43:40 -0500 |
---|---|---|
committer | pg9182 <96569817+pg9182@users.noreply.github.com> | 2022-11-20 06:12:13 -0500 |
commit | 250fb6e6505d2ac91e14f88edc4975f61e6da62a (patch) | |
tree | 1ce621af32b43b66c213bb79e8a4941ca24c593c /pkg/api/api0 | |
parent | ff52bfe36e9c28fdc227bf68192777a9c655be08 (diff) | |
download | Atlas-250fb6e6505d2ac91e14f88edc4975f61e6da62a.tar.gz Atlas-250fb6e6505d2ac91e14f88edc4975f61e6da62a.zip |
pkg/api/api0: Set server region field from IP on create/update
Diffstat (limited to 'pkg/api/api0')
-rw-r--r-- | pkg/api/api0/metrics.go | 6 | ||||
-rw-r--r-- | pkg/api/api0/server.go | 28 |
2 files changed, 33 insertions, 1 deletions
diff --git a/pkg/api/api0/metrics.go b/pkg/api/api0/metrics.go index d9fa16d..57e3b0a 100644 --- a/pkg/api/api0/metrics.go +++ b/pkg/api/api0/metrics.go @@ -134,7 +134,9 @@ type apiMetrics struct { success *metrics.Histogram failure *metrics.Histogram } - server_remove_requests_total struct { + server_upsert_ip2location_errors_total *metrics.Counter + server_upsert_getregion_errors_total *metrics.Counter + server_remove_requests_total struct { success *metrics.Counter reject_unauthorized_ip *metrics.Counter reject_bad_request *metrics.Counter @@ -390,6 +392,8 @@ func (h *Handler) m() *apiMetrics { } mo.server_upsert_verify_time_seconds.success = mo.set.NewHistogram(`atlas_api0_server_upsert_verify_time_seconds{success="true"}`) mo.server_upsert_verify_time_seconds.failure = mo.set.NewHistogram(`atlas_api0_server_upsert_verify_time_seconds{success="false"}`) + mo.server_upsert_ip2location_errors_total = mo.set.NewCounter(`atlas_api0_server_upsert_ip2location_errors_total`) + mo.server_upsert_getregion_errors_total = mo.set.NewCounter(`atlas_api0_server_upsert_getregion_errors_total`) mo.server_remove_requests_total.success = mo.set.NewCounter(`atlas_api0_server_remove_requests_total{result="success"}`) mo.server_remove_requests_total.reject_unauthorized_ip = mo.set.NewCounter(`atlas_api0_server_remove_requests_total{result="reject_unauthorized_ip"}`) mo.server_remove_requests_total.reject_bad_request = mo.set.NewCounter(`atlas_api0_server_remove_requests_total{result="reject_bad_request"}`) diff --git a/pkg/api/api0/server.go b/pkg/api/api0/server.go index 0edccee..9daf2aa 100644 --- a/pkg/api/api0/server.go +++ b/pkg/api/api0/server.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/pg9182/ip2x/ip2location" "github.com/r2northstar/atlas/pkg/a2s" "github.com/r2northstar/atlas/pkg/api/api0/api0gameserver" "github.com/rs/zerolog/hlog" @@ -159,6 +160,33 @@ func (h *Handler) handleServerUpsert(w http.ResponseWriter, r *http.Request) { } if canCreate || canUpdate { + if h.LookupIP != nil && h.GetRegion != nil { + if rec, err := h.LookupIP(raddr.Addr(), ip2location.CountryLong|ip2location.CountryShort|ip2location.Region|ip2location.City|ip2location.Latitude|ip2location.Longitude); err == nil { + region, err := h.GetRegion(raddr.Addr(), rec) + if err == nil || region != "" { // if an error occurs, we may still have a best-effort region + if canCreate { + s.Region = region + } + if canUpdate { + u.Region = ®ion + } + } + if err != nil { + h.m().server_upsert_getregion_errors_total.Inc() + if region == "" { + hlog.FromRequest(r).Err(err).Str("ip", raddr.Addr().String()).Msg("failed to compute region, no best-effort region available") + } else { + hlog.FromRequest(r).Err(err).Str("ip", raddr.Addr().String()).Msgf("failed to compute region, using best-effort region %q", region) + } + } + } else { + h.m().server_upsert_ip2location_errors_total.Inc() + hlog.FromRequest(r).Err(err).Str("ip", raddr.Addr().String()).Msg("failed to lookup remote ip in ip2location database") + } + } + } + + if canCreate || canUpdate { if v := q.Get("name"); v == "" { if isCreate { h.m().server_upsert_requests_total.reject_bad_request(action).Inc() |