@@ -8,8 +8,11 @@ import (
15 "github.com/justinas/alice"
18 "github.com/justinas/alice"
@@ -21,6 +24,7 @@ import (
21 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/authentication/basic"
24 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/authentication/basic"
22 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/cookies"
25 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/cookies"
23 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption"
26 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/encryption"
27+ proxyhttp "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/http"
24 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/ip"
28 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/ip"
25 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
29 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
26 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/middleware"
30 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/middleware"
@@ -102,6 +106,7 @@ type OAuthProxy struct {
102 headersChain alice.Chain
106 headersChain alice.Chain
103 preAuthChain alice.Chain
107 preAuthChain alice.Chain
104 pageWriter pagewriter.Writer
108 pageWriter pagewriter.Writer
109+ server proxyhttp.Server
107// NewOAuthProxy creates a new instance of OAuthProxy from the options provided
112// NewOAuthProxy creates a new instance of OAuthProxy from the options provided
@@ -184,7 +189,7 @@ func NewOAuthProxy(opts *options.Options, validator func(string) bool) (*OAuthPr
184 return nil, fmt.Errorf("could not build headers chain: %v", err)
189 return nil, fmt.Errorf("could not build headers chain: %v", err)
187− return &OAuthProxy{
192+ p := &OAuthProxy{
188 CookieName: opts.Cookie.Name,
193 CookieName: opts.Cookie.Name,
189 CSRFCookieName: fmt.Sprintf("%v_%v", opts.Cookie.Name, "csrf"),
194 CSRFCookieName: fmt.Sprintf("%v_%v", opts.Cookie.Name, "csrf"),
190 CookieSeed: opts.Cookie.Secret,
195 CookieSeed: opts.Cookie.Secret,
@@ -223,7 +228,60 @@ func NewOAuthProxy(opts *options.Options, validator func(string) bool) (*OAuthPr
223 headersChain: headersChain,
228 headersChain: headersChain,
224 preAuthChain: preAuthChain,
229 preAuthChain: preAuthChain,
225 pageWriter: pageWriter,
230 pageWriter: pageWriter,
233+ if err := p.setupServer(opts); err != nil {
234+ return nil, fmt.Errorf("error setting up server: %v", err)
240+func (p *OAuthProxy) Start() error {
241+ if p.server == nil {
242+ // We have to call setupServer before Start is called.
243+ // If this doesn't happen it's a programming error.
244+ panic("server has not been initialised")
247+ ctx, cancel := context.WithCancel(context.Background())
249+ // Observe signals in background goroutine.
251+ sigint := make(chan os.Signal, 1)
252+ signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
254+ cancel() // cancel the context
257+ return p.server.Start(ctx)
260+func (p *OAuthProxy) setupServer(opts *options.Options) error {
261+ serverOpts := proxyhttp.Opts{
263+ BindAddress: opts.Server.BindAddress,
264+ SecureBindAddress: opts.Server.SecureBindAddress,
265+ TLS: opts.Server.TLS,
268+ appServer, err := proxyhttp.NewServer(serverOpts)
270+ return fmt.Errorf("could not build app server: %v", err)
273+ metricsServer, err := proxyhttp.NewServer(proxyhttp.Opts{
274+ Handler: middleware.DefaultMetricsHandler,
275+ BindAddress: opts.MetricsServer.BindAddress,
276+ SecureBindAddress: opts.MetricsServer.BindAddress,
277+ TLS: opts.MetricsServer.TLS,
280+ return fmt.Errorf("could not build metrics server: %v", err)
283+ p.server = proxyhttp.NewServerGroup(appServer, metricsServer)
226− }, nil
284+ return nil
229// buildPreAuthChain constructs a chain that should process every request before
287// buildPreAuthChain constructs a chain that should process every request before
@@ -233,9 +291,9 @@ func buildPreAuthChain(opts *options.Options) (alice.Chain, error) {
233 chain := alice.New(middleware.NewScope(opts.ReverseProxy))
291 chain := alice.New(middleware.NewScope(opts.ReverseProxy))
235 if opts.ForceHTTPS {
293 if opts.ForceHTTPS {
236− _, httpsPort, err := net.SplitHostPort(opts.HTTPSAddress)
294+ _, httpsPort, err := net.SplitHostPort(opts.Server.SecureBindAddress)
237 if err != nil {
295 if err != nil {
238− return alice.Chain{}, fmt.Errorf("invalid HTTPS address %q: %v", opts.HTTPAddress, err)
296+ return alice.Chain{}, fmt.Errorf("invalid HTTPS address %q: %v", opts.Server.SecureBindAddress, err)
240 chain = chain.Append(middleware.NewRedirectToHTTPS(httpsPort))
298 chain = chain.Append(middleware.NewRedirectToHTTPS(httpsPort))