diff --git a/README.md b/README.md index ad5ca72..58d148c 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Server that determines your physical location by looking at headers sent from va Try it with: [AWS CloudFront](https://aws-geo.redirect2.me/) +| [Bunny.net](https://bunny-geo.redirect2.me/) | [Cloudflare](https://cf-geo.redirect2.me/) | [Fastly](https://cdn-geo.global.ssl.fastly.net/) | [Google AppEngine](https://ae-geo.redirect2.me/) @@ -35,6 +36,12 @@ Good luck!
+
+Bunny.net + +Very easy to setup: Add a [pull zone](https://dash.bunny.net/cdn/add) pointing to your server. Bunny.net fills in headers for state and country. + +
Cloudflare [Website](https://www.cloudflare.com/) | @@ -88,6 +95,7 @@ Send a `callback` parameter to get JSONP instead of JSON. ## Credits [![AWS](https://www.vectorlogo.zone/logos/amazon_aws/amazon_aws-ar21.svg)](https://aws.amazon.com/ "CDN and Geolocation") +[![Bunny.net](https://www.vectorlogo.zone/logos/bunnynet/bunnynet-ar21.svg)](https://www.bunny.net/ "CDN and Geolocation") [![Cloudflare](https://www.vectorlogo.zone/logos/cloudflare/cloudflare-ar21.svg)](https://www.cloudflare.com/ "CDN and Geolocation") [![Fastly](https://www.vectorlogo.zone/logos/fastly/fastly-ar21.svg)](https://www.fastly.com/ "CDN") [![Git](https://www.vectorlogo.zone/logos/git-scm/git-scm-ar21.svg)](https://git-scm.com/ "Version control") diff --git a/bunny.go b/bunny.go new file mode 100644 index 0000000..232ccc5 --- /dev/null +++ b/bunny.go @@ -0,0 +1,90 @@ +package main + +import ( + // "bytes" + // "context" + // "encoding/json" + + "fmt" + "html" + "time" + + // "io/ioutil" + + // "net" + "net/http" + // "net/url" + // "strings" +) + +func bunnyRootHandler(w http.ResponseWriter, r *http.Request) { + if r.URL.Path[1:] == "" { + w.Header().Set("Content-Type", "text/html; charset=utf8") + w.Write([]byte(` + + + Bunny.net Geolocation - Resolve.rs + + + + + +

+ Resolve.rs geolocation logo + Bunny.net Geolocation +

+

+ Determine your real (physical) location based on your IP address, powered by Bunny.net. +

+

+ Your IP address:`)) + + fmt.Fprintf(w, "%s", getIpAddress(r)) + fmt.Fprintf(w, "

") + + fmt.Fprintf(w, "State: %s
", html.EscapeString(getHeader(r, "CDN-requeststatecode", "(none)"))) + fmt.Fprintf(w, "Country: %s
", html.EscapeString(getHeader(r, "CDN-requestcountrycode", "(none)"))) + + w.Write([]byte(`

+

+ How this works, including API details and source code! +

+

+ Resolve.rs + has more + diagnostic tools. + including a + comparison of different geolocation APIs. +

+ +`)) + } else { + http.Redirect(w, r, "/", http.StatusTemporaryRedirect) + } +} + +type bunnyApiResponse struct { + Success bool `json:"success"` + Message string `json:"message"` + Timestamp string `json:"timestamp"` + IpAddress string `json:"ip"` + Country string `json:"country"` + State string `json:"state"` + ServerZone string `json:"serverzone"` + Raw map[string]string `json:"raw"` +} + +func bunnyApiHandler(w http.ResponseWriter, r *http.Request) { + result := bunnyApiResponse{} + result.Timestamp = time.Now().UTC().Format(time.RFC3339) + result.IpAddress = getIpAddress(r) + result.Raw = getFlatHeaders(r, "Cdn-") + + result.Success = true + result.Message = "Free for light, non-commercial use" + result.Country = getHeader(r, "Cdn-Requestcountrycode", "(not set)") + result.State = getHeader(r, "Cdn-Requeststatecode", "(not set)") + result.ServerZone = getHeader(r, "Cdn-Serverzone", "(not set)") + + write_with_callback(w, r, result) +} diff --git a/run.sh b/run.sh index 4dba82e..511e3b5 100755 --- a/run.sh +++ b/run.sh @@ -3,4 +3,4 @@ export LASTMOD=$(date -u) export COMMIT=local -go run server.go appengine.go aws.go cloudflare.go fastly.go faviconIco.go headers.go jsonp.go status.go util.go --port=4000 --verbose --awshost=localhost:4000 +go run server.go appengine.go aws.go bunny.go cloudflare.go fastly.go faviconIco.go headers.go jsonp.go status.go util.go --port=4000 --verbose --awshost=localhost:4000 diff --git a/server.go b/server.go index 3e81fb4..92520cc 100644 --- a/server.go +++ b/server.go @@ -21,6 +21,7 @@ var ( aeHostname = flag.String("aehost", "ae-geo.redirect2.me", "hostname for AppEngine") cfHostname = flag.String("cfhost", "cf-geo.redirect2.me", "hostname for Cloudflare") awsHostname = flag.String("awshost", "origin-aws-geo.redirect2.me", "origin hostname for AWS CloudFront (not the actual hostname)") + bunnyHostname = flag.String("bunnyhost", "origin-bunny-geo.redirect2.me", "origin hostname for Bunny.net (not the actual hostname)") fastlyHostname = flag.String("fastlyhost", "cdn-geo.global.ssl.fastly.net", "hostname for Fastly") logger = log.New(os.Stdout, "R2ME-GEO: ", log.Ldate|log.Ltime|log.Lmicroseconds|log.LUTC) @@ -69,6 +70,8 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { fastlyRootHandler(w, r) } else if host == *awsHostname { awsRootHandler(w, r) + } else if host == *bunnyHostname { + bunnyRootHandler(w, r) } else { logger.Printf("WARN: unknown host '%s'\n", host) http.Redirect(w, r, "https://github.com/redirect2me/cdn-geolocation", http.StatusTemporaryRedirect) @@ -101,6 +104,7 @@ func main() { http.HandleFunc("/headers.html", headersHandler) http.HandleFunc("/api/appengine.json", appengineApiHandler) + http.HandleFunc("/api/bunny.json", bunnyApiHandler) http.HandleFunc("/api/cloudflare.json", cloudflareApiHandler) http.HandleFunc("/api/fastly.json", fastlyApiHandler) http.HandleFunc("/api/aws.json", awsApiHandler)