import (
"net/netip"
+
+ g "github.com/anacrolix/generics"
)
+func setAdd[K comparable](m *map[K]struct{}, elem K) {
+ g.MakeMapIfNilAndSet(m, elem, struct{}{})
+}
+
type clientHolepunchAddrSets struct {
undialableWithoutHolepunch map[netip.AddrPort]struct{}
undialableWithoutHolepunchDialedAfterHolepunchConnect map[netip.AddrPort]struct{}
dialableOnlyAfterHolepunch map[netip.AddrPort]struct{}
dialedSuccessfullyAfterHolepunchConnect map[netip.AddrPort]struct{}
probablyOnlyConnectedDueToHolepunch map[netip.AddrPort]struct{}
+ accepted map[netip.AddrPort]struct{}
}
type ClientStats struct {
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()
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)
}
}
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()
}
return nil
case utHolepunch.Connect:
- t.logger.Printf("got holepunch connect request for %v from %p", msg.AddrPort, sender)
+ holepunchAddr := msg.AddrPort
+ t.logger.Printf("got holepunch connect request for %v from %p", holepunchAddr, sender)
+ if g.MapContains(t.cl.undialableWithoutHolepunch, holepunchAddr) {
+ setAdd(&t.cl.undialableWithoutHolepunchDialedAfterHolepunchConnect, holepunchAddr)
+ if g.MapContains(t.cl.accepted, holepunchAddr) {
+ setAdd(&t.cl.probablyOnlyConnectedDueToHolepunch, holepunchAddr)
+ }
+ }
opts := outgoingConnOpts{
peerInfo: PeerInfo{
Addr: msg.AddrPort,