2017-05-23 18:56:10 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2018-06-11 13:13:19 +00:00
|
|
|
"log"
|
2017-05-23 18:56:10 +00:00
|
|
|
"net/http"
|
|
|
|
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
2017-05-23 18:56:10 +00:00
|
|
|
)
|
|
|
|
|
2018-07-20 09:02:06 +00:00
|
|
|
type localProxy struct {
|
2017-05-23 18:56:10 +00:00
|
|
|
Transport *proxyTransport
|
|
|
|
}
|
|
|
|
|
2018-07-20 09:02:06 +00:00
|
|
|
func (proxy *localProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2017-05-23 18:56:10 +00:00
|
|
|
// Force URL/domain to http/unixsocket to be able to
|
|
|
|
// use http.Transport RoundTrip to do the requests via the socket
|
|
|
|
r.URL.Scheme = "http"
|
|
|
|
r.URL.Host = "unixsocket"
|
|
|
|
|
|
|
|
res, err := proxy.Transport.proxyDockerRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
code := http.StatusInternalServerError
|
|
|
|
if res != nil && res.StatusCode != 0 {
|
|
|
|
code = res.StatusCode
|
|
|
|
}
|
2018-06-11 13:13:19 +00:00
|
|
|
httperror.WriteError(w, code, "Unable to proxy the request via the Docker socket", err)
|
2017-05-23 18:56:10 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
for k, vv := range res.Header {
|
|
|
|
for _, v := range vv {
|
|
|
|
w.Header().Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
2017-09-14 09:09:36 +00:00
|
|
|
|
|
|
|
w.WriteHeader(res.StatusCode)
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
if _, err := io.Copy(w, res.Body); err != nil {
|
2018-06-11 13:13:19 +00:00
|
|
|
log.Printf("proxy error: %s\n", err)
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
}
|