Skip to content

Commit

Permalink
fix: set external ip to fix relay connection reservation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma233 committed Oct 21, 2024
1 parent 7d12465 commit ed96b2a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dephy-pproxy"
version = "0.4.0"
version = "0.4.1"
edition = "2021"

[dependencies]
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::net::IpAddr;
use std::net::SocketAddr;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
Expand Down Expand Up @@ -118,9 +119,10 @@ impl PProxy {
listen_addr: SocketAddr,
proxy_addr: Option<SocketAddr>,
access_server_endpoint: Option<reqwest::Url>,
external_ip: Option<IpAddr>,
) -> Result<(Self, PProxyHandle)> {
let (command_tx, command_rx) = mpsc::channel(DEFAULT_CHANNEL_SIZE);
let swarm = crate::p2p::new_swarm(keypair, listen_addr)
let swarm = crate::p2p::new_swarm(keypair, listen_addr, external_ip)
.map_err(|e| Error::Libp2pSwarmCreateError(e.to_string()))?;
let stream_control = swarm.behaviour().stream.new_control();
let access_client = access_server_endpoint.map(AccessClient::new);
Expand Down
11 changes: 11 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ fn parse_args() -> Command {
.num_args(1)
.action(ArgAction::Set)
.help("Access server endpoint is used to verify if one peer can access another. If not set, all access is allowed."),
)
.arg(
Arg::new("EXTERNAL_IP")
.long("external-ip")
.num_args(1)
.action(ArgAction::Set)
.help("External ip for relay behaviour. If not set, no external ip will be assigned."),
);

let create_tunnel_server = Command::new("create_tunnel_server")
Expand Down Expand Up @@ -142,6 +149,9 @@ async fn serve(args: &ArgMatches) {
let access_server_endpoint = args
.get_one::<String>("ACCESS_SERVER_ENDPOINT")
.map(|endpoint| Url::parse(endpoint).expect("Invalid access server endpoint"));
let external_ip = args
.get_one::<String>("EXTERNAL_IP")
.map(|addr| addr.parse().expect("Invalid external ip"));

println!("server_addr: {}", server_addr);
println!("commander_server_addr: {}", commander_server_addr);
Expand All @@ -151,6 +161,7 @@ async fn serve(args: &ArgMatches) {
server_addr,
proxy_addr,
access_server_endpoint,
external_ip,
)
.expect("Create pproxy failed");

Expand Down
11 changes: 11 additions & 0 deletions src/p2p/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub const PPROXY_PROTOCOL: StreamProtocol = StreamProtocol::new("/pproxy/1.0.0")
pub(crate) fn new_swarm(
keypair: Keypair,
listen_addr: SocketAddr,
external_ip: Option<IpAddr>,
) -> std::result::Result<Swarm<PProxyNetworkBehaviour>, Box<dyn std::error::Error>> {
let (ip_type, ip, port) = match listen_addr.ip() {
IpAddr::V4(ip) => ("ip4", ip.to_string(), listen_addr.port()),
Expand All @@ -26,6 +27,12 @@ pub(crate) fn new_swarm(

let listen_multiaddr = format!("/{ip_type}/{ip}/tcp/{port}").parse()?;

let external_multiaddr = match external_ip {
None => None,
Some(IpAddr::V4(ip)) => Some(format!("/ip4/{ip}/tcp/{port}").parse()?),
Some(IpAddr::V6(ip)) => Some(format!("/ip6/{ip}/tcp/{port}").parse()?),
};

let mut swarm = libp2p::SwarmBuilder::with_existing_identity(keypair)
.with_tokio()
.with_tcp(
Expand All @@ -40,5 +47,9 @@ pub(crate) fn new_swarm(

swarm.listen_on(listen_multiaddr)?;

if let Some(external_multiaddr) = external_multiaddr {
swarm.add_external_address(external_multiaddr);
}

Ok(swarm)
}

0 comments on commit ed96b2a

Please sign in to comment.