150 Commits

Author SHA1 Message Date
Marco Munizaga
da810a1764 feat: Migrate to log/slog (#3364) 2025-08-25 14:00:01 -07:00
sukun
90b841260e basichost: move observed address manager to basichost (#3332) 2025-08-18 12:16:16 -07:00
sukun
26a5710d35 basichost: fix deadlock with addrs_manager (#3348) 2025-07-30 17:08:12 +05:30
Piotr Galar
335c75a398 Merge branch 'master' into uci-golangci-lint 2025-06-11 10:42:45 +01:00
galargh
50f065b4df chore: fix revive issues 2025-06-11 10:00:47 +01:00
sukun
b82a39cb89 transport: rate limit new connections (#3283)
This rate limits new connections to prevent DoS attacks. 

For effectively rate limiting QUIC connections, we now gate QUIC connection attempts before the handshake, so that we don't spend compute on handshakes for connections that will eventually be cancelled. 

We can only set a single ConnContext per quic-go Transport, as there's only 1 listener per quic-go Transport. So we cannot set a different ConnContext for listeners on the same address. 

As we're now gating QUIC connections before the handshake, we use source address verification to ensure that spoofed IPs cannot DoS new connections from a particular IP. This is done by ensuring that some of the connection attempts always verify the source address. We get DoS protection at the expense of increased latency of source address verification.
2025-06-05 02:16:32 +05:30
sukun
fb1d9512e8 basichost: use autonatv2 to verify reachability (#3231)
This introduces addrsReachabilityTracker that tracks reachability on
a set of addresses. It probes reachability for addresses periodically
and has an exponential backoff in case there are too many errors
or we don't have any valid autonatv2 peer.

There's no smartness in the address selection logic currently. We just
test all provided addresses. It also doesn't use the addresses provided
by `AddrsFactory`, so currently there's no way to get a user provided
address tested for reachability, something that would be a problem for
dns addresses. I intend to introduce an alternative to
`AddrsFactory`, something like, `AnnounceAddrs(addrs []ma.Multiaddr)`
that's just appended to the set of addresses that we have, and check
reachability for those addresses.

There's only one method exposed in the BasicHost right now that's
`ReachableAddrs() []ma.Multiadd`r that returns the host's reachable
addrs. Users can also use the event `EvtHostReachableAddrsChanged`
to be notified when any addrs reachability changes.
2025-06-03 17:13:56 +05:30
sukun
6249e685e9 transport: add GatedMaListener type (#3186)
This introduces a new GatedMaListener type which gates conns
accepted from a manet.Listener with a gater and creates the rcmgr
scope for it. Explicitly passing the scope allows for many guardrails
that the previous interface assertion didn't.

This breaks the previous responsibility of the upgradeListener method
into two, one gating the connection initially, and the other upgrading
the connection with a security and muxer selection.

This split makes it easy to gate the connection with the resource
manager as early as possible. This is especially true for websocket
because we want to gate the connection just after the TCP connection is
established, and not after the tls handshake + websocket upgrade is
completed.
2025-03-25 22:09:57 +05:30
sukun
914331ba90 autorelay: send addresses on eventbus; dont wrap address factory (#3071) 2025-02-18 18:36:17 +05:30
sukun
ca611f1c4e tcpreuse: error on using tcpreuse with pnet (#3129)
Co-authored-by: Marco Munizaga <git@marcopolo.io>
2025-01-10 13:14:35 -05:00
Ivan Shvedunov
8423de3a64 basichost: avoid modifying slice returned by AddrsFactory (#3068) 2024-11-28 22:02:47 +05:30
Adin Schmahmann
5a47a90938 feat(tcpreuse): add options for sharing TCP listeners amongst TCP, WS and WSS transports (#2984)
Allows the same socket to be shared amongst TCP,WS,WSS transports.

---------

Co-authored-by: sukun <sukunrt@gmail.com>
Co-authored-by: Marco Munizaga <git@marcopolo.io>
2024-11-04 09:41:32 -08:00
Marco Munizaga
c4c3a34545 feat: Add WithFxOption (#2956)
* feat: Add WithFxOption

* Add get id service test
2024-10-22 11:36:40 -07:00
sukun
b9cb861a98 autonat: fix interaction with autorelay (#2967)
* autonat: fix interaction with autorelay

* Fix race in test

* Use deadline from context if available for DialBack

* Return hasNewAddrs correctly

* nit: cleanup contains check

* Shuffle peers

* nits

* Change comment to indicate the bug

* holepuncher: pass address function in constructor (#2979)

* holepunch: pass address function in constructor

* nit

* Remove getPublicAddrs

---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>

* Make a copy of the multiaddr slice in Addrs()

---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>
2024-10-16 20:08:43 -07:00
Marco Munizaga
e8b6685edb fix: swarm: refactor address resolution (#2990)
* Remove unused resolver in basic host

* Refactor Swarm.resolveAddrs

Refactors how DNS Address resolution works.

* lint

* Move MultiaddrDNSResolver interface to core

* Reserve output space for addresses left to resolve

* feat: core/transport: Add SkipResolver interface (#2989)

* Rebase on top of resolveAddrs refactor

* Add comments

* Sanitize address inputs when returning a reservation message (#3006)
2024-10-16 12:20:21 -07:00
sukun
8a11b7c8e4 basic_host: close swarm on Close (#2916)
Using the `BasicHost` constructor transfers the ownership of the swarm.
This is similar to how using `libp2p.New` transfers the ownership of
user provided config options like `ResourceManager`, all of which are
closed on `host.Close`
2024-08-15 18:18:35 +05:30
Marco Munizaga
db41da3b26 feat: WebRTC reuse QUIC conn (#2889)
* feat: WebRTC reuse QUIC conn

* Fix transport constructor in test

* Move provide to where the transports are
2024-08-01 07:36:46 -07:00
sukun
475cb47cae quic: add support for quic-go metrics (#2823) 2024-07-31 18:03:31 +05:30
sukun
f80d18f24c config: fix AddrFactory for AutoNAT (#2868)
---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>
2024-07-31 16:56:32 +05:30
sukun
6c178d4edf libp2p: use rcmgr for autonat dials (#2842) 2024-06-26 14:02:41 +05:30
sukun
87c435512c autonatv2: implement autonatv2 spec (#2469) 2024-06-21 14:55:22 +05:30
sukun
9f854e2a7c config: fix Insecure security constructor (#2810) 2024-05-23 19:52:22 +05:30
sukun
6861cecb3c identify: refactor observed address manager to do address mapping at thin waist(IP+TCP/UDP) layer (#2793)
* refactor observed address manager to do mapping at thin waist layer

---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>

* restrict output message size, add top level option to disable address
discovery

* Comment nit

* Increase maxPeerRecordSize

---------

Co-authored-by: Marco Munizaga <git@marcopolo.io>
2024-05-15 20:17:25 -07:00
Marco Munizaga
86a6720a62 autonat: Clean up after close (#2749)
* Clean up after autonat

* Close
2024-03-27 20:14:32 -07:00
Marten Seemann
9d149fa3cb use Fx to start and stop the host, swarm, autorelay and quicreuse (#2118)
* config: refactor AutoNAT construction into separate method

* config: use a lifecycle hook to start listening on swarm addresses

* use Fx to construct the host

* add a test for constructing a routed host

* use Fx hooks to start the host

* config: use Fx lifecycle hooks to start AutoRelay and for PeerRouting

* basichost: don't close the swarm

The swarm is not constructed by the basic host, thus is shouldn't be
closed by it.

* config: use Fx hook to close the quicreuse connection manager

* test for goroutine leaks when starting/stopping fx

To do this, I've had to move a few leaky tests into a separate package.
I've filed a bug for the AutoNAT issue (#2743) but the "error on
startup" issue is going to require some pretty invasive changes (we need
to construct _then_ start).

* go fmt

* Ignore one more top function

* Typo

* Ignore any not top

---------

Co-authored-by: Sukun <sukunrt@gmail.com>
Co-authored-by: Steven Allen <steven@stebalien.com>
Co-authored-by: Marco Munizaga <git@marcopolo.io>
2024-03-21 10:18:34 -07:00
Marten Seemann
210133267d quic: update quic-go to v0.39.1, set a static resumption token generator key (#2572) 2023-10-22 12:03:00 +05:30
piersy
7f72151bcc config: warn if connmgr limits conflict with rcmgr (#2527)
Co-authored-by: Sukun <sukunrt@gmail.com>
2023-09-25 18:39:46 +05:30
Sukun
b438d18924 host: disable black hole detection on autonat dialer (#2529) 2023-08-28 12:00:56 +05:30
Sukun
381f23057b swarm: deprecate libp2p.DialRanker option (#2430)
* swarm: deprecate libp2p.DialRanker option

* remove whitespace diff

* Apply suggestions from code review

Co-authored-by: Marten Seemann <martenseemann@gmail.com>

---------

Co-authored-by: Marten Seemann <martenseemann@gmail.com>
2023-07-13 23:16:12 -07:00
Sukun
b38652548a swarm: enable smart dialing by default (#2420) 2023-07-10 11:52:19 -07:00
Sukun
757bf59136 swarm: make black hole detection configurable (#2403)
* swarm: make black hole detection configurable

* address review comments
2023-07-06 22:42:28 -07:00
Sukun
173fef8a2e rcmgr: move StatsTraceReporter to rcmgr package (#2388) 2023-06-27 22:53:44 -07:00
Sukun
dfaeb7ed48 rcmgr: register prometheus metrics with the libp2p registerer (#2370)
* rcmgr: register prometheus metrics with the libp2p registerer

* update dashboard readme
2023-06-21 03:44:27 -07:00
VM
b4c9fed04a docs: fix some comments errors (#2356) 2023-06-13 10:43:21 -07:00
Sukun
82e6227b34 swarm: make smart-dialing opt in (#2340) 2023-06-08 22:08:03 +05:30
Sukun
6f27081cac swarm: implement smart dialing logic (#2260)
* implement smart dialing

* add more comments and tests

* change address ranking logic to dial one quic address before others

* add randomized worker loop tests

* simplify priority queue implementation

* improve DialRanker docs

* one more test

* add explanatory comments and rename variables

* fix allocations in dialQueue

* fix allocations in dialRanker

* Apply suggestions from code review

Co-authored-by: Marten Seemann <martenseemann@gmail.com>

* fix comments

* add logging

* add holepunching test

* add metrics for tracking dial prioritisation impact

clean up redundant address filtering committed

* add test for webtransport filtering

* update changelog

* fix flaky test

* update dashboard

* update dial ranking delay dashboard to use pie chart

* change <=1ms label to 'No delay' in dashboard

* add defensive check to map presence

---------

Co-authored-by: Marten Seemann <martenseemann@gmail.com>
2023-06-03 23:56:58 -07:00
Marten Seemann
0f9ad8c242 revert "feat:add contexts to all peerstore methods (#2312)" (#2328) 2023-06-03 16:55:08 +03:00
Prem Chaitanya Prathi
8864d1c33e feat:add contexts to all peerstore methods (#2312) 2023-06-03 02:24:50 -07:00
Sukun
15ec149403 autorelay: add metrics (#2185)
* autorelay: add metrics
metrics added:
relay finder status

reservation request outcomes
current reservations

candidate circuit v2 support
current candidates

relay addresses updated
num relay address

scheduled work times

* autorelay: fix refresh reservations bug

* fix max value hack

* improve tracking errors in reservation requests

* fix config-query in grafana

* add candidate loop state panel

* fix logging

* reset metrics on relayfinder stop

* update dashboard

* update dashboard
2023-04-05 16:27:26 -07:00
Marten Seemann
581a015412 swarm: emit PeerConnectedness event from swarm instead of from hosts (#1574)
* pass an event bus to the swarm constructor

* make the eventbus parameter a required swarm constructor parameter

* emit Connectedness notifications from the swarm

* remove peer connectedness watchers from hosts

* swarm: emit connectedness events when holding the mutex
2023-02-24 19:49:51 -08:00
Marco Munizaga
b74205d265 autorelay: refactor relay finder and start autorelay after identify (#2120)
* Refactor relay_finder and start autorelay after identify

* Clock fork

* Remove multiple timers and use a single rate limiting chan for findNodes

* Remove clock fork

* Rename

* Use scheduledWork.nextAllowedCallToPeerSource.Add(rf.conf.minInterval)

* Fix flaky test that relied on time
2023-02-22 21:51:59 -08:00
Sukun
a491074d89 metrics: add options to disable metrics and to set Prometheus registerer (#2116)
* provided a WithRegisterer option for metrics

* provide a libp2p.Option to setup metrics
2023-02-20 22:48:17 -08:00
Sukun
3dcb4b503d autonat: add metrics (#2086)
* add autonat metrics

* add benchmarks

* use increase instead of sum by with rate in dashboard

* add interface assertion

* add no alloc test

* update dashboard

* autonat: minor dashboard tweaks

---------

Co-authored-by: Marten Seemann <martenseemann@gmail.com>
2023-02-15 22:10:38 -08:00
Marten Seemann
8ae7a37916 use quic-go and webtransport-go from quic-go organization (#2040) 2023-02-02 18:08:44 -08:00
Marten Seemann
3919359872 swarm: add a basic metrics tracer (#1973)
* swarm: add very basic metrics for opening and closing connections

* swarm: use a sync.Pool to make metrics collection allocation-free

* swarm: introduce a MetricsTracer interface

* swarm: add the transport to the dial error metric

* swarm: add Grafana dashboard

* swarm: use the prometheus namespace option
2023-01-26 16:18:55 -08:00
Marten Seemann
f73205096f quic / webtransport: make it possible to listen on the same address / port (#1905)
* quic: add an integration test for QUIC version support

* quic: refactor the stateless reset test

* quic: simplify the interface of the noreuseConn

DecreaseCount now closes the underlying UDP conn, so that callers don't
need to pay attention if they're dealing with a reuseConn or a
noreuseConn.

* implement a quicreuse to manage QUIC connections

* quicreuse: introduce options

* config: construct the quicreuse.ConnManager using fx

* webtransport: use the quicreuse

* add integration test for QUIC and WebTranport sharing the same UDP addr

* Handle errors in accept loop goroutine

* Add comment

* Remove todo

* Rename mutexes

* Cleanup extra close

* Only log on err

* Use webtransport-go 0.4.0

* Fix expected error

Co-authored-by: Marco Munizaga <git@marcopolo.io>
2022-11-28 15:55:38 -08:00
Marten Seemann
d72d35c025 add an integration test for the ordering of security protocols 2022-11-22 10:46:32 +13:00
Marten Seemann
1ecf340f51 config: respect the user's security protocol preference order 2022-11-22 10:46:32 +13:00
Marten Seemann
e2a246d5b6 upgrader: absorb SSMuxer into the upgrader 2022-11-20 18:30:00 +13:00
Marten Seemann
133f002117 config: supply the host ID to fx
This is needed to construct the insecure transport.
2022-11-17 12:03:28 +13:00