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