David K. Bainbridge | 215e024 | 2017-09-05 23:18:24 -0700 | [diff] [blame] | 1 | // Package sockets provides helper functions to create and configure Unix or TCP sockets. |
| 2 | package sockets |
| 3 | |
| 4 | import ( |
| 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. |
| 12 | func 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 | } |