Skip to content

Commit

Permalink
move filteredPROXYListener wrapping to Listen()
Browse files Browse the repository at this point in the history
  • Loading branch information
mastercactapus committed Jan 14, 2017
1 parent f98ba63 commit c94366f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
5 changes: 3 additions & 2 deletions caddyhttp/httpserver/proxyprotocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"time"

"github.com/armon/go-proxyproto"
"github.com/mholt/caddy"
)

// filteredPROXYListener suports the Proxy Protocol on accepted sockets, and implements the `net.Listener` interface.
type filteredPROXYListener struct {
net.Listener
caddy.Listener
timeout time.Duration
subnets []*net.IPNet
}
Expand All @@ -20,7 +21,7 @@ type filteredPROXYListener struct {
// zero for no timeout, or provide a timeout for receiving the PROXY header. `subnets` is a comma-delimited list of CIDR
// ranges, for which to accept PROXY headers. If a new connection does not originate from one of the ranges, it will not be processed
// for a PROXY header. If the CIDR ranges are invalid or cannot be parsed, an error is returned.
func newFilteredPROXYListener(ln net.Listener, headerTimeout time.Duration, subnets string) (*filteredPROXYListener, error) {
func newFilteredPROXYListener(ln caddy.Listener, headerTimeout time.Duration, subnets string) (*filteredPROXYListener, error) {
cidrs := strings.Split(subnets, ",")
s := make([]*net.IPNet, len(cidrs))
var err error
Expand Down
28 changes: 15 additions & 13 deletions caddyhttp/httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,23 @@ func (s *Server) Listen() (net.Listener, error) {
}
}

if ProxyProtocolSubnets != "" {
// need to wrap this now, because Serve won't if it doesn't get a *net.TCPListener struct
tln := tcpKeepAliveListener{TCPListener: ln.(*net.TCPListener)}

// We decode proxy protocol after the TCP wrapper, but before
// anything else. As long as the wrapper happens before things
// that care about RemoteAddr, we should be ok.
fln, err := newFilteredPROXYListener(tln, ProxyProtocolTimeout, ProxyProtocolSubnets)
if err != nil {
return nil, fmt.Errorf("configuring Proxy Protocol support: %v", err)
}
ln = fln
}

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

// ListenPacket is a noop to implement the Server interface.
Expand All @@ -160,18 +174,6 @@ func (s *Server) Serve(ln net.Listener) error {
ln = tcpKeepAliveListener{TCPListener: tcpLn}
}

if ProxyProtocolSubnets != "" {
// We decode proxy protocol after the TCP wrapper, but before
// anything else. As long as the wrapper happens before things
// that care about RemoteAddr, we should be ok.
fln, err := newFilteredPROXYListener(ln, ProxyProtocolTimeout, ProxyProtocolSubnets)
if err != nil {
fmt.Printf("[ERROR] configuring Proxy Protocol support: %v\n", err)
} else {
ln = fln
}
}

ln = newGracefulListener(ln, &s.connWg)

s.listenerMu.Lock()
Expand Down

0 comments on commit c94366f

Please sign in to comment.