Last active
November 9, 2020 10:26
-
-
Save mpfund/b0d06b476a23cc732e61 to your computer and use it in GitHub Desktop.
simple golang http proxy
This file contains 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 ( | |
"fmt" | |
"io" | |
"net/http" | |
) | |
// A very simple http proxy | |
func main() { | |
simpleProxyHandler := http.HandlerFunc(simpleProxyHandlerFunc) | |
http.ListenAndServe(":8080", simpleProxyHandler) | |
} | |
func simpleProxyHandlerFunc(w http.ResponseWriter, r *http.Request) { | |
httpc := &http.Client{} | |
// request uri can't be set in client requests | |
r.RequestURI = "" | |
if r.Method == "CONNECT" { | |
fmt.Println(r.URL, " CONNECT / SSL not implemented") | |
return | |
} | |
fmt.Println(r.URL, r.Method) | |
resp, err := httpc.Do(r) | |
if err != nil { | |
fmt.Println(r.URL, " ", err.Error()) | |
return | |
} | |
copyHeaders(w.Header(), resp.Header) | |
// copy content | |
io.Copy(w, resp.Body) | |
} | |
func copyHeaders(dest http.Header, source http.Header) { | |
for header := range source { | |
dest.Add(header, source.Get(header)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment