Created
July 14, 2016 15:09
-
-
Save bancek/9153a81623183fe80694001a06fb0bc3 to your computer and use it in GitHub Desktop.
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 ( | |
"crypto/tls" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
func main() { | |
tlsConfig := &tls.Config{ | |
InsecureSkipVerify: true, | |
} | |
transport := &http.Transport{ | |
TLSClientConfig: tlsConfig, | |
DisableCompression: true, | |
Proxy: http.ProxyFromEnvironment, | |
} | |
proxy := func(w http.ResponseWriter, r *http.Request) { | |
log.Printf("%s %s", r.Method, r.RequestURI) | |
log.Print(r) | |
r.URL.Scheme = "https" | |
r.URL.Host = "REALHOSTHERE" | |
if r.Header.Get("Authorization") == "Basic TESTAUTHHERE" { | |
r.Header.Set("Authorization", "Basic REALAUTHHERE") | |
} | |
r.Proto = "HTTP/1.1" | |
r.ProtoMajor = 1 | |
r.ProtoMinor = 1 | |
r.Close = true | |
r.Header.Del("Connection") | |
r.Header.Del("Transfer-Encoding") | |
res, err := transport.RoundTrip(r) | |
if err != nil { | |
log.Print(err) | |
w.WriteHeader(http.StatusInternalServerError) | |
return | |
} | |
body, err := ioutil.ReadAll(res.Body) | |
if err != nil { | |
log.Print(err) | |
w.WriteHeader(http.StatusInternalServerError) | |
return | |
} | |
// change body | |
res.Header.Del("Connection") | |
res.Header.Del("Transfer-Encoding") | |
res.Header.Del("Connection") | |
res.Header.Del("Transfer-Encoding") | |
for k, vv := range res.Header { | |
for _, v := range vv { | |
w.Header().Add(k, v) | |
} | |
} | |
w.WriteHeader(res.StatusCode) | |
_, err = w.Write(body) | |
if err != nil { | |
log.Print(err) | |
return | |
} | |
} | |
log.Print("Reverse proxy listening on 0.0.0.0:1284") | |
log.Fatal(http.ListenAndServeTLS(":1284", "server.pem", "server.key", http.HandlerFunc(proxy))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment