Expose peer.RemoteAddr

This commit is contained in:
Matt Joiner 2020-07-15 16:15:38 +10:00
parent 7455b98a0f
commit 9d0ea15044
5 changed files with 28 additions and 28 deletions

View File

@ -881,7 +881,7 @@ func (cl *Client) runReceivedConn(c *PeerConn) {
).Log(cl.logger)
torrent.Add("error receiving handshake", 1)
cl.lock()
cl.onBadAccept(c.remoteAddr)
cl.onBadAccept(c.RemoteAddr)
cl.unlock()
return
}
@ -889,7 +889,7 @@ func (cl *Client) runReceivedConn(c *PeerConn) {
torrent.Add("received handshake for unloaded torrent", 1)
log.Fmsg("received handshake for unloaded torrent").SetLevel(log.Debug).Log(cl.logger)
cl.lock()
cl.onBadAccept(c.remoteAddr)
cl.onBadAccept(c.RemoteAddr)
cl.unlock()
return
}
@ -946,7 +946,7 @@ func (cl *Client) sendInitialMessages(conn *PeerConn, torrent *Torrent) {
},
V: cl.config.ExtendedHandshakeClientVersion,
Reqq: 64, // TODO: Really?
YourIp: pp.CompactIp(addrIpOrNil(conn.remoteAddr)),
YourIp: pp.CompactIp(addrIpOrNil(conn.RemoteAddr)),
Encryption: cl.config.HeaderObfuscationPolicy.Preferred || !cl.config.HeaderObfuscationPolicy.RequirePreferred,
Port: cl.incomingPeerPort(),
MetadataSize: torrent.metadataSize(),
@ -1336,7 +1336,7 @@ func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr net.Addr,
peerChoking: true,
PeerMaxRequests: 250,
remoteAddr: remoteAddr,
RemoteAddr: remoteAddr,
network: network,
connString: connString,
},

View File

