MCPcopy Create free account
hub / github.com/chromedp/examples / newProxy

Function newProxy

proxy/main.go:96–118  ·  view source on GitHub ↗

newProxy creates a proxy that requires authentication.

()

Source from the content-addressed store, hash-verified

94
95// newProxy creates a proxy that requires authentication.
96func 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
120type transport struct {
121 http.RoundTripper

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected