oauth2_proxy/http.go

115 lines
2.8 KiB
Go
Raw Permalink Normal View History

2015-06-08 01:51:47 +00:00
package main
import (
"crypto/tls"
"log"
"net"
"net/http"
"strings"
"time"
)
// Server represents an HTTP server
2015-06-08 01:51:47 +00:00
type Server struct {
Handler http.Handler
Opts *Options
}
// ListenAndServe will serve traffic on HTTP or HTTPS depending on TLS options
2015-06-08 01:51:47 +00:00
func (s *Server) ListenAndServe() {
if s.Opts.TLSKeyFile != "" || s.Opts.TLSCertFile != "" {
s.ServeHTTPS()
} else {
s.ServeHTTP()
}
}
// ServeHTTP constructs a net.Listener and starts handling HTTP requests
2015-06-08 01:51:47 +00:00
func (s *Server) ServeHTTP() {
2018-11-29 14:26:41 +00:00
HTTPAddress := s.Opts.HTTPAddress
var scheme string
2017-04-01 19:10:33 +00:00
2018-11-29 14:26:41 +00:00
i := strings.Index(HTTPAddress, "://")
2017-04-01 19:10:33 +00:00
if i > -1 {
2018-11-29 14:26:41 +00:00
scheme = HTTPAddress[0:i]
2015-06-08 01:51:47 +00:00
}
var networkType string
2017-04-01 19:10:33 +00:00
switch scheme {
2015-06-08 01:51:47 +00:00
case "", "http":
networkType = "tcp"
default:
2017-04-01 19:10:33 +00:00
networkType = scheme
2015-06-08 01:51:47 +00:00
}
2017-04-01 19:10:33 +00:00
2018-11-29 14:26:41 +00:00
slice := strings.SplitN(HTTPAddress, "//", 2)
2017-04-01 19:10:33 +00:00
listenAddr := slice[len(slice)-1]
2015-06-08 01:51:47 +00:00
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())
}
// ServeHTTPS constructs a net.Listener and starts handling HTTPS requests
2015-06-08 01:51:47 +00:00
func (s *Server) ServeHTTPS() {
2018-11-29 14:26:41 +00:00
addr := s.Opts.HTTPSAddress
2015-06-08 01:51:47 +00:00
config := &tls.Config{
MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS12,
}
if config.NextProtos == nil {
config.NextProtos = []string{"http/1.1"}
}
var err error
config.Certificates = make([]tls.Certificate, 1)
config.Certificates[0], err = tls.LoadX509KeyPair(s.Opts.TLSCertFile, s.Opts.TLSKeyFile)
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
}