Skip to content

Commit

Permalink
Merge pull request #7 from weaveworks/expose-dstaddr
Browse files Browse the repository at this point in the history
Add a method to expose the proxy protocol destination address.
  • Loading branch information
armon authored Feb 11, 2019
2 parents 5b7edb6 + 0a8c12a commit 68259f7
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Conn struct {
conn net.Conn
dstAddr *net.TCPAddr
srcAddr *net.TCPAddr
useConnRemoteAddr bool
useConnAddr bool
once sync.Once
proxyHeaderTimeout time.Duration
}
Expand All @@ -71,18 +71,18 @@ func (p *Listener) Accept() (net.Conn, error) {
if err != nil {
return nil, err
}
var useConnRemoteAddr bool
var useConnAddr bool
if p.SourceCheck != nil {
allowed, err := p.SourceCheck(conn.RemoteAddr())
if err != nil {
return nil, err
}
if !allowed {
useConnRemoteAddr = true
useConnAddr = true
}
}
newConn := NewConn(conn, p.ProxyHeaderTimeout)
newConn.useConnRemoteAddr = useConnRemoteAddr
newConn.useConnAddr = useConnAddr
return newConn, nil
}

Expand Down Expand Up @@ -128,6 +128,10 @@ func (p *Conn) Close() error {
}

func (p *Conn) LocalAddr() net.Addr {
p.checkPrefixOnce()
if p.dstAddr != nil && !p.useConnAddr {
return p.dstAddr
}
return p.conn.LocalAddr()
}

Expand All @@ -139,14 +143,8 @@ func (p *Conn) LocalAddr() net.Addr {
// client is slow. Using a Deadline is recommended if this is called
// before Read()
func (p *Conn) RemoteAddr() net.Addr {
p.once.Do(func() {
if err := p.checkPrefix(); err != nil && err != io.EOF {
log.Printf("[ERR] Failed to read proxy prefix: %v", err)
p.Close()
p.bufReader = bufio.NewReader(p.conn)
}
})
if p.srcAddr != nil && !p.useConnRemoteAddr {
p.checkPrefixOnce()
if p.srcAddr != nil && !p.useConnAddr {
return p.srcAddr
}
return p.conn.RemoteAddr()
Expand All @@ -164,6 +162,16 @@ func (p *Conn) SetWriteDeadline(t time.Time) error {
return p.conn.SetWriteDeadline(t)
}

func (p *Conn) checkPrefixOnce() {
p.once.Do(func() {
if err := p.checkPrefix(); err != nil && err != io.EOF {
log.Printf("[ERR] Failed to read proxy prefix: %v", err)
p.Close()
p.bufReader = bufio.NewReader(p.conn)
}
})
}

func (p *Conn) checkPrefix() error {
if p.proxyHeaderTimeout != 0 {
readDeadLine := time.Now().Add(p.proxyHeaderTimeout)
Expand Down

0 comments on commit 68259f7

Please sign in to comment.