Skip to content

Commit

Permalink
Add first hacky version
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrister committed Jul 10, 2018
1 parent b43a64b commit e9a5f9c
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"fmt"
"log"
"net/http"
"net/http/cookiejar"
"net/http/httputil"
"net/url"
"time"

"golang.org/x/net/publicsuffix"
)

//https://github.com/pkg/browser

func main() {
proxyAddr := ":8124"

baseURL, err := url.Parse("https://SETME")
if err != nil {
log.Fatalf("Failed to parse baseURL: %v", err)
}

jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
log.Fatalf("Failed to create cookie jar: %v", err)
}

client := http.Client{
Jar: jar,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}

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.Path = "/oauth2/callback"

resp, err := client.Get(realCallbackURL.String())
if err != nil {
log.Fatalf("Failed to fetch callback URL: %v", err)
http.Error(w, "Failed to fetch callback URL", http.StatusInternalServerError)
return
}

var authCookie *http.Cookie
for _, c := range resp.Cookies() {
if c.Name == "_oauth2_proxy" {
authCookie = c
}
}

w.Write([]byte(fmt.Sprintf(
"Authentication complete. You can now use the proxy at %s\n\n"+
"Upstream: %s\n\n"+
"You can close this window.",
proxyAddr, baseURL)))
log.Printf("Cookie: %+v", authCookie)
log.Printf("Response: %+v", resp)

reverseProxyDirector := func(r *http.Request) {
log.Printf("Original request: %+v", r)
r.Host = baseURL.Host
r.URL.Scheme = baseURL.Scheme
r.URL.Host = baseURL.Host

r.AddCookie(authCookie)

log.Printf("Forwarding request: %+v", r)
log.Printf("Request URI: %v", r.RequestURI)
}

proxyHandler := &httputil.ReverseProxy{
Director: reverseProxyDirector,
}

proxyServer := &http.Server{
Addr: proxyAddr,
Handler: proxyHandler,
}
go proxyServer.ListenAndServe()

})

server := &http.Server{
Addr: ":8123",
Handler: serverMux,
}
go server.ListenAndServe()

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

identityProviderURL := resp.Header.Get("Location")
if identityProviderURL == "" {
log.Fatalf("Got empty identity provider URL in response: %+v", resp)
}

log.Printf("Go to %s", identityProviderURL)
time.Sleep(1 * time.Hour)
}

0 comments on commit e9a5f9c

Please sign in to comment.