|
@@ -11,12 +11,12 @@ use crate::{
|
|
},
|
|
},
|
|
BridgeDistributor,
|
|
BridgeDistributor,
|
|
};
|
|
};
|
|
|
|
+use anyhow::{anyhow, Result};
|
|
use lox_cli::{networking::*, *};
|
|
use lox_cli::{networking::*, *};
|
|
use lox_library::{
|
|
use lox_library::{
|
|
bridge_table::BridgeLine, cred::Lox, proto::check_blockage::MIN_TRUST_LEVEL, scalar_u32,
|
|
bridge_table::BridgeLine, cred::Lox, proto::check_blockage::MIN_TRUST_LEVEL, scalar_u32,
|
|
};
|
|
};
|
|
use rand::Rng;
|
|
use rand::Rng;
|
|
-use serde_json::error::Error;
|
|
|
|
use std::{cmp::min, collections::HashMap};
|
|
use std::{cmp::min, collections::HashMap};
|
|
use x25519_dalek::PublicKey;
|
|
use x25519_dalek::PublicKey;
|
|
|
|
|
|
@@ -45,7 +45,7 @@ pub struct User {
|
|
}
|
|
}
|
|
|
|
|
|
impl User {
|
|
impl User {
|
|
- pub async fn new(config: &Config, is_censor: bool) -> Result<Self, Error> {
|
|
|
|
|
|
+ pub async fn new(config: &Config, is_censor: bool) -> Result<Self> {
|
|
let cred = get_lox_credential(
|
|
let cred = get_lox_credential(
|
|
&config.la_net,
|
|
&config.la_net,
|
|
&get_open_invitation(&config.la_net).await?,
|
|
&get_open_invitation(&config.la_net).await?,
|
|
@@ -75,7 +75,7 @@ impl User {
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
- pub async fn trusted_user(config: &Config) -> Result<Self, Error> {
|
|
|
|
|
|
+ pub async fn trusted_user(config: &Config) -> Result<Self> {
|
|
let cred = get_lox_credential(
|
|
let cred = get_lox_credential(
|
|
&config.la_net,
|
|
&config.la_net,
|
|
&get_open_invitation(&config.la_net).await?,
|
|
&get_open_invitation(&config.la_net).await?,
|
|
@@ -98,7 +98,7 @@ impl User {
|
|
config: &Config,
|
|
config: &Config,
|
|
censor: &mut Censor,
|
|
censor: &mut Censor,
|
|
invited_user_is_censor: bool,
|
|
invited_user_is_censor: bool,
|
|
- ) -> Result<Self, Error> {
|
|
|
|
|
|
+ ) -> Result<Self> {
|
|
let etable = get_reachability_credential(&config.la_net).await?;
|
|
let etable = get_reachability_credential(&config.la_net).await?;
|
|
let (new_cred, invite) = issue_invite(
|
|
let (new_cred, invite) = issue_invite(
|
|
&config.la_net,
|
|
&config.la_net,
|
|
@@ -205,7 +205,7 @@ impl User {
|
|
true
|
|
true
|
|
}
|
|
}
|
|
|
|
|
|
- pub async fn get_new_credential(config: &Config) -> Result<(Lox, BridgeLine), Error> {
|
|
|
|
|
|
+ pub async fn get_new_credential(config: &Config) -> Result<(Lox, BridgeLine)> {
|
|
get_lox_credential(
|
|
get_lox_credential(
|
|
&config.la_net,
|
|
&config.la_net,
|
|
&get_open_invitation(&config.la_net).await?,
|
|
&get_open_invitation(&config.la_net).await?,
|
|
@@ -214,7 +214,10 @@ impl User {
|
|
.await
|
|
.await
|
|
}
|
|
}
|
|
|
|
|
|
- pub async fn send_negative_reports(config: &Config, reports: Vec<NegativeReport>) {
|
|
|
|
|
|
+ pub async fn send_negative_reports(
|
|
|
|
+ config: &Config,
|
|
|
|
+ reports: Vec<NegativeReport>,
|
|
|
|
+ ) -> Result<()> {
|
|
let date = get_date();
|
|
let date = get_date();
|
|
let pubkey = serde_json::from_slice::<Option<PublicKey>>(
|
|
let pubkey = serde_json::from_slice::<Option<PublicKey>>(
|
|
&config
|
|
&config
|
|
@@ -223,9 +226,8 @@ impl User {
|
|
"/nrkey".to_string(),
|
|
"/nrkey".to_string(),
|
|
serde_json::to_string(&date).unwrap().into(),
|
|
serde_json::to_string(&date).unwrap().into(),
|
|
)
|
|
)
|
|
- .await,
|
|
|
|
- )
|
|
|
|
- .unwrap()
|
|
|
|
|
|
+ .await?,
|
|
|
|
+ )?
|
|
.unwrap();
|
|
.unwrap();
|
|
for report in reports {
|
|
for report in reports {
|
|
config
|
|
config
|
|
@@ -234,17 +236,22 @@ impl User {
|
|
"/negativereport".to_string(),
|
|
"/negativereport".to_string(),
|
|
bincode::serialize(&report.encrypt(&pubkey)).unwrap(),
|
|
bincode::serialize(&report.encrypt(&pubkey)).unwrap(),
|
|
)
|
|
)
|
|
- .await;
|
|
|
|
|
|
+ .await?;
|
|
}
|
|
}
|
|
|
|
+ Ok(())
|
|
}
|
|
}
|
|
|
|
|
|
- pub async fn send_positive_reports(config: &Config, reports: Vec<PositiveReport>) {
|
|
|
|
|
|
+ pub async fn send_positive_reports(
|
|
|
|
+ config: &Config,
|
|
|
|
+ reports: Vec<PositiveReport>,
|
|
|
|
+ ) -> Result<()> {
|
|
for report in reports {
|
|
for report in reports {
|
|
config
|
|
config
|
|
.tp_net
|
|
.tp_net
|
|
.request("/positivereport".to_string(), report.to_json().into_bytes())
|
|
.request("/positivereport".to_string(), report.to_json().into_bytes())
|
|
- .await;
|
|
|
|
|
|
+ .await?;
|
|
}
|
|
}
|
|
|
|
+ Ok(())
|
|
}
|
|
}
|
|
|
|
|
|
pub async fn daily_tasks(
|
|
pub async fn daily_tasks(
|
|
@@ -254,7 +261,7 @@ impl User {
|
|
num_censor_invites: u32,
|
|
num_censor_invites: u32,
|
|
bridges: &mut HashMap<[u8; 20], Bridge>,
|
|
bridges: &mut HashMap<[u8; 20], Bridge>,
|
|
censor: &mut Censor,
|
|
censor: &mut Censor,
|
|
- ) -> Result<Vec<User>, Error> {
|
|
|
|
|
|
+ ) -> Result<Vec<User>> {
|
|
if self.is_censor {
|
|
if self.is_censor {
|
|
self.daily_tasks_censor(config, bridges, censor).await
|
|
self.daily_tasks_censor(config, bridges, censor).await
|
|
} else {
|
|
} else {
|
|
@@ -280,14 +287,17 @@ impl User {
|
|
num_censor_invites: u32,
|
|
num_censor_invites: u32,
|
|
bridges: &mut HashMap<[u8; 20], Bridge>,
|
|
bridges: &mut HashMap<[u8; 20], Bridge>,
|
|
censor: &mut Censor,
|
|
censor: &mut Censor,
|
|
- ) -> Result<Vec<User>, Error> {
|
|
|
|
|
|
+ ) -> Result<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
|
|
// assume that this step can be done even if the user can't
|
|
// assume that this step can be done even if the user can't
|
|
// actually talk to the LA.)
|
|
// actually talk to the LA.)
|
|
let (bucket, reachcred) = get_bucket(&config.la_net, &self.primary_cred).await?;
|
|
let (bucket, reachcred) = get_bucket(&config.la_net, &self.primary_cred).await?;
|
|
- let level = scalar_u32(&self.primary_cred.trust_level).unwrap();
|
|
|
|
|
|
+ let level = match scalar_u32(&self.primary_cred.trust_level) {
|
|
|
|
+ Some(v) => v,
|
|
|
|
+ None => return Err(anyhow!("Failed to get trust level from credential")),
|
|
|
|
+ };
|
|
|
|
|
|
// 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 {
|
|
@@ -506,14 +516,17 @@ impl User {
|
|
}
|
|
}
|
|
|
|
|
|
if negative_reports.len() > 0 {
|
|
if negative_reports.len() > 0 {
|
|
- Self::send_negative_reports(&config, negative_reports).await;
|
|
|
|
|
|
+ Self::send_negative_reports(&config, negative_reports).await?;
|
|
}
|
|
}
|
|
if positive_reports.len() > 0 {
|
|
if positive_reports.len() > 0 {
|
|
- Self::send_positive_reports(&config, positive_reports).await;
|
|
|
|
|
|
+ Self::send_positive_reports(&config, positive_reports).await?;
|
|
}
|
|
}
|
|
|
|
|
|
// Invite friends if applicable
|
|
// Invite friends if applicable
|
|
- let invitations = scalar_u32(&self.primary_cred.invites_remaining).unwrap();
|
|
|
|
|
|
+ let invitations = match scalar_u32(&self.primary_cred.invites_remaining) {
|
|
|
|
+ Some(v) => v,
|
|
|
|
+ None => 0, // This is probably an error case that should not happen
|
|
|
|
+ };
|
|
let mut new_friends = Vec::<User>::new();
|
|
let mut new_friends = Vec::<User>::new();
|
|
for _i in 0..min(invitations, num_users_requesting_invites) {
|
|
for _i in 0..min(invitations, num_users_requesting_invites) {
|
|
if event_happens(config.prob_user_invites_friend) {
|
|
if event_happens(config.prob_user_invites_friend) {
|
|
@@ -561,7 +574,7 @@ impl User {
|
|
config: &Config,
|
|
config: &Config,
|
|
bridges: &mut HashMap<[u8; 20], Bridge>,
|
|
bridges: &mut HashMap<[u8; 20], Bridge>,
|
|
censor: &mut Censor,
|
|
censor: &mut Censor,
|
|
- ) -> Result<Vec<User>, Error> {
|
|
|
|
|
|
+ ) -> Result<Vec<User>> {
|
|
// Download bucket to see if bridge is still reachable and if we
|
|
// Download bucket to see if bridge is still reachable and if we
|
|
// have any new bridges
|
|
// have any new bridges
|
|
let (bucket, reachcred) = get_bucket(&config.la_net, &self.primary_cred).await?;
|
|
let (bucket, reachcred) = get_bucket(&config.la_net, &self.primary_cred).await?;
|