~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/coreos/go-systemd/activation/listeners.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 CoreOS, Inc.
 
2
//
 
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
 
6
//
 
7
//     http://www.apache.org/licenses/LICENSE-2.0
 
8
//
 
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.
 
14
 
 
15
package activation
 
16
 
 
17
import (
 
18
        "crypto/tls"
 
19
        "net"
 
20
)
 
21
 
 
22
// Listeners returns a slice containing a net.Listener for each matching socket type
 
23
// passed to this process.
 
24
//
 
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))
 
31
 
 
32
        for i, f := range files {
 
33
                if pc, err := net.FileListener(f); err == nil {
 
34
                        listeners[i] = pc
 
35
                }
 
36
        }
 
37
        return listeners, nil
 
38
}
 
39
 
 
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)
 
45
 
 
46
        if listeners == nil || err != nil {
 
47
                return nil, err
 
48
        }
 
49
 
 
50
        if tlsConfig != nil && err == nil {
 
51
                tlsConfig.NextProtos = []string{"http/1.1"}
 
52
 
 
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)
 
57
                        }
 
58
                }
 
59
        }
 
60
 
 
61
        return listeners, err
 
62
}