-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlisten.go
92 lines (80 loc) · 1.74 KB
/
listen.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"log"
"net"
"os"
"strconv"
"github.com/zeebo/errs/v2"
)
func closeListener(lis net.Listener) {
if lis != nil {
_ = lis.Close()
}
}
func listen(ctx context.Context) (lis443, lis80 net.Listener, err error) {
defer func() {
if err != nil {
closeListener(lis443)
closeListener(lis80)
}
}()
lis443, lis80, err = listenFromEnv(ctx)
if err != nil {
log.Printf("error listening from env: %+v", err)
}
if lis443 == nil {
lis443, err = net.Listen("tcp", ":443")
if err != nil {
return lis443, lis80, errs.Wrap(err)
}
}
if lis80 == nil {
lis80, err = net.Listen("tcp", ":80")
if err != nil {
return lis443, lis80, errs.Wrap(err)
}
}
return lis443, lis80, nil
}
func listenFromEnv(ctx context.Context) (lis443, lis80 net.Listener, err error) {
defer func() {
if err != nil {
closeListener(lis443)
closeListener(lis80)
}
}()
envPid, envFDs := os.Getenv("LISTEN_PID"), os.Getenv("LISTEN_FDS")
if envPid == "" || envFDs == "" {
return nil, nil, nil
}
pid, err := strconv.Atoi(envPid)
if err != nil {
return lis443, lis80, errs.Wrap(err)
} else if pid != os.Getpid() {
return lis443, lis80, errs.Errorf("invalid pid specified")
}
nfds, err := strconv.Atoi(envFDs)
if err != nil {
return lis443, lis80, errs.Wrap(err)
}
for i := 0; i < nfds; i++ {
lis, err := net.FileListener(os.NewFile(uintptr(3+i), ""))
if err != nil {
return lis443, lis80, errs.Wrap(err)
}
if lis.Addr().Network() != "tcp" {
_ = lis.Close()
continue
}
switch addr := lis.Addr().String(); {
case lis443 == nil && addr == "0.0.0.0:443":
lis443 = lis
case lis80 == nil && addr == "0.0.0.0:80":
lis80 = lis
default:
_ = lis.Close()
}
}
return lis443, lis80, nil
}