This commit is contained in:
65
geoipapi.go
65
geoipapi.go
@@ -1,16 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/oschwald/geoip2-golang"
|
||||
"go.ntppool.org/common/logger"
|
||||
"go.ntppool.org/common/tracing"
|
||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type geoType uint8
|
||||
@@ -25,26 +34,39 @@ var dbFiles map[geoType][]string
|
||||
|
||||
func init() {
|
||||
dbFiles = map[geoType][]string{
|
||||
countryDB: []string{"GeoIP2-Country.mmdb", "GeoLite2-Country.mmdb"},
|
||||
asnDB: []string{"GeoIP2-ISP.mmdb"},
|
||||
cityDB: []string{"GeoIP2-City.mmdb", "GeoLite2-City.mmdb"},
|
||||
countryDB: {"GeoIP2-Country.mmdb", "GeoLite2-Country.mmdb"},
|
||||
asnDB: {"GeoIP2-ISP.mmdb"},
|
||||
cityDB: {"GeoIP2-City.mmdb", "GeoLite2-City.mmdb"},
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
|
||||
log := logger.FromContext(ctx)
|
||||
|
||||
tpShutdown, err := tracing.SetupSDK(ctx, &tracing.TracerConfig{
|
||||
ServiceName: "geoipapi",
|
||||
})
|
||||
if err != nil {
|
||||
log.ErrorContext(ctx, "could not setup tracing", "err", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
defer tpShutdown(context.Background()) // todo: with timeout
|
||||
|
||||
rdr, err := open(cityDB)
|
||||
if err != nil {
|
||||
log.Fatalf("opening db: %s", err)
|
||||
log.ErrorContext(ctx, "could not open geodb", "err", err)
|
||||
os.Exit(3)
|
||||
}
|
||||
|
||||
if len(os.Args) > 1 {
|
||||
for _, str := range os.Args[1:] {
|
||||
log.Printf("%q", str)
|
||||
ip := net.ParseIP(str)
|
||||
city, err := rdr.City(ip)
|
||||
if err != nil {
|
||||
log.Printf("error looking up %q: %s", ip, err)
|
||||
fmt.Printf("error looking up %q\n: %s", ip, err)
|
||||
continue
|
||||
}
|
||||
fmt.Printf("%s: %s\n", ip, city.Country.IsoCode)
|
||||
@@ -52,18 +74,38 @@ func main() {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
err = setupHTTP(rdr)
|
||||
err = setupHTTP(ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("http: %s", err)
|
||||
log.ErrorContext(ctx, "http server", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func setupHTTP(rdr *geoip2.Reader) error {
|
||||
func setupHTTP(ctx context.Context) error {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/api/country", handleCountry)
|
||||
mux.HandleFunc("/api/json", handleJSON)
|
||||
mux.HandleFunc("/healthz", handleHealth)
|
||||
return http.ListenAndServe(":8009", mux)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: ":8009",
|
||||
BaseContext: func(_ net.Listener) context.Context { return ctx },
|
||||
ReadTimeout: time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
Handler: otelhttp.NewHandler(mux, "geoipapi"),
|
||||
}
|
||||
srvErr := make(chan error, 1)
|
||||
go func() {
|
||||
srvErr <- srv.ListenAndServe()
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-srvErr:
|
||||
// Error when starting HTTP server.
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
}
|
||||
|
||||
return srv.Shutdown(context.Background())
|
||||
}
|
||||
|
||||
func getCityIP(ip net.IP) (*geoip2.City, error) {
|
||||
@@ -80,8 +122,10 @@ func getCityIP(ip net.IP) (*geoip2.City, error) {
|
||||
}
|
||||
|
||||
func getCity(req *http.Request) (*geoip2.City, error) {
|
||||
span := trace.SpanFromContext(req.Context())
|
||||
req.ParseForm()
|
||||
ipStr := req.FormValue("ip")
|
||||
span.SetAttributes(attribute.String("ip", ipStr))
|
||||
ip := net.ParseIP(ipStr)
|
||||
if ip == nil {
|
||||
return nil, fmt.Errorf("missing IP address")
|
||||
@@ -133,7 +177,6 @@ func handleHealth(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
func open(t geoType) (*geoip2.Reader, error) {
|
||||
|
||||
dir := findDB()
|
||||
|
||||
var fileName string
|
||||
|
Reference in New Issue
Block a user