Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(autonat): Implement AutoNATv2 #5526

Merged
merged 248 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
248 commits
Select commit Hold shift + click to select a range
326a2b9
Proposed API design
umgefahren Sep 27, 2023
25e6ccf
Implement proposed changes to the Transport trait.
umgefahren Sep 27, 2023
85ea10a
Merge branch 'master' into transport-redesign
umgefahren Sep 27, 2023
b90eac7
Merge branch 'master' into transport-redesign
umgefahren Sep 28, 2023
c9e33e1
Reworked proposed API changes
umgefahren Sep 28, 2023
dc47b08
Merge branch 'master' into transport-redesign
umgefahren Sep 28, 2023
461494b
Introduced listener presence structure. Allows for efficient detectio…
umgefahren Sep 29, 2023
309fad6
Merge remote-tracking branch 'origin/transport-redesign' into transpo…
umgefahren Sep 29, 2023
d6ea5bc
Continous updates and BTree
umgefahren Sep 29, 2023
9f6ffa5
Merge branch 'master' into transport-redesign
umgefahren Sep 29, 2023
9cc3ff5
Make for better testability
umgefahren Sep 29, 2023
5570b60
Nevermind let's use HashMap
umgefahren Sep 29, 2023
cfd8786
Resolved conflict
umgefahren Sep 29, 2023
31ad325
Little error
umgefahren Sep 29, 2023
dbd3277
Write more comments and switch to u16.
umgefahren Sep 29, 2023
6c11a8b
extremly reduced complexity
umgefahren Oct 4, 2023
9aa1df2
extremly reduced complexity and removed the now unnecessary files
umgefahren Oct 4, 2023
092c3ba
Merge branch 'master' into transport-redesign
umgefahren Oct 4, 2023
0fbe7e7
extremly reduced complexity and removed the now unnecessary files
umgefahren Oct 4, 2023
3a9e865
Merge branch 'master' into transport-redesign
umgefahren Oct 5, 2023
d7a0fb4
Merge into single function
umgefahren Oct 5, 2023
a33cb6b
Add first tests
umgefahren Oct 6, 2023
35b8002
I wrote so many tests i think i'm gonna be insane
umgefahren Oct 6, 2023
283c4e6
Make tests pass
umgefahren Oct 7, 2023
b7dec26
Remove major mistake
umgefahren Oct 7, 2023
842c8a0
Merge branch 'master' into transport-redesign
umgefahren Oct 9, 2023
9053dc7
Implement discussed changes
umgefahren Oct 11, 2023
1df8949
Merge branch 'master' into transport-redesign
umgefahren Oct 11, 2023
cb0558c
Merge branch 'master' into transport-redesign
umgefahren Oct 12, 2023
3487ae3
Implement port use propagation
umgefahren Oct 17, 2023
d9385de
Fix the behaviours
umgefahren Oct 17, 2023
7747bc1
Merge branch 'master' into transport-redesign
umgefahren Oct 18, 2023
2755696
Make the tests pass sometimes
umgefahren Oct 19, 2023
c4796ac
Transition TCP
umgefahren Oct 19, 2023
2856b6f
Format files
umgefahren Oct 19, 2023
3ccd2f7
Merge branch 'master' into transport-redesign
umgefahren Oct 19, 2023
ccd9025
Merge branch 'master' into transport-redesign
umgefahren Oct 20, 2023
99df328
Apply suggestions from code review
umgefahren Oct 20, 2023
0c2000e
Implement all suggestions
umgefahren Oct 20, 2023
49a0d3e
Remove some stuff, make tests pass
umgefahren Oct 21, 2023
a735f64
Merge branch 'master' into transport-redesign
umgefahren Oct 21, 2023
23207bd
Address another issue
umgefahren Oct 22, 2023
9597d53
Apply suggestions from code review
umgefahren Oct 23, 2023
6352e65
Implemented really the lateset review
umgefahren Oct 23, 2023
3f7249a
Merge branch 'master' into transport-redesign
umgefahren Oct 23, 2023
8e9d6ce
Merge branch 'master' into transport-redesign
umgefahren Oct 28, 2023
f33c3be
Merge branch 'master' into transport-redesign
umgefahren Oct 30, 2023
6cc5d7c
Implement the request response part of AutoNATv2
umgefahren Oct 30, 2023
6d44a1e
First steps of implementing the client
umgefahren Nov 7, 2023
81cd442
Extract ip-global and implemented lot of behaviour
umgefahren Nov 8, 2023
c0fd889
Intermediate commit
umgefahren Nov 18, 2023
4ac520d
Resolved conflict
umgefahren Nov 18, 2023
fadf904
WIP client
umgefahren Nov 19, 2023
60b4ccc
Run cargo fix
umgefahren Nov 19, 2023
71487fc
Correct minor things
umgefahren Nov 20, 2023
0da3753
Resolving merge conflict
umgefahren Nov 20, 2023
63b31e9
Implement suggestions
umgefahren Nov 20, 2023
546d2b2
Merged master
umgefahren Dec 1, 2023
c16235f
Remove gloabl only
umgefahren Dec 1, 2023
3bc107e
Initial server implementation
umgefahren Dec 1, 2023
50fb5e4
Pass the basic test for the first time.
umgefahren Dec 6, 2023
d34782e
Move forwards to fewer bug fixes
umgefahren Dec 6, 2023
5b489c6
Merge branch 'master' into transport-redesign
thomaseizinger Dec 6, 2023
d1bd57d
Resolve merge conflict
umgefahren Dec 6, 2023
f9075dc
Merge branch 'libp2p:master' into transport-redesign
umgefahren Dec 12, 2023
fee9de1
Make the things actually expire
umgefahren Dec 12, 2023
faa694a
Tests are working
umgefahren Dec 12, 2023
7d361ff
Merge branch 'master' into transport-redesign
umgefahren Dec 19, 2023
b01bae5
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Dec 19, 2023
6282cae
Implement in call suggested changes
umgefahren Dec 22, 2023
d711fe1
Merge branch 'master' into transport-redesign
umgefahren Dec 22, 2023
dab99d2
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Dec 22, 2023
da2a790
Implement all suggestions from the PR review
umgefahren Dec 22, 2023
b2bb8f3
Merge branch 'master' into transport-redesign
umgefahren Dec 27, 2023
be35e80
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Dec 27, 2023
101a325
Implement latest suggestions
umgefahren Dec 27, 2023
bf2662a
Implement old suggestions i overlooked
umgefahren Dec 27, 2023
efdbe35
Resolve conflict
umgefahren Dec 27, 2023
c7350f9
Port dns transport
umgefahren Dec 27, 2023
3079aa9
Port request response
umgefahren Dec 27, 2023
596bb06
Port websocket
umgefahren Dec 27, 2023
37cd309
Port allow block list and connection limits
umgefahren Dec 27, 2023
9eea564
Address more concerns
umgefahren Dec 29, 2023
9a85209
Move autonat down
umgefahren Dec 29, 2023
4399bab
Move autonatv2 to autonat
umgefahren Dec 29, 2023
663055e
Format code
thomaseizinger Dec 30, 2023
ba232cb
Alphabetically sort members list
thomaseizinger Dec 30, 2023
22b96b3
Undo changes to global-only transport
thomaseizinger Dec 30, 2023
a19d7c8
Make deprecation warning work
thomaseizinger Dec 30, 2023
46b8e84
Avoid diff in v1
thomaseizinger Dec 30, 2023
037aa99
Undo behaviour change in v1
thomaseizinger Dec 30, 2023
230ad08
Rename client config fields
thomaseizinger Dec 30, 2023
7cbf413
Streamline creation of `peer_info` state
thomaseizinger Dec 30, 2023
16b4d7c
Rewrite pending nonce handling
thomaseizinger Dec 30, 2023
fdc0c2d
A server might not support autonat on every connection
thomaseizinger Dec 30, 2023
a78c265
Prefer early-exit over `else`
thomaseizinger Dec 30, 2023
8f6c27a
Use shorthand field logging syntax
thomaseizinger Dec 30, 2023
170270a
Fix variable name
thomaseizinger Dec 30, 2023
589cf6c
Improve log message
thomaseizinger Dec 30, 2023
feea82f
Log error using `fmt::Display`
thomaseizinger Dec 30, 2023
8b0a544
Remove `server` field from `InternalStatusUpdate`
thomaseizinger Dec 30, 2023
86849e5
Loop instead of repeating code
thomaseizinger Dec 30, 2023
372d88d
Remove set that we never write to
thomaseizinger Dec 30, 2023
a2fcadd
Remove superflous tests
thomaseizinger Dec 30, 2023
90d7d07
Handle tick reset next to polling
thomaseizinger Dec 30, 2023
2a6ad94
Rewrite generation of dial requests
thomaseizinger Dec 30, 2023
b2d147f
Add docs and additional log statements for no candidates
thomaseizinger Dec 30, 2023
7e3da61
Add more docs and rename protocol name constants
thomaseizinger Dec 30, 2023
e2fe41d
Inline `DialBack` struct
thomaseizinger Dec 30, 2023
1e1aeca
Simplify handling of closed connection
thomaseizinger Dec 30, 2023
d5724c1
ConnectionIds are unique, meaning we can always just insert
thomaseizinger Dec 30, 2023
de91c55
Remove handling of `DialFailure`
thomaseizinger Dec 30, 2023
3719440
Disable connection for future autonat requests on error
thomaseizinger Dec 30, 2023
7b6d354
Remove unnecessary trait implementations
thomaseizinger Dec 30, 2023
8ce2a01
Add comment
thomaseizinger Dec 30, 2023
42ac03c
Rewrite dial-request to always index by nonce
thomaseizinger Dec 30, 2023
3181113
Reset TestStatus on errors
thomaseizinger Dec 30, 2023
0641b4b
Merge branch 'master' into transport-redesign
umgefahren Dec 30, 2023
c51f8cc
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Dec 30, 2023
88b1092
Merge pull request #2 from libp2p/autonatv2-thomas-followup
umgefahren Dec 30, 2023
4d96c33
Delay and use Result
umgefahren Dec 30, 2023
8a135ea
Perform sleep correct
umgefahren Dec 30, 2023
fac1196
Remove is_local
umgefahren Dec 30, 2023
9ed44d9
Remove the unnecessary check for confirmation
umgefahren Dec 30, 2023
a974707
Fix deprecation
umgefahren Dec 30, 2023
b04e9d5
Merge branch 'master' into transport-redesign
umgefahren Jan 28, 2024
117075f
Merge completed
umgefahren Jan 28, 2024
3f2476a
Implement first jan suggestions
umgefahren Jan 28, 2024
9254faa
Implement an example and a public tester
umgefahren Jan 30, 2024
4dd8db2
Merge branch 'master' into transport-redesign
umgefahren Feb 4, 2024
54e4c1f
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Feb 4, 2024
d723c50
Port quic
umgefahren Feb 4, 2024
daf2a0b
Make it compile
umgefahren Feb 5, 2024
6b85b82
Allow deprecated items
umgefahren Feb 5, 2024
e29e1c3
Merge branch 'master' into transport-redesign
umgefahren Feb 5, 2024
e6afb5a
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Feb 5, 2024
0b94a01
Make the tests run
umgefahren Feb 5, 2024
a09e26d
Implement the latest changes to the protocol
umgefahren Feb 14, 2024
61ae32e
Fix minor nits
umgefahren Feb 14, 2024
ad6ad8f
Implement DialBackOk correctly
umgefahren Feb 14, 2024
fbdc85f
Format
umgefahren Feb 14, 2024
553511c
Fix issues raised by clippy
umgefahren Feb 14, 2024
9bf52d4
Update protocols/autonat/src/v2/client/behaviour.rs
umgefahren Feb 19, 2024
70f41f3
Update protocols/autonat/src/v2/client/handler/dial_back.rs
umgefahren Feb 19, 2024
0773694
Update protocols/autonat/src/v2/client/handler/dial_back.rs
umgefahren Feb 19, 2024
89acd28
Make error richer and trace it at a different point
umgefahren Feb 19, 2024
d571e51
Avoid stream close
umgefahren Feb 19, 2024
10e4cee
Better error handling
umgefahren Feb 19, 2024
d4d671e
Correct one failing CI and change the license
umgefahren Feb 19, 2024
7617c5d
Merge branch 'master' into transport-redesign
umgefahren Feb 19, 2024
2dc574e
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Feb 19, 2024
330fc51
Correct minor things
umgefahren Mar 6, 2024
952cf54
Merge branch 'master' into transport-redesign
umgefahren Mar 6, 2024
5f6f2f6
Resolve merge conflict
umgefahren Mar 6, 2024
367273a
Add quic and dns to the example
umgefahren Mar 7, 2024
d79bca3
Remove unused import
umgefahren Mar 7, 2024
8df0183
Change dial opts to make wasm tests pass
umgefahren Mar 7, 2024
5d24d03
Remove futures-time to compile on wasm
umgefahren Mar 7, 2024
41804be
Fix concerns by clippy
umgefahren Mar 7, 2024
bd495f9
Format
umgefahren Mar 7, 2024
38be2b4
Use workaround to make generated code CI pass
umgefahren Mar 7, 2024
ce7596c
Correct webtransport test
umgefahren Mar 7, 2024
0506c7b
Handle strange variable unused on windows
umgefahren Mar 7, 2024
0b6dbb1
Remove clippy nit
umgefahren Mar 7, 2024
d2fddd5
Simplify a little bit and get rid of every performance opt
umgefahren Mar 7, 2024
8f1fcbb
Version bumps and added author
umgefahren Mar 8, 2024
8095e84
Upstream transport changes
umgefahren Mar 8, 2024
0d5dcba
Merge branch 'master' into transport-redesign
umgefahren Apr 30, 2024
a1ae4e9
Update protocols/autonat/src/v2/client/handler/dial_request.rs
umgefahren Apr 30, 2024
e640920
Resolve merge conflict
umgefahren Apr 30, 2024
480337e
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren Apr 30, 2024
a84c998
Format
umgefahren Apr 30, 2024
1d6727f
Merge branch 'master' into transport-redesign
umgefahren May 8, 2024
0dc5003
Resolve concern
umgefahren May 8, 2024
0918ea8
Remove deprecated
umgefahren May 8, 2024
b07a463
Merge from master
umgefahren May 8, 2024
0724086
Merge branch 'transport-redesign' into autonat-v2-transport-changes
umgefahren May 8, 2024
45863c3
Formatting
umgefahren May 8, 2024
e7dd2ec
Merge branch 'master' into implement-autonat-v2
umgefahren May 8, 2024
7f389b6
Merge branch 'transport-redesign' into implement-autonat-v2
umgefahren May 8, 2024
a38d35f
Merge pull request #3 from umgefahren/autonat-v2-transport-changes
umgefahren May 14, 2024
86650bd
Don't close stream
umgefahren May 14, 2024
5c2658a
Merge master
umgefahren May 14, 2024
dd39a16
Merge master
umgefahren May 14, 2024
e41f667
identify detects if address translation is required
stormshield-ebzh May 16, 2024
235447b
homogeneize fully-p2p-qualified addresses to avoid double probe
stormshield-ebzh May 16, 2024
6103cd8
feat(nat): get rid of Transport::address_translation
stormshield-ebzh May 22, 2024
4b16e42
Thomas remarks
stormshield-ebzh May 23, 2024
0f538f3
removed Transport::address_translation and moved generic address_tran…
stormshield-ebzh May 23, 2024
7bea1c2
Merge pull request #4 from stormshield-ebzh/transport-redesign-addres…
umgefahren Jun 1, 2024
efd5882
Update lock file
umgefahren Jun 4, 2024
0c750ef
Update transport to always use a new endpoint when dialing without reuse
umgefahren Jun 4, 2024
7c78fee
Merge master
umgefahren Jun 4, 2024
aa7af5b
No reference in observed
umgefahren Jun 4, 2024
51b1333
Remove now defunct test
umgefahren Jun 4, 2024
4c3382f
Add changelog to core
umgefahren Jun 4, 2024
3764d10
Remove expected output /p2p/
umgefahren Jun 4, 2024
f506edd
Port reuse test
umgefahren Jun 4, 2024
64b0136
Merge branch 'master' into transport-redesign
umgefahren Jun 4, 2024
9458749
I don't need Protocol anymore
umgefahren Jun 4, 2024
20319cf
Restore doc test
umgefahren Jun 4, 2024
8b136cf
Add CHANGELOG entries
umgefahren Jun 4, 2024
aaa8c7c
Update versions in root Cargo.toml
umgefahren Jun 4, 2024
725c564
Bump autonat version
umgefahren Jun 4, 2024
1253ee3
Update versions (again)
umgefahren Jun 4, 2024
9d84b7c
Merge latest changes
umgefahren Jun 4, 2024
df2592b
Correct bad merge
umgefahren Jun 4, 2024
75e8011
Apply suggestions from code review
umgefahren Jun 6, 2024
6e0839d
Address review comments
umgefahren Jun 9, 2024
f266619
Merge master
umgefahren Jun 9, 2024
158c017
Remove HEAD
umgefahren Jun 9, 2024
1dc9f4b
Format
umgefahren Jun 9, 2024
3dbc6c9
Make compilable
umgefahren Jun 9, 2024
0b20c21
Correct the webrtc-utils version
umgefahren Jun 9, 2024
269783c
Merge branch 'master' into transport-redesign
umgefahren Jun 11, 2024
7fc5546
Address @thomaseizinger comments
umgefahren Jun 11, 2024
cba6fb9
Revert bad changes from #9d84b7c
umgefahren Jun 11, 2024
09204d4
Revert bad changes from #9d84b7c
umgefahren Jun 11, 2024
cba4113
Fix clippy issues
umgefahren Jun 11, 2024
6a632b5
Don't format
umgefahren Jun 14, 2024
0be96af
Merge branch 'master' into transport-redesign
umgefahren Jun 14, 2024
3359e60
Correct bad merge
umgefahren Jun 14, 2024
fc14ba8
Delete unused files
umgefahren Jun 14, 2024
73eddc7
Fix lock
umgefahren Jun 14, 2024
03c2717
Detect if local host supports ipv6
umgefahren Jun 14, 2024
236b356
Revert "Detect if local host supports ipv6"
umgefahren Jun 14, 2024
b0adbd7
Detect reachability through catching the error
umgefahren Jun 14, 2024
4fac6da
Compile on wasm and make clippy happy
umgefahren Jun 14, 2024
ff806a3
Switch to protobuf version 3
umgefahren Jun 14, 2024
75a86c2
Revert network unreachable detection
umgefahren Jun 16, 2024
73584cd
Merge branch 'master' into implement-autonat-v2
umgefahren Aug 3, 2024
5fb6715
Fix issues after merge
umgefahren Aug 3, 2024
0a566d4
Add changelog entry
umgefahren Aug 4, 2024
3e85ffb
Add documentation
umgefahren Aug 4, 2024
531e8df
Merge branch 'master' into implement-autonat-v2
umgefahren Aug 5, 2024
921f5f1
Undo a bad merge and update changelog for silent roll out
umgefahren Aug 5, 2024
bd76259
Merge branch 'master' into implement-autonat-v2
umgefahren Aug 5, 2024
bf45de8
Update protocols/autonat/CHANGELOG.md
umgefahren Aug 6, 2024
ea419f7
Update protocols/autonat/CHANGELOG.md
umgefahren Aug 6, 2024
6d1b431
this file is useless
umgefahren Aug 6, 2024
431d7b6
Trigger CI
umgefahren Aug 6, 2024
475c76b
Include doc cfg
umgefahren Aug 6, 2024
80981d9
Trigger CI
umgefahren Aug 6, 2024
c5a21f3
Merge branch 'master' into implement-autonat-v2
umgefahren Aug 6, 2024
61b1f2d
Correct bad merge
umgefahren Aug 6, 2024
75646ca
Merge branch 'master' into implement-autonat-v2
umgefahren Aug 7, 2024
123c3a4
Merge branch 'master' into implement-autonat-v2
umgefahren Aug 8, 2024
ec56cbc
Merge branch 'master' into implement-autonat-v2
mergify[bot] Aug 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 196 additions & 31 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"core",
"examples/autonat",
"examples/autonatv2",
"examples/browser-webrtc",
"examples/chat",
"examples/dcutr",
Expand Down
38 changes: 38 additions & 0 deletions examples/autonatv2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[package]
name = "autonatv2"
version = "0.1.0"
edition = "2021"
publish = false
license = "MIT or Apache-2.0"

[package.metadata.release]
release = false

[[bin]]
name = "autonatv2_client"

[[bin]]
name = "autonatv2_server"

[dependencies]
libp2p = { workspace = true, features = ["macros", "tokio", "tcp", "noise", "yamux", "autonat", "identify", "dns", "quic"] }
clap = { version = "4.4.18", features = ["derive"] }
tokio = { version = "1.35.1", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
rand = "0.8.5"
opentelemetry = { version = "0.21.0", optional = true }
opentelemetry_sdk = { version = "0.21.1", optional = true, features = ["rt-tokio"] }
tracing-opentelemetry = { version = "0.22.0", optional = true }
opentelemetry-jaeger = { version = "0.20.0", optional = true, features = ["rt-tokio"] }
cfg-if = "1.0.0"

[features]
jaeger = ["opentelemetry", "opentelemetry_sdk", "tracing-opentelemetry", "opentelemetry-jaeger"]
opentelemetry = ["dep:opentelemetry"]
opentelemetry_sdk = ["dep:opentelemetry_sdk"]
tracing-opentelemetry = ["dep:tracing-opentelemetry"]
opentelemetry-jaeger = ["dep:opentelemetry-jaeger"]

[lints]
workspace = true
20 changes: 20 additions & 0 deletions examples/autonatv2/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM rust:1.75-alpine as builder

RUN apk add musl-dev

WORKDIR /workspace
COPY . .
RUN --mount=type=cache,target=./target \
--mount=type=cache,target=/usr/local/cargo/registry \
cargo build --release --package autonatv2 --bin autonatv2_server -F jaeger

RUN --mount=type=cache,target=./target \
mv ./target/release/autonatv2_server /usr/local/bin/autonatv2_server

FROM alpine:latest

COPY --from=builder /usr/local/bin/autonatv2_server /app/autonatv2_server

EXPOSE 4884

ENTRYPOINT [ "/app/autonatv2_server", "-l", "4884" ]
16 changes: 16 additions & 0 deletions examples/autonatv2/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '3'

services:
autonatv2:
build:
context: ../..
dockerfile: examples/autonatv2/Dockerfile
ports:
- 4884:4884
jaeger:
image: jaegertracing/all-in-one
ports:
- 6831:6831/udp
- 6832:6832/udp
- 16686:16686
- 14268:14268
111 changes: 111 additions & 0 deletions examples/autonatv2/src/bin/autonatv2_client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use std::{error::Error, net::Ipv4Addr, time::Duration};

use clap::Parser;
use libp2p::{
autonat,
futures::StreamExt,
identify, identity,
multiaddr::Protocol,
noise,
swarm::{dial_opts::DialOpts, NetworkBehaviour, SwarmEvent},
tcp, yamux, Multiaddr, SwarmBuilder,
};
use rand::rngs::OsRng;
use tracing_subscriber::EnvFilter;

#[derive(Debug, Parser)]
#[clap(name = "libp2p autonatv2 client")]
struct Opt {
/// Port where the client will listen for incoming connections.
#[clap(short = 'p', long, default_value_t = 0)]
listen_port: u16,

/// Address of the server where want to connect to.
#[clap(short = 'a', long)]
server_address: Multiaddr,

/// Probe interval in seconds.
#[clap(short = 't', long, default_value = "2")]
probe_interval: u64,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.try_init();

let opt = Opt::parse();

let mut swarm = SwarmBuilder::with_new_identity()
.with_tokio()
.with_tcp(
tcp::Config::default(),
noise::Config::new,
yamux::Config::default,
)?
.with_quic()
.with_dns()?
.with_behaviour(|key| Behaviour::new(key.public(), opt.probe_interval))?
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(10)))
.build();

swarm.listen_on(
Multiaddr::empty()
.with(Protocol::Ip4(Ipv4Addr::UNSPECIFIED))
.with(Protocol::Tcp(opt.listen_port)),
)?;

swarm.dial(
DialOpts::unknown_peer_id()
.address(opt.server_address)
.build(),
)?;

loop {
match swarm.select_next_some().await {
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening on {address:?}");
}
SwarmEvent::Behaviour(BehaviourEvent::Autonat(autonat::v2::client::Event {
server,
tested_addr,
bytes_sent,
result: Ok(()),
})) => {
println!("Tested {tested_addr} with {server}. Sent {bytes_sent} bytes for verification. Everything Ok and verified.");
}
SwarmEvent::Behaviour(BehaviourEvent::Autonat(autonat::v2::client::Event {
server,
tested_addr,
bytes_sent,
result: Err(e),
})) => {
println!("Tested {tested_addr} with {server}. Sent {bytes_sent} bytes for verification. Failed with {e:?}.");
}
SwarmEvent::ExternalAddrConfirmed { address } => {
println!("External address confirmed: {address}");
}
_ => {}
}
}
}

#[derive(NetworkBehaviour)]
pub struct Behaviour {
autonat: autonat::v2::client::Behaviour,
identify: identify::Behaviour,
}

impl Behaviour {
pub fn new(key: identity::PublicKey, probe_interval: u64) -> Self {
Self {
autonat: autonat::v2::client::Behaviour::new(
OsRng,
autonat::v2::client::Config::default()
.with_probe_interval(Duration::from_secs(probe_interval)),
),
identify: identify::Behaviour::new(identify::Config::new("/ipfs/0.1.0".into(), key)),
}
}
}
87 changes: 87 additions & 0 deletions examples/autonatv2/src/bin/autonatv2_server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use std::{error::Error, net::Ipv4Addr, time::Duration};

use cfg_if::cfg_if;
use clap::Parser;
use libp2p::{
autonat,
futures::StreamExt,
identify, identity,
multiaddr::Protocol,
noise,
swarm::{NetworkBehaviour, SwarmEvent},
tcp, yamux, Multiaddr, SwarmBuilder,
};
use rand::rngs::OsRng;

#[derive(Debug, Parser)]
#[clap(name = "libp2p autonatv2 server")]
struct Opt {
#[clap(short, long, default_value_t = 0)]
listen_port: u16,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
cfg_if! {
if #[cfg(feature = "jaeger")] {
use tracing_subscriber::layer::SubscriberExt;
use opentelemetry_sdk::runtime::Tokio;
let tracer = opentelemetry_jaeger::new_agent_pipeline()
.with_endpoint("jaeger:6831")
.with_service_name("autonatv2")
.install_batch(Tokio)?;
let telemetry = tracing_opentelemetry::layer().with_tracer(tracer);
let subscriber = tracing_subscriber::Registry::default()
.with(telemetry);
} else {
let subscriber = tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.finish();
}
}
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");

let opt = Opt::parse();

let mut swarm = SwarmBuilder::with_new_identity()
.with_tokio()
.with_tcp(
tcp::Config::default(),
noise::Config::new,
yamux::Config::default,
)?
.with_quic()
.with_dns()?
.with_behaviour(|key| Behaviour::new(key.public()))?
.with_swarm_config(|c| c.with_idle_connection_timeout(Duration::from_secs(60)))
.build();

swarm.listen_on(
Multiaddr::empty()
.with(Protocol::Ip4(Ipv4Addr::UNSPECIFIED))
.with(Protocol::Tcp(opt.listen_port)),
)?;

loop {
match swarm.select_next_some().await {
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"),
SwarmEvent::Behaviour(event) => println!("{event:?}"),
e => println!("{e:?}"),
}
}
}

#[derive(NetworkBehaviour)]
pub struct Behaviour {
autonat: autonat::v2::server::Behaviour,
identify: identify::Behaviour,
}

impl Behaviour {
pub fn new(key: identity::PublicKey) -> Self {
Self {
autonat: autonat::v2::server::Behaviour::new(OsRng),
identify: identify::Behaviour::new(identify::Config::new("/ipfs/0.1.0".into(), key)),
}
}
}
7 changes: 7 additions & 0 deletions protocols/autonat/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
- Due to the refactor of `Transport` it's no longer required to create a seperate transport for
AutoNAT where port reuse is disabled. This information is now passed by the behaviour.
See [PR 4568](https://github.com/libp2p/rust-libp2p/pull/4568).
- Introduce the new AutoNATv2 protocol.
It's split into a client and a server part, represented in their respective modules
Features:
- The server now always dials back over a newly allocated port.
This more accurately reflects the reachability state for other peers and avoids accidental hole punching.
- The server can now test addresses different from the observed address (i.e., the connection to the server was made through a `p2p-circuit`). To mitigate against DDoS attacks, the client has to send more data to the server than the dial-back costs.
See [PR 5526](https://github.com/libp2p/rust-libp2p/pull/5526).

<!-- Update to libp2p-swarm v0.45.0 -->

Expand Down
31 changes: 23 additions & 8 deletions protocols/autonat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,47 @@ name = "libp2p-autonat"
edition = "2021"
rust-version = { workspace = true }
description = "NAT and firewall detection for libp2p"
authors = ["David Craven <david@craven.ch>", "Elena Frank <elena.frank@protonmail.com>"]
version = "0.13.0"
authors = ["David Craven <david@craven.ch>", "Elena Frank <elena.frank@protonmail.com>", "Hannes Furmans <hannes@umgefahren.xyz>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]


[dependencies]
async-trait = "0.1"
async-trait = { version = "0.1", optional = true }
asynchronous-codec = { workspace = true }
bytes = { version = "1", optional = true }
either = { version = "1.9.0", optional = true }
futures = { workspace = true }
futures-bounded = { workspace = true, optional = true }
futures-timer = "3.0"
web-time = { workspace = true }
web-time = { workspace = true, optional = true }
libp2p-core = { workspace = true }
libp2p-swarm = { workspace = true }
libp2p-request-response = { workspace = true }
libp2p-identity = { workspace = true }
libp2p-request-response = { workspace = true, optional = true }
libp2p-swarm = { workspace = true }
quick-protobuf = "0.8"
rand = "0.8"
tracing = { workspace = true }
quick-protobuf-codec = { workspace = true }
asynchronous-codec = { workspace = true }
rand = "0.8"
rand_core = { version = "0.6", optional = true }
thiserror = { version = "1.0.52", optional = true }
void = { version = "1", optional = true }

[dev-dependencies]
tokio = { version = "1", features = ["macros", "rt", "sync"]}
async-std = { version = "1.10", features = ["attributes"] }
libp2p-swarm-test = { path = "../../swarm-test" }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
libp2p-identify = { workspace = true }
libp2p-swarm = { workspace = true, features = ["macros"]}

[features]
default = ["v1", "v2"]
v1 = ["dep:libp2p-request-response", "dep:web-time", "dep:async-trait"]
v2 = ["dep:bytes", "dep:either", "dep:futures-bounded", "dep:thiserror", "dep:void", "dep:rand_core"]

# Passing arguments to the docsrs builder in order to properly document cfg's.
# More information: https://docs.rs/about/builds#cross-compiling
Expand Down
Loading
Loading