|
@@ -165,6 +165,18 @@ impl fmt::Display for BridgeCountryInfo {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/// We store a set of all known bridges so that we can later iterate over them.
|
|
|
+/// This function just adds a bridge fingerprint to that set.
|
|
|
+pub fn add_bridge_to_db(db: &Db, fingerprint: [u8; 20]) {
|
|
|
+ let mut bridges = match db.get("bridges").unwrap() {
|
|
|
+ Some(v) => bincode::deserialize(&v).unwrap(),
|
|
|
+ None => HashSet::<[u8; 20]>::new(),
|
|
|
+ };
|
|
|
+ bridges.insert(fingerprint);
|
|
|
+ db.insert("bridges", bincode::serialize(&bridges).unwrap())
|
|
|
+ .unwrap();
|
|
|
+}
|
|
|
+
|
|
|
// Process extra-infos
|
|
|
|
|
|
/// Adds the extra-info data for a single bridge to the database. If the
|
|
@@ -172,18 +184,14 @@ impl fmt::Display for BridgeCountryInfo {
|
|
|
/// but this extra-info contains different data for some reason, use the
|
|
|
/// greater count of connections from each country.
|
|
|
pub fn add_extra_info_to_db(db: &Db, extra_info: ExtraInfo) {
|
|
|
- let mut bridges = match db.get("bridges").unwrap() {
|
|
|
+ let fingerprint = extra_info.fingerprint;
|
|
|
+ let mut bridge_info = match db.get(fingerprint).unwrap() {
|
|
|
Some(v) => bincode::deserialize(&v).unwrap(),
|
|
|
- None => BTreeMap::<[u8; 20], BridgeInfo>::new(),
|
|
|
+ None => {
|
|
|
+ add_bridge_to_db(&db, fingerprint);
|
|
|
+ BridgeInfo::new(fingerprint, &extra_info.nickname)
|
|
|
+ }
|
|
|
};
|
|
|
- let fingerprint = extra_info.fingerprint;
|
|
|
- if !bridges.contains_key(&fingerprint) {
|
|
|
- bridges.insert(
|
|
|
- fingerprint,
|
|
|
- BridgeInfo::new(fingerprint, &extra_info.nickname),
|
|
|
- );
|
|
|
- }
|
|
|
- let bridge_info = bridges.get_mut(&fingerprint).unwrap();
|
|
|
for country in extra_info.bridge_ips.keys() {
|
|
|
if bridge_info.info_by_country.contains_key::<String>(country) {
|
|
|
bridge_info
|
|
@@ -209,12 +217,12 @@ pub fn add_extra_info_to_db(db: &Db, extra_info: ExtraInfo) {
|
|
|
}
|
|
|
}
|
|
|
// Commit changes to database
|
|
|
- db.insert("bridges", bincode::serialize(&bridges).unwrap())
|
|
|
+ db.insert(fingerprint, bincode::serialize(&bridge_info).unwrap())
|
|
|
.unwrap();
|
|
|
}
|
|
|
|
|
|
/// Download new extra-infos files and add their data to the database
|
|
|
-pub async fn update_extra_infos(db: &Db) {
|
|
|
+pub async fn update_extra_infos(db: &Db, base_url: &str) {
|
|
|
// Track which files have been processed. This is slightly redundant
|
|
|
// because we're only downloading files we don't already have, but it
|
|
|
// might be a good idea to check in case we downloaded a file but didn't
|
|
@@ -224,7 +232,7 @@ pub async fn update_extra_infos(db: &Db) {
|
|
|
None => HashSet::<String>::new(),
|
|
|
};
|
|
|
|
|
|
- let new_files = extra_info::download_extra_infos().await.unwrap();
|
|
|
+ let new_files = extra_info::download_extra_infos(base_url).await.unwrap();
|
|
|
|
|
|
let mut new_extra_infos = HashSet::<ExtraInfo>::new();
|
|
|
|
|
@@ -331,19 +339,17 @@ pub async fn update_negative_reports(db: &Db, distributors: &BTreeMap<BridgeDist
|
|
|
let country = first_report.country;
|
|
|
let count_valid = verify_negative_reports(&distributors, reports).await;
|
|
|
|
|
|
- let mut bridges = match db.get("bridges").unwrap() {
|
|
|
+ // Get bridge info or make new one
|
|
|
+ let mut bridge_info = match db.get(fingerprint).unwrap() {
|
|
|
Some(v) => bincode::deserialize(&v).unwrap(),
|
|
|
- None => BTreeMap::<[u8; 20], BridgeInfo>::new(),
|
|
|
+ None => {
|
|
|
+ // This case shouldn't happen unless the bridge hasn't
|
|
|
+ // published any bridge stats.
|
|
|
+ add_bridge_to_db(&db, fingerprint);
|
|
|
+ BridgeInfo::new(fingerprint, &String::default())
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
- // Get bridge info or make new one
|
|
|
- if !bridges.contains_key(&fingerprint) {
|
|
|
- // This case shouldn't happen unless the bridge hasn't published
|
|
|
- // any bridge stats.
|
|
|
- bridges.insert(fingerprint, BridgeInfo::new(fingerprint, &"".to_string()));
|
|
|
- }
|
|
|
- let bridge_info = bridges.get_mut(&fingerprint).unwrap();
|
|
|
-
|
|
|
// Add the new report count to it
|
|
|
if bridge_info.info_by_country.contains_key(&country) {
|
|
|
let bridge_country_info = bridge_info.info_by_country.get_mut(&country).unwrap();
|
|
@@ -358,7 +364,7 @@ pub async fn update_negative_reports(db: &Db, distributors: &BTreeMap<BridgeDist
|
|
|
}
|
|
|
|
|
|
// Commit changes to database
|
|
|
- db.insert("bridges", bincode::serialize(&bridges).unwrap())
|
|
|
+ db.insert(fingerprint, bincode::serialize(&bridge_info).unwrap())
|
|
|
.unwrap();
|
|
|
}
|
|
|
}
|
|
@@ -449,20 +455,17 @@ pub async fn update_positive_reports(db: &Db, distributors: &BTreeMap<BridgeDist
|
|
|
let country = first_report.country.clone();
|
|
|
let count_valid = verify_positive_reports(&distributors, reports).await;
|
|
|
|
|
|
- // Get bridge data from database
|
|
|
- let mut bridges = match db.get("bridges").unwrap() {
|
|
|
+ // Get bridge info or make new one
|
|
|
+ let mut bridge_info = match db.get(fingerprint).unwrap() {
|
|
|
Some(v) => bincode::deserialize(&v).unwrap(),
|
|
|
- None => BTreeMap::<[u8; 20], BridgeInfo>::new(),
|
|
|
+ None => {
|
|
|
+ // This case shouldn't happen unless the bridge hasn't
|
|
|
+ // published any bridge stats.
|
|
|
+ add_bridge_to_db(&db, fingerprint);
|
|
|
+ BridgeInfo::new(fingerprint, &String::default())
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
- // Get bridge info or make new one
|
|
|
- if !bridges.contains_key(&fingerprint) {
|
|
|
- // This case shouldn't happen unless the bridge hasn't published
|
|
|
- // any bridge stats.
|
|
|
- bridges.insert(fingerprint, BridgeInfo::new(fingerprint, &"".to_string()));
|
|
|
- }
|
|
|
- let bridge_info = bridges.get_mut(&fingerprint).unwrap();
|
|
|
-
|
|
|
// Add the new report count to it
|
|
|
if bridge_info.info_by_country.contains_key(&country) {
|
|
|
let bridge_country_info = bridge_info.info_by_country.get_mut(&country).unwrap();
|
|
@@ -476,7 +479,7 @@ pub async fn update_positive_reports(db: &Db, distributors: &BTreeMap<BridgeDist
|
|
|
.insert(country, bridge_country_info);
|
|
|
}
|
|
|
// Commit changes to database
|
|
|
- db.insert("bridges", bincode::serialize(&bridges).unwrap())
|
|
|
+ db.insert(fingerprint, bincode::serialize(&bridge_info).unwrap())
|
|
|
.unwrap();
|
|
|
}
|
|
|
}
|
|
@@ -498,14 +501,16 @@ pub fn guess_blockages(db: &Db, analyzer: &dyn Analyzer) -> HashMap<[u8; 20], Ha
|
|
|
// Map of bridge fingerprint to set of countries which newly block it
|
|
|
let mut blockages = HashMap::<[u8; 20], HashSet<String>>::new();
|
|
|
|
|
|
- // Get bridge data from database
|
|
|
- let mut bridges = match db.get("bridges").unwrap() {
|
|
|
+ // Get list of bridges from database
|
|
|
+ let bridges = match db.get("bridges").unwrap() {
|
|
|
Some(v) => bincode::deserialize(&v).unwrap(),
|
|
|
- None => BTreeMap::<[u8; 20], BridgeInfo>::new(),
|
|
|
+ None => HashSet::<[u8; 20]>::new(),
|
|
|
};
|
|
|
|
|
|
// Guess for each bridge
|
|
|
- for (fingerprint, bridge_info) in &mut bridges {
|
|
|
+ for fingerprint in bridges {
|
|
|
+ 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);
|
|
|
for country in blocked_in {
|
|
@@ -516,12 +521,12 @@ pub fn guess_blockages(db: &Db, analyzer: &dyn Analyzer) -> HashMap<[u8; 20], Ha
|
|
|
bridge_country_info.blocked = true;
|
|
|
}
|
|
|
}
|
|
|
- blockages.insert(*fingerprint, new_blockages);
|
|
|
- }
|
|
|
+ blockages.insert(fingerprint, new_blockages);
|
|
|
|
|
|
- // Commit changes to database
|
|
|
- db.insert("bridges", bincode::serialize(&bridges).unwrap())
|
|
|
- .unwrap();
|
|
|
+ // Commit changes to database
|
|
|
+ db.insert(fingerprint, bincode::serialize(&bridge_info).unwrap())
|
|
|
+ .unwrap();
|
|
|
+ }
|
|
|
|
|
|
// Return map of new blockages
|
|
|
blockages
|