@ -47,7 +47,7 @@ type peer struct {
connString string
outgoing bool
network string
remoteAddr net.Addr
RemoteAddr net.Addr
// True if the connection is operating over MSE obfuscation.
headerEncrypted bool
cryptoMethod mse.CryptoMethod
@ -151,7 +151,7 @@ func (cn *peer) expectingChunks() bool {
// Returns true if the connection is over IPv6.
func (cn *PeerConn) ipv6() bool {
ip := addrIpOrNil(cn.remoteAddr)
ip := addrIpOrNil(cn.RemoteAddr)
if ip.To4() != nil {
return false
}
@ -1119,7 +1119,7 @@ func (c *PeerConn) mainReadLoop() (err error) {
req := newRequestFromMessage(&msg)
c.onPeerSentCancel(req)
case pp.Port:
ipa, ok := tryIpPortFromNetAddr(c.remoteAddr)
ipa, ok := tryIpPortFromNetAddr(c.RemoteAddr)
if !ok {
break
}
@ -1549,11 +1549,11 @@ func (c *peer) peerPriority() (peerPriority, error) {
}
func (c *peer) remoteIp() net.IP {
return addrIpOrNil(c.remoteAddr)
return addrIpOrNil(c.RemoteAddr)
}
func (c *peer) remoteIpPort() IpPort {
ipa, _ := tryIpPortFromNetAddr(c.remoteAddr)
ipa, _ := tryIpPortFromNetAddr(c.RemoteAddr)
return IpPort{ipa.IP, uint16(ipa.Port)}
}
@ -1565,7 +1565,7 @@ func (c *PeerConn) pexPeerFlags() pp.PexPeerFlags {
if c.outgoing {
f |= pp.PexOutgoingConn
}
if c.remoteAddr != nil && strings.Contains(c.remoteAddr.Network(), "udp") {
if c.RemoteAddr != nil && strings.Contains(c.RemoteAddr.Network(), "udp") {
f |= pp.PexSupportsUtp
}
return f
@ -1573,7 +1573,7 @@ func (c *PeerConn) pexPeerFlags() pp.PexPeerFlags {
func (c *PeerConn) dialAddr() net.Addr {
if !c.outgoing && c.PeerListenPort != 0 {
switch addr := c.remoteAddr.(type) {
switch addr := c.RemoteAddr.(type) {
case *net.TCPAddr:
dialAddr := *addr
dialAddr.Port = c.PeerListenPort
@ -1584,7 +1584,7 @@ func (c *PeerConn) dialAddr() net.Addr {
return &dialAddr
}
}
return c.remoteAddr
return c.RemoteAddr
}
func (c *PeerConn) pexEvent(t pexEventType) pexEvent {

View File

@ -159,10 +159,10 @@ func TestConnPexPeerFlags(t *testing.T) {
{&PeerConn{peer: peer{outgoing: false, PeerPrefersEncryption: true}}, pp.PexPrefersEncryption},
{&PeerConn{peer: peer{outgoing: true, PeerPrefersEncryption: false}}, pp.PexOutgoingConn},
{&PeerConn{peer: peer{outgoing: true, PeerPrefersEncryption: true}}, pp.PexOutgoingConn | pp.PexPrefersEncryption},
{&PeerConn{peer: peer{remoteAddr: udpAddr}}, pp.PexSupportsUtp},
{&PeerConn{peer: peer{remoteAddr: udpAddr, outgoing: true}}, pp.PexOutgoingConn | pp.PexSupportsUtp},
{&PeerConn{peer: peer{remoteAddr: tcpAddr, outgoing: true}}, pp.PexOutgoingConn},
{&PeerConn{peer: peer{remoteAddr: tcpAddr}}, 0},
{&PeerConn{peer: peer{RemoteAddr: udpAddr}}, pp.PexSupportsUtp},
{&PeerConn{peer: peer{RemoteAddr: udpAddr, outgoing: true}}, pp.PexOutgoingConn | pp.PexSupportsUtp},
{&PeerConn{peer: peer{RemoteAddr: tcpAddr, outgoing: true}}, pp.PexOutgoingConn},
{&PeerConn{peer: peer{RemoteAddr: tcpAddr}}, 0},
}
for i, tc := range testcases {
f := tc.conn.pexPeerFlags()
@ -184,22 +184,22 @@ func TestConnPexEvent(t *testing.T) {
}{
{
pexAdd,
&PeerConn{peer: peer{remoteAddr: udpAddr}},
&PeerConn{peer: peer{RemoteAddr: udpAddr}},
pexEvent{pexAdd, udpAddr, pp.PexSupportsUtp},
},
{
pexDrop,
&PeerConn{peer: peer{remoteAddr: tcpAddr, outgoing: true, PeerListenPort: dialTcpAddr.Port}},
&PeerConn{peer: peer{RemoteAddr: tcpAddr, outgoing: true, PeerListenPort: dialTcpAddr.Port}},
pexEvent{pexDrop, tcpAddr, pp.PexOutgoingConn},
},
{
pexAdd,
&PeerConn{peer: peer{remoteAddr: tcpAddr, PeerListenPort: dialTcpAddr.Port}},
&PeerConn{peer: peer{RemoteAddr: tcpAddr, PeerListenPort: dialTcpAddr.Port}},
pexEvent{pexAdd, dialTcpAddr, 0},
},
{
pexDrop,
&PeerConn{peer: peer{remoteAddr: udpAddr, PeerListenPort: dialUdpAddr.Port}},
&PeerConn{peer: peer{RemoteAddr: udpAddr, PeerListenPort: dialUdpAddr.Port}},
pexEvent{pexDrop, dialUdpAddr, pp.PexSupportsUtp},
},
}

View File

@ -23,7 +23,7 @@ var (
func TestPexAdded(t *testing.T) {
t.Run("noHold", func(t *testing.T) {
s := new(pexState)
s.Add(&PeerConn{peer: peer{remoteAddr: addrs[0], outgoing: true}})
s.Add(&PeerConn{peer: peer{RemoteAddr: addrs[0], outgoing: true}})
targ := &pexState{
ev: []pexEvent{
pexEvent{pexAdd, addrs[0], pp.PexOutgoingConn},
@ -39,7 +39,7 @@ func TestPexAdded(t *testing.T) {
},
nc: 0,
}
s.Add(&PeerConn{peer: peer{remoteAddr: addrs[0]}})
s.Add(&PeerConn{peer: peer{RemoteAddr: addrs[0]}})
targ := &pexState{
hold: []pexEvent{
pexEvent{pexDrop, addrs[1], 0},
@ -59,7 +59,7 @@ func TestPexAdded(t *testing.T) {
},
nc: pexTargAdded,
}
s.Add(&PeerConn{peer: peer{remoteAddr: addrs[0]}})
s.Add(&PeerConn{peer: peer{RemoteAddr: addrs[0]}})
targ := &pexState{
hold: []pexEvent{},
ev: []pexEvent{
@ -75,7 +75,7 @@ func TestPexAdded(t *testing.T) {
func TestPexDropped(t *testing.T) {
t.Run("belowTarg", func(t *testing.T) {
s := &pexState{nc: 1}
s.Drop(&PeerConn{peer: peer{remoteAddr: addrs[0], pex: pexConnState{Listed: true}}})
s.Drop(&PeerConn{peer: peer{RemoteAddr: addrs[0], pex: pexConnState{Listed: true}}})
targ := &pexState{
hold: []pexEvent{pexEvent{pexDrop, addrs[0], 0}},
nc: 0,
@ -84,7 +84,7 @@ func TestPexDropped(t *testing.T) {
})
t.Run("aboveTarg", func(t *testing.T) {
s := &pexState{nc: pexTargAdded + 1}
s.Drop(&PeerConn{peer: peer{remoteAddr: addrs[0], pex: pexConnState{Listed: true}}})
s.Drop(&PeerConn{peer: peer{RemoteAddr: addrs[0], pex: pexConnState{Listed: true}}})
targ := &pexState{
ev: []pexEvent{pexEvent{pexDrop, addrs[0], 0}},
nc: pexTargAdded,
@ -93,7 +93,7 @@ func TestPexDropped(t *testing.T) {
})
t.Run("aboveTargNotListed", func(t *testing.T) {
s := &pexState{nc: pexTargAdded + 1}
s.Drop(&PeerConn{peer: peer{remoteAddr: addrs[0], pex: pexConnState{Listed: false}}})
s.Drop(&PeerConn{peer: peer{RemoteAddr: addrs[0], pex: pexConnState{Listed: false}}})
targ := &pexState{nc: pexTargAdded + 1}
require.EqualValues(t, targ, s)
})

View File

@ -206,7 +206,7 @@ func (t *Torrent) KnownSwarm() (ks []PeerInfo) {
ks = append(ks, PeerInfo{
Id: conn.PeerID,
Addr: conn.remoteAddr,
Addr: conn.RemoteAddr,
Source: conn.Discovery,
// > If the connection is encrypted, that's certainly enough to set SupportsEncryption.
// > But if we're not connected to them with an encrypted connection, I couldn't say
@ -245,7 +245,7 @@ func (t *Torrent) addrActive(addr string) bool {
return true
}
for c := range t.conns {
ra := c.remoteAddr
ra := c.RemoteAddr
if ra.String() == addr {
return true
}