newProxy creates a proxy that requires authentication.
()
| 94 | |
| 95 | // newProxy creates a proxy that requires authentication. |
| 96 | func newProxy() *httputil.ReverseProxy { |
| 97 | return &httputil.ReverseProxy{ |
| 98 | Director: func(r *http.Request) { |
| 99 | if dump, err := httputil.DumpRequest(r, true); err == nil { |
| 100 | log.Printf("%s", dump) |
| 101 | } |
| 102 | // hardcode username/password "u:p" (base64 encoded: dTpw ) to make it simple |
| 103 | if auth := r.Header.Get("Proxy-Authorization"); auth != "Basic dTpw" { |
| 104 | r.Header.Set("X-Failed", "407") |
| 105 | } |
| 106 | }, |
| 107 | Transport: &transport{http.DefaultTransport}, |
| 108 | ErrorHandler: func(w http.ResponseWriter, r *http.Request, err error) { |
| 109 | if err.Error() == "407" { |
| 110 | log.Println("proxy: not authorized") |
| 111 | w.Header().Add("Proxy-Authenticate", `Basic realm="Proxy Authorization"`) |
| 112 | w.WriteHeader(407) |
| 113 | } else { |
| 114 | w.WriteHeader(http.StatusBadGateway) |
| 115 | } |
| 116 | }, |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | type transport struct { |
| 121 | http.RoundTripper |