1
// Copyright 2015 CoreOS, Inc.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
22
// Listeners returns a slice containing a net.Listener for each matching socket type
23
// passed to this process.
25
// The order of the file descriptors is preserved in the returned slice.
26
// Nil values are used to fill any gaps. For example if systemd were to return file descriptors
27
// corresponding with "udp, tcp, tcp", then the slice would contain {nil, net.Listener, net.Listener}
28
func Listeners(unsetEnv bool) ([]net.Listener, error) {
29
files := Files(unsetEnv)
30
listeners := make([]net.Listener, len(files))
32
for i, f := range files {
33
if pc, err := net.FileListener(f); err == nil {
40
// TLSListeners returns a slice containing a net.listener for each matching TCP socket type
41
// passed to this process.
42
// It uses default Listeners func and forces TCP sockets handlers to use TLS based on tlsConfig.
43
func TLSListeners(unsetEnv bool, tlsConfig *tls.Config) ([]net.Listener, error) {
44
listeners, err := Listeners(unsetEnv)
46
if listeners == nil || err != nil {
50
if tlsConfig != nil && err == nil {
51
tlsConfig.NextProtos = []string{"http/1.1"}
53
for i, l := range listeners {
54
// Activate TLS only for TCP sockets
55
if l.Addr().Network() == "tcp" {
56
listeners[i] = tls.NewListener(l, tlsConfig)