13 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
14 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options/util"
15 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
16 "golang.org/x/sync/errgroup"
19// Server represents an HTTP or HTTPS server.
20type Server interface {
21 // Start blocks and runs the server.
22 Start(ctx context.Context) error
25// Opts contains the information required to set up the server.
27 // Handler is the http.Handler to be used to serve http pages by the server.
30 // BindAddress is the address the HTTP server should listen on.
33 // SecureBindAddress is the address the HTTPS server should listen on.
34 SecureBindAddress string
36 // TLS is the TLS configuration for the server.
40// NewServer creates a new Server from the options given.
41func NewServer(opts Opts) (Server, error) {
43 handler: opts.Handler,
45 if err := s.setupListener(opts); err != nil {
46 return nil, fmt.Errorf("error setting up listener: %v", err)
48 if err := s.setupTLSListener(opts); err != nil {
49 return nil, fmt.Errorf("error setting up TLS listener: %v", err)
55// server is an implementation of the Server interface.
60 tlsListener net.Listener
63// setupListener sets the server listener if the HTTP server is enabled.
64// The HTTP server can be disabled by setting the BindAddress to "-" or by
66func (s *server) setupListener(opts Opts) error {
67 if opts.BindAddress == "" || opts.BindAddress == "-" {
68 // No HTTP listener required
72 networkType := getNetworkScheme(opts.BindAddress)
73 listenAddr := getListenAddress(opts.BindAddress)
75 listener, err := net.Listen(networkType, listenAddr)
77 return fmt.Errorf("listen (%s, %s) failed: %v", networkType, listenAddr, err)
84// setupTLSListener sets the server TLS listener if the HTTPS server is enabled.
85// The HTTPS server can be disabled by setting the SecureBindAddress to "-" or by
87func (s *server) setupTLSListener(opts Opts) error {
88 if opts.SecureBindAddress == "" || opts.SecureBindAddress == "-" {
89 // No HTTPS listener required
93 config := &tls.Config{
94 MinVersion: tls.VersionTLS12,
95 MaxVersion: tls.VersionTLS13,
96 NextProtos: []string{"http/1.1"},
99 return errors.New("no TLS config provided")
101 cert, err := getCertificate(opts.TLS)
103 return fmt.Errorf("could not load certificate: %v", err)
105 config.Certificates = []tls.Certificate{cert}
107 listenAddr := getListenAddress(opts.SecureBindAddress)
109 listener, err := net.Listen("tcp", listenAddr)
111 return fmt.Errorf("listen (%s) failed: %v", listenAddr, err)
114 s.tlsListener = tls.NewListener(tcpKeepAliveListener{listener.(*net.TCPListener)}, config)
118// Start starts the HTTP and HTTPS server if applicable.
119// It will block until the context is cancelled.
120// If any errors occur, only the first error will be returned.
121func (s *server) Start(ctx context.Context) error {
122 g, groupCtx := errgroup.WithContext(ctx)
124 if s.listener != nil {
126 if err := s.startServer(groupCtx, s.listener); err != nil {
127 return fmt.Errorf("error starting insecure server: %v", err)
133 if s.tlsListener != nil {
135 if err := s.startServer(groupCtx, s.tlsListener); err != nil {
136 return fmt.Errorf("error starting secure server: %v", err)
145// startServer creates and starts a new server with the given listener.
146// When the given context is cancelled the server will be shutdown.
147// If any errors occur, only the first error will be returned.
148func (s *server) startServer(ctx context.Context, listener net.Listener) error {
149 srv := &http.Server{Handler: s.handler}
150 g, groupCtx := errgroup.WithContext(ctx)
155 if err := srv.Shutdown(context.Background()); err != nil {
156 return fmt.Errorf("error shutting down server: %v", err)
162 if err := srv.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
163 return fmt.Errorf("could not start server: %v", err)
171// getNetworkScheme gets the scheme for the HTTP server.
172func getNetworkScheme(addr string) string {
174 i := strings.Index(addr, "://")
187// getListenAddress gets the address for the HTTP server.
188func getListenAddress(addr string) string {
189 slice := strings.SplitN(addr, "//", 2)
190 return slice[len(slice)-1]
193// getCertificate loads the certificate data from the TLS config.
194func getCertificate(opts *options.TLS) (tls.Certificate, error) {
195 keyData, err := getSecretValue(opts.Key)
197 return tls.Certificate{}, fmt.Errorf("could not load key data: %v", err)
200 certData, err := getSecretValue(opts.Cert)
202 return tls.Certificate{}, fmt.Errorf("could not load cert data: %v", err)
205 cert, err := tls.X509KeyPair(certData, keyData)
207 return tls.Certificate{}, fmt.Errorf("could not parse certificate data: %v", err)
213// getSecretValue wraps util.GetSecretValue so that we can return an error if no
214// source is provided.
215func getSecretValue(src *options.SecretSource) ([]byte, error) {
217 return nil, errors.New("no configuration provided")
219 return util.GetSecretValue(src)
222// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
223// connections. It's used by so that dead TCP connections (e.g. closing laptop
224// mid-download) eventually go away.
225type tcpKeepAliveListener struct {
229// Accept implements the TCPListener interface.
230// It sets the keep alive period to 3 minutes for each connection.
231func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
232 tc, err := ln.AcceptTCP()
236 err = tc.SetKeepAlive(true)
238 logger.Errorf("Error setting Keep-Alive: %v", err)
240 err = tc.SetKeepAlivePeriod(3 * time.Minute)
242 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