Created
May 16, 2018 13:21
-
-
Save alikhil/adc538521ad42263e92b0c464bfa58cc to your computer and use it in GitHub Desktop.
Redirect to site using parameter from url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "log" | |
| "net/http" | |
| ) | |
| var destination string | |
| func redirect(w http.ResponseWriter, r *http.Request) { | |
| htmlString := ` | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Authenticator</title> | |
| </head> | |
| <body> | |
| <h1>Redirecting you...</h1> | |
| </body> | |
| <script> | |
| // https://stackoverflow.com/a/901144/452081 | |
| function getParameterByName(name, url) { | |
| if (!url) url = window.location.href; | |
| name = name.replace(/[\[\]]/g, "\\$&"); | |
| var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url); | |
| if (!results) return null; | |
| if (!results[2]) return ''; | |
| return decodeURIComponent(results[2].replace(/\+/g, " ")); | |
| } | |
| var redirect_url = getParameterByName("redirect", window.location.href) || ""; | |
| console.log("Redirection url: " + redirect_url); | |
| if(redirect_url.match('^https://([a-z0-9-\.]+\.)?YOURDOMAIN\.COM.*') ) window.location = redirect_url; | |
| </script> | |
| </html> | |
| ` | |
| w.Write([]byte(htmlString)) | |
| } | |
| func health(w http.ResponseWriter, r *http.Request) { | |
| w.Write([]byte{'o', 'k'}) | |
| } | |
| func main() { | |
| http.HandleFunc("/", redirect) | |
| http.HandleFunc("/health", health) | |
| err := http.ListenAndServe(":8080", nil) | |
| if err != nil { | |
| log.Fatal("ListenAndServe: ", err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment