Kaynağa Gözat

fud: add verify_node task

epiphany 8 ay önce
ebeveyn
işleme
8765e82300
2 değiştirilmiş dosya ile 20 ekleme ve 0 silme
  1. 8 0
      bin/fud/fud/src/lib.rs
  2. 12 0
      bin/fud/fud/src/tasks.rs

+ 8 - 0
bin/fud/fud/src/lib.rs

@@ -157,6 +157,10 @@ pub struct Fud {
     lookup_tx: channel::Sender<blake3::Hash>,
     lookup_tx: channel::Sender<blake3::Hash>,
     /// Lookup requests receiver
     /// Lookup requests receiver
     lookup_rx: channel::Receiver<blake3::Hash>,
     lookup_rx: channel::Receiver<blake3::Hash>,
+    /// Verify node requests sender
+    verify_node_tx: channel::Sender<FudNode>,
+    /// Verify node requests receiver
+    verify_node_rx: channel::Receiver<FudNode>,
     /// Currently active downloading tasks (running the `fud.fetch_resource()` method)
     /// Currently active downloading tasks (running the `fud.fetch_resource()` method)
     fetch_tasks: Arc<RwLock<HashMap<blake3::Hash, Arc<StoppableTask>>>>,
     fetch_tasks: Arc<RwLock<HashMap<blake3::Hash, Arc<StoppableTask>>>>,
     /// Currently active put tasks (running the `fud.insert_resource()` method)
     /// Currently active put tasks (running the `fud.insert_resource()` method)
@@ -217,6 +221,7 @@ impl Fud {
         let (get_tx, get_rx) = smol::channel::unbounded();
         let (get_tx, get_rx) = smol::channel::unbounded();
         let (put_tx, put_rx) = smol::channel::unbounded();
         let (put_tx, put_rx) = smol::channel::unbounded();
         let (lookup_tx, lookup_rx) = smol::channel::unbounded();
         let (lookup_tx, lookup_rx) = smol::channel::unbounded();
+        let (verify_node_tx, verify_node_rx) = smol::channel::unbounded();
         let fud = Arc::new(Self {
         let fud = Arc::new(Self {
             node_data: Arc::new(RwLock::new(node_data)),
             node_data: Arc::new(RwLock::new(node_data)),
             secret_key: Arc::new(RwLock::new(secret_key)),
             secret_key: Arc::new(RwLock::new(secret_key)),
@@ -236,6 +241,8 @@ impl Fud {
             put_rx,
             put_rx,
             lookup_tx,
             lookup_tx,
             lookup_rx,
             lookup_rx,
+            verify_node_tx,
+            verify_node_rx,
             fetch_tasks: Arc::new(RwLock::new(HashMap::new())),
             fetch_tasks: Arc::new(RwLock::new(HashMap::new())),
             put_tasks: Arc::new(RwLock::new(HashMap::new())),
             put_tasks: Arc::new(RwLock::new(HashMap::new())),
             lookup_tasks: Arc::new(RwLock::new(HashMap::new())),
             lookup_tasks: Arc::new(RwLock::new(HashMap::new())),
@@ -266,6 +273,7 @@ impl Fud {
             tasks
             tasks
         );
         );
         start_task!(self, "lookup", tasks::lookup_task, tasks);
         start_task!(self, "lookup", tasks::lookup_task, tasks);
+        start_task!(self, "verify node", tasks::verify_node_task, tasks);
         start_task!(self, "announce", tasks::announce_seed_task, tasks);
         start_task!(self, "announce", tasks::announce_seed_task, tasks);
         start_task!(self, "node ID", tasks::node_id_task, tasks);
         start_task!(self, "node ID", tasks::node_id_task, tasks);
     }
     }

+ 12 - 0
bin/fud/fud/src/tasks.rs

@@ -178,6 +178,18 @@ pub async fn lookup_task(fud: Arc<Fud>) -> Result<()> {
     }
     }
 }
 }
 
 
+/// After pinging an inbound connection, this task is triggered to make sure
+/// that you are able to reach at least one of the node's external address.
+/// [`Fud::ping()`] will take care of adding the node to our buckets.
+pub async fn verify_node_task(fud: Arc<Fud>) -> Result<()> {
+    loop {
+        let node = fud.verify_node_rx.recv().await.unwrap();
+        if let Ok((channel, _)) = fud.dht.create_channel_to_node(&node).await {
+            fud.dht.cleanup_channel(channel).await;
+        }
+    }
+}
+
 /// Background task that announces our files once every hour.
 /// Background task that announces our files once every hour.
 /// Also removes seeders that did not announce for too long.
 /// Also removes seeders that did not announce for too long.
 pub async fn announce_seed_task(fud: Arc<Fud>) -> Result<()> {
 pub async fn announce_seed_task(fud: Arc<Fud>) -> Result<()> {