Created
February 26, 2020 13:32
-
-
Save evbruno/daf44d1b58392ddd928499568008d249 to your computer and use it in GitHub Desktop.
Go Proxy Go
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
// eg: go run proxy.go -url http://localhost:8443 | |
package main | |
import ( | |
"fmt" | |
"log" | |
"flag" | |
//"bytes" | |
//"io/ioutil" | |
"net/http" | |
"net/http/httputil" | |
"net/url" | |
) | |
type Prox struct { | |
target *url.URL | |
proxy *httputil.ReverseProxy | |
} | |
func NewProxy(target string) *Prox { | |
url, _ := url.Parse(target) | |
return &Prox{target: url, proxy: httputil.NewSingleHostReverseProxy(url)} | |
} | |
func (p *Prox) handle(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("X-GoProxy", "GoProxy") | |
p.proxy.Transport = &myTransport{} | |
p.proxy.ServeHTTP(w, r) | |
} | |
type myTransport struct { | |
} | |
// func (t *myTransport) RoundTrip(request *http.Request) (*http.Response, error) { | |
// buf, _ := ioutil.ReadAll(request.Body) | |
// rdr1 := ioutil.NopCloser(bytes.NewBuffer(buf)) | |
// rdr2 := ioutil.NopCloser(bytes.NewBuffer(buf)) | |
// fmt.Println("Request body : ", rdr1) | |
// request.Body = rdr2 | |
// response, err := http.DefaultTransport.RoundTrip(request) | |
// if err != nil { | |
// print("\n\ncame in error resp here", err) | |
// return nil, err //Server is not reachable. Server not working | |
// } | |
// body, err := httputil.DumpResponse(response, true) | |
// if err != nil { | |
// print("\n\nerror in dumb response") | |
// // copying the response body did not work | |
// return nil, err | |
// } | |
// log.Println("Response Body : ", string(body)) | |
// return response, err | |
// } | |
func (t *myTransport) RoundTrip(request *http.Request) (*http.Response, error) { | |
body, err := httputil.DumpRequestOut(request, true) | |
if err != nil { | |
print("What?", err) | |
return nil, err | |
} | |
fmt.Println("Request Body : ", string(body)) | |
response, err := http.DefaultTransport.RoundTrip(request) | |
if err != nil { | |
print("\n\ncame in error resp here", err) | |
return nil, err //Server is not reachable. Server not working | |
} | |
// body, err := httputil.DumpResponse(response, true) | |
// if err != nil { | |
// print("\n\nerror in dumb response") | |
// // copying the response body did not work | |
// return nil, err | |
// } | |
log.Println("Response Body : ", string(body)) | |
return response, err | |
} | |
func main() { | |
const ( | |
defaultPort = ":9090" | |
defaultPortUsage = "default server port, ':9090'" | |
defaultTarget = "http://127.0.0.1:8080" | |
defaultTargetUsage = "default redirect url, 'http://127.0.0.1:8080'" | |
) | |
// flags | |
port := flag.String("port", defaultPort, defaultPortUsage) | |
redirecturl := flag.String("url", defaultTarget, defaultTargetUsage) | |
flag.Parse() | |
fmt.Println("server will run on :", *port) | |
fmt.Println("redirecting to :", *redirecturl) | |
// proxy | |
proxy := NewProxy(*redirecturl) | |
//http.HandleFunc("/proxyServer", ProxyServer) | |
// server redirection | |
http.HandleFunc("/", proxy.handle) | |
log.Fatal(http.ListenAndServe(*port, nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment