feat(brahman-net+handshake): swarm-level deny via libp2p block_list
Optimizacion de seguridad: la denylist ya no espera al handshake brahman para rechazar — ahora se proyecta al block_list behaviour del swarm libp2p. Conexiones desde peers baneados son rechazadas ANTES del Noise handshake, ahorrando el round-trip TCP+Noise por cada intento denegado. brahman-net: - Nuevo behaviour block_list: allow_block_list::Behaviour<BlockedPeers> añadido al BrahmanBehaviour derivado. Default vacio. - Nuevos comandos BlockPeer / UnblockPeer en el enum interno. - API publica: BrahmanNet::block_peer / unblock_peer. Idempotentes. - Dep nueva: libp2p-allow-block-list 0.6 (sub-crate, no es feature de libp2p en 0.56). brahman_handshake::peer_policy: - PeerPolicy gana net: Arc<RwLock<Option<Arc<BrahmanNet>>>>. Default None preserva callers existentes. - Nuevo attach_to_net(net): sync inicial (block_peer por cada en deny) + guarda net para diff-sync en cada reload. - reload extendido: snapshot prev_deny ANTES de mutar inner. Tras mutar, sync_deny_to_swarm aplica block/unblock por cada added/removed. - Atomicidad preservada: si parse falla, sync no ocurre y la version anterior persiste tanto en policy como en block_list. ente-zero: tras setup_brahman_net + setup_brahman_policy, si AMBOS estan presentes -> policy.attach_to_net(net.clone()) con log informativo. Tests: 1 nuevo E2E swarm_level_deny_blocks_before_noise. A configura policy con deny + attach_to_net. Cliente baneado intenta connect_libp2p; en lugar del Unauthorized del handshake, ahora falla con error de transporte/stream o timeout — el dial nunca completa porque el swarm rechaza la conexion. 5 tests verdes en network_libp2p.rs. 31 tests totales en brahman- handshake + brahman-net. Trade-offs documentados: - Mas eficiente contra DoS (no consume CPU del Noise por peer baneado). - Misma fuente de verdad: PeerPolicy. Swarm es cache derivado, sync via diff en cada reload, sin drift posible. - El handshake-level gate sigue activo como segunda linea (defensa en profundidad si por bug/race un peer baneado pasa el block_list).
This commit is contained in:
@@ -51,6 +51,7 @@ use libp2p::{
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, yamux, Swarm, SwarmBuilder,
|
||||
};
|
||||
use libp2p_allow_block_list::{self as allow_block_list, BlockedPeers};
|
||||
use libp2p_stream as stream;
|
||||
use tokio::sync::{mpsc, oneshot, Mutex};
|
||||
|
||||
@@ -66,6 +67,13 @@ const IDLE_CONNECTION_TIMEOUT: Duration = Duration::from_secs(60);
|
||||
|
||||
#[derive(NetworkBehaviour)]
|
||||
struct BrahmanBehaviour {
|
||||
/// Block-list a nivel de swarm: peers en este behaviour son
|
||||
/// rechazados ANTES del handshake Noise. Más eficiente que
|
||||
/// rechazar al nivel del handshake brahman (ahorra round-trip
|
||||
/// TCP+Noise por intento denegado). Sincronizado con la
|
||||
/// `PeerPolicy.deny` vía `block_peer`/`unblock_peer` exposed
|
||||
/// en `BrahmanNet`.
|
||||
block_list: allow_block_list::Behaviour<BlockedPeers>,
|
||||
stream: stream::Behaviour,
|
||||
kad: kad::Behaviour<kad::store::MemoryStore>,
|
||||
identify: identify::Behaviour,
|
||||
@@ -86,6 +94,8 @@ enum Command {
|
||||
StartProviding(Vec<u8>),
|
||||
StopProviding(Vec<u8>),
|
||||
GetProviders(Vec<u8>, oneshot::Sender<Vec<PeerId>>),
|
||||
BlockPeer(PeerId),
|
||||
UnblockPeer(PeerId),
|
||||
}
|
||||
|
||||
/// Peer descubierto vía DHT: identidad + direcciones conocidas.
|
||||
@@ -164,6 +174,7 @@ impl BrahmanNet {
|
||||
.with_agent_version(format!("brahman-net/{}", env!("CARGO_PKG_VERSION"))),
|
||||
);
|
||||
BrahmanBehaviour {
|
||||
block_list: allow_block_list::Behaviour::default(),
|
||||
stream: stream::Behaviour::new(),
|
||||
kad,
|
||||
identify,
|
||||
@@ -228,6 +239,12 @@ impl BrahmanNet {
|
||||
let qid = swarm.behaviour_mut().kad.get_providers(key.into());
|
||||
pending_providers.insert(qid, (Vec::new(), tx));
|
||||
}
|
||||
Command::BlockPeer(peer) => {
|
||||
swarm.behaviour_mut().block_list.block_peer(peer);
|
||||
}
|
||||
Command::UnblockPeer(peer) => {
|
||||
swarm.behaviour_mut().block_list.unblock_peer(peer);
|
||||
}
|
||||
}
|
||||
}
|
||||
event = swarm.select_next_some() => {
|
||||
@@ -323,6 +340,21 @@ impl BrahmanNet {
|
||||
self.keypair.clone()
|
||||
}
|
||||
|
||||
/// Bloquea conexiones desde/hacia `peer` a nivel del swarm.
|
||||
/// Conexiones existentes se cierran y nuevos intentos son
|
||||
/// rechazados ANTES del Noise handshake — más eficiente que
|
||||
/// rechazar al nivel del handshake brahman (ahorra round-trip
|
||||
/// TCP+Noise por intento). Idempotente.
|
||||
pub fn block_peer(&self, peer: PeerId) {
|
||||
let _ = self.cmd_tx.send(Command::BlockPeer(peer));
|
||||
}
|
||||
|
||||
/// Quita a `peer` de la block-list del swarm. Conexiones futuras
|
||||
/// son aceptadas con normalidad. Idempotente.
|
||||
pub fn unblock_peer(&self, peer: PeerId) {
|
||||
let _ = self.cmd_tx.send(Command::UnblockPeer(peer));
|
||||
}
|
||||
|
||||
/// Empieza a escuchar en `addr`. Bloquea hasta que el listener
|
||||
/// publique su dirección real (Multiaddr resuelta — útil cuando
|
||||
/// pediste `/ip4/0.0.0.0/tcp/0` y querés saber qué puerto te tocó).
|
||||
|
||||
Reference in New Issue
Block a user