-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
56 lines (47 loc) · 1.05 KB
/
net.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package service
import (
"crypto/tls"
"fmt"
"net"
"github.com/omecodes/common/errors"
"github.com/omecodes/libome"
)
func (opts *Options) listen(port int, security ome.Security) (net.Listener, *tls.Config, error) {
var (
listener net.Listener
err error
address string
)
if port > 0 {
address = fmt.Sprintf("%s:%d", opts.Host(), port)
} else {
address = fmt.Sprintf("%s:", opts.Host())
}
var tc *tls.Config
if security != ome.Security_Insecure {
err = opts.loadOrGenerateCertificateKeyPair()
if err != nil {
return nil, nil, err
}
if security == ome.Security_Tls {
tc, err = opts.ServerTLS()
} else if security == ome.Security_MutualTls {
tc, err = opts.serverMutualTLS()
} else {
return nil, nil, errors.New("unsupported security type")
}
if err != nil {
return nil, nil, err
}
listener, err = tls.Listen("tcp", address, tc)
if err != nil {
return nil, nil, err
}
} else {
listener, err = net.Listen("tcp", address)
if err != nil {
return nil, nil, err
}
}
return listener, tc, err
}