2015-07-25 04:35:07 -04:00
|
|
|
// Package sockets provides helper functions to create and configure Unix or TCP
|
|
|
|
// sockets.
|
2015-05-20 19:48:39 -04:00
|
|
|
package sockets
|
2015-03-31 16:37:49 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
2015-05-27 18:21:18 -04:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2015-03-31 16:37:49 -04:00
|
|
|
)
|
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// NewTCPSocket creates a TCP socket listener with the specified address and
|
|
|
|
// and the specified tls configuration. If TLSConfig is set, will encapsulate the
|
|
|
|
// TCP listener inside a TLS one.
|
2015-11-25 14:05:31 -05:00
|
|
|
func NewTCPSocket(addr string, tlsConfig *tls.Config) (net.Listener, error) {
|
|
|
|
l, err := net.Listen("tcp", addr)
|
2015-03-31 16:37:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-07 12:49:07 -04:00
|
|
|
if tlsConfig != nil {
|
|
|
|
tlsConfig.NextProtos = []string{"http/1.1"}
|
|
|
|
l = tls.NewListener(l, tlsConfig)
|
2015-03-31 16:37:49 -04:00
|
|
|
}
|
|
|
|
return l, nil
|
|
|
|
}
|
2015-05-27 18:21:18 -04:00
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// ConfigureTCPTransport configures the specified Transport according to the
|
|
|
|
// specified proto and addr.
|
|
|
|
// If the proto is unix (using a unix socket to communicate) the compression
|
|
|
|
// is disabled.
|
2015-05-27 18:21:18 -04:00
|
|
|
func ConfigureTCPTransport(tr *http.Transport, proto, addr string) {
|
|
|
|
// Why 32? See https://github.com/docker/docker/pull/8035.
|
|
|
|
timeout := 32 * time.Second
|
|
|
|
if proto == "unix" {
|
|
|
|
// No need for compression in local communications.
|
|
|
|
tr.DisableCompression = true
|
|
|
|
tr.Dial = func(_, _ string) (net.Conn, error) {
|
|
|
|
return net.DialTimeout(proto, addr, timeout)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tr.Proxy = http.ProxyFromEnvironment
|
|
|
|
tr.Dial = (&net.Dialer{Timeout: timeout}).Dial
|
|
|
|
}
|
|
|
|
}
|