Skip to content

Commit

Permalink
fix: reconnect node after ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
Ma233 committed Oct 18, 2024
1 parent 2ec4998 commit 7d12465
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub enum Error {
SocketAddrParseError(String),
#[error("Failed to extract peer id from multiaddr: {0}")]
FailedToExtractPeerIdFromMultiaddr(String),
#[error("Unknown peer: {0}")]
UnknownPeer(String),
#[error("PeerId parse error: {0}")]
PeerIdParseError(String),
#[error("TunnelId parse error: {0}")]
Expand Down
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub struct PProxy {
command_tx: mpsc::Sender<(PProxyCommand, CommandNotifier)>,
command_rx: mpsc::Receiver<(PProxyCommand, CommandNotifier)>,
swarm: Swarm<PProxyNetworkBehaviour>,
known_peers: HashMap<PeerId, Multiaddr>,
stream_control: libp2p_stream::Control,
inbound_tunnels: HashMap<(PeerId, TunnelId), Tunnel>,
proxy_addr: Option<SocketAddr>,
Expand Down Expand Up @@ -129,6 +130,7 @@ impl PProxy {
command_tx: command_tx.clone(),
command_rx,
swarm,
known_peers: HashMap::new(),
stream_control,
inbound_tunnels: HashMap::new(),
proxy_addr,
Expand Down Expand Up @@ -272,7 +274,8 @@ impl PProxy {
peer_id: PeerId,
tx: CommandNotifier,
) -> Result<()> {
self.swarm.dial(multiaddr)?;
self.swarm.dial(multiaddr.clone())?;
self.known_peers.insert(peer_id, multiaddr);
tx.send(Ok(PProxyCommandResponse::AddPeer { peer_id }))
.map_err(|_| Error::EssentialTaskClosed)
}
Expand All @@ -296,6 +299,14 @@ impl PProxy {
tunnel_id: TunnelId,
tx: CommandNotifier,
) -> Result<()> {
let multiaddr = self
.known_peers
.get(&peer_id)
.ok_or_else(|| Error::UnknownPeer(peer_id.to_string()))?;
if let Err(e) = self.swarm.dial(multiaddr.clone()) {
tracing::debug!("failed to dial to peer when connect tunnel: {e:?}");
}

let mut remote_stream = self
.stream_control
.open_stream(peer_id, PPROXY_PROTOCOL)
Expand Down

0 comments on commit 7d12465

Please sign in to comment.