listeners_windows.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package listeners
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "net"
  6. "strings"
  7. "github.com/Microsoft/go-winio"
  8. "github.com/docker/go-connections/sockets"
  9. )
  10. // Init creates new listeners for the server.
  11. func Init(proto, addr, socketGroup string, tlsConfig *tls.Config) ([]net.Listener, error) {
  12. ls := []net.Listener{}
  13. switch proto {
  14. case "tcp":
  15. l, err := sockets.NewTCPSocket(addr, tlsConfig)
  16. if err != nil {
  17. return nil, err
  18. }
  19. ls = append(ls, l)
  20. case "npipe":
  21. // allow Administrators and SYSTEM, plus whatever additional users or groups were specified
  22. sddl := "D:P(A;;GA;;;BA)(A;;GA;;;SY)"
  23. if socketGroup != "" {
  24. for _, g := range strings.Split(socketGroup, ",") {
  25. sid, err := winio.LookupSidByName(g)
  26. if err != nil {
  27. return nil, err
  28. }
  29. sddl += fmt.Sprintf("(A;;GRGW;;;%s)", sid)
  30. }
  31. }
  32. c := winio.PipeConfig{
  33. SecurityDescriptor: sddl,
  34. MessageMode: true, // Use message mode so that CloseWrite() is supported
  35. InputBufferSize: 65536, // Use 64KB buffers to improve performance
  36. OutputBufferSize: 65536,
  37. }
  38. l, err := winio.ListenPipe(addr, &c)
  39. if err != nil {
  40. return nil, err
  41. }
  42. ls = append(ls, l)
  43. default:
  44. return nil, fmt.Errorf("invalid protocol format: windows only supports tcp and npipe")
  45. }
  46. return ls, nil
  47. }