pkg: listeners: separate out the listeners package
This code will be used in containerd and is quite useful in general to people who want a nice way of creating listeners from proto://address arguments (even supporting socket activation). Separate it out from docker/ so people can use it much more easily. Signed-off-by: Aleksa Sarai <asarai@suse.de>
This commit is contained in:
parent
e2664472b6
commit
d61bb3048b
3 changed files with 199 additions and 0 deletions
22
listeners/listeners.go
Normal file
22
listeners/listeners.go
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
package listeners
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
|
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
|
"github.com/docker/go-connections/sockets"
|
||||||
|
)
|
||||||
|
|
||||||
|
func initTCPSocket(addr string, tlsConfig *tls.Config) (l net.Listener, err error) {
|
||||||
|
if tlsConfig == nil || tlsConfig.ClientAuth != tls.RequireAndVerifyClientCert {
|
||||||
|
logrus.Warn("/!\\ DON'T BIND ON ANY IP ADDRESS WITHOUT setting -tlsverify IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
|
||||||
|
}
|
||||||
|
if l, err = sockets.NewTCPSocket(addr, tlsConfig); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := allocateDaemonPort(addr); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
119
listeners/listeners_unix.go
Normal file
119
listeners/listeners_unix.go
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package listeners
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/Sirupsen/logrus"
|
||||||
|
"github.com/coreos/go-systemd/activation"
|
||||||
|
"github.com/docker/go-connections/sockets"
|
||||||
|
"github.com/docker/libnetwork/portallocator"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Init creates new listeners for the server.
|
||||||
|
func Init(proto, addr, socketGroup string, tlsConfig *tls.Config) (ls []net.Listener, err error) {
|
||||||
|
switch proto {
|
||||||
|
case "fd":
|
||||||
|
ls, err = listenFD(addr, tlsConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
case "tcp":
|
||||||
|
l, err := initTCPSocket(addr, tlsConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ls = append(ls, l)
|
||||||
|
case "unix":
|
||||||
|
l, err := sockets.NewUnixSocket(addr, socketGroup)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("can't create unix socket %s: %v", addr, err)
|
||||||
|
}
|
||||||
|
ls = append(ls, l)
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("Invalid protocol format: %q", proto)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// listenFD returns the specified socket activated files as a slice of
|
||||||
|
// net.Listeners or all of the activated files if "*" is given.
|
||||||
|
func listenFD(addr string, tlsConfig *tls.Config) ([]net.Listener, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
listeners []net.Listener
|
||||||
|
)
|
||||||
|
// socket activation
|
||||||
|
if tlsConfig != nil {
|
||||||
|
listeners, err = activation.TLSListeners(false, tlsConfig)
|
||||||
|
} else {
|
||||||
|
listeners, err = activation.Listeners(false)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(listeners) == 0 {
|
||||||
|
return nil, fmt.Errorf("No sockets found. Make sure the docker daemon was started by systemd.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// default to all fds just like unix:// and tcp://
|
||||||
|
if addr == "" || addr == "*" {
|
||||||
|
return listeners, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fdNum, err := strconv.Atoi(addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse systemd address, should be number: %v", err)
|
||||||
|
}
|
||||||
|
fdOffset := fdNum - 3
|
||||||
|
if len(listeners) < int(fdOffset)+1 {
|
||||||
|
return nil, fmt.Errorf("Too few socket activated files passed in")
|
||||||
|
}
|
||||||
|
if listeners[fdOffset] == nil {
|
||||||
|
return nil, fmt.Errorf("failed to listen on systemd activated file at fd %d", fdOffset+3)
|
||||||
|
}
|
||||||
|
for i, ls := range listeners {
|
||||||
|
if i == fdOffset || ls == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := ls.Close(); err != nil {
|
||||||
|
logrus.Errorf("Failed to close systemd activated file at fd %d: %v", fdOffset+3, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []net.Listener{listeners[fdOffset]}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// allocateDaemonPort ensures that there are no containers
|
||||||
|
// that try to use any port allocated for the docker server.
|
||||||
|
func allocateDaemonPort(addr string) error {
|
||||||
|
host, port, err := net.SplitHostPort(addr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
intPort, err := strconv.Atoi(port)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var hostIPs []net.IP
|
||||||
|
if parsedIP := net.ParseIP(host); parsedIP != nil {
|
||||||
|
hostIPs = append(hostIPs, parsedIP)
|
||||||
|
} else if hostIPs, err = net.LookupIP(host); err != nil {
|
||||||
|
return fmt.Errorf("failed to lookup %s address in host specification", host)
|
||||||
|
}
|
||||||
|
|
||||||
|
pa := portallocator.Get()
|
||||||
|
for _, hostIP := range hostIPs {
|
||||||
|
if _, err := pa.RequestPort(hostIP, "tcp", intPort); err != nil {
|
||||||
|
return fmt.Errorf("failed to allocate daemon listening port %d (err: %v)", intPort, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
58
listeners/listeners_windows.go
Normal file
58
listeners/listeners_windows.go
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
package listeners
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Microsoft/go-winio"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Init creates new listeners for the server.
|
||||||
|
func Init(proto, addr, socketGroup string, tlsConfig *tls.Config) (ls []net.Listener, err error) {
|
||||||
|
switch proto {
|
||||||
|
case "tcp":
|
||||||
|
l, err := initTCPSocket(addr, tlsConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ls = append(ls, l)
|
||||||
|
|
||||||
|
case "npipe":
|
||||||
|
// allow Administrators and SYSTEM, plus whatever additional users or groups were specified
|
||||||
|
sddl := "D:P(A;;GA;;;BA)(A;;GA;;;SY)"
|
||||||
|
if socketGroup != "" {
|
||||||
|
for _, g := range strings.Split(socketGroup, ",") {
|
||||||
|
sid, err := winio.LookupSidByName(g)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
sddl += fmt.Sprintf("(A;;GRGW;;;%s)", sid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c := winio.PipeConfig{
|
||||||
|
SecurityDescriptor: sddl,
|
||||||
|
MessageMode: true, // Use message mode so that CloseWrite() is supported
|
||||||
|
InputBufferSize: 65536, // Use 64KB buffers to improve performance
|
||||||
|
OutputBufferSize: 65536,
|
||||||
|
}
|
||||||
|
l, err := winio.ListenPipe(addr, &c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ls = append(ls, l)
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, errors.New("Invalid protocol format. Windows only supports tcp and npipe.")
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// allocateDaemonPort ensures that there are no containers
|
||||||
|
// that try to use any port allocated for the docker server.
|
||||||
|
func allocateDaemonPort(addr string) error {
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in a new issue