]> Sergey Matveev's repositories - btrtrc.git/blobdiff - client.go
Attribute accepted connection to holepunching when connect message is late
[btrtrc.git] / client.go
index a87e44391f657508406c243b74df06602c81b030..f1a054abc6708a223e3c4159de7e53cb0c35ce84 100644 (file)
--- a/client.go
+++ b/client.go
@@ -498,6 +498,22 @@ func (cl *Client) acceptConnections(l Listener) {
        for {
                conn, err := l.Accept()
                torrent.Add("client listener accepts", 1)
+               if err == nil {
+                       holepunchAddr, holepunchErr := addrPortFromPeerRemoteAddr(conn.RemoteAddr())
+                       if holepunchErr == nil {
+                               cl.lock()
+                               if g.MapContains(cl.undialableWithoutHolepunch, holepunchAddr) {
+                                       setAdd(&cl.accepted, holepunchAddr)
+                               }
+                               if g.MapContains(
+                                       cl.undialableWithoutHolepunchDialedAfterHolepunchConnect,
+                                       holepunchAddr,
+                               ) {
+                                       setAdd(&cl.probablyOnlyConnectedDueToHolepunch, holepunchAddr)
+                               }
+                               cl.unlock()
+                       }
+               }
                conn = pproffd.WrapNetConn(conn)
                cl.rLock()
                closed := cl.closed.IsSet()
@@ -516,20 +532,6 @@ func (cl *Client) acceptConnections(l Listener) {
                        log.Fmsg("error accepting connection: %s", err).LogLevel(log.Debug, cl.logger)
                        continue
                }
-               {
-                       holepunchAddr, holepunchErr := addrPortFromPeerRemoteAddr(conn.RemoteAddr())
-                       if holepunchErr == nil {
-                               cl.lock()
-                               if g.MapContains(
-                                       cl.undialableWithoutHolepunchDialedAfterHolepunchConnect,
-                                       holepunchAddr,
-                               ) {
-                                       g.MakeMapIfNil(&cl.probablyOnlyConnectedDueToHolepunch)
-                                       g.MapInsert(cl.probablyOnlyConnectedDueToHolepunch, holepunchAddr, struct{}{})
-                               }
-                               cl.unlock()
-                       }
-               }
                go func() {
                        if reject != nil {
                                torrent.Add("rejected accepted connections", 1)
@@ -761,17 +763,6 @@ func (cl *Client) dialAndCompleteHandshake(opts outgoingConnOpts) (c *PeerConn,
                }
        }
        holepunchAddr, holepunchAddrErr := addrPortFromPeerRemoteAddr(addr)
-       if holepunchAddrErr == nil && opts.receivedHolepunchConnect {
-               cl.lock()
-               if g.MapContains(cl.undialableWithoutHolepunch, holepunchAddr) {
-                       g.MakeMapIfNilAndSet(
-                               &cl.undialableWithoutHolepunchDialedAfterHolepunchConnect,
-                               holepunchAddr,
-                               struct{}{},
-                       )
-               }
-               cl.unlock()
-       }
        headerObfuscationPolicy := opts.HeaderObfuscationPolicy
        obfuscatedHeaderFirst := headerObfuscationPolicy.Preferred
        firstDialResult := dialPool.getFirst()