2024-01-22 23:06:50 -05:00
|
|
|
use lazy_static::lazy_static;
|
2023-11-28 13:18:08 -05:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-02-07 18:36:40 -05:00
|
|
|
use sled::Db;
|
|
|
|
use std::{
|
|
|
|
collections::{BTreeMap, HashMap, HashSet},
|
|
|
|
fmt,
|
|
|
|
fs::File,
|
|
|
|
io::BufReader,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub mod extra_info;
|
|
|
|
pub mod negative_report;
|
|
|
|
pub mod positive_report;
|
|
|
|
|
|
|
|
use extra_info::*;
|
|
|
|
use negative_report::*;
|
|
|
|
use positive_report::*;
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub struct Config {
|
|
|
|
pub db: DbConfig,
|
|
|
|
require_bridge_token: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub struct DbConfig {
|
|
|
|
// The path for the server database, default is "server_db"
|
|
|
|
pub db_path: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for DbConfig {
|
|
|
|
fn default() -> DbConfig {
|
|
|
|
DbConfig {
|
|
|
|
db_path: "server_db".to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-22 23:06:50 -05:00
|
|
|
|
|
|
|
lazy_static! {
|
2024-02-07 18:36:40 -05:00
|
|
|
// known country codes based on Tor geoIP database
|
|
|
|
// Produced with `cat /usr/share/tor/geoip{,6} | grep -v ^# | grep -o ..$ | sort | uniq | tr '[:upper:]' '[:lower:]' | tr '\n' ',' | sed 's/,/","/g'`
|
|
|
|
pub static ref COUNTRY_CODES: HashSet<&'static str> = HashSet::from(["??","ad","ae","af","ag","ai","al","am","ao","ap","aq","ar","as","at","au","aw","ax","az","ba","bb","bd","be","bf","bg","bh","bi","bj","bl","bm","bn","bo","bq","br","bs","bt","bv","bw","by","bz","ca","cc","cd","cf","cg","ch","ci","ck","cl","cm","cn","co","cr","cs","cu","cv","cw","cx","cy","cz","de","dj","dk","dm","do","dz","ec","ee","eg","eh","er","es","et","eu","fi","fj","fk","fm","fo","fr","ga","gb","gd","ge","gf","gg","gh","gi","gl","gm","gn","gp","gq","gr","gs","gt","gu","gw","gy","hk","hm","hn","hr","ht","hu","id","ie","il","im","in","io","iq","ir","is","it","je","jm","jo","jp","ke","kg","kh","ki","km","kn","kp","kr","kw","ky","kz","la","lb","lc","li","lk","lr","ls","lt","lu","lv","ly","ma","mc","md","me","mf","mg","mh","mk","ml","mm","mn","mo","mp","mq","mr","ms","mt","mu","mv","mw","mx","my","mz","na","nc","ne","nf","ng","ni","nl","no","np","nr","nu","nz","om","pa","pe","pf","pg","ph","pk","pl","pm","pn","pr","ps","pt","pw","py","qa","re","ro","rs","ru","rw","sa","sb","sc","sd","se","sg","sh","si","sj","sk","sl","sm","sn","so","sr","ss","st","sv","sx","sy","sz","tc","td","tf","tg","th","tj","tk","tl","tm","tn","to","tr","tt","tv","tw","tz","ua","ug","um","us","uy","uz","va","vc","ve","vg","vi","vn","vu","wf","ws","ye","yt","za","zm","zw"]);
|
2023-12-05 18:05:44 -05:00
|
|
|
|
2024-02-07 18:36:40 -05:00
|
|
|
// read config data at run time
|
|
|
|
pub static ref CONFIG: Config = serde_json::from_reader(
|
|
|
|
BufReader::new(
|
|
|
|
File::open("config.json").expect("Could not read config file") // TODO: Make config filename configurable
|
|
|
|
)
|
|
|
|
).expect("Reading config file from JSON failed");
|
|
|
|
}
|
2024-01-17 18:53:40 -05:00
|
|
|
|
2023-11-28 13:18:08 -05:00
|
|
|
/// Get Julian date
|
2024-01-22 23:06:50 -05:00
|
|
|
pub fn get_date() -> u32 {
|
2023-11-28 13:18:08 -05:00
|
|
|
time::OffsetDateTime::now_utc()
|
|
|
|
.date()
|
|
|
|
.to_julian_day()
|
|
|
|
.try_into()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2024-02-07 18:36:40 -05:00
|
|
|
/// All the info for a bridge, to be stored in the database
|
2023-11-28 13:18:08 -05:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2024-02-07 18:36:40 -05:00
|
|
|
pub struct BridgeInfo {
|
|
|
|
/// hashed fingerprint (SHA-1 hash of 20-byte bridge ID)
|
|
|
|
pub fingerprint: [u8; 20],
|
|
|
|
/// nickname of bridge (probably not necessary)
|
|
|
|
pub nickname: String,
|
|
|
|
/// flag indicating whether the bridge is believed to be blocked
|
|
|
|
pub is_blocked: bool,
|
|
|
|
/// map of dates to data for that day
|
|
|
|
pub info_by_day: HashMap<u32, DailyBridgeInfo>,
|
2023-11-28 13:18:08 -05:00
|
|
|
}
|
|
|
|
|
2024-02-07 18:36:40 -05:00
|
|
|
impl BridgeInfo {
|
|
|
|
pub fn new(fingerprint: [u8; 20], nickname: String) -> Self {
|
|
|
|
Self {
|
|
|
|
fingerprint: fingerprint,
|
|
|
|
nickname: nickname,
|
|
|
|
is_blocked: false,
|
|
|
|
info_by_day: HashMap::<u32, DailyBridgeInfo>::new(),
|
2023-12-05 19:55:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-07 18:36:40 -05:00
|
|
|
impl fmt::Display for BridgeInfo {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let mut str = format!(
|
|
|
|
"fingerprint:{}\n",
|
|
|
|
array_bytes::bytes2hex("", self.fingerprint).as_str()
|
|
|
|
);
|
|
|
|
str.push_str(format!("nickname: {}\n", self.nickname).as_str());
|
|
|
|
str.push_str(format!("is_blocked: {}\n", self.is_blocked).as_str());
|
|
|
|
str.push_str("info_by_day:");
|
|
|
|
for day in self.info_by_day.keys() {
|
|
|
|
str.push_str(format!("\n day: {}", day).as_str());
|
|
|
|
let daily_info = self.info_by_day.get(day).unwrap();
|
|
|
|
for line in daily_info.to_string().lines() {
|
|
|
|
str.push_str(format!("\n {}", line).as_str());
|
2023-11-28 17:56:49 -05:00
|
|
|
}
|
2023-11-28 13:18:08 -05:00
|
|
|
}
|
2024-02-07 18:36:40 -05:00
|
|
|
write!(f, "{}", str)
|
2023-12-05 19:55:33 -05:00
|
|
|
}
|
2023-11-28 13:18:08 -05:00
|
|
|
}
|
|
|
|
|
2024-02-07 18:36:40 -05:00
|
|
|
// TODO: Should this be an enum to make it easier to implement different
|
|
|
|
// versions for plugins?
|
2024-01-22 23:06:50 -05:00
|
|
|
|
2024-02-07 18:36:40 -05:00
|
|
|
/// Information about bridge reachability, gathered daily
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct DailyBridgeInfo {
|
|
|
|
/// Map of country codes and how many users (rounded up to a multiple of
|
|
|
|
/// 8) have connected to that bridge during the day.
|
|
|
|
pub bridge_ips: BTreeMap<String, u32>,
|
|
|
|
/// Set of negative reports received during this day
|
|
|
|
pub negative_reports: Vec<SerializableNegativeReport>,
|
|
|
|
/// Set of positive reports received during this day
|
|
|
|
pub positive_reports: Vec<SerializablePositiveReport>,
|
|
|
|
// We don't care about ordering of the reports, but I'm using vectors for
|
|
|
|
// reports because we don't want a set to deduplicate our reports, and
|
|
|
|
// I don't want to implement Hash or Ord. Another possibility might be a
|
|
|
|
// map of the report to the number of that exact report we received.
|
|
|
|
// Positive reports include a Lox proof and should be unique, but negative
|
|
|
|
// reports could be deduplicated.
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DailyBridgeInfo {
|
|
|
|
pub fn new() -> Self {
|
2023-11-28 13:18:08 -05:00
|
|
|
Self {
|
2024-02-07 18:36:40 -05:00
|
|
|
bridge_ips: BTreeMap::<String, u32>::new(),
|
|
|
|
negative_reports: Vec::<SerializableNegativeReport>::new(),
|
|
|
|
positive_reports: Vec::<SerializablePositiveReport>::new(),
|
2023-11-28 13:18:08 -05:00
|
|
|
}
|
|
|
|
}
|
2024-01-22 23:06:50 -05:00
|
|
|
}
|
|
|
|
|
2024-02-07 18:36:40 -05:00
|
|
|
impl fmt::Display for DailyBridgeInfo {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let mut str = String::from("bridge_ips:");
|
|
|
|
for country in self.bridge_ips.keys() {
|
|
|
|
str.push_str(
|
|
|
|
format!(
|
|
|
|
"\n cc: {}, connections: {}",
|
|
|
|
country,
|
|
|
|
self.bridge_ips.get(country).unwrap()
|
|
|
|
)
|
|
|
|
.as_str(),
|
|
|
|
);
|
2024-01-22 23:06:50 -05:00
|
|
|
}
|
2024-02-07 18:36:40 -05:00
|
|
|
write!(f, "{}", str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds the extra-info data for a single bridge to the database. If the
|
|
|
|
/// database already contains an extra-info for this bridge for thid date,
|
|
|
|
/// 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 fingerprint = extra_info.fingerprint;
|
|
|
|
let mut bridge_info = match db.get(&fingerprint).unwrap() {
|
|
|
|
Some(v) => bincode::deserialize(&v).unwrap(),
|
|
|
|
None => BridgeInfo::new(fingerprint, extra_info.nickname),
|
|
|
|
};
|
|
|
|
// If we already have an entry, compare it with the new one. For each
|
|
|
|
// country:count mapping, use the greater of the two counts.
|
|
|
|
if bridge_info.info_by_day.contains_key(&extra_info.published) {
|
|
|
|
let daily_bridge_info = bridge_info
|
|
|
|
.info_by_day
|
|
|
|
.get_mut(&extra_info.published)
|
|
|
|
.unwrap();
|
|
|
|
if extra_info.bridge_ips != daily_bridge_info.bridge_ips {
|
|
|
|
for country in extra_info.bridge_ips.keys() {
|
|
|
|
if daily_bridge_info.bridge_ips.contains_key(country) {
|
|
|
|
// Use greatest value we've seen today
|
|
|
|
if daily_bridge_info.bridge_ips.get(country).unwrap()
|
|
|
|
< extra_info.bridge_ips.get(country).unwrap()
|
2024-01-22 23:06:50 -05:00
|
|
|
{
|
2024-02-07 18:36:40 -05:00
|
|
|
daily_bridge_info.bridge_ips.insert(
|
|
|
|
country.to_string(),
|
|
|
|
*extra_info.bridge_ips.get(country).unwrap(),
|
|
|
|
);
|
2024-01-22 23:06:50 -05:00
|
|
|
}
|
2024-02-07 18:36:40 -05:00
|
|
|
} else {
|
|
|
|
daily_bridge_info.bridge_ips.insert(
|
|
|
|
country.to_string(),
|
|
|
|
*extra_info.bridge_ips.get(country).unwrap(),
|
|
|
|
);
|
2024-01-22 23:06:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-07 18:36:40 -05:00
|
|
|
} else {
|
|
|
|
// No existing entry; make a new one.
|
|
|
|
let daily_bridge_info = DailyBridgeInfo {
|
|
|
|
bridge_ips: extra_info.bridge_ips,
|
|
|
|
negative_reports: Vec::<SerializableNegativeReport>::new(),
|
|
|
|
positive_reports: Vec::<SerializablePositiveReport>::new(),
|
|
|
|
};
|
|
|
|
bridge_info
|
|
|
|
.info_by_day
|
|
|
|
.insert(extra_info.published, daily_bridge_info);
|
|
|
|
}
|
|
|
|
// Commit changes to database
|
|
|
|
db.insert(fingerprint, bincode::serialize(&bridge_info).unwrap())
|
|
|
|
.unwrap();
|
2023-11-28 13:18:08 -05:00
|
|
|
}
|