Automate port-forwarding via UPnP
masterrust-libp2p supports automating port-forwarding using protocols like UPnP. This serves as a complementary technique to hole punching.repository·master·Indexed 26 days ago
https://github.com/libp2p/rust-libp2pThe central implementation of the libp2p specification in Rust. It provides core networking primitives, transport protocols, and application-level protocols for building decentralized peer-to-peer applications. The project includes libp2p-core for fundamental traits and structs (such as Transport, StreamMuxer, PeerId, and Multiaddr), libp2p-stream for inbound and outbound stream interaction, and support for various transports including QUIC, WebTransport, and WebRTC.
rust-libp2p supports automating port-forwarding using protocols like UPnP. This serves as a complementary technique to hole punching.rust-libp2p, which wraps quinn-proto. This provides a QUIC-based transport layer for peer-to-peer connectivity.To run the WebTransport tests manually, you must first set up an echo server using Docker, then execute the tests using wasm-pack in a browser environment.
Run the following commands to build and start the server on your host network:
docker build -t webtransport-echo-server echo-server
docker run -it --rm --network=host webtransport-echo-serverIn a separate terminal, use wasm-pack to run the tests in Chrome.
For interactive testing (visible browser):
wasm-pack test --chromeAfter running this, navigate to http://127.0.0.1:8000 in your browser.
For headless testing (no visible browser):
wasm-pack test --chrome --headlesschromedriver have the same major version.chromedriver is not in your PATH, specify it using the --chromedriver flag:wasm-pack test --chrome --headless --chromedriver=/path/to/chromedriverdocker build -t webtransport-echo-server echo-server
docker run -it --rm --network=host webtransport-echo-server
wasm-pack test --chromerust-libp2p, refer to the official documentation on docs.rs/libp2p. For practical implementation guidance, explore the examples/ directory in the repository, which contains small binaries demonstrating various protocols and common Transport configurations.When designing asynchronous systems where a command eventually results in a response via an event, ensure that the command includes a unique identifier. This identifier must be included in the subsequent asynchronous response event so that users can reliably match responses to their original requests (e.g., matching a specific connection request to a specific new connection).
struct Command {
id: Id,
// ...
}
struct Response {
command_id: Id,
// ...
}Depending on your needs, use the following channels for communication:
rust-libp2p repository.libp2p-webrtc-websys crate provides WebRTC transport capabilities for Rust libp2p when running in a browser environment via web-sys bindings. To use this transport, you must initialize your Swarm using Swarm::with_wasm_executor to enable the wasm-bindgen executor required for browser compatibility.To initiate a new outbound stream to a specific peer for a particular protocol, use the Control::open_stream method. This method is asynchronous and returns the stream once the connection is established.
# fn main() {
# use libp2p_swarm::{Swarm, StreamProtocol};
# use libp2p_stream as stream;
# use libp2p_identity::PeerId;
let mut swarm: Swarm<stream::Behaviour> = todo!();
let peer_id: PeerId = todo!();
let mut control = swarm.behaviour().new_control();
let protocol_future = async move {
let stream = control.open_stream(peer_id, StreamProtocol::new("/my-protocol")).await.unwrap();
// Execute your protocol here using `stream`.
};
# }To run interop tests against all released libp2p versions, you must have the libp2p/test-plans repository checked out. Perform these steps from the root directory of the rust-libp2p repository:
docker build -t rust-libp2p-head . -f interop-tests/Dockerfile.test-plans directory: (cd <path to >/libp2p/test-plans/multidim-interop/ && make).npm run test from the multidim-interop directory, passing the path to the rust-libp2p root and a name filter.To run the WebAssembly (WASM) tests in rust-libp2p, you must install the following dependencies:
chromedriver must match your installed version of Chrome/Chromium.You can execute the WASM tests by running the provided shell scripts located in the wasm-tests directory.
Use run-all.sh to run the full suite or run.sh for the standard test execution.