2016-04-01 13:43:05 +00:00
|
|
|
package listeners
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Microsoft/go-winio"
|
2016-04-09 06:49:33 +00:00
|
|
|
"github.com/docker/go-connections/sockets"
|
2016-04-01 13:43:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Init creates new listeners for the server.
|
2016-04-09 06:49:33 +00:00
|
|
|
func Init(proto, addr, socketGroup string, tlsConfig *tls.Config) ([]net.Listener, error) {
|
|
|
|
ls := []net.Listener{}
|
|
|
|
|
2016-04-01 13:43:05 +00:00
|
|
|
switch proto {
|
|
|
|
case "tcp":
|
2016-04-09 06:49:33 +00:00
|
|
|
l, err := sockets.NewTCPSocket(addr, tlsConfig)
|
2016-04-01 13:43:05 +00:00
|
|
|
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:
|
2016-04-01 13:51:29 +00:00
|
|
|
return nil, fmt.Errorf("invalid protocol format: windows only supports tcp and npipe")
|
2016-04-01 13:43:05 +00:00
|
|
|
}
|
|
|
|
|
2016-04-09 06:49:33 +00:00
|
|
|
return ls, nil
|
2016-04-01 13:43:05 +00:00
|
|
|
}
|