Compare commits

...

2 Commits

Author SHA1 Message Date
Vecna fc03309af2 Clean up 2024-05-25 17:27:39 -04:00
Vecna c4828d691f Fix a bunch of issues with the simulation 2024-05-25 16:49:50 -04:00
6 changed files with 131 additions and 113 deletions

View File

@ -6,7 +6,13 @@
use troll_patrol::{ use troll_patrol::{
extra_info::ExtraInfo, extra_info::ExtraInfo,
increment_simulated_date, increment_simulated_date,
simulation::{bridge::Bridge, censor::Censor, extra_infos_server, state::State, user::User}, simulation::{
bridge::Bridge,
censor::{Censor, Hides::*, Speed::*, Totality::*},
extra_infos_server,
state::State,
user::User,
},
}; };
use clap::Parser; use clap::Parser;
@ -18,8 +24,9 @@ use std::{
fs::File, fs::File,
io::BufReader, io::BufReader,
path::PathBuf, path::PathBuf,
time::Duration,
}; };
use tokio::spawn; use tokio::{spawn, time::sleep};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
@ -70,7 +77,7 @@ pub async fn main() {
hostname: format!("http://localhost:{}", config.tp_test_port), hostname: format!("http://localhost:{}", config.tp_test_port),
}; };
let extra_infos_net = HyperNet { let extra_infos_net = HyperNet {
hostname: "http://localhost:8003".to_string(), hostname: "http://localhost:8004".to_string(),
}; };
let la_pubkeys = get_lox_auth_keys(&la_net).await; let la_pubkeys = get_lox_auth_keys(&la_net).await;
@ -84,7 +91,7 @@ pub async fn main() {
prob_user_invites_friend: config.prob_user_invites_friend, prob_user_invites_friend: config.prob_user_invites_friend,
prob_user_is_censor: config.prob_user_is_censor, prob_user_is_censor: config.prob_user_is_censor,
prob_user_submits_reports: config.prob_user_submits_reports, prob_user_submits_reports: config.prob_user_submits_reports,
probs_user_in_country: config.probs_user_in_country, probs_user_in_country: config.probs_user_in_country.clone(),
sharing: config.sharing, sharing: config.sharing,
}; };
@ -92,6 +99,10 @@ pub async fn main() {
// Set up censors // Set up censors
let mut censors = HashMap::<String, Censor>::new(); let mut censors = HashMap::<String, Censor>::new();
for i in 0..config.probs_user_in_country.len() {
let cc = config.probs_user_in_country[i].0.clone();
censors.insert(cc.clone(), Censor::new(cc, Fast, Overt, Full));
}
// Set up bridges (no bridges yet) // Set up bridges (no bridges yet)
let mut bridges = HashMap::<[u8; 20], Bridge>::new(); let mut bridges = HashMap::<[u8; 20], Bridge>::new();
@ -103,9 +114,15 @@ pub async fn main() {
spawn(async move { spawn(async move {
extra_infos_server::server().await; extra_infos_server::server().await;
}); });
sleep(Duration::from_millis(1)).await;
let mut fp = 0;
let mut tp = 0;
// Main loop // Main loop
for _ in 0..config.num_days { for day in 1..=config.num_days {
println!("Starting day {} of the simulation", day);
// USER TASKS // USER TASKS
// Add some new users // Add some new users
@ -120,8 +137,7 @@ pub async fn main() {
// Users do daily actions // Users do daily actions
for user in &mut users { for user in &mut users {
// TODO: Refactor out connections from return // TODO: Refactor out connections from return
let (mut invited_friends, _connections) = let mut invited_friends = user.daily_tasks(&state, &mut bridges, &mut censors).await;
user.daily_tasks(&state, &mut bridges, &mut censors).await;
// If this user invited any friends, add them to the list of users // If this user invited any friends, add them to the list of users
new_users.append(&mut invited_friends); new_users.append(&mut invited_friends);
@ -158,7 +174,18 @@ pub async fn main() {
let new_blockages: HashMap<String, HashSet<String>> = let new_blockages: HashMap<String, HashSet<String>> =
serde_json::from_slice(&new_blockages_resp).unwrap(); serde_json::from_slice(&new_blockages_resp).unwrap();
// TODO: Track stats about new blockages // TODO: Track more stats about new blockages
for (bridge, ccs) in new_blockages {
let fingerprint = array_bytes::hex2array(bridge).unwrap();
for cc in ccs {
let censor = censors.get(&cc).unwrap();
if censor.knows_bridge(&fingerprint) {
tp += 1;
} else {
fp += 1;
}
}
}
// LOX AUTHORITY TASKS // LOX AUTHORITY TASKS
@ -175,4 +202,7 @@ pub async fn main() {
// Advance simulated time to tomorrow // Advance simulated time to tomorrow
increment_simulated_date(); increment_simulated_date();
} }
println!("True Positives: {}", tp);
println!("False Positives: {}", fp);
} }

View File

@ -871,9 +871,12 @@ pub async fn report_blockages(
let mut blockages_str = HashMap::<String, HashSet<String>>::new(); let mut blockages_str = HashMap::<String, HashSet<String>>::new();
for (fingerprint, countries) in blockages { for (fingerprint, countries) in blockages {
let fpr_string = array_bytes::bytes2hex("", fingerprint); let fpr_string = array_bytes::bytes2hex("", fingerprint);
if countries.len() > 0 {
blockages_str.insert(fpr_string, countries); blockages_str.insert(fpr_string, countries);
} }
}
if blockages_str.len() > 0 {
// Report blocked bridges to bridge distributor // Report blocked bridges to bridge distributor
let client = Client::new(); let client = Client::new();
let req = Request::builder() let req = Request::builder()
@ -885,6 +888,7 @@ pub async fn report_blockages(
let buf = hyper::body::to_bytes(resp).await.unwrap(); let buf = hyper::body::to_bytes(resp).await.unwrap();
let resp_str: String = serde_json::from_slice(&buf).unwrap(); let resp_str: String = serde_json::from_slice(&buf).unwrap();
assert_eq!("OK", resp_str); assert_eq!("OK", resp_str);
}
} }
// Unit tests // Unit tests

View File

@ -155,6 +155,9 @@ async fn context_manager(
) { ) {
let db: Db = sled::open(&db_config.db_path).unwrap(); let db: Db = sled::open(&db_config.db_path).unwrap();
// Create negative report key for today if we don't have one
new_negative_report_key(&db, get_date());
while let Some(cmd) = context_rx.recv().await { while let Some(cmd) = context_rx.recv().await {
use Command::*; use Command::*;
match cmd { match cmd {

View File

@ -19,19 +19,15 @@ impl Bridge {
} }
pub fn from_bridge_line(bridgeline: &BridgeLine) -> Self { pub fn from_bridge_line(bridgeline: &BridgeLine) -> Self {
Self::new(&bridgeline.fingerprint) Self::new(&bridgeline.get_hashed_fingerprint())
} }
pub fn connect_real(&mut self, country: &str) { pub fn connect_real(&mut self, country: &str) {
if self.real_connections.contains_key(country) { if self.real_connections.contains_key(country) {
let prev = self.real_connections.get(country).unwrap(); let prev = self.real_connections.get(country).unwrap();
self.real_connections self.real_connections.insert(country.to_string(), prev + 1);
.insert(country.to_string(), prev + 1)
.unwrap();
} else { } else {
self.real_connections self.real_connections.insert(country.to_string(), 1);
.insert(country.to_string(), 1)
.unwrap();
} }
self.connect_total(country); self.connect_total(country);
} }
@ -39,13 +35,9 @@ impl Bridge {
pub fn connect_total(&mut self, country: &str) { pub fn connect_total(&mut self, country: &str) {
if self.total_connections.contains_key(country) { if self.total_connections.contains_key(country) {
let prev = self.total_connections.get(country).unwrap(); let prev = self.total_connections.get(country).unwrap();
self.total_connections self.total_connections.insert(country.to_string(), prev + 1);
.insert(country.to_string(), prev + 1)
.unwrap();
} else { } else {
self.total_connections self.total_connections.insert(country.to_string(), 1);
.insert(country.to_string(), 1)
.unwrap();
} }
} }
@ -54,19 +46,17 @@ impl Bridge {
if self.total_connections.contains_key(country) { if self.total_connections.contains_key(country) {
let prev = self.total_connections.get(country).unwrap(); let prev = self.total_connections.get(country).unwrap();
self.total_connections self.total_connections
.insert(country.to_string(), prev + num_connections) .insert(country.to_string(), prev + num_connections);
.unwrap();
} else { } else {
self.total_connections self.total_connections
.insert(country.to_string(), num_connections) .insert(country.to_string(), num_connections);
.unwrap();
} }
} }
// Generate an extra-info report for today // Generate an extra-info report for today
pub fn gen_extra_info(&self) -> ExtraInfo { pub fn gen_extra_info(&self) -> ExtraInfo {
ExtraInfo { ExtraInfo {
nickname: String::default(), nickname: String::from("simulation-bridge"),
fingerprint: self.fingerprint, fingerprint: self.fingerprint,
date: get_date(), date: get_date(),
bridge_ips: self.total_connections.clone(), bridge_ips: self.total_connections.clone(),

View File

@ -11,7 +11,7 @@ use serde_json::json;
use std::{collections::HashSet, convert::Infallible, net::SocketAddr, time::Duration}; use std::{collections::HashSet, convert::Infallible, net::SocketAddr, time::Duration};
use tokio::{ use tokio::{
spawn, spawn,
sync::{broadcast, mpsc, oneshot}, sync::{mpsc, oneshot},
time::sleep, time::sleep,
}; };
@ -44,12 +44,8 @@ async fn serve_extra_infos(
pub async fn server() { pub async fn server() {
let (context_tx, context_rx) = mpsc::channel(32); let (context_tx, context_rx) = mpsc::channel(32);
let request_tx = context_tx.clone(); let request_tx = context_tx.clone();
let shutdown_cmd_tx = context_tx.clone();
let (shutdown_tx, mut shutdown_rx) = broadcast::channel(16);
let kill_context = shutdown_tx.subscribe();
let context_manager = spawn(async move { create_context_manager(context_rx).await });
spawn(async move { create_context_manager(context_rx, kill_context).await });
let addr = SocketAddr::from(([127, 0, 0, 1], 8004)); let addr = SocketAddr::from(([127, 0, 0, 1], 8004));
let make_svc = make_service_fn(move |_conn: &AddrStream| { let make_svc = make_service_fn(move |_conn: &AddrStream| {
@ -75,13 +71,9 @@ pub async fn server() {
} }
} }
async fn create_context_manager( async fn create_context_manager(context_rx: mpsc::Receiver<Command>) {
context_rx: mpsc::Receiver<Command>,
mut kill: broadcast::Receiver<()>,
) {
tokio::select! { tokio::select! {
create_context = context_manager(context_rx) => create_context, create_context = context_manager(context_rx) => create_context,
_ = kill.recv() => {println!("Shut down context_manager");},
} }
} }
@ -98,9 +90,6 @@ async fn context_manager(mut context_rx: mpsc::Receiver<Command>) {
} }
sleep(Duration::from_millis(1)).await; sleep(Duration::from_millis(1)).await;
} }
Shutdown { shutdown_sig } => {
drop(shutdown_sig);
}
} }
} }
} }
@ -111,9 +100,6 @@ enum Command {
req: Request<Body>, req: Request<Body>,
sender: oneshot::Sender<Result<Response<Body>, Infallible>>, sender: oneshot::Sender<Result<Response<Body>, Infallible>>,
}, },
Shutdown {
shutdown_sig: broadcast::Sender<()>,
},
} }
fn add_extra_infos(extra_infos_pages: &mut Vec<String>, request: Bytes) -> Response<Body> { fn add_extra_infos(extra_infos_pages: &mut Vec<String>, request: Bytes) -> Response<Body> {
@ -130,7 +116,9 @@ fn add_extra_infos(extra_infos_pages: &mut Vec<String>, request: Bytes) -> Respo
for extra_info in extra_infos { for extra_info in extra_infos {
extra_infos_file.push_str(extra_info.to_string().as_str()); extra_infos_file.push_str(extra_info.to_string().as_str());
} }
if extra_infos_file.len() > 0 {
extra_infos_pages.push(extra_infos_file); extra_infos_pages.push(extra_infos_file);
}
prepare_header("OK".to_string()) prepare_header("OK".to_string())
} }

View File

@ -9,7 +9,7 @@ use crate::{
censor::{Censor, Hides::*, Speed::*, Totality::*}, censor::{Censor, Hides::*, Speed::*, Totality::*},
state::State, state::State,
}, },
BridgeDistributor, BridgeDistributor, COUNTRY_CODES,
}; };
use lox_cli::{networking::*, *}; use lox_cli::{networking::*, *};
use lox_library::{ use lox_library::{
@ -69,7 +69,7 @@ impl User {
let mut cc = String::default(); let mut cc = String::default();
for (country, prob) in &state.probs_user_in_country { for (country, prob) in &state.probs_user_in_country {
let prob = *prob; let prob = *prob;
if prob < num { if num < prob {
cc = country.to_string(); cc = country.to_string();
break; break;
} else { } else {
@ -78,6 +78,7 @@ impl User {
} }
cc cc
}; };
assert!(COUNTRY_CODES.contains(cc.as_str()));
// Randomly determine how likely this user is to use bridges on // Randomly determine how likely this user is to use bridges on
// a given day // a given day
@ -135,7 +136,7 @@ impl User {
let mut cc = String::default(); let mut cc = String::default();
for (country, prob) in &state.probs_user_in_country { for (country, prob) in &state.probs_user_in_country {
let prob = *prob; let prob = *prob;
if prob < num { if num < prob {
cc = country.to_string(); cc = country.to_string();
break; break;
} else { } else {
@ -237,7 +238,7 @@ impl User {
state: &State, state: &State,
bridges: &mut HashMap<[u8; 20], Bridge>, bridges: &mut HashMap<[u8; 20], Bridge>,
censors: &mut HashMap<String, Censor>, censors: &mut HashMap<String, Censor>,
) -> (Vec<User>, Vec<[u8; 20]>) { ) -> Vec<User> {
// Probabilistically decide if the user should use bridges today // Probabilistically decide if the user should use bridges today
if event_happens(self.prob_use_bridges) { if event_happens(self.prob_use_bridges) {
// Download bucket to see if bridge is still reachable. (We // Download bucket to see if bridge is still reachable. (We
@ -248,23 +249,25 @@ impl User {
// Make sure each bridge in bucket is in the global bridges set // Make sure each bridge in bucket is in the global bridges set
for bridgeline in bucket { for bridgeline in bucket {
if !bridges.contains_key(&bridgeline.fingerprint) { if bridgeline != BridgeLine::default() {
if !bridges.contains_key(&bridgeline.get_hashed_fingerprint()) {
let bridge = Bridge::from_bridge_line(&bridgeline); let bridge = Bridge::from_bridge_line(&bridgeline);
bridges.insert(bridgeline.fingerprint, bridge).unwrap(); bridges.insert(bridgeline.get_hashed_fingerprint(), bridge);
} }
// Also, if this user cooperates with censors, make sure // Also, if this user cooperates with censors, make sure
// each applicable censor knows about their bridges. // each applicable censor knows about their bridges.
if self.censor { if self.censor {
if state.sharing { if state.sharing {
for c in censors.values_mut() { for c in censors.values_mut() {
if !c.knows_bridge(&bridgeline.fingerprint) { if !c.knows_bridge(&bridgeline.get_hashed_fingerprint()) {
c.learn_bridge(&bridgeline.fingerprint); c.learn_bridge(&bridgeline.get_hashed_fingerprint());
} }
} }
} else { } else {
let censor = censors.get_mut(&self.country).unwrap(); let censor = censors.get_mut(&self.country).unwrap();
if !censor.knows_bridge(&bridgeline.fingerprint) { if !censor.knows_bridge(&bridgeline.get_hashed_fingerprint()) {
censor.learn_bridge(&bridgeline.fingerprint); censor.learn_bridge(&bridgeline.get_hashed_fingerprint());
}
} }
} }
} }
@ -282,14 +285,17 @@ impl User {
// Can we level up the secondary credential? // Can we level up the secondary credential?
let mut second_level_up = false; let mut second_level_up = false;
// Attempt to connect to each bridge
let mut failed = Vec::<BridgeLine>::new(); let mut failed = Vec::<BridgeLine>::new();
let mut succeeded = Vec::<BridgeLine>::new(); let mut succeeded = Vec::<BridgeLine>::new();
for i in 0..bucket.len() { for i in 0..bucket.len() {
// At level 0, we only have 1 bridge // At level 0, we only have 1 bridge
if level > 0 || i == 0 { if bucket[i] != BridgeLine::default() {
if self.connect( if self.connect(
&state, &state,
bridges.get_mut(&bucket[i].fingerprint).unwrap(), bridges
.get_mut(&bucket[i].get_hashed_fingerprint())
.unwrap(),
&censors.get(&self.country).unwrap(), &censors.get(&self.country).unwrap(),
) { ) {
succeeded.push(bucket[i]); succeeded.push(bucket[i]);
@ -321,45 +327,49 @@ impl User {
let second_cred = second_cred.as_ref().unwrap(); let second_cred = second_cred.as_ref().unwrap();
let (second_bucket, second_reachcred) = let (second_bucket, second_reachcred) =
get_bucket(&state.la_net, &second_cred).await; get_bucket(&state.la_net, &second_cred).await;
if !bridges.contains_key(&second_bucket[0].fingerprint) { for bridgeline in second_bucket {
bridges if bridgeline != BridgeLine::default() {
.insert( if !bridges.contains_key(&bridgeline.get_hashed_fingerprint()) {
second_bucket[0].fingerprint, bridges.insert(
Bridge::from_bridge_line(&second_bucket[0]), bridgeline.get_hashed_fingerprint(),
) Bridge::from_bridge_line(&bridgeline),
.unwrap(); );
} }
if self.connect( if self.connect(
&state, &state,
bridges.get_mut(&second_bucket[0].fingerprint).unwrap(), bridges
.get_mut(&bridgeline.get_hashed_fingerprint())
.unwrap(),
&censors.get(&self.country).unwrap(), &censors.get(&self.country).unwrap(),
) { ) {
succeeded.push(second_bucket[0]); succeeded.push(bridgeline);
if second_reachcred.is_some() if second_reachcred.is_some()
&& eligible_for_trust_promotion(&state.la_net, &second_cred).await && eligible_for_trust_promotion(&state.la_net, &second_cred).await
{ {
second_level_up = true; second_level_up = true;
} }
} else { } else {
failed.push(second_bucket[0]); failed.push(bridgeline);
}
}
} }
} }
let mut negative_reports = Vec::<NegativeReport>::new(); let mut negative_reports = Vec::<NegativeReport>::new();
let mut positive_reports = Vec::<PositiveReport>::new(); let mut positive_reports = Vec::<PositiveReport>::new();
if self.submits_reports { if self.submits_reports {
for bridge in &failed { for bridgeline in &failed {
negative_reports.push(NegativeReport::from_bridgeline( negative_reports.push(NegativeReport::from_bridgeline(
*bridge, *bridgeline,
self.country.to_string(), self.country.to_string(),
BridgeDistributor::Lox, BridgeDistributor::Lox,
)); ));
} }
if level >= 3 { if level >= 3 {
for bridge in &succeeded { for bridgeline in &succeeded {
positive_reports.push( positive_reports.push(
PositiveReport::from_lox_credential( PositiveReport::from_lox_credential(
bridge.fingerprint, bridgeline.get_hashed_fingerprint(),
None, None,
&self.primary_cred, &self.primary_cred,
get_lox_pub(&state.la_pubkeys), get_lox_pub(&state.la_pubkeys),
@ -392,8 +402,8 @@ impl User {
let censor = censors.get_mut(&self.country).unwrap(); let censor = censors.get_mut(&self.country).unwrap();
let (bucket, reachcred) = get_bucket(&state.la_net, &self.primary_cred).await; let (bucket, reachcred) = get_bucket(&state.la_net, &self.primary_cred).await;
for bl in bucket { for bl in bucket {
censor.learn_bridge(&bl.fingerprint); censor.learn_bridge(&bl.get_hashed_fingerprint());
censor.give_lox_cred(&bl.fingerprint, &self.primary_cred); censor.give_lox_cred(&bl.get_hashed_fingerprint(), &self.primary_cred);
} }
} }
} }
@ -461,16 +471,9 @@ impl User {
} }
} }
// List of fingerprints we contacted. This should not new_friends
// actually be more than one.
let mut connections = Vec::<[u8; 20]>::new();
for bridge in succeeded {
connections.push(bridge.get_hashed_fingerprint());
}
(new_friends, connections)
} else { } else {
(Vec::<User>::new(), Vec::<[u8; 20]>::new()) Vec::<User>::new()
} }
} }
} }