Skip to content

Commit 2cc77e4

Browse files
committed
Add bunny.net
1 parent 31c8b1f commit 2cc77e4

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Server that determines your physical location by looking at headers sent from va
44

55
Try it with:
66
[AWS CloudFront](https://aws-geo.redirect2.me/)
7+
| [Bunny.net](https://bunny-geo.redirect2.me/)
78
| [Cloudflare](https://cf-geo.redirect2.me/)
89
| [Fastly](https://cdn-geo.global.ssl.fastly.net/)
910
| [Google AppEngine](https://ae-geo.redirect2.me/)
@@ -35,6 +36,12 @@ Good luck!
3536
</details>
3637

3738
<details>
39+
<details>
40+
<summary>Bunny.net</summary>
41+
42+
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.
43+
44+
</details>
3845
<summary>Cloudflare</summary>
3946

4047
[Website](https://www.cloudflare.com/) |
@@ -88,6 +95,7 @@ Send a `callback` parameter to get JSONP instead of JSON.
8895
## Credits
8996

9097
[![AWS](https://www.vectorlogo.zone/logos/amazon_aws/amazon_aws-ar21.svg)](https://aws.amazon.com/ "CDN and Geolocation")
98+
[![Bunny.net](https://www.vectorlogo.zone/logos/bunnynet/bunnynet-ar21.svg)](https://www.bunny.net/ "CDN and Geolocation")
9199
[![Cloudflare](https://www.vectorlogo.zone/logos/cloudflare/cloudflare-ar21.svg)](https://www.cloudflare.com/ "CDN and Geolocation")
92100
[![Fastly](https://www.vectorlogo.zone/logos/fastly/fastly-ar21.svg)](https://www.fastly.com/ "CDN")
93101
[![Git](https://www.vectorlogo.zone/logos/git-scm/git-scm-ar21.svg)](https://git-scm.com/ "Version control")

bunny.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package main
2+
3+
import (
4+
// "bytes"
5+
// "context"
6+
// "encoding/json"
7+
8+
"fmt"
9+
"html"
10+
"time"
11+
12+
// "io/ioutil"
13+
14+
// "net"
15+
"net/http"
16+
// "net/url"
17+
// "strings"
18+
)
19+
20+
func bunnyRootHandler(w http.ResponseWriter, r *http.Request) {
21+
if r.URL.Path[1:] == "" {
22+
w.Header().Set("Content-Type", "text/html; charset=utf8")
23+
w.Write([]byte(`<html>
24+
<head>
25+
<meta charset="utf-8">
26+
<title>Bunny.net Geolocation - Resolve.rs</title>
27+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
28+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/light.min.css" />
29+
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
30+
</head>
31+
<body>
32+
<h1>
33+
<img alt="Resolve.rs geolocation logo" src="favicon.svg" style="height:2.2em;vertical-align:middle;" />
34+
Bunny.net Geolocation
35+
</h1>
36+
<p>
37+
Determine your real (physical) location based on your IP address, powered by Bunny.net.
38+
</p>
39+
<p>
40+
Your IP address:`))
41+
42+
fmt.Fprintf(w, "%s", getIpAddress(r))
43+
fmt.Fprintf(w, "</p><p>")
44+
45+
fmt.Fprintf(w, "State: %s<br/>", html.EscapeString(getHeader(r, "CDN-requeststatecode", "(none)")))
46+
fmt.Fprintf(w, "Country: %s<br/>", html.EscapeString(getHeader(r, "CDN-requestcountrycode", "(none)")))
47+
48+
w.Write([]byte(`</p>
49+
<p>
50+
<a href="https://github.com/redirect2me/cdn-geolocation">How this works</a>, including API details and source code!
51+
</p>
52+
<p>
53+
<a href="https://resolve.rs/">Resolve.rs</a>
54+
has more
55+
<a href="https://resolve.rs/tools.html">diagnostic tools</a>.
56+
including a
57+
<a href="https://resolve.rs/ip/geolocation.html">comparison of different geolocation APIs</a>.
58+
</p>
59+
</body>
60+
</html>`))
61+
} else {
62+
http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
63+
}
64+
}
65+
66+
type bunnyApiResponse struct {
67+
Success bool `json:"success"`
68+
Message string `json:"message"`
69+
Timestamp string `json:"timestamp"`
70+
IpAddress string `json:"ip"`
71+
Country string `json:"country"`
72+
State string `json:"state"`
73+
ServerZone string `json:"serverzone"`
74+
Raw map[string]string `json:"raw"`
75+
}
76+
77+
func bunnyApiHandler(w http.ResponseWriter, r *http.Request) {
78+
result := bunnyApiResponse{}
79+
result.Timestamp = time.Now().UTC().Format(time.RFC3339)
80+
result.IpAddress = getIpAddress(r)
81+
result.Raw = getFlatHeaders(r, "Cdn-")
82+
83+
result.Success = true
84+
result.Message = "Free for light, non-commercial use"
85+
result.Country = getHeader(r, "Cdn-Requestcountrycode", "(not set)")
86+
result.State = getHeader(r, "Cdn-Requeststatecode", "(not set)")
87+
result.ServerZone = getHeader(r, "Cdn-Serverzone", "(not set)")
88+
89+
write_with_callback(w, r, result)
90+
}

run.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
export LASTMOD=$(date -u)
44
export COMMIT=local
55

6-
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
6+
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

server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
aeHostname = flag.String("aehost", "ae-geo.redirect2.me", "hostname for AppEngine")
2222
cfHostname = flag.String("cfhost", "cf-geo.redirect2.me", "hostname for Cloudflare")
2323
awsHostname = flag.String("awshost", "origin-aws-geo.redirect2.me", "origin hostname for AWS CloudFront (not the actual hostname)")
24+
bunnyHostname = flag.String("bunnyhost", "origin-bunny-geo.redirect2.me", "origin hostname for Bunny.net (not the actual hostname)")
2425
fastlyHostname = flag.String("fastlyhost", "cdn-geo.global.ssl.fastly.net", "hostname for Fastly")
2526

2627
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) {
6970
fastlyRootHandler(w, r)
7071
} else if host == *awsHostname {
7172
awsRootHandler(w, r)
73+
} else if host == *bunnyHostname {
74+
bunnyRootHandler(w, r)
7275
} else {
7376
logger.Printf("WARN: unknown host '%s'\n", host)
7477
http.Redirect(w, r, "https://github.com/redirect2me/cdn-geolocation", http.StatusTemporaryRedirect)
@@ -101,6 +104,7 @@ func main() {
101104
http.HandleFunc("/headers.html", headersHandler)
102105

103106
http.HandleFunc("/api/appengine.json", appengineApiHandler)
107+
http.HandleFunc("/api/bunny.json", bunnyApiHandler)
104108
http.HandleFunc("/api/cloudflare.json", cloudflareApiHandler)
105109
http.HandleFunc("/api/fastly.json", fastlyApiHandler)
106110
http.HandleFunc("/api/aws.json", awsApiHandler)

0 commit comments

Comments
 (0)