-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(upnp): add implementation based on IGD protocol #4156
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
Merged
Merged
Changes from all commits
Commits
Show all changes
66 commits
Select commit
Hold shift + click to select a range
bddebad
feat(protocols): Add initial UPnP protocol
jxs ba6ad5a
cargo clippy
jxs 3638fd9
fix docs
jxs 6671af3
review: address Thomas review
jxs 7edc88b
review: address Thomas review
jxs 5c59216
review: address Thomas review
jxs cb2dbbc
update copyright
jxs 4172755
rename Provider trait to Gateway
jxs 6fc4cfc
replace UnoderedFutures on the main behaviour
jxs 0eb75da
update Cargo.toml formatting
jxs 3f3f745
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs 7d679c8
revert Box<dyn Error> to String conversion.
jxs 28e0fde
clippy
jxs bdb0818
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs 293133b
address Thomas and Max review
jxs a6b170f
convert Gateway back into Provider and make it a message interface
jxs 96e3a44
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs 72eec83
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs e24cc16
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs b0f8f7a
refactor behaviour to reduce the amount of code
jxs 4a90a78
update provider to handle tasks sequentially instead of concurrently
jxs f2207b6
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs 960a50e
switch to idg-next ,
jxs 1874193
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs 92f5cea
add Changelog entries
jxs 0b326bd
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs 5384ae6
update Cargo.lock
jxs 7398842
fix docs
jxs e106ad9
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs ac78e67
update multiaddr_to_socketaddr_protocol,
jxs 13f3e74
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs 3843453
clippy
jxs 27b2442
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs 5b3b905
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs 97129b6
downgrade time and pin to 0.3.23
jxs b1affdd
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs 2a0a757
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs 550c248
publicly export upnp::Event
jxs 82cfe2a
add upnp-example
jxs b599faf
address Thomas review
jxs f3e0fe8
check if the Gateway's external addres is public
jxs 50d6d4e
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs d5c2e82
cargo clippy
jxs 1a81c76
fix docs
jxs 502ec30
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs eb54f3a
address review
jxs 3216093
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs d631d3a
remove config
jxs fe3c94f
fix examples and feature flags
jxs 98d00de
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs ef6fd56
use SearchOptions::default
jxs e480e61
feature gate everything
jxs 029eaf6
address review
jxs 194c792
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs e9b5cd6
do not do io on the behaviour,
jxs 7c8c534
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs e5cb61d
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs 316b524
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs ebc4fdb
replace example with tokio
jxs 98ba415
Merge branch 'master' of github.com:libp2p/rust-libp2p into add-upnp-…
jxs b642cd9
deprecate async-std support
jxs 4c18dab
address review
jxs 1de13e8
address missing remark on review
jxs 4b68a48
add publish false to overcome cargo semver
jxs 98108f7
Merge branch 'master' into add-upnp-protocol
jxs 1a36609
Merge branch 'master' into add-upnp-protocol
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "upnp-example" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
license = "MIT" | ||
|
||
[dependencies] | ||
tokio = { version = "1", features = [ "rt-multi-thread", "macros"] } | ||
futures = "0.3.28" | ||
libp2p = { path = "../../libp2p", features = ["tokio", "dns", "macros", "noise", "ping", "tcp", "websocket", "yamux", "upnp"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
## Description | ||
|
||
The upnp example showcases how to use the upnp network behaviour to externally open ports on the network gateway. | ||
|
||
|
||
## Usage | ||
|
||
To run the example, follow these steps: | ||
|
||
1. In a terminal window, run the following command: | ||
|
||
```sh | ||
cargo run | ||
``` | ||
|
||
2. This command will start the swarm and print the `NewExternalAddr` if the gateway supports `UPnP` or | ||
`GatewayNotFound` if it doesn't. | ||
|
||
|
||
## Conclusion | ||
|
||
The upnp example demonstrates the usage of **libp2p** to externally open a port on the gateway if it | ||
supports [`UPnP`](https://en.wikipedia.org/wiki/Universal_Plug_and_Play). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright 2023 Protocol Labs. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a | ||
// copy of this software and associated documentation files (the "Software"), | ||
// to deal in the Software without restriction, including without limitation | ||
// the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
// and/or sell copies of the Software, and to permit persons to whom the | ||
// Software is furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | ||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
// DEALINGS IN THE SOFTWARE. | ||
|
||
#![doc = include_str!("../README.md")] | ||
|
||
use futures::prelude::*; | ||
use libp2p::core::upgrade::Version; | ||
use libp2p::{ | ||
identity, noise, | ||
swarm::{SwarmBuilder, SwarmEvent}, | ||
tcp, upnp, yamux, Multiaddr, PeerId, Transport, | ||
}; | ||
use std::error::Error; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn Error>> { | ||
let local_key = identity::Keypair::generate_ed25519(); | ||
let local_peer_id = PeerId::from(local_key.public()); | ||
println!("Local peer id: {local_peer_id:?}"); | ||
|
||
let transport = tcp::tokio::Transport::default() | ||
.upgrade(Version::V1Lazy) | ||
.authenticate(noise::Config::new(&local_key)?) | ||
.multiplex(yamux::Config::default()) | ||
.boxed(); | ||
|
||
let mut swarm = SwarmBuilder::with_tokio_executor( | ||
transport, | ||
upnp::tokio::Behaviour::default(), | ||
local_peer_id, | ||
) | ||
.build(); | ||
|
||
// Tell the swarm to listen on all interfaces and a random, OS-assigned | ||
// port. | ||
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?; | ||
|
||
// Dial the peer identified by the multi-address given as the second | ||
// command-line argument, if any. | ||
if let Some(addr) = std::env::args().nth(1) { | ||
let remote: Multiaddr = addr.parse()?; | ||
swarm.dial(remote)?; | ||
println!("Dialed {addr}") | ||
} | ||
|
||
loop { | ||
match swarm.select_next_some().await { | ||
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {address:?}"), | ||
SwarmEvent::Behaviour(upnp::Event::NewExternalAddr(addr)) => { | ||
println!("New external address: {addr}"); | ||
} | ||
SwarmEvent::Behaviour(upnp::Event::GatewayNotFound) => { | ||
println!("Gateway does not support UPnP"); | ||
break; | ||
} | ||
SwarmEvent::Behaviour(upnp::Event::NonRoutableGateway) => { | ||
println!("Gateway is not exposed directly to the public Internet, i.e. it itself has a private IP address."); | ||
break; | ||
} | ||
_ => {} | ||
} | ||
} | ||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 0.1.0 - unreleased | ||
|
||
- Initial version |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
[package] | ||
name = "libp2p-upnp" | ||
edition = "2021" | ||
rust-version = "1.60.0" | ||
description = "UPnP support for libp2p transports" | ||
version = "0.1.0" | ||
license = "MIT" | ||
repository = "https://github.com/libp2p/rust-libp2p" | ||
keywords = ["peer-to-peer", "libp2p", "networking"] | ||
categories = ["network-programming", "asynchronous"] | ||
publish = false | ||
|
||
thomaseizinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[dependencies] | ||
futures = "0.3.28" | ||
futures-timer = "3.0.2" | ||
igd-next = "0.14.2" | ||
libp2p-core = { workspace = true } | ||
libp2p-swarm = { workspace = true } | ||
log = "0.4.19" | ||
void = "1.0.2" | ||
tokio = { version = "1.29", default-features = false, features = ["rt"], optional = true } | ||
|
||
[features] | ||
tokio = ["igd-next/aio_tokio", "dep:tokio"] | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.