Compare commits

..

No commits in common. "fc03309af206695d89b2bfeb3182ed7b30c17b31" and "7a075d42ff83a68243d2b97103ce6e501d400936" have entirely different histories.

6 changed files with 113 additions and 131 deletions

View File

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

View File

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

View File

@ -155,9 +155,6 @@ async fn context_manager(
) {
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 {
use Command::*;
match cmd {

View File

@ -19,15 +19,19 @@ impl Bridge {
}
pub fn from_bridge_line(bridgeline: &BridgeLine) -> Self {
Self::new(&bridgeline.get_hashed_fingerprint())
Self::new(&bridgeline.fingerprint)
}
pub fn connect_real(&mut self, country: &str) {
if self.real_connections.contains_key(country) {
let prev = self.real_connections.get(country).unwrap();
self.real_connections.insert(country.to_string(), prev + 1);
self.real_connections
.insert(country.to_string(), prev + 1)
.unwrap();
} else {
self.real_connections.insert(country.to_string(), 1);
self.real_connections
.insert(country.to_string(), 1)
.unwrap();
}
self.connect_total(country);
}
@ -35,9 +39,13 @@ impl Bridge {
pub fn connect_total(&mut self, country: &str) {
if self.total_connections.contains_key(country) {
let prev = self.total_connections.get(country).unwrap();
self.total_connections.insert(country.to_string(), prev + 1);
self.total_connections
.insert(country.to_string(), prev + 1)
.unwrap();
} else {
self.total_connections.insert(country.to_string(), 1);
self.total_connections
.insert(country.to_string(), 1)
.unwrap();
}
}
@ -46,17 +54,19 @@ impl Bridge {
if self.total_connections.contains_key(country) {
let prev = self.total_connections.get(country).unwrap();
self.total_connections
.insert(country.to_string(), prev + num_connections);
.insert(country.to_string(), prev + num_connections)
.unwrap();
} else {
self.total_connections
.insert(country.to_string(), num_connections);
.insert(country.to_string(), num_connections)
.unwrap();
}
}
// Generate an extra-info report for today
pub fn gen_extra_info(&self) -> ExtraInfo {
ExtraInfo {
nickname: String::from("simulation-bridge"),
nickname: String::default(),
fingerprint: self.fingerprint,
date: get_date(),
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 tokio::{
spawn,
sync::{mpsc, oneshot},
sync::{broadcast, mpsc, oneshot},
time::sleep,
};
@ -44,8 +44,12 @@ async fn serve_extra_infos(
pub async fn server() {
let (context_tx, context_rx) = mpsc::channel(32);
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();
spawn(async move { create_context_manager(context_rx).await });
let context_manager =
spawn(async move { create_context_manager(context_rx, kill_context).await });
let addr = SocketAddr::from(([127, 0, 0, 1], 8004));
let make_svc = make_service_fn(move |_conn: &AddrStream| {
@ -71,9 +75,13 @@ pub async fn server() {
}
}
async fn create_context_manager(context_rx: mpsc::Receiver<Command>) {
async fn create_context_manager(
context_rx: mpsc::Receiver<Command>,
mut kill: broadcast::Receiver<()>,
) {
tokio::select! {
create_context = context_manager(context_rx) => create_context,
_ = kill.recv() => {println!("Shut down context_manager");},
}
}
@ -90,6 +98,9 @@ async fn context_manager(mut context_rx: mpsc::Receiver<Command>) {
}
sleep(Duration::from_millis(1)).await;
}
Shutdown { shutdown_sig } => {
drop(shutdown_sig);
}
}
}
}
@ -100,6 +111,9 @@ enum Command {
req: Request<Body>,
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> {
@ -116,9 +130,7 @@ fn add_extra_infos(extra_infos_pages: &mut Vec<String>, request: Bytes) -> Respo
for extra_info in extra_infos {
extra_infos_file.push_str(extra_info.to_string().as_str());
}
if extra_infos_file.len() > 0 {
extra_infos_pages.push(extra_infos_file);
}
prepare_header("OK".to_string())
}

View File

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