Use offer_id for webrtc conn string

This commit is contained in:
Matt Joiner 2020-04-13 14:04:34 +10:00
parent 3e5c6d3d44
commit f45dac1e38
6 changed files with 24 additions and 13 deletions

View File

@ -461,12 +461,17 @@ func (cl *Client) acceptConnections(l net.Listener) {
} }
} }
func regularConnString(nc net.Conn) string {
return fmt.Sprintf("%s-%s", nc.LocalAddr(), nc.RemoteAddr())
}
func (cl *Client) incomingConnection(nc net.Conn) { func (cl *Client) incomingConnection(nc net.Conn) {
defer nc.Close() defer nc.Close()
if tc, ok := nc.(*net.TCPConn); ok { if tc, ok := nc.(*net.TCPConn); ok {
tc.SetLinger(0) tc.SetLinger(0)
} }
c := cl.newConnection(nc, false, nc.RemoteAddr(), nc.RemoteAddr().Network()) c := cl.newConnection(nc, false, nc.RemoteAddr(), nc.RemoteAddr().Network(),
regularConnString(nc))
c.Discovery = PeerSourceIncoming c.Discovery = PeerSourceIncoming
cl.runReceivedConn(c) cl.runReceivedConn(c)
} }
@ -623,8 +628,10 @@ func (cl *Client) noLongerHalfOpen(t *Torrent, addr string) {
// Performs initiator handshakes and returns a connection. Returns nil *connection if no connection // Performs initiator handshakes and returns a connection. Returns nil *connection if no connection
// for valid reasons. // for valid reasons.
func (cl *Client) handshakesConnection(ctx context.Context, nc net.Conn, t *Torrent, encryptHeader bool, remoteAddr net.Addr, network string) (c *PeerConn, err error) { func (cl *Client) handshakesConnection(ctx context.Context, nc net.Conn, t *Torrent, encryptHeader bool, remoteAddr net.Addr,
c = cl.newConnection(nc, true, remoteAddr, network) network, connString string,
) (c *PeerConn, err error) {
c = cl.newConnection(nc, true, remoteAddr, network, connString)
c.headerEncrypted = encryptHeader c.headerEncrypted = encryptHeader
ctx, cancel := context.WithTimeout(ctx, cl.config.HandshakesTimeout) ctx, cancel := context.WithTimeout(ctx, cl.config.HandshakesTimeout)
defer cancel() defer cancel()
@ -640,8 +647,7 @@ func (cl *Client) handshakesConnection(ctx context.Context, nc net.Conn, t *Torr
return return
} }
// Returns nil connection and nil error if no connection could be established // Returns nil connection and nil error if no connection could be established for valid reasons.
// for valid reasons.
func (cl *Client) establishOutgoingConnEx(t *Torrent, addr net.Addr, obfuscatedHeader bool) (*PeerConn, error) { func (cl *Client) establishOutgoingConnEx(t *Torrent, addr net.Addr, obfuscatedHeader bool) (*PeerConn, error) {
dialCtx, cancel := context.WithTimeout(context.Background(), func() time.Duration { dialCtx, cancel := context.WithTimeout(context.Background(), func() time.Duration {
cl.rLock() cl.rLock()
@ -657,7 +663,7 @@ func (cl *Client) establishOutgoingConnEx(t *Torrent, addr net.Addr, obfuscatedH
} }
return nil, errors.New("dial failed") return nil, errors.New("dial failed")
} }
c, err := cl.handshakesConnection(context.Background(), nc, t, obfuscatedHeader, addr, dr.Network) c, err := cl.handshakesConnection(context.Background(), nc, t, obfuscatedHeader, addr, dr.Network, regularConnString(nc))
if err != nil { if err != nil {
nc.Close() nc.Close()
} }
@ -1228,7 +1234,7 @@ func (cl *Client) banPeerIP(ip net.IP) {
cl.badPeerIPs[ip.String()] = struct{}{} cl.badPeerIPs[ip.String()] = struct{}{}
} }
func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr net.Addr, network string) (c *PeerConn) { func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr net.Addr, network, connString string) (c *PeerConn) {
c = &PeerConn{ c = &PeerConn{
conn: nc, conn: nc,
outgoing: outgoing, outgoing: outgoing,
@ -1238,6 +1244,7 @@ func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr net.Addr,
writeBuffer: new(bytes.Buffer), writeBuffer: new(bytes.Buffer),
remoteAddr: remoteAddr, remoteAddr: remoteAddr,
network: network, network: network,
connString: connString,
} }
c.logger = cl.logger.WithValues(c, c.logger = cl.logger.WithValues(c,
log.Debug, // I want messages to default to debug, and can set it here as it's only used by new code log.Debug, // I want messages to default to debug, and can set it here as it's only used by new code

View File

@ -43,6 +43,7 @@ type PeerConn struct {
t *Torrent t *Torrent
// The actual Conn, used for closing, and setting socket options. // The actual Conn, used for closing, and setting socket options.
conn net.Conn conn net.Conn
connString string
outgoing bool outgoing bool
network string network string
remoteAddr net.Addr remoteAddr net.Addr
@ -280,7 +281,7 @@ func (cn *PeerConn) downloadRate() float64 {
func (cn *PeerConn) writeStatus(w io.Writer, t *Torrent) { func (cn *PeerConn) writeStatus(w io.Writer, t *Torrent) {
// \t isn't preserved in <pre> blocks? // \t isn't preserved in <pre> blocks?
fmt.Fprintf(w, "%+-55q %s %s-%s\n", cn.PeerID, cn.PeerExtensionBytes, cn.localAddr(), cn.remoteAddr) fmt.Fprintf(w, "%+-55q %s %s\n", cn.PeerID, cn.PeerExtensionBytes, cn.connString)
fmt.Fprintf(w, " last msg: %s, connected: %s, last helpful: %s, itime: %s, etime: %s\n", fmt.Fprintf(w, " last msg: %s, connected: %s, last helpful: %s, itime: %s, etime: %s\n",
eventAgeString(cn.lastMessageReceived), eventAgeString(cn.lastMessageReceived),
eventAgeString(cn.completedHandshake), eventAgeString(cn.completedHandshake),

View File

@ -23,7 +23,7 @@ func TestSendBitfieldThenHave(t *testing.T) {
config: TestingConfig(), config: TestingConfig(),
} }
cl.initLogger() cl.initLogger()
c := cl.newConnection(nil, false, nil, "") c := cl.newConnection(nil, false, nil, "", "")
c.setTorrent(cl.newTorrent(metainfo.Hash{}, nil)) c.setTorrent(cl.newTorrent(metainfo.Hash{}, nil))
c.t.setInfo(&metainfo.Info{ c.t.setInfo(&metainfo.Info{
Pieces: make([]byte, metainfo.HashSize*3), Pieces: make([]byte, metainfo.HashSize*3),
@ -107,7 +107,7 @@ func BenchmarkConnectionMainReadLoop(b *testing.B) {
t.setChunkSize(defaultChunkSize) t.setChunkSize(defaultChunkSize)
t._pendingPieces.Set(0, PiecePriorityNormal.BitmapPriority()) t._pendingPieces.Set(0, PiecePriorityNormal.BitmapPriority())
r, w := net.Pipe() r, w := net.Pipe()
cn := cl.newConnection(r, true, nil, "") cn := cl.newConnection(r, true, nil, "", "")
cn.setTorrent(t) cn.setTorrent(t)
mrlErr := make(chan error) mrlErr := make(chan error)
msg := pp.Message{ msg := pp.Message{

View File

@ -1276,6 +1276,7 @@ func (t *Torrent) onWebRtcConn(
false, false,
webrtcNetAddr{dcc.Remote}, webrtcNetAddr{dcc.Remote},
webrtcNetwork, webrtcNetwork,
fmt.Sprintf("webrtc offer_id %x", dcc.OfferId),
) )
if err != nil { if err != nil {
t.logger.Printf("error in handshaking webrtc connection: %v", err) t.logger.Printf("error in handshaking webrtc connection: %v", err)

View File

@ -41,6 +41,7 @@ func binaryToJsonString(b []byte) string {
type DataChannelContext struct { type DataChannelContext struct {
Local, Remote webrtc.SessionDescription Local, Remote webrtc.SessionDescription
OfferId string
LocalOffered bool LocalOffered bool
} }
@ -146,7 +147,7 @@ func (c *Client) trackerReadLoop() error {
} }
switch { switch {
case ar.Offer != nil: case ar.Offer != nil:
_, answer, err := NewTransportFromOffer(*ar.Offer, c.onConn) _, answer, err := NewTransportFromOffer(*ar.Offer, c.onConn, ar.OfferID)
if err != nil { if err != nil {
return fmt.Errorf("write AnnounceResponse: %w", err) return fmt.Errorf("write AnnounceResponse: %w", err)
} }
@ -184,6 +185,7 @@ func (c *Client) trackerReadLoop() error {
c.onConn(dc, DataChannelContext{ c.onConn(dc, DataChannelContext{
Local: offer.originalOffer, Local: offer.originalOffer,
Remote: *ar.Answer, Remote: *ar.Answer,
OfferId: ar.OfferID,
LocalOffered: true, LocalOffered: true,
}) })
}) })

View File

@ -67,7 +67,7 @@ func NewTransport() (*Transport, webrtc.SessionDescription, error) {
// NewTransportFromOffer creates a transport from a WebRTC offer and and returns a WebRTC answer to // NewTransportFromOffer creates a transport from a WebRTC offer and and returns a WebRTC answer to
// be announced. // be announced.
func NewTransportFromOffer(offer webrtc.SessionDescription, onOpen onDataChannelOpen) (*Transport, webrtc.SessionDescription, error) { func NewTransportFromOffer(offer webrtc.SessionDescription, onOpen onDataChannelOpen, offerId string) (*Transport, webrtc.SessionDescription, error) {
peerConnection, err := newPeerConnection() peerConnection, err := newPeerConnection()
if err != nil { if err != nil {
return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to peer connection: %v", err) return nil, webrtc.SessionDescription{}, fmt.Errorf("failed to peer connection: %v", err)
@ -92,7 +92,7 @@ func NewTransportFromOffer(offer webrtc.SessionDescription, onOpen onDataChannel
t.dc = d t.dc = d
t.lock.Unlock() t.lock.Unlock()
t.handleOpen(func(dc datachannel.ReadWriteCloser) { t.handleOpen(func(dc datachannel.ReadWriteCloser) {
onOpen(dc, DataChannelContext{answer, offer, false}) onOpen(dc, DataChannelContext{answer, offer, offerId, false})
}) })
}) })
err = peerConnection.SetLocalDescription(answer) err = peerConnection.SetLocalDescription(answer)