mirror of https://github.com/docker/docs.git
Merge pull request #224 from vieux/fix_proxy_tls
fix proxy function with TLS
This commit is contained in:
commit
e5cc8636bd
|
@ -2,6 +2,7 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -25,6 +26,7 @@ type context struct {
|
||||||
eventsHandler *eventsHandler
|
eventsHandler *eventsHandler
|
||||||
debug bool
|
debug bool
|
||||||
version string
|
version string
|
||||||
|
tlsConfig *tls.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
type handler func(c *context, w http.ResponseWriter, r *http.Request)
|
type handler func(c *context, w http.ResponseWriter, r *http.Request)
|
||||||
|
@ -218,7 +220,7 @@ func proxyContainerAndForceRefresh(c *context, w http.ResponseWriter, r *http.Re
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := proxy(container, w, r); err != nil {
|
if err := proxy(c.tlsConfig, container, w, r); err != nil {
|
||||||
httpError(w, err.Error(), http.StatusInternalServerError)
|
httpError(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -234,7 +236,7 @@ func proxyContainer(c *context, w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := proxy(container, w, r); err != nil {
|
if err := proxy(c.tlsConfig, container, w, r); err != nil {
|
||||||
httpError(w, err.Error(), http.StatusInternalServerError)
|
httpError(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -247,7 +249,7 @@ func proxyHijack(c *context, w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := hijack(container, w, r); err != nil {
|
if err := hijack(c.tlsConfig, container, w, r); err != nil {
|
||||||
httpError(w, err.Error(), http.StatusInternalServerError)
|
httpError(w, err.Error(), http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@ func ListenAndServe(c *cluster.Cluster, s *scheduler.Scheduler, hosts []string,
|
||||||
scheduler: s,
|
scheduler: s,
|
||||||
version: version,
|
version: version,
|
||||||
eventsHandler: NewEventsHandler(),
|
eventsHandler: NewEventsHandler(),
|
||||||
|
tlsConfig: tlsConfig,
|
||||||
}
|
}
|
||||||
c.Events(context.eventsHandler)
|
c.Events(context.eventsHandler)
|
||||||
r, err := createRouter(context, enableCors)
|
r, err := createRouter(context, enableCors)
|
||||||
|
|
22
api/utils.go
22
api/utils.go
|
@ -1,6 +1,7 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
@ -33,9 +34,13 @@ func getContainerFromVars(c *context, vars map[string]string) (*cluster.Containe
|
||||||
return nil, errors.New("Not found")
|
return nil, errors.New("Not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func proxy(container *cluster.Container, w http.ResponseWriter, r *http.Request) error {
|
func proxy(tlsConfig *tls.Config, container *cluster.Container, w http.ResponseWriter, r *http.Request) error {
|
||||||
// Use a new client for each request
|
// Use a new client for each request
|
||||||
client := &http.Client{}
|
client := &http.Client{
|
||||||
|
Transport: &http.Transport{
|
||||||
|
TLSClientConfig: tlsConfig,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// RequestURI may not be sent to client
|
// RequestURI may not be sent to client
|
||||||
r.RequestURI = ""
|
r.RequestURI = ""
|
||||||
|
@ -60,7 +65,7 @@ func proxy(container *cluster.Container, w http.ResponseWriter, r *http.Request)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func hijack(container *cluster.Container, w http.ResponseWriter, r *http.Request) error {
|
func hijack(tlsConfig *tls.Config, container *cluster.Container, w http.ResponseWriter, r *http.Request) error {
|
||||||
addr := container.Node().Addr
|
addr := container.Node().Addr
|
||||||
if parts := strings.SplitN(container.Node().Addr, "://", 2); len(parts) == 2 {
|
if parts := strings.SplitN(container.Node().Addr, "://", 2); len(parts) == 2 {
|
||||||
addr = parts[1]
|
addr = parts[1]
|
||||||
|
@ -68,7 +73,16 @@ func hijack(container *cluster.Container, w http.ResponseWriter, r *http.Request
|
||||||
|
|
||||||
log.Debugf("[HIJACK PROXY] --> %s", addr)
|
log.Debugf("[HIJACK PROXY] --> %s", addr)
|
||||||
|
|
||||||
d, err := net.Dial("tcp", addr)
|
var (
|
||||||
|
d net.Conn
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if tlsConfig != nil {
|
||||||
|
d, err = tls.Dial("tcp", addr, tlsConfig)
|
||||||
|
} else {
|
||||||
|
d, err = net.Dial("tcp", addr)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue