12 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
13 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
16// Server represents an HTTP server
20 stop chan struct{} // channel for waiting shutdown
23// ListenAndServe will serve traffic on HTTP or HTTPS depending on TLS options
24func (s *Server) ListenAndServe() {
25 if s.Opts.TLSKeyFile != "" || s.Opts.TLSCertFile != "" {
32// ServeHTTP constructs a net.Listener and starts handling HTTP requests
33func (s *Server) ServeHTTP() {
34 HTTPAddress := s.Opts.HTTPAddress
37 i := strings.Index(HTTPAddress, "://")
39 scheme = HTTPAddress[0:i]
42 var networkType string
50 slice := strings.SplitN(HTTPAddress, "//", 2)
51 listenAddr := slice[len(slice)-1]
53 listener, err := net.Listen(networkType, listenAddr)
55 logger.Fatalf("FATAL: listen (%s, %s) failed - %s", networkType, listenAddr, err)
57 logger.Printf("HTTP: listening on %s", listenAddr)
59 logger.Printf("HTTP: closing %s", listener.Addr())
62// ServeHTTPS constructs a net.Listener and starts handling HTTPS requests
63func (s *Server) ServeHTTPS() {
64 addr := s.Opts.HTTPSAddress
65 config := &tls.Config{
66 MinVersion: tls.VersionTLS12,
67 MaxVersion: tls.VersionTLS12,
69 if config.NextProtos == nil {
70 config.NextProtos = []string{"http/1.1"}
74 config.Certificates = make([]tls.Certificate, 1)
75 config.Certificates[0], err = tls.LoadX509KeyPair(s.Opts.TLSCertFile, s.Opts.TLSKeyFile)
77 logger.Fatalf("FATAL: loading tls config (%s, %s) failed - %s", s.Opts.TLSCertFile, s.Opts.TLSKeyFile, err)
80 ln, err := net.Listen("tcp", addr)
82 logger.Fatalf("FATAL: listen (%s) failed - %s", addr, err)
84 logger.Printf("HTTPS: listening on %s", ln.Addr())
86 tlsListener := tls.NewListener(tcpKeepAliveListener{ln.(*net.TCPListener)}, config)
88 logger.Printf("HTTPS: closing %s", tlsListener.Addr())
91func (s *Server) serve(listener net.Listener) {
92 srv := &http.Server{Handler: s.Handler}
94 // See https://golang.org/pkg/net/http/#Server.Shutdown
95 idleConnsClosed := make(chan struct{})
97 <-s.stop // wait notification for stopping server
99 // We received an interrupt signal, shut down.
100 if err := srv.Shutdown(context.Background()); err != nil {
101 // Error from closing listeners, or context timeout:
102 logger.Printf("HTTP server Shutdown: %v", err)
104 close(idleConnsClosed)
107 err := srv.Serve(listener)
108 if err != nil && !errors.Is(err, http.ErrServerClosed) {
109 logger.Errorf("ERROR: http.Serve() - %s", err)
114// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
115// connections. It's used by ListenAndServe and ListenAndServeTLS so
116// dead TCP connections (e.g. closing laptop mid-download) eventually
118type tcpKeepAliveListener struct {
122func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
123 tc, err := ln.AcceptTCP()
127 err = tc.SetKeepAlive(true)
129 logger.Printf("Error setting Keep-Alive: %v", err)
131 err = tc.SetKeepAlivePeriod(3 * time.Minute)
133 logger.Printf("Error setting Keep-Alive period: %v", err)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list