Skip to content

Commit

Permalink
Add optional support for the PROXY protocol
Browse files Browse the repository at this point in the history
This adds support for the commonly implemented PROXY protocol, allowing
TCP proxies to pass along upstream client information. When this is
enabled gorouter will read the PROXY preamble and inject the upstream
information into the X-Forwarded-For header.

http://blog.haproxy.com/haproxy/proxy-protocol/
  • Loading branch information
Jonty committed Mar 31, 2016
1 parent a5307c5 commit 160bec3
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 9 deletions.
8 changes: 8 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Godeps/_workspace/src/github.com/armon/go-proxyproto/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions Godeps/_workspace/src/github.com/armon/go-proxyproto/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

194 changes: 194 additions & 0 deletions Godeps/_workspace/src/github.com/armon/go-proxyproto/protocol.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Config struct {
AccessLog AccessLog `yaml:"access_log"`
EnableAccessLogStreaming bool `yaml:"enable_access_log_streaming"`
DebugAddr string `yaml:"debug_addr"`
EnablePROXY bool `yaml:"enable_proxy"`
EnableSSL bool `yaml:"enable_ssl"`
SSLPort uint16 `yaml:"ssl_port"`
SSLCertPath string `yaml:"ssl_cert_path"`
Expand Down Expand Up @@ -141,11 +142,12 @@ var defaultConfig = Config{
Nats: []NatsConfig{defaultNatsConfig},
Logging: defaultLoggingConfig,

Port: 8081,
Index: 0,
GoMaxProcs: -1,
EnableSSL: false,
SSLPort: 443,
Port: 8081,
Index: 0,
GoMaxProcs: -1,
EnablePROXY: false,
EnableSSL: false,
SSLPort: 443,

EndpointTimeoutInSeconds: 60,
RouteServiceTimeoutInSeconds: 60,
Expand Down
10 changes: 10 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ token_fetcher_expiration_buffer_time: 40
Expect(config.TokenFetcherRetryIntervalInSeconds).To(Equal(5))
Expect(config.TokenFetcherExpirationBufferTimeInSeconds).To(Equal(int64(30)))
})

It("sets proxy protocol", func() {
var b = []byte(`
enable_proxy: true
`)

config.Initialize(b)

Expect(config.EnablePROXY).To(Equal(true))
})
})

Describe("Process", func() {
Expand Down
17 changes: 13 additions & 4 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sync"
"syscall"

"github.com/armon/go-proxyproto"
"github.com/apcera/nats"
"github.com/cloudfoundry/dropsonde"
vcap "github.com/cloudfoundry/gorouter/common"
Expand Down Expand Up @@ -267,10 +268,14 @@ func (r *Router) serveHTTPS(server *http.Server, errChan chan error) error {
}

r.tlsListener = tlsListener
r.logger.Info(fmt.Sprintf("Listening on %s", tlsListener.Addr()))
if r.config.EnablePROXY {
r.tlsListener = &proxyproto.Listener{tlsListener}
}

r.logger.Info(fmt.Sprintf("Listening on %s", r.tlsListener.Addr()))

go func() {
err := server.Serve(tlsListener)
err := server.Serve(r.tlsListener)
r.stopLock.Lock()
if !r.stopping {
errChan <- err
Expand All @@ -290,10 +295,14 @@ func (r *Router) serveHTTP(server *http.Server, errChan chan error) error {
}

r.listener = listener
r.logger.Info(fmt.Sprintf("Listening on %s", listener.Addr()))
if r.config.EnablePROXY {
r.listener = &proxyproto.Listener{listener}
}

r.logger.Info(fmt.Sprintf("Listening on %s", r.listener.Addr()))

go func() {
err := server.Serve(listener)
err := server.Serve(r.listener)
r.stopLock.Lock()
if !r.stopping {
errChan <- err
Expand Down
Loading

0 comments on commit 160bec3

Please sign in to comment.