netdir: add the ability to add authorities from a chutney directory.

This commit is contained in:
Nick Mathewson 2020-09-08 14:34:43 -04:00
parent 92830810d5
commit e40a63c013
2 changed files with 37 additions and 1 deletions

View File

@ -1,10 +1,25 @@
use log::LevelFilter;
use std::path::Path;
use tor_netdir::*;
fn main() -> Result<()> {
simple_logging::log_to_stderr(LevelFilter::Info);
let mut cfg = NetDirConfig::new();
cfg.add_default_authorities();
let argv: Vec<_> = std::env::args().skip(1).collect();
let chutney_dir = if argv.len() == 2 && argv[0] == "chutney" {
Some(argv[1].clone())
} else {
None
};
match chutney_dir {
Some(d) => {
cfg.add_authorities_from_chutney(Path::new(&d))?;
cfg.set_cache_path(Path::new(&d));
}
None => cfg.add_default_authorities(),
};
let dir = cfg.load()?;
for r in dir.relays() {

View File

@ -83,6 +83,27 @@ impl NetDirConfig {
.unwrap();
}
pub fn add_authorities_from_chutney(&mut self, path: &Path) -> Result<()> {
use std::io::{self, BufRead};
let pb = path.join("torrc");
let f = fs::File::open(pb)?;
for line in io::BufReader::new(f).lines() {
let line = line?;
let line = line.trim();
if !line.starts_with("DirAuthority") {
continue;
}
let elts: Vec<_> = line.split_ascii_whitespace().collect();
let name = elts[1];
let v3ident = elts[4];
if !v3ident.starts_with("v3ident=") {
warn!("Chutney torrc not in expected format.");
}
self.add_authority(name, &v3ident[8..])?;
}
Ok(())
}
pub fn set_cache_path(&mut self, path: &Path) {
self.cache_path = Some(path.to_path_buf());
}