Create a gist now

Instantly share code, notes, and snippets.

@dustin /proxy.go Secret
Created Sep 10, 2011

Deauthing proxy.
package main
import (
"flag"
"log"
"net/http"
"net/http/httputil"
"net/url"
)
var target = flag.String("target", "http://www.google.com/", "Where to go.")
var addr = flag.String("listen", ":12345", "Address/port on which to listen.")
var auth = flag.String("auth", "", "Authorization header to add (optional).")
func main() {
flag.Parse()
targetUrl, uerr := url.Parse(*target)
if uerr != nil {
log.Fatalf("Error parsing target ``%v'': %v", target, uerr)
}
proxy := httputil.ReverseProxy{Director: func(req *http.Request) {
req.URL.Scheme = targetUrl.Scheme
req.URL.Host = targetUrl.Host
req.Host = targetUrl.Host
if *auth != "" {
req.Header.Set("Authorization", *auth)
}
}}
err := http.ListenAndServe(*addr, &proxy)
if err != nil {
log.Fatalf("ListenAndServe: %v", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment