Last active
December 15, 2017 05:42
-
-
Save inotnako/dff2d2ac302df1f7a0844692218af250 to your computer and use it in GitHub Desktop.
auth_proxy.go
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 ( | |
"flag" | |
"net/http" | |
"net/http/httputil" | |
"net/url" | |
"strings" | |
) | |
var ( | |
direction = flag.String(`direction`, `https://getacorn.com/`, `set your url`) | |
addr = flag.String(`addr`, `localhost:8888`, `listen address`) | |
) | |
func singleJoiningSlash(a, b string) string { | |
aslash := strings.HasSuffix(a, "/") | |
bslash := strings.HasPrefix(b, "/") | |
switch { | |
case aslash && bslash: | |
return a + b[1:] | |
case !aslash && !bslash: | |
return a + "/" + b | |
} | |
return a + b | |
} | |
func main() { | |
flag.Parse() | |
target, err := url.Parse(*direction) | |
if err != nil { | |
panic(err) | |
} | |
targetQuery := target.RawQuery | |
proxy := &httputil.ReverseProxy{ | |
Director: func(req *http.Request) { | |
req.URL.Scheme = target.Scheme | |
req.URL.Host = target.Host | |
req.Host = target.Host | |
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path) | |
if targetQuery == "" || req.URL.RawQuery == "" { | |
req.URL.RawQuery = targetQuery + req.URL.RawQuery | |
} else { | |
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery | |
} | |
if _, ok := req.Header["User-Agent"]; !ok { | |
// explicitly disable User-Agent so it's not set to default value | |
req.Header.Set("User-Agent", "") | |
} | |
}, | |
ModifyResponse: func(resp *http.Response) error { | |
// here we can accept all and req / and resp | |
dumpReq, _ := httputil.DumpRequestOut(resp.Request, true) | |
var printBodyResponse bool | |
if strings.Contains(resp.Header.Get(`Content-Type`), `application/json`) { | |
printBodyResponse = true | |
} | |
dumpResp, _ := httputil.DumpResponse(resp, printBodyResponse) | |
println(`>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>`) | |
println(string(dumpReq)) | |
println(`<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<`) | |
println(string(dumpResp)) | |
return nil | |
}, | |
} | |
http.ListenAndServe(*addr, proxy) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
logs