Skip to content

Commit

Permalink
Make upstream URL configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrister committed Jul 10, 2018
1 parent 6f1fd17 commit 787626c
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/cookiejar"
"net/http/httputil"
"net/url"
"os"
"time"

"github.com/pkg/browser"
Expand All @@ -15,9 +16,19 @@ import (
func main() {
proxyAddr := "127.0.0.1:8124"

baseURL, err := url.Parse("https://SETME")
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr,
"Usage: %s UPSTREAM_URL\n"+
"Example: %s https://proxy-dev.example.com\n\n"+
"Note: The oauth2_proxy running at UPSTREAM_URL must be configured with a\n"+
" special redirect URL for this development proxy.\n",
os.Args[0], os.Args[0])
os.Exit(1)
}

upstreamURL, err := url.Parse(os.Args[1])
if err != nil {
log.Fatalf("Failed to parse baseURL: %v", err)
log.Fatalf("Failed to parse target URL: %v", err)
}

// We don't need a public suffix list for cookies - we only make requests
Expand All @@ -38,8 +49,8 @@ func main() {
serverMux := http.NewServeMux()
serverMux.HandleFunc("/oauth2/callback", func(w http.ResponseWriter, r *http.Request) {
realCallbackURL, _ := r.URL.Parse(r.URL.String())
realCallbackURL.Scheme = baseURL.Scheme
realCallbackURL.Host = baseURL.Host
realCallbackURL.Scheme = upstreamURL.Scheme
realCallbackURL.Host = upstreamURL.Host
// realCallbackURL.Path = "/oauth2/callback"

resp, err := client.Get(realCallbackURL.String())
Expand All @@ -57,9 +68,9 @@ func main() {
}

reverseProxyDirector := func(r *http.Request) {
r.Host = baseURL.Host
r.URL.Scheme = baseURL.Scheme
r.URL.Host = baseURL.Host
r.Host = upstreamURL.Host
r.URL.Scheme = upstreamURL.Scheme
r.URL.Host = upstreamURL.Host

r.AddCookie(authCookie)

Expand All @@ -76,8 +87,9 @@ func main() {
}
go proxyServer.ListenAndServe()

authCompleteText := fmt.Sprintf("Authentication complete. You can now use the proxy at %s\n\n"+
"Upstream: %s\n\n", proxyAddr, baseURL)
authCompleteText := fmt.Sprintf("Authentication complete. You can now use the proxy at "+
"http://%s\n\n"+
"Upstream: %s\n\n", proxyAddr, upstreamURL)

w.Write([]byte(authCompleteText + "You can close this window."))

Expand All @@ -92,7 +104,7 @@ func main() {
// TODO check server is actually running to prevent sending auth code to another app
go server.ListenAndServe()

resp, err := client.Get(baseURL.String() + "/oauth2/start")
resp, err := client.Get(upstreamURL.String() + "/oauth2/start")
if err != nil {
log.Fatalf("Request failed: %v", err)
}
Expand Down

0 comments on commit 787626c

Please sign in to comment.