Skip to content

Commit

Permalink
Merge pull request #96 from mwcproject/v5.3.3/sync
Browse files Browse the repository at this point in the history
Fix inbound connection for tor
  • Loading branch information
bayk authored Dec 24, 2024
2 parents 0634581 + b5b2dd3 commit e19454b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
5 changes: 5 additions & 0 deletions p2p/src/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ impl Peers {
self.store.get_peer(peer_addr).map_err(From::from)
}

/// Get and delete peer from the store by address. It is needed for peer renaming
pub fn delete_peer(&self, peer_addr: &PeerAddr) -> Result<(), Error> {
self.store.delete_peer(peer_addr).map_err(From::from)
}

/// Whether we've already seen a peer with the provided address
pub fn exists_peer(&self, peer_addr: &PeerAddr) -> Result<bool, Error> {
self.store.exists_peer(peer_addr).map_err(From::from)
Expand Down
40 changes: 32 additions & 8 deletions p2p/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,46 @@ impl MessageHandler for Protocol {
Consumed::None
}
Message::TorAddress(tor_address) => {
info!(
debug!(
"TorAddress received from {:?}, address = {:?}",
self.peer_info, tor_address
);

let new_peer_addr = PeerAddr::Onion(tor_address.address.clone());
error!("new peer = {:?}", new_peer_addr);
info!("Adding newly connected Healthy peer {:?}", new_peer_addr);

let peer = self.server.peers.get_peer(&self.peer_info.addr);
if let Err(e) = self.server.peers.delete_peer(&self.peer_info.addr) {
warn!(
"Unable to delete expected TOR temp peer: {}, Error: {}",
self.peer_info.addr, e
);
}
match peer {
Ok(mut peer) => {
peer.addr = new_peer_addr.clone();
self.server.peers.save_peer(&peer)?;
}
Err(e) => {
warn!(
"Unable to delete expected TOR temp peer: {}, Error: {}",
self.peer_info.addr, e
);
}
}

if self.server.peers.is_banned(&new_peer_addr) {
let peer = self.server.peers.get_peer(&self.peer_info.addr)?;
warn!("banned peer tried to connect! {:?}", peer);
} else {
let peer = self.server.peers.get_peer(&self.peer_info.addr);
if peer.is_ok() {
let mut peer = peer.unwrap();
peer.addr = new_peer_addr;
self.server.peers.save_peer(&peer)?;
if let Err(e) = self.server.peers.ban_peer(
&peer.addr,
ReasonForBan::BadHandshake,
"banned peer tried to connect",
) {
warn!(
"Unable to ban tried to connect peer {}, Error: {}",
peer.addr, e
);
}
}
Consumed::None
Expand Down
8 changes: 3 additions & 5 deletions p2p/src/serv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,9 @@ impl Server {
self.sync_state.clone(),
self.clone(),
)?;
// if we are using TOR, we must reject the local addressed because it comes from the proxy
// Will add peer after it share the TOR address
if self.self_onion_address.is_none() {
self.peers.add_connected(Arc::new(peer))?;
}
// if we are using TOR, it will be the local addressed because it comes from the proxy
// Will still need to save all the peers and renameit after peer will share the TOR address
self.peers.add_connected(Arc::new(peer))?;
Ok(())
}

Expand Down
1 change: 0 additions & 1 deletion p2p/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ impl PeerStore {
}

/// TODO - allow below added to avoid github issue reports
#[allow(dead_code)]
pub fn delete_peer(&self, peer_addr: &PeerAddr) -> Result<(), Error> {
let batch = self.db.batch_write()?;
batch.delete(&peer_key(peer_addr)[..])?;
Expand Down

0 comments on commit e19454b

Please sign in to comment.