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:
@@ -6,6 +6,74 @@ ratio/diff ver `git show <sha>`.
|
|||||||
|
|
||||||
## 2026-05-09
|
## 2026-05-09
|
||||||
|
|
||||||
|
### feat(brahman-net+handshake): swarm-level deny — la denylist se proyecta al block_list de libp2p
|
||||||
|
Optimización 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.
|
||||||
|
|
||||||
|
Wire de bajo nivel (`brahman-net`):
|
||||||
|
- Nuevo behaviour `block_list: allow_block_list::Behaviour<BlockedPeers>`
|
||||||
|
añadido al `BrahmanBehaviour` derivado. Vive junto a `stream`,
|
||||||
|
`kad`, `identify`. Default vacío al construir.
|
||||||
|
- Nuevos comandos `BlockPeer(PeerId)` y `UnblockPeer(PeerId)` en el
|
||||||
|
enum interno + handlers que llaman
|
||||||
|
`swarm.behaviour_mut().block_list.{block_peer,unblock_peer}`.
|
||||||
|
- API pública: `BrahmanNet::block_peer(peer)` y
|
||||||
|
`BrahmanNet::unblock_peer(peer)`. Idempotentes.
|
||||||
|
- Dep nueva: `libp2p-allow-block-list = "0.6"` (sub-crate, no es
|
||||||
|
feature de `libp2p` en 0.56).
|
||||||
|
|
||||||
|
Wire en la política (`brahman_handshake::peer_policy`):
|
||||||
|
- `PeerPolicy` gana campo opcional `net: Arc<RwLock<Option<Arc<BrahmanNet>>>>`.
|
||||||
|
Default `None` para preservar callers existentes.
|
||||||
|
- Nuevo método `attach_to_net(net: Arc<BrahmanNet>)`:
|
||||||
|
- Sincronización inicial: itera la deny actual y llama
|
||||||
|
`net.block_peer(p)` por cada uno.
|
||||||
|
- Guarda el net para diff-sync en cada `reload`.
|
||||||
|
- `reload()` extendido: snapshot de `prev_deny` ANTES de mutar el
|
||||||
|
inner. Tras la mutación, llama `sync_deny_to_swarm(prev, new)`
|
||||||
|
que aplica `block_peer` por cada added y `unblock_peer` por cada
|
||||||
|
removed.
|
||||||
|
- Atomicidad preservada: si un archivo falla al parsear, el sync
|
||||||
|
no ocurre y la versión anterior persiste tanto en la policy
|
||||||
|
como en el block_list del swarm.
|
||||||
|
|
||||||
|
Wire en Arje (`ente-zero`):
|
||||||
|
- Tras setup_brahman_net + setup_brahman_policy, si AMBOS están
|
||||||
|
presentes se llama `policy.attach_to_net(net.clone())` con un log
|
||||||
|
informativo. Sin policy o sin net, no hay attach (modo abierto
|
||||||
|
o solo gate-level deny).
|
||||||
|
|
||||||
|
Tests: 1 nuevo E2E en `network_libp2p.rs`:
|
||||||
|
`swarm_level_deny_blocks_before_noise`. A configura policy con
|
||||||
|
deny de un peer + attach_to_net. Cliente baneado intenta
|
||||||
|
`connect_libp2p`; en lugar del `HandshakeError::Unauthorized` que
|
||||||
|
recibíamos antes (que requería completar Noise primero), ahora
|
||||||
|
falla con error de transporte/stream (o timeout, según timing) —
|
||||||
|
el dial nunca completa porque el swarm rechaza la conexión.
|
||||||
|
|
||||||
|
5 tests verdes en `network_libp2p.rs` (roundtrip, mismatched signing,
|
||||||
|
allowlist, denylist handshake-level, denylist swarm-level). 31 tests
|
||||||
|
totales en brahman-handshake + brahman-net. Sin regresión en
|
||||||
|
ente-zero.
|
||||||
|
|
||||||
|
Trade-offs:
|
||||||
|
- **Más eficiente** contra DoS: un atacante que prueba miles de
|
||||||
|
peer_ids no consume CPU del Noise handshake.
|
||||||
|
- **Misma fuente de verdad**: la denylist sigue viviendo en
|
||||||
|
`PeerPolicy` (un solo archivo, hot-reloadable). El swarm es un
|
||||||
|
cache derivado que se actualiza vía diff. No hay drift posible —
|
||||||
|
cada reload re-sincroniza atómicamente.
|
||||||
|
- **El handshake-level gate sigue activo** como segunda línea: si
|
||||||
|
por alguna razón un peer baneado pasa el block_list (race entre
|
||||||
|
reload y nueva conexión, o bug del crate), el handshake brahman
|
||||||
|
igual lo rechaza con `Unauthorized`. Defensa en profundidad.
|
||||||
|
|
||||||
|
Pendientes futuros del changelog:
|
||||||
|
- Rotación de keypair sin perder peer_id (multi-key identity).
|
||||||
|
|
||||||
### feat(brahman-handshake+ente-zero): denylist + hot reload de la política de peers
|
### feat(brahman-handshake+ente-zero): denylist + hot reload de la política de peers
|
||||||
Consolida `PeerAllowlist` + nueva `PeerDenylist` en un único
|
Consolida `PeerAllowlist` + nueva `PeerDenylist` en un único
|
||||||
`PeerPolicy` con allow + deny + hot reload vía `notify`. Cubre los
|
`PeerPolicy` con allow + deny + hot reload vía `notify`. Cubre los
|
||||||
|
|||||||
Generated
+1
@@ -1221,6 +1221,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"futures",
|
"futures",
|
||||||
"libp2p",
|
"libp2p",
|
||||||
|
"libp2p-allow-block-list",
|
||||||
"libp2p-stream",
|
"libp2p-stream",
|
||||||
"thiserror 2.0.18",
|
"thiserror 2.0.18",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
|||||||
@@ -143,6 +143,7 @@ rusqlite = { version = "0.31", features = ["bundled", "blob"] }
|
|||||||
# === P2P (minga) ===
|
# === P2P (minga) ===
|
||||||
libp2p = { version = "0.56", features = ["tokio", "tcp", "noise", "yamux", "macros", "kad", "identify"] }
|
libp2p = { version = "0.56", features = ["tokio", "tcp", "noise", "yamux", "macros", "kad", "identify"] }
|
||||||
libp2p-stream = "=0.4.0-alpha"
|
libp2p-stream = "=0.4.0-alpha"
|
||||||
|
libp2p-allow-block-list = "0.6"
|
||||||
|
|
||||||
# === Code parsing (minga) ===
|
# === Code parsing (minga) ===
|
||||||
tree-sitter = "0.24"
|
tree-sitter = "0.24"
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ use std::path::{Path, PathBuf};
|
|||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use brahman_net::PeerId;
|
use brahman_net::{BrahmanNet, PeerId};
|
||||||
use tracing::{debug, info, warn};
|
use tracing::{debug, info, warn};
|
||||||
|
|
||||||
/// Política de admisión combinada (allow + deny). Clone barato (todos
|
/// Política de admisión combinada (allow + deny). Clone barato (todos
|
||||||
@@ -47,6 +47,14 @@ use tracing::{debug, info, warn};
|
|||||||
pub struct PeerPolicy {
|
pub struct PeerPolicy {
|
||||||
inner: Arc<RwLock<PolicyInner>>,
|
inner: Arc<RwLock<PolicyInner>>,
|
||||||
paths: Arc<PolicyPaths>,
|
paths: Arc<PolicyPaths>,
|
||||||
|
/// `BrahmanNet` opcional asociado vía [`Self::attach_to_net`].
|
||||||
|
/// Si está set, cada cambio en la denylist se sincroniza con el
|
||||||
|
/// `block_list` behaviour del swarm — los peers baneados son
|
||||||
|
/// rechazados ANTES del Noise handshake. `RwLock<Option<...>>`
|
||||||
|
/// para que `attach_to_net` se pueda llamar después del
|
||||||
|
/// constructor (típico en ente-zero: primero arma la policy,
|
||||||
|
/// después el net, después attach).
|
||||||
|
net: Arc<RwLock<Option<Arc<BrahmanNet>>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@@ -111,6 +119,7 @@ impl PeerPolicy {
|
|||||||
Self {
|
Self {
|
||||||
inner: Arc::new(RwLock::new(PolicyInner::default())),
|
inner: Arc::new(RwLock::new(PolicyInner::default())),
|
||||||
paths: Arc::new(PolicyPaths::default()),
|
paths: Arc::new(PolicyPaths::default()),
|
||||||
|
net: Arc::new(RwLock::new(None)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,6 +129,7 @@ impl PeerPolicy {
|
|||||||
Self {
|
Self {
|
||||||
inner: Arc::new(RwLock::new(PolicyInner { allow, deny })),
|
inner: Arc::new(RwLock::new(PolicyInner { allow, deny })),
|
||||||
paths: Arc::new(PolicyPaths::default()),
|
paths: Arc::new(PolicyPaths::default()),
|
||||||
|
net: Arc::new(RwLock::new(None)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,6 +155,7 @@ impl PeerPolicy {
|
|||||||
allow_path: allow_path.map(Path::to_path_buf),
|
allow_path: allow_path.map(Path::to_path_buf),
|
||||||
deny_path: deny_path.map(Path::to_path_buf),
|
deny_path: deny_path.map(Path::to_path_buf),
|
||||||
}),
|
}),
|
||||||
|
net: Arc::new(RwLock::new(None)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,6 +195,11 @@ impl PeerPolicy {
|
|||||||
/// falla, la versión anterior persiste y el error se devuelve.
|
/// falla, la versión anterior persiste y el error se devuelve.
|
||||||
/// Esto evita que un typo en el archivo deje al Init en modo
|
/// Esto evita que un typo en el archivo deje al Init en modo
|
||||||
/// inseguro.
|
/// inseguro.
|
||||||
|
///
|
||||||
|
/// Si hay un `BrahmanNet` attached vía [`Self::attach_to_net`],
|
||||||
|
/// el cambio de denylist se sincroniza con el `block_list` del
|
||||||
|
/// swarm: se calcula el diff (added/removed) y se aplican
|
||||||
|
/// `block_peer`/`unblock_peer` por cada cambio.
|
||||||
pub fn reload(&self) -> Result<(), PolicyError> {
|
pub fn reload(&self) -> Result<(), PolicyError> {
|
||||||
let new_allow = match &self.paths.allow_path {
|
let new_allow = match &self.paths.allow_path {
|
||||||
Some(p) => Some(parse_peer_set(p)?),
|
Some(p) => Some(parse_peer_set(p)?),
|
||||||
@@ -193,13 +209,58 @@ impl PeerPolicy {
|
|||||||
Some(p) => parse_peer_set(p)?,
|
Some(p) => parse_peer_set(p)?,
|
||||||
None => BTreeSet::new(),
|
None => BTreeSet::new(),
|
||||||
};
|
};
|
||||||
|
// Snapshot de la deny actual ANTES de mutar, para diff.
|
||||||
|
let prev_deny = self
|
||||||
|
.inner
|
||||||
|
.read()
|
||||||
|
.map(|g| g.deny.clone())
|
||||||
|
.unwrap_or_default();
|
||||||
if let Ok(mut inner) = self.inner.write() {
|
if let Ok(mut inner) = self.inner.write() {
|
||||||
inner.allow = new_allow;
|
inner.allow = new_allow;
|
||||||
inner.deny = new_deny;
|
inner.deny = new_deny.clone();
|
||||||
}
|
}
|
||||||
|
self.sync_deny_to_swarm(&prev_deny, &new_deny);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Asocia esta política a un `BrahmanNet`. Sincroniza el snapshot
|
||||||
|
/// actual de la denylist con el `block_list` behaviour del swarm
|
||||||
|
/// (cada peer baneado se rechaza ANTES del Noise handshake), y
|
||||||
|
/// registra el net para re-sincronizarse en cada [`Self::reload`].
|
||||||
|
///
|
||||||
|
/// Si ya había un net attached, se reemplaza (caso esperado:
|
||||||
|
/// un Init no debería tener dos `BrahmanNet`s).
|
||||||
|
pub fn attach_to_net(&self, net: Arc<BrahmanNet>) {
|
||||||
|
// Sync inicial: bloquear todos los peers actualmente en deny.
|
||||||
|
if let Ok(inner) = self.inner.read() {
|
||||||
|
for peer in &inner.deny {
|
||||||
|
net.block_peer(*peer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Ok(mut slot) = self.net.write() {
|
||||||
|
*slot = Some(net);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Calcula el diff entre `prev` y `new` y aplica
|
||||||
|
/// `block_peer`/`unblock_peer` al net asociado (si hay).
|
||||||
|
/// No-op si no hay net attached.
|
||||||
|
fn sync_deny_to_swarm(&self, prev: &BTreeSet<PeerId>, new: &BTreeSet<PeerId>) {
|
||||||
|
let net = match self.net.read() {
|
||||||
|
Ok(g) => match g.as_ref() {
|
||||||
|
Some(n) => n.clone(),
|
||||||
|
None => return,
|
||||||
|
},
|
||||||
|
Err(_) => return,
|
||||||
|
};
|
||||||
|
for added in new.difference(prev) {
|
||||||
|
net.block_peer(*added);
|
||||||
|
}
|
||||||
|
for removed in prev.difference(new) {
|
||||||
|
net.unblock_peer(*removed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Arranca un thread que vigila los archivos asociados con
|
/// Arranca un thread que vigila los archivos asociados con
|
||||||
/// `notify` y llama [`Self::reload`] cuando cambian. Debounce
|
/// `notify` y llama [`Self::reload`] cuando cambian. Debounce
|
||||||
/// 250ms para coalescer múltiples eventos por save (los editores
|
/// 250ms para coalescer múltiples eventos por save (los editores
|
||||||
|
|||||||
@@ -321,3 +321,78 @@ async fn libp2p_handshake_denylist_blocks_listed_peer() {
|
|||||||
}
|
}
|
||||||
other_client.farewell().await.ok();
|
other_client.farewell().await.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Swarm-level deny via `PeerPolicy::attach_to_net`: cuando la deny
|
||||||
|
/// se aplica al swarm vía `block_list`, el peer baneado es rechazado
|
||||||
|
/// en el dial — la conexión TCP/Noise nunca completa, así que el
|
||||||
|
/// cliente nunca llega siquiera a mandar el Hello. Más eficiente que
|
||||||
|
/// el handshake-level deny.
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||||
|
async fn swarm_level_deny_blocks_before_noise() {
|
||||||
|
let banned_kp = Keypair::generate_ed25519();
|
||||||
|
let banned_peer = banned_kp.public().to_peer_id();
|
||||||
|
|
||||||
|
let tmp = TempDir::new().unwrap();
|
||||||
|
let unix_socket = tmp.path().join("brahman-init.sock");
|
||||||
|
let policy = brahman_handshake::peer_policy::PeerPolicy::from_sets(
|
||||||
|
None,
|
||||||
|
[banned_peer].into_iter().collect(),
|
||||||
|
);
|
||||||
|
let server = Arc::new(
|
||||||
|
Server::bind(
|
||||||
|
&unix_socket,
|
||||||
|
ServerConfig {
|
||||||
|
init_attached: true,
|
||||||
|
broker: None,
|
||||||
|
net: None,
|
||||||
|
policy: Some(policy.clone()),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
let server_net = Arc::new(BrahmanNet::new().unwrap());
|
||||||
|
let server_peer = server_net.peer_id;
|
||||||
|
|
||||||
|
// ATTACH: la deny se proyecta al swarm. Es lo nuevo de este
|
||||||
|
// commit — sin esta llamada, el deny seguiría aplicando sólo
|
||||||
|
// al nivel de handshake brahman (lo que también funciona pero
|
||||||
|
// gasta un round-trip Noise).
|
||||||
|
policy.attach_to_net(server_net.clone());
|
||||||
|
|
||||||
|
let actual = server_net
|
||||||
|
.listen("/ip4/127.0.0.1/tcp/0".parse().unwrap())
|
||||||
|
.await;
|
||||||
|
let mut full = actual.clone();
|
||||||
|
full.push(Protocol::P2p(server_peer));
|
||||||
|
|
||||||
|
tokio::spawn(run_libp2p_accept_loop(server.clone(), server_net.clone()));
|
||||||
|
|
||||||
|
// Cliente baneado intenta dial + handshake. Con swarm-level
|
||||||
|
// deny, la conexión libp2p ni siquiera completa: `connect_libp2p`
|
||||||
|
// falla con error de open_stream (peer inalcanzable / connection
|
||||||
|
// refused) en lugar del Unauthorized del handshake-level path.
|
||||||
|
let banned_net = BrahmanNet::with_keypair(banned_kp.clone()).unwrap();
|
||||||
|
banned_net.dial(full.clone());
|
||||||
|
|
||||||
|
let card = sample_card("test.swarm_banned");
|
||||||
|
// Timeout corto: si el block falla, el handshake completaría
|
||||||
|
// rápido en localhost. Si funciona, debería fallar el dial casi
|
||||||
|
// instantáneo o colgarse hasta el timeout.
|
||||||
|
let result = tokio::time::timeout(
|
||||||
|
Duration::from_secs(3),
|
||||||
|
connect_libp2p(&banned_net, server_peer, card, None, &banned_kp),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(Ok(_)) => panic!("peer baneado a nivel swarm NO debería completar handshake"),
|
||||||
|
Ok(Err(e)) => {
|
||||||
|
// Esperado: error de transporte/stream, no de handshake.
|
||||||
|
tracing::info!(error = %e, "swarm-level deny rechazó como esperado");
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
// También aceptable: timeout porque el dial nunca completa.
|
||||||
|
tracing::info!("swarm-level deny → connect timeout (también OK)");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -175,6 +175,21 @@ async fn primordial_loop(
|
|||||||
// observando los archivos para hot reload.
|
// observando los archivos para hot reload.
|
||||||
let (brahman_policy, _policy_watcher) = setup_brahman_policy();
|
let (brahman_policy, _policy_watcher) = setup_brahman_policy();
|
||||||
|
|
||||||
|
// Si tenemos AMBOS net y policy, attachamos: el deny de la
|
||||||
|
// policy se proyecta al block_list del swarm para rechazar
|
||||||
|
// conexiones ANTES del Noise handshake (más eficiente que
|
||||||
|
// rechazar en el handshake brahman). Cada hot-reload de la
|
||||||
|
// policy también re-sincroniza vía diff.
|
||||||
|
if let (Some(net), Some(policy)) = (&brahman_net, &brahman_policy) {
|
||||||
|
policy.attach_to_net(net.clone());
|
||||||
|
let (allow, deny) = policy.sizes();
|
||||||
|
info!(
|
||||||
|
allow = ?allow,
|
||||||
|
deny = deny,
|
||||||
|
"policy attached al swarm — denies enforcedeados a nivel libp2p"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let brahman_sock = brahman_handshake::transport::default_socket_path();
|
let brahman_sock = brahman_handshake::transport::default_socket_path();
|
||||||
match brahman_handshake::server::Server::bind(
|
match brahman_handshake::server::Server::bind(
|
||||||
&brahman_sock,
|
&brahman_sock,
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ description = "Brahman — capa de transporte P2P compartida (libp2p TCP+Noise+Y
|
|||||||
futures = { workspace = true }
|
futures = { workspace = true }
|
||||||
libp2p = { workspace = true }
|
libp2p = { workspace = true }
|
||||||
libp2p-stream = { workspace = true }
|
libp2p-stream = { workspace = true }
|
||||||
|
libp2p-allow-block-list = { workspace = true }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
tokio = { workspace = true }
|
tokio = { workspace = true }
|
||||||
tracing = { workspace = true }
|
tracing = { workspace = true }
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ use libp2p::{
|
|||||||
swarm::{NetworkBehaviour, SwarmEvent},
|
swarm::{NetworkBehaviour, SwarmEvent},
|
||||||
tcp, yamux, Swarm, SwarmBuilder,
|
tcp, yamux, Swarm, SwarmBuilder,
|
||||||
};
|
};
|
||||||
|
use libp2p_allow_block_list::{self as allow_block_list, BlockedPeers};
|
||||||
use libp2p_stream as stream;
|
use libp2p_stream as stream;
|
||||||
use tokio::sync::{mpsc, oneshot, Mutex};
|
use tokio::sync::{mpsc, oneshot, Mutex};
|
||||||
|
|
||||||
@@ -66,6 +67,13 @@ const IDLE_CONNECTION_TIMEOUT: Duration = Duration::from_secs(60);
|
|||||||
|
|
||||||
#[derive(NetworkBehaviour)]
|
#[derive(NetworkBehaviour)]
|
||||||
struct BrahmanBehaviour {
|
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,
|
stream: stream::Behaviour,
|
||||||
kad: kad::Behaviour<kad::store::MemoryStore>,
|
kad: kad::Behaviour<kad::store::MemoryStore>,
|
||||||
identify: identify::Behaviour,
|
identify: identify::Behaviour,
|
||||||
@@ -86,6 +94,8 @@ enum Command {
|
|||||||
StartProviding(Vec<u8>),
|
StartProviding(Vec<u8>),
|
||||||
StopProviding(Vec<u8>),
|
StopProviding(Vec<u8>),
|
||||||
GetProviders(Vec<u8>, oneshot::Sender<Vec<PeerId>>),
|
GetProviders(Vec<u8>, oneshot::Sender<Vec<PeerId>>),
|
||||||
|
BlockPeer(PeerId),
|
||||||
|
UnblockPeer(PeerId),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Peer descubierto vía DHT: identidad + direcciones conocidas.
|
/// Peer descubierto vía DHT: identidad + direcciones conocidas.
|
||||||
@@ -164,6 +174,7 @@ impl BrahmanNet {
|
|||||||
.with_agent_version(format!("brahman-net/{}", env!("CARGO_PKG_VERSION"))),
|
.with_agent_version(format!("brahman-net/{}", env!("CARGO_PKG_VERSION"))),
|
||||||
);
|
);
|
||||||
BrahmanBehaviour {
|
BrahmanBehaviour {
|
||||||
|
block_list: allow_block_list::Behaviour::default(),
|
||||||
stream: stream::Behaviour::new(),
|
stream: stream::Behaviour::new(),
|
||||||
kad,
|
kad,
|
||||||
identify,
|
identify,
|
||||||
@@ -228,6 +239,12 @@ impl BrahmanNet {
|
|||||||
let qid = swarm.behaviour_mut().kad.get_providers(key.into());
|
let qid = swarm.behaviour_mut().kad.get_providers(key.into());
|
||||||
pending_providers.insert(qid, (Vec::new(), tx));
|
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() => {
|
event = swarm.select_next_some() => {
|
||||||
@@ -323,6 +340,21 @@ impl BrahmanNet {
|
|||||||
self.keypair.clone()
|
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
|
/// Empieza a escuchar en `addr`. Bloquea hasta que el listener
|
||||||
/// publique su dirección real (Multiaddr resuelta — útil cuando
|
/// 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ó).
|
/// pediste `/ip4/0.0.0.0/tcp/0` y querés saber qué puerto te tocó).
|
||||||
|
|||||||
Reference in New Issue
Block a user