Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proxyprotocol directive and listener middleware plugin type #1349

Merged
merged 8 commits into from
Mar 10, 2017
6 changes: 6 additions & 0 deletions caddyhttp/httpserver/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"path"
"time"

"github.com/mholt/caddy"
)

func init() {
Expand All @@ -18,6 +20,10 @@ type (
// passed the next Handler in the chain.
Middleware func(Handler) Handler

// ListenerMiddleware is similar to the Middleware type, except it
// chains one net.Listener to the next.
ListenerMiddleware func(caddy.Listener) caddy.Listener

// Handler is like http.Handler except ServeHTTP may return a status
// code and/or error.
//
Expand Down
3 changes: 3 additions & 0 deletions caddyhttp/httpserver/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ var directives = []string{
"realip", // github.com/captncraig/caddy-realip
"git", // github.com/abiosoft/caddy-git

// directives that add listener middleware to the stack
"proxyprotocol", // github.com/mastercactapus/caddy-proxyprotocol

// directives that add middleware to the stack
"locale", // github.com/simia-tech/caddy-locale
"log",
Expand Down
17 changes: 12 additions & 5 deletions caddyhttp/httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,20 @@ func (s *Server) Listen() (net.Listener, error) {
}
}

if tcpLn, ok := ln.(*net.TCPListener); ok {
ln = tcpKeepAliveListener{TCPListener: tcpLn}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mastercactapus This is already done in the call to Serve() on (now-)line 249. Is there a reason we do this twice? I do not think this is desirable...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mastercactapus Ready to merge other than this. :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pulled it out of Serve, since a TCPListener is not necessarily returned from Listen anymore.


cln := ln.(caddy.Listener)
for _, site := range s.sites {
for _, m := range site.listenerMiddleware {
cln = m(cln)
}
}

// Very important to return a concrete caddy.Listener
// implementation for graceful restarts.
return ln.(*net.TCPListener), nil
return cln.(caddy.Listener), nil
}

// ListenPacket creates udp connection for QUIC if it is enabled,
Expand All @@ -234,10 +245,6 @@ func (s *Server) ListenPacket() (net.PacketConn, error) {

// Serve serves requests on ln. It blocks until ln is closed.
func (s *Server) Serve(ln net.Listener) error {
if tcpLn, ok := ln.(*net.TCPListener); ok {
ln = tcpKeepAliveListener{TCPListener: tcpLn}
}

s.listenerMu.Lock()
s.listener = ln
s.listenerMu.Unlock()
Expand Down
13 changes: 13 additions & 0 deletions caddyhttp/httpserver/siteconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type SiteConfig struct {
// Compiled middleware stack
middlewareChain Handler

// listener middleware stack
listenerMiddleware []ListenerMiddleware

// Directory from which to serve files
Root string

Expand Down Expand Up @@ -80,6 +83,11 @@ func (s *SiteConfig) AddMiddleware(m Middleware) {
s.middleware = append(s.middleware, m)
}

// AddListenerMiddleware adds a listener middleware to a site's listenerMiddleware stack.
func (s *SiteConfig) AddListenerMiddleware(l ListenerMiddleware) {
s.listenerMiddleware = append(s.listenerMiddleware, l)
}

// TLSConfig returns s.TLS.
func (s SiteConfig) TLSConfig() *caddytls.Config {
return s.TLS
Expand All @@ -99,3 +107,8 @@ func (s SiteConfig) Port() string {
func (s SiteConfig) Middleware() []Middleware {
return s.middleware
}

// ListenerMiddleware returns s.listenerMiddleware
func (s SiteConfig) ListenerMiddleware() []ListenerMiddleware {
return s.listenerMiddleware
}