Skip to content

Commit

Permalink
Return proxy destination address in LocalAddr()
Browse files Browse the repository at this point in the history
Also extended the existing SourceCheck() mechanism to govern both
remote and local addresses.
  • Loading branch information
bboreham committed Sep 20, 2018
1 parent 8c94fb4 commit 0a8c12a
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 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,12 +128,11 @@ func (p *Conn) Close() error {
}

func (p *Conn) LocalAddr() net.Addr {
return p.conn.LocalAddr()
}

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

// RemoteAddr returns the address of the client if the proxy
Expand All @@ -145,7 +144,7 @@ func (p *Conn) DstAddr() net.Addr {
// before Read()
func (p *Conn) RemoteAddr() net.Addr {
p.checkPrefixOnce()
if p.srcAddr != nil && !p.useConnRemoteAddr {
if p.srcAddr != nil && !p.useConnAddr {
return p.srcAddr
}
return p.conn.RemoteAddr()
Expand Down

0 comments on commit 0a8c12a

Please sign in to comment.