Skip to content

Commit

Permalink
Add bunny.net
Browse files Browse the repository at this point in the history
  • Loading branch information
fileformat committed Nov 8, 2024
1 parent 31c8b1f commit 2cc77e4
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down Expand Up @@ -35,6 +36,12 @@ Good luck!
</details>

<details>
<details>
<summary>Bunny.net</summary>

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.

</details>
<summary>Cloudflare</summary>

[Website](https://www.cloudflare.com/) |
Expand Down Expand Up @@ -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")
Expand Down
90 changes: 90 additions & 0 deletions bunny.go
Original file line number Diff line number Diff line change
@@ -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(`<html>
<head>
<meta charset="utf-8">
<title>Bunny.net Geolocation - Resolve.rs</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/light.min.css" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
</head>
<body>
<h1>
<img alt="Resolve.rs geolocation logo" src="favicon.svg" style="height:2.2em;vertical-align:middle;" />
Bunny.net Geolocation
</h1>
<p>
Determine your real (physical) location based on your IP address, powered by Bunny.net.
</p>
<p>
Your IP address:`))

fmt.Fprintf(w, "%s", getIpAddress(r))
fmt.Fprintf(w, "</p><p>")

fmt.Fprintf(w, "State: %s<br/>", html.EscapeString(getHeader(r, "CDN-requeststatecode", "(none)")))
fmt.Fprintf(w, "Country: %s<br/>", html.EscapeString(getHeader(r, "CDN-requestcountrycode", "(none)")))

w.Write([]byte(`</p>
<p>
<a href="https://github.com/redirect2me/cdn-geolocation">How this works</a>, including API details and source code!
</p>
<p>
<a href="https://resolve.rs/">Resolve.rs</a>
has more
<a href="https://resolve.rs/tools.html">diagnostic tools</a>.
including a
<a href="https://resolve.rs/ip/geolocation.html">comparison of different geolocation APIs</a>.
</p>
</body>
</html>`))
} 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)
}
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2cc77e4

Please sign in to comment.