Move (unhashed) fingerprint to field in BridgeLine

This commit is contained in:
Vecna 2024-03-11 15:58:31 -04:00
parent e1bed84e27
commit 503c026964
3 changed files with 24 additions and 12 deletions

View File

@ -1,5 +1,5 @@
use chrono::{Duration, Utc}; use chrono::{Duration, Utc};
use lox_library::bridge_table::{BridgeLine, BRIDGE_BYTES, MAX_BRIDGES_PER_BUCKET}; use lox_library::bridge_table::{BridgeLine, BRIDGE_INFO_BYTES, MAX_BRIDGES_PER_BUCKET};
use rdsys_backend::proto::Resource; use rdsys_backend::proto::Resource;
pub const ACCEPTED_HOURS_OF_FAILURE: i64 = 3; pub const ACCEPTED_HOURS_OF_FAILURE: i64 = 3;
@ -19,11 +19,12 @@ pub fn parse_into_bridgelines(
let resource_uid = resource let resource_uid = resource
.get_uid() .get_uid()
.expect("Unable to get Fingerprint UID of resource"); .expect("Unable to get Fingerprint UID of resource");
let fingerprint: [u8; 20] = array_bytes::hex2array(&resource.fingerprint).expect("Failed to parse bridge fingerprint");
let infostr: String = format!( let infostr: String = format!(
"type={} fingerprint={:?} params={:?}", "type={} params={:?}",
resource.r#type, resource.fingerprint, resource.params, resource.r#type, resource.params,
); );
let mut info_bytes: [u8; BRIDGE_BYTES - 26] = [0; BRIDGE_BYTES - 26]; let mut info_bytes: [u8; BRIDGE_INFO_BYTES] = [0; BRIDGE_INFO_BYTES];
info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes()); info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes());
let mut blocked = false; let mut blocked = false;
@ -40,6 +41,7 @@ pub fn parse_into_bridgelines(
addr: ip_bytes, addr: ip_bytes,
port: resource.port, port: resource.port,
uid_fingerprint: resource_uid, uid_fingerprint: resource_uid,
fingerprint: fingerprint,
info: info_bytes, info: info_bytes,
}); });
} else { } else {
@ -47,6 +49,7 @@ pub fn parse_into_bridgelines(
addr: ip_bytes, addr: ip_bytes,
port: resource.port, port: resource.port,
uid_fingerprint: resource_uid, uid_fingerprint: resource_uid,
fingerprint: fingerprint,
info: info_bytes, info: info_bytes,
}); });
} }

View File

@ -25,7 +25,10 @@ use std::convert::{TryFrom, TryInto};
use subtle::ConstantTimeEq; use subtle::ConstantTimeEq;
/// Each bridge information line is serialized into this many bytes /// Each bridge information line is serialized into this many bytes
pub const BRIDGE_BYTES: usize = 250; pub const BRIDGE_BYTES: usize = 230;
/// The bridge info field is this many bytes long
pub const BRIDGE_INFO_BYTES: usize = BRIDGE_BYTES - 46;
/// The max number of bridges per bucket /// The max number of bridges per bucket
pub const MAX_BRIDGES_PER_BUCKET: usize = 3; pub const MAX_BRIDGES_PER_BUCKET: usize = 3;
@ -46,10 +49,12 @@ pub struct BridgeLine {
pub port: u16, pub port: u16,
/// fingerprint /// fingerprint
pub uid_fingerprint: u64, pub uid_fingerprint: u64,
/// unhashed fingerprint (20-byte bridge ID)
pub fingerprint: [u8; 20],
/// other protocol information, including pluggable transport, /// other protocol information, including pluggable transport,
/// public key, etc. /// public key, etc.
#[serde_as(as = "[_; BRIDGE_BYTES - 26]")] #[serde_as(as = "[_; BRIDGE_INFO_BYTES]")]
pub info: [u8; BRIDGE_BYTES - 26], pub info: [u8; BRIDGE_INFO_BYTES],
} }
/// A bucket contains MAX_BRIDGES_PER_BUCKET bridges plus the /// A bucket contains MAX_BRIDGES_PER_BUCKET bridges plus the
@ -73,7 +78,8 @@ impl Default for BridgeLine {
addr: [0; 16], addr: [0; 16],
port: 0, port: 0,
uid_fingerprint: 0, uid_fingerprint: 0,
info: [0; BRIDGE_BYTES - 26], fingerprint: [0; 20],
info: [0; BRIDGE_INFO_BYTES],
} }
} }
} }
@ -85,7 +91,8 @@ impl BridgeLine {
res[0..16].copy_from_slice(&self.addr); res[0..16].copy_from_slice(&self.addr);
res[16..18].copy_from_slice(&self.port.to_be_bytes()); res[16..18].copy_from_slice(&self.port.to_be_bytes());
res[18..26].copy_from_slice(&self.uid_fingerprint.to_be_bytes()); res[18..26].copy_from_slice(&self.uid_fingerprint.to_be_bytes());
res[26..].copy_from_slice(&self.info); res[26..46].copy_from_slice(&self.fingerprint);
res[46..].copy_from_slice(&self.info);
res res
} }
/// Decode a BridgeLine from a byte array /// Decode a BridgeLine from a byte array
@ -94,7 +101,8 @@ impl BridgeLine {
res.addr.copy_from_slice(&data[0..16]); res.addr.copy_from_slice(&data[0..16]);
res.port = u16::from_be_bytes(data[16..18].try_into().unwrap()); res.port = u16::from_be_bytes(data[16..18].try_into().unwrap());
res.uid_fingerprint = u64::from_be_bytes(data[18..26].try_into().unwrap()); res.uid_fingerprint = u64::from_be_bytes(data[18..26].try_into().unwrap());
res.info.copy_from_slice(&data[26..]); res.fingerprint.copy_from_slice(&data[26..46]);
res.info.copy_from_slice(&data[46..]);
res res
} }
/// Encode a bucket to a byte array, including a Bucket Reachability /// Encode a bucket to a byte array, including a Bucket Reachability

View File

@ -1,7 +1,7 @@
/*! Unit tests that require access to the testing-only function /*! Unit tests that require access to the testing-only function
BridgeLine::random() or private fields */ BridgeLine::random() or private fields */
use super::bridge_table::{BridgeLine, BRIDGE_BYTES}; use super::bridge_table::{BridgeLine, BRIDGE_INFO_BYTES};
use super::proto::*; use super::proto::*;
use super::*; use super::*;
@ -995,7 +995,7 @@ fn test_update_bridge() {
"obfs2", "obfs2",
"moat", "moat",
); );
let mut updated_info_bytes: [u8; BRIDGE_BYTES - 26] = [0; BRIDGE_BYTES - 26]; let mut updated_info_bytes: [u8; BRIDGE_INFO_BYTES] = [0; BRIDGE_INFO_BYTES];
updated_info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes()); updated_info_bytes[..infostr.len()].copy_from_slice(infostr.as_bytes());
@ -1003,6 +1003,7 @@ fn test_update_bridge() {
addr: bridgeline_to_update.addr, addr: bridgeline_to_update.addr,
port: bridgeline_to_update.port, port: bridgeline_to_update.port,
uid_fingerprint: bridgeline_to_update.uid_fingerprint, uid_fingerprint: bridgeline_to_update.uid_fingerprint,
fingerprint: bridgeline_to_update.fingerprint,
info: updated_info_bytes, info: updated_info_bytes,
}; };