From db98de94f47a831334a82d2044d08ebb2274e8d9 Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Thu, 20 Jan 2022 23:47:36 +0100 Subject: fail gracefully if error getting the ip record --- main.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index f7914da..a66da4b 100644 --- a/main.go +++ b/main.go @@ -138,13 +138,13 @@ func (g *geodb) geolocateGateways(b *bonafide) { g.GatewayTree = kdtree.NewKDTree(gatewayPoints) } -func (g *geodb) getRecordForIP(ipstr string) *geoip2.City { +func (g *geodb) getRecordForIP(ipstr string) (*geoip2.City, error) { ip := net.ParseIP(ipstr) record, err := g.db.City(ip) if err != nil { - log.Fatal(err) + return record, err } - return record + return record, nil } func geolocateCity(city string) coordinates { @@ -181,11 +181,12 @@ type GeolocationJSON struct { func (jh *jsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { ipstr := getRemoteIP(req) - record := jh.geoipdb.getRecordForIP(ipstr) - sortedGateways := jh.geoipdb.sortGateways(record.Location.Latitude, record.Location.Longitude) - - hitsPerCountry.With(prometheus.Labels{"country": record.Country.IsoCode}).Inc() - + record, err := jh.geoipdb.getRecordForIP(ipstr) + sortedGateways := []string{""} + if err != nil { + sortedGateways = jh.geoipdb.sortGateways(record.Location.Latitude, record.Location.Longitude) + hitsPerCountry.With(prometheus.Labels{"country": record.Country.IsoCode}).Inc() + } data := &GeolocationJSON{ ipstr, record.Country.IsoCode, @@ -205,7 +206,7 @@ type txtHandler struct { func (th *txtHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { ipstr := getRemoteIP(req) - record := th.geoipdb.getRecordForIP(ipstr) + record, _ := th.geoipdb.getRecordForIP(ipstr) fmt.Fprintf(w, "Your IP: %s\n", ipstr) fmt.Fprintf(w, "Your Country: %s\n", record.Country.IsoCode) -- cgit v1.2.3