tcp_socket.go 609 B

1234567891011121314151617181920212223
  1. // Package sockets provides helper functions to create and configure Unix or TCP sockets.
  2. package sockets
  3. import (
  4. "crypto/tls"
  5. "net"
  6. )
  7. // NewTCPSocket creates a TCP socket listener with the specified address and
  8. // and the specified tls configuration. If TLSConfig is set, will encapsulate the
  9. // TCP listener inside a TLS one.
  10. func NewTCPSocket(addr string, tlsConfig *tls.Config) (net.Listener, error) {
  11. l, err := net.Listen("tcp", addr)
  12. if err != nil {
  13. return nil, err
  14. }
  15. if tlsConfig != nil {
  16. tlsConfig.NextProtos = []string{"http/1.1"}
  17. l = tls.NewListener(l, tlsConfig)
  18. }
  19. return l, nil
  20. }