forked from fabiolb/fabio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listen.go
151 lines (124 loc) · 3.55 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"regexp"
"time"
"github.com/armon/go-proxyproto"
"github.com/eBay/fabio/config"
"github.com/eBay/fabio/exit"
"github.com/eBay/fabio/proxy"
)
var quit = make(chan bool)
var commas = regexp.MustCompile(`\s*,\s*`)
var semicolons = regexp.MustCompile(`\s*;\s*`)
func init() {
exit.Listen(func(os.Signal) { close(quit) })
}
// startListeners runs one or more listeners for the handler
func startListeners(listen []config.Listen, wait time.Duration, h http.Handler) {
for _, l := range listen {
go listenAndServe(l, h)
}
// wait for shutdown signal
<-quit
// disable routing for all requests
proxy.Shutdown()
// trigger graceful shutdown
log.Printf("[INFO] Graceful shutdown over %s", wait)
time.Sleep(wait)
log.Print("[INFO] Down")
}
func listenAndServe(l config.Listen, h http.Handler) {
srv, err := newServer(l, h)
if err != nil {
log.Fatal("[FATAL] ", err)
}
if srv.TLSConfig != nil {
log.Printf("[INFO] HTTPS proxy listening on %s with certificate %s", l.Addr, l.CertFile)
if srv.TLSConfig.ClientAuth == tls.RequireAndVerifyClientCert {
log.Printf("[INFO] Client certificate authentication enabled on %s with certificates from %s", l.Addr, l.ClientAuthFile)
}
} else {
log.Printf("[INFO] HTTP proxy listening on %s", l.Addr)
}
if err := serve(srv); err != nil {
log.Fatal("[FATAL] ", err)
}
}
var tlsLoadX509KeyPair = tls.LoadX509KeyPair
func newServer(l config.Listen, h http.Handler) (*http.Server, error) {
srv := &http.Server{
Addr: l.Addr,
Handler: h,
ReadTimeout: l.ReadTimeout,
WriteTimeout: l.WriteTimeout,
}
if l.CertFile != "" {
cert, err := tlsLoadX509KeyPair(l.CertFile, l.KeyFile)
if err != nil {
return nil, err
}
srv.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
}
if l.ClientAuthFile != "" {
pemBlock, err := ioutil.ReadFile(l.ClientAuthFile)
if err != nil {
return nil, err
}
pool := x509.NewCertPool()
for p, rest := pem.Decode(pemBlock); p != nil; p, rest = pem.Decode(rest) {
cert, err := x509.ParseCertificate(p.Bytes)
if err != nil {
return nil, err
}
// Issue #108: Allow generated AWS API Gateway certs to be used for client cert authentication
if l.AWSApiGWCertCN != "" && l.AWSApiGWCertCN == cert.Issuer.CommonName {
cert.BasicConstraintsValid = true
cert.IsCA = true
cert.KeyUsage = x509.KeyUsageCertSign
log.Print("[INFO] Enabling AWS Api Gateway workaround for certificate %s", cert.Issuer.CommonName)
}
pool.AddCert(cert)
}
srv.TLSConfig.ClientCAs = pool
srv.TLSConfig.ClientAuth = tls.RequireAndVerifyClientCert
}
}
return srv, nil
}
func serve(srv *http.Server) error {
ln, err := net.Listen("tcp", srv.Addr)
if err != nil {
log.Fatal("[FATAL] ", err)
}
ln = &proxyproto.Listener{tcpKeepAliveListener{ln.(*net.TCPListener)}}
if srv.TLSConfig != nil {
ln = tls.NewListener(ln, srv.TLSConfig)
}
return srv.Serve(ln)
}
// copied from http://golang.org/src/net/http/server.go?s=54604:54695#L1967
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections. It's used by ListenAndServe and ListenAndServeTLS so
// dead TCP connections (e.g. closing laptop mid-download) eventually
// go away.
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := ln.AcceptTCP()
if err != nil {
return
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}