From 8f908cadd40fef069f594b65e2382bd7edd9ce05 Mon Sep 17 00:00:00 2001 From: Yifu Yu Date: Sun, 27 Oct 2019 16:56:09 +0800 Subject: [PATCH] Added /mapping entry point. --- main.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/main.go b/main.go index b85afdd..f9479b9 100644 --- a/main.go +++ b/main.go @@ -107,6 +107,22 @@ func main() { fmt.Fprintln(resp, "Allocated CDN node for you:", server) fmt.Fprintln(resp) }) + + http.HandleFunc("/mapping", func(resp http.ResponseWriter, req *http.Request) { + host, _, err := net.SplitHostPort(req.RemoteAddr) + if err != nil { + responseWithError(resp, http.StatusPreconditionFailed, errIPv4Only) + return + } + if net.ParseIP(host).To4() == nil { + responseWithError(resp, http.StatusPreconditionFailed, errIPv4Only) + return + } + resp.Header().Set("Content-Type", "text/plain") + resp.WriteHeader(http.StatusOK) + server := mapping.Get(host) + fmt.Fprint(resp, server) + }) ipgeo.Init() http.ListenAndServe("0.0.0.0:8086", nil) }