Merge branch 'expose-dstaddr' of https://github.com/weaveworks/go-proxyproto into HEAD

This commit is contained in:
Dolf Schimmel
2017-04-23 18:13:37 +02:00

View File

@@ -99,6 +99,11 @@ func (p *Conn) LocalAddr() net.Addr {
return p.conn.LocalAddr() return p.conn.LocalAddr()
} }
func (p *Conn) DstAddr() net.Addr {
p.checkPrefixOnce()
return p.dstAddr
}
// RemoteAddr returns the address of the client if the proxy // RemoteAddr returns the address of the client if the proxy
// protocol is being used, otherwise just returns the address of // protocol is being used, otherwise just returns the address of
// the socket peer. If there is an error parsing the header, the // the socket peer. If there is an error parsing the header, the
@@ -107,13 +112,7 @@ func (p *Conn) LocalAddr() net.Addr {
// client is slow. Using a Deadline is recommended if this is called // client is slow. Using a Deadline is recommended if this is called
// before Read() // before Read()
func (p *Conn) RemoteAddr() net.Addr { func (p *Conn) RemoteAddr() net.Addr {
p.once.Do(func() { p.checkPrefixOnce()
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 { if p.srcAddr != nil {
return p.srcAddr return p.srcAddr
} }
@@ -132,6 +131,16 @@ func (p *Conn) SetWriteDeadline(t time.Time) error {
return p.conn.SetWriteDeadline(t) 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 { func (p *Conn) checkPrefix() error {
if p.proxyHeaderTimeout != 0 { if p.proxyHeaderTimeout != 0 {
readDeadLine := time.Now().Add(p.proxyHeaderTimeout) readDeadLine := time.Now().Add(p.proxyHeaderTimeout)