]> Sergey Matveev's repositories - btrtrc.git/blobdiff - client.go
Use offer_id for webrtc conn string
[btrtrc.git] / client.go
index 43a4c46fd5431897f53637e3d8296ed466b20dd1..98c87171b65d1f9db372625b3ada25cbb392ce8c 100644 (file)
--- a/client.go
+++ b/client.go
@@ -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) {
        defer nc.Close()
        if tc, ok := nc.(*net.TCPConn); ok {
                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
        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
 // 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) {
-       c = cl.newConnection(nc, true, remoteAddr, network)
+func (cl *Client) handshakesConnection(ctx context.Context, nc net.Conn, t *Torrent, encryptHeader bool, remoteAddr net.Addr,
+       network, connString string,
+) (c *PeerConn, err error) {
+       c = cl.newConnection(nc, true, remoteAddr, network, connString)
        c.headerEncrypted = encryptHeader
        ctx, cancel := context.WithTimeout(ctx, cl.config.HandshakesTimeout)
        defer cancel()
@@ -640,8 +647,7 @@ func (cl *Client) handshakesConnection(ctx context.Context, nc net.Conn, t *Torr
        return
 }
 
-// Returns nil connection and nil error if no connection could be established
-// for valid reasons.
+// Returns nil connection and nil error if no connection could be established for valid reasons.
 func (cl *Client) establishOutgoingConnEx(t *Torrent, addr net.Addr, obfuscatedHeader bool) (*PeerConn, error) {
        dialCtx, cancel := context.WithTimeout(context.Background(), func() time.Duration {
                cl.rLock()
@@ -657,7 +663,7 @@ func (cl *Client) establishOutgoingConnEx(t *Torrent, addr net.Addr, obfuscatedH
                }
                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 {
                nc.Close()
        }
@@ -1228,7 +1234,7 @@ func (cl *Client) banPeerIP(ip net.IP) {
        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{
                conn:            nc,
                outgoing:        outgoing,
@@ -1238,6 +1244,7 @@ func (cl *Client) newConnection(nc net.Conn, outgoing bool, remoteAddr net.Addr,
                writeBuffer:     new(bytes.Buffer),
                remoteAddr:      remoteAddr,
                network:         network,
+               connString:      connString,
        }
        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