Added /mapping entry point.

This commit is contained in:
Yifu Yu 2019-10-27 16:56:09 +08:00
parent 9fb539eb91
commit 8f908cadd4
1 changed files with 16 additions and 0 deletions

16
main.go
View File

@ -107,6 +107,22 @@ func main() {
fmt.Fprintln(resp, "Allocated CDN node for you:", server) fmt.Fprintln(resp, "Allocated CDN node for you:", server)
fmt.Fprintln(resp) 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() ipgeo.Init()
http.ListenAndServe("0.0.0.0:8086", nil) http.ListenAndServe("0.0.0.0:8086", nil)
} }