forked from bitly/oauth2_proxy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
133 lines (113 loc) · 3.28 KB
/
http.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
package main
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
"time"
)
type Server struct {
Handler http.Handler
Opts *Options
}
func (s *Server) ListenAndServe() {
if len(s.Opts.TLSCertFile) != 0 {
s.ServeHTTPS()
} else {
s.ServeHTTP()
}
}
func (s *Server) ServeHTTP() {
httpAddress := s.Opts.HttpAddress
scheme := ""
i := strings.Index(httpAddress, "://")
if i > -1 {
scheme = httpAddress[0:i]
}
var networkType string
switch scheme {
case "", "http":
networkType = "tcp"
default:
networkType = scheme
}
slice := strings.SplitN(httpAddress, "//", 2)
listenAddr := slice[len(slice)-1]
listener, err := net.Listen(networkType, listenAddr)
if err != nil {
log.Fatalf("FATAL: listen (%s, %s) failed - %s", networkType, listenAddr, err)
}
log.Printf("HTTP: listening on %s", listenAddr)
server := &http.Server{Handler: s.Handler}
err = server.Serve(listener)
if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
log.Printf("ERROR: http.Serve() - %s", err)
}
log.Printf("HTTP: closing %s", listener.Addr())
}
func (s *Server) ServeHTTPS() {
addr := s.Opts.HttpsAddress
config := &tls.Config{
MinVersion: tls.VersionTLS12,
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, // Go 1.8 only
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, // Go 1.8 only
},
NextProtos: []string{"h2", "http/1.1"},
}
if s.Opts.TLSClientCAFile != "" {
certs, err := ioutil.ReadFile(s.Opts.TLSClientCAFile)
if err != nil {
log.Fatalf("FATAL: could not read CA certs, %v", err)
}
pool := x509.NewCertPool()
if !pool.AppendCertsFromPEM(certs) {
log.Fatalf("FATAL: failed appending certs to pool")
}
config.ClientCAs = pool
config.ClientAuth = tls.VerifyClientCertIfGiven
}
if len(s.Opts.TLSCertFile) != len(s.Opts.TLSKeyFile) {
log.Fatalf("FATAL: there should be an equal number of certs and matching keys")
}
var err error
config.Certificates = make([]tls.Certificate, len(s.Opts.TLSCertFile))
for i := range config.Certificates {
config.Certificates[i], err = tls.LoadX509KeyPair(s.Opts.TLSCertFile[i], s.Opts.TLSKeyFile[i])
if err != nil {
log.Fatalf("FATAL: loading tls config (%s, %s) failed - %s", s.Opts.TLSCertFile, s.Opts.TLSKeyFile, err)
}
}
ln, err := net.Listen("tcp", addr)
if err != nil {
log.Fatalf("FATAL: listen (%s) failed - %s", addr, err)
}
log.Printf("HTTPS: listening on %s", ln.Addr())
tlsListener := tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, config)
srv := &http.Server{Handler: s.Handler}
err = srv.Serve(tlsListener)
if err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
log.Printf("ERROR: https.Serve() - %s", err)
}
log.Printf("HTTPS: closing %s", tlsListener.Addr())
}
// 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
}