|
| 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 | +} |
0 commit comments