Pass confidence to analyzer

This commit is contained in:
Vecna 2024-04-12 12:52:30 -04:00
parent 5c6a076289
commit cba8aea553
4 changed files with 18 additions and 7 deletions

View File

@ -7,6 +7,7 @@
"Lox": "127.0.0.1:8002"
},
"extra_infos_base_url": "https://collector.torproject.org/recent/bridge-descriptors/extra-infos/",
"confidence": 0.95,
"port": 8003,
"require_bridge_token": false,
"updater_schedule": "* * 22 * * * *"

View File

@ -3,14 +3,14 @@ use std::collections::HashSet;
/// Provides a function for predicting which countries block this bridge
pub trait Analyzer {
fn blocked_in(&self, bridge_info: &BridgeInfo) -> HashSet<String>;
fn blocked_in(&self, bridge_info: &BridgeInfo, confidence: f64) -> HashSet<String>;
}
pub struct ExampleAnalyzer {}
/// Dummy example which just tells us about blockages we already know about
impl Analyzer for ExampleAnalyzer {
fn blocked_in(&self, bridge_info: &BridgeInfo) -> HashSet<String> {
fn blocked_in(&self, bridge_info: &BridgeInfo, confidence: f64) -> HashSet<String> {
let mut blocked_in = HashSet::<String>::new();
for (country, info) in &bridge_info.info_by_country {
if info.blocked {

View File

@ -41,6 +41,8 @@ pub struct Config {
// map of distributor name to IP:port to contact it
pub distributors: BTreeMap<BridgeDistributor, String>,
extra_infos_base_url: String,
// confidence required to consider a bridge blocked
confidence: f64,
//require_bridge_token: bool,
port: u16,
updater_schedule: String,
@ -64,13 +66,14 @@ async fn update_daily_info(
db: &Db,
distributors: &BTreeMap<BridgeDistributor, String>,
extra_infos_base_url: &str,
confidence: f64,
) {
update_extra_infos(&db, &extra_infos_base_url)
.await
.unwrap();
update_negative_reports(&db, &distributors).await;
update_positive_reports(&db, &distributors).await;
let new_blockages = guess_blockages(&db, &analyzer::ExampleAnalyzer {});
let new_blockages = guess_blockages(&db, &analyzer::ExampleAnalyzer {}, confidence);
report_blockages(&distributors, new_blockages).await;
}
@ -82,11 +85,12 @@ async fn create_context_manager(
db_config: DbConfig,
distributors: BTreeMap<BridgeDistributor, String>,
extra_infos_base_url: &str,
confidence: f64,
context_rx: mpsc::Receiver<Command>,
mut kill: broadcast::Receiver<()>,
) {
tokio::select! {
create_context = context_manager(db_config, distributors, extra_infos_base_url, context_rx) => create_context,
create_context = context_manager(db_config, distributors, extra_infos_base_url, confidence, context_rx) => create_context,
_ = kill.recv() => {println!("Shut down manager");},
}
}
@ -95,6 +99,7 @@ async fn context_manager(
db_config: DbConfig,
distributors: BTreeMap<BridgeDistributor, String>,
extra_infos_base_url: &str,
confidence: f64,
mut context_rx: mpsc::Receiver<Command>,
) {
let db: Db = sled::open(&db_config.db_path).unwrap();
@ -115,7 +120,7 @@ async fn context_manager(
println!("Shutdown Sent.");
}
Update {} => {
update_daily_info(&db, &distributors, &extra_infos_base_url).await;
update_daily_info(&db, &distributors, &extra_infos_base_url, confidence).await;
}
}
}
@ -182,6 +187,7 @@ async fn main() {
config.db,
config.distributors,
&config.extra_infos_base_url,
config.confidence,
request_rx,
kill,
)

View File

@ -564,7 +564,11 @@ pub async fn update_positive_reports(db: &Db, distributors: &BTreeMap<BridgeDist
/// Guess which countries block a bridge. This function returns a map of new
/// blockages (fingerprint : set of countries which block the bridge)
pub fn guess_blockages(db: &Db, analyzer: &dyn Analyzer) -> HashMap<[u8; 20], HashSet<String>> {
pub fn guess_blockages(
db: &Db,
analyzer: &dyn Analyzer,
confidence: f64,
) -> HashMap<[u8; 20], HashSet<String>> {
// Map of bridge fingerprint to set of countries which newly block it
let mut blockages = HashMap::<[u8; 20], HashSet<String>>::new();
@ -579,7 +583,7 @@ pub fn guess_blockages(db: &Db, analyzer: &dyn Analyzer) -> HashMap<[u8; 20], Ha
let mut bridge_info: BridgeInfo =
bincode::deserialize(&db.get(fingerprint).unwrap().unwrap()).unwrap();
let mut new_blockages = HashSet::<String>::new();
let blocked_in = analyzer.blocked_in(&bridge_info);
let blocked_in = analyzer.blocked_in(&bridge_info, confidence);
for country in blocked_in {
let bridge_country_info = bridge_info.info_by_country.get_mut(&country).unwrap();
if !bridge_country_info.blocked {