[tor-commits] [fallback-scripts/main] Update code to the latest arti

dgoulet at torproject.org dgoulet at torproject.org
Tue Oct 26 14:59:11 UTC 2021


commit 2575c46bcd6983b8eea7a199f23de30197dd4329
Author: David Goulet <dgoulet at torproject.org>
Date:   Tue Oct 26 10:59:06 2021 -0400

    Update code to the latest arti
    
    Signed-off-by: David Goulet <dgoulet at torproject.org>
---
 Cargo.toml     | 11 ++++---
 src/main.rs    | 96 ++++++++++++++++++++++++++++------------------------------
 src/onionoo.rs |  1 -
 3 files changed, 53 insertions(+), 55 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index f4077d2..d711781 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,18 +6,19 @@ edition = "2018"
 
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
-#[features]
-#tokio = [ "tor-client/tokio", "tor-rtcompat/tokio" ]
+[features]
+default = [ "tokio" ]
+tokio = [ "tor-rtcompat/tokio" ]
 
 [dependencies]
 anyhow = "1.0.38"
 chrono = "0.4"
 rand = "0.8.3"
 reqwest = { version = "0.11", features = ["json"] }
-serde = { version = "1.0.124", features = ["derive"] }
-tokio = { version = "1", features = ["full"] }
+serde = { version = "1", features = ["derive"] }
+tokio-crate = { package = "tokio", version = "1.7.0", features = ["rt", "rt-multi-thread", "io-util", "net", "time", "macros" ] }
 
-tor-client = { git = "https://gitlab.torproject.org/tpo/core/arti.git", features = ["experimental-api"]}
+arti-client = { git = "https://gitlab.torproject.org/tpo/core/arti.git", features = ["experimental-api"]}
 tor-dirmgr = { git = "https://gitlab.torproject.org/tpo/core/arti.git" }
 tor-netdir = { git = "https://gitlab.torproject.org/tpo/core/arti.git", features = ["experimental-api"] }
 tor-netdoc = { git = "https://gitlab.torproject.org/tpo/core/arti.git" }
diff --git a/src/main.rs b/src/main.rs
index c1802c2..52d1e8c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,19 +3,18 @@ use chrono::Utc;
 use rand::seq::SliceRandom;
 use std::fs::File;
 use std::io::{BufWriter, Write};
-use std::sync::Arc;
+use tokio_crate as tokio;
+use tor_rtcompat::tokio::TokioRuntimeHandle;
 
-use tor_client;
-use tor_dirmgr;
+use arti_client::{self, TorClientConfig};
 use tor_netdir;
-use tor_netdoc::doc::netstatus::RouterFlags;
+use tor_netdoc::doc::netstatus::RelayFlags;
 
 mod onionoo;
 
 // Header format from dir-list-spec.txt but static so Stem is happy about it. In the future, we
 // want to stop doing that.
-static HEADER_COMMENT: &'static str =
-"/* type=fallback */
+static HEADER_COMMENT: &'static str = "/* type=fallback */
 /* version=4.0.0 */
 /* timestamp=20210412000000 */
 /* source=offer-list */";
@@ -64,7 +63,8 @@ fn write_file_tor_arti(writer: &mut BufWriter<&File>, relay: &tor_netdir::Relay)
     writeln!(writer, "        &[")?;
     writeln!(
         writer,
-        "{: <12}{}", "",
+        "{: <12}{}",
+        "",
         relay
             .rs()
             .orport_addrs()
@@ -93,57 +93,55 @@ fn write_header_to_file(writer: &mut BufWriter<&File>) -> Result<()> {
     Ok(())
 }
 
-fn main() -> Result<()> {
-    let mut builder = tor_dirmgr::NetDirConfigBuilder::new();
-    builder.use_default_cache_path()?;
-    let config: tor_dirmgr::NetDirConfig = builder.finalize()?;
+#[tokio::main]
+async fn main() -> Result<()> {
+    let config = TorClientConfig::sane_defaults()?;
+    let rt: TokioRuntimeHandle = tokio_crate::runtime::Handle::current().into();
 
     println!("[+] Fetching onionoo relays...");
-    let onionoo_relays_fprs = onionoo::get_relay_fprs_from_onionoo()?;
-
-    tor_rtcompat::task::block_on(async {
-        println!("[+] Bootstrapping to the Tor network...");
-        let tor_client = Arc::new(tor_client::TorClient::bootstrap(config).await?);
-        let netdir = tor_client.dirmgr().netdir();
-
-        println!("[+] Cross-referencing relays between Onionoo and Tor consensus...");
-
-        let relays: Vec<_> = netdir
-            .relays()
-            .filter(|r| {
-                r.is_dir_cache()
-                    && r.rs().flags().contains(RouterFlags::FAST)
-                    && r.rs().flags().contains(RouterFlags::STABLE)
-                    && onionoo_relays_fprs.contains(&r.rsa_id().to_string().to_uppercase())
-            })
-            .collect();
+    let onionoo_relays_fprs = onionoo::get_relay_fprs_from_onionoo().await?;
+
+    println!("[+] Bootstrapping to the Tor network...");
+    let arti_client = arti_client::TorClient::bootstrap(rt, config).await?;
+    let netdir = arti_client.dirmgr().netdir();
+
+    println!("[+] Cross-referencing relays between Onionoo and Tor consensus...");
+
+    let relays: Vec<_> = netdir
+        .relays()
+        .filter(|r| {
+            r.is_dir_cache()
+                && r.rs().flags().contains(RelayFlags::FAST)
+                && r.rs().flags().contains(RelayFlags::STABLE)
+                && onionoo_relays_fprs.contains(&r.rsa_id().to_string().to_uppercase())
+        })
+        .collect();
 
-        println!("Got {} relays. Randomly sampling 200...", relays.len());
+    println!("Got {} relays. Randomly sampling 200...", relays.len());
 
-        let picks = relays.choose_multiple(&mut rand::thread_rng(), 200);
+    let picks = relays.choose_multiple(&mut rand::thread_rng(), 200);
 
-        // Create files.
-        let tor_git_file = File::create("tor-git_fallback_dirs.inc")?;
-        let tor_arti_file = File::create("tor-arti_fallback_dirs.inc")?;
+    // Create files.
+    let tor_git_file = File::create("tor-git_fallback_dirs.inc")?;
+    let tor_arti_file = File::create("tor-arti_fallback_dirs.inc")?;
 
-        // Create writers.
-        let mut tor_git_writer = BufWriter::new(&tor_git_file);
-        let mut tor_arti_writer = BufWriter::new(&tor_arti_file);
+    // Create writers.
+    let mut tor_git_writer = BufWriter::new(&tor_git_file);
+    let mut tor_arti_writer = BufWriter::new(&tor_arti_file);
 
-        // Write header to both files.
-        write_header_to_file(&mut tor_git_writer)?;
-        write_header_to_file(&mut tor_arti_writer)?;
+    // Write header to both files.
+    write_header_to_file(&mut tor_git_writer)?;
+    write_header_to_file(&mut tor_arti_writer)?;
 
-        // Start the arti file.
-        writeln!(tor_arti_writer, "vec![")?;
+    // Start the arti file.
+    writeln!(tor_arti_writer, "vec![")?;
 
-        for relay in picks {
-            write_relay_to_files(&mut tor_git_writer, &mut tor_arti_writer, &relay)?;
-        }
+    for relay in picks {
+        write_relay_to_files(&mut tor_git_writer, &mut tor_arti_writer, &relay)?;
+    }
 
-        // End the arti file.
-        writeln!(tor_arti_writer, "]")?;
+    // End the arti file.
+    writeln!(tor_arti_writer, "]")?;
 
-        Ok(())
-    })
+    Ok(())
 }
diff --git a/src/onionoo.rs b/src/onionoo.rs
index c1d028e..96ac051 100644
--- a/src/onionoo.rs
+++ b/src/onionoo.rs
@@ -14,7 +14,6 @@ pub struct OnionooRelay {
 
 /// Search onionoo and return a vector of fingerprint strings corresponding to
 /// the stablest relays
-#[tokio::main]
 pub async fn get_relay_fprs_from_onionoo() -> Result<Vec<String>, Error> {
     // Query URL found in the old fallbackdir scripts
     let request_url = format!("https://onionoo.torproject.org/details?fields=fingerprint%2Cnickname%2Ccontact%2Clast_changed_address_or_port%2Cconsensus_weight%2Cadvertised_bandwidth%2Cor_addresses%2Cdir_address%2Crecommended_version%2Cflags%2Ceffective_family%2Cplatform&type=relay&first_seen_days=90-&last_seen_days=-0&flag=V2Dir&order=-consensus_weight%2Cfirst_seen");



More information about the tor-commits mailing list