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 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;
pub const ACCEPTED_HOURS_OF_FAILURE: i64 = 3;
@ -19,11 +19,12 @@ pub fn parse_into_bridgelines(
let resource_uid = resource
.get_uid()
.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!(
"type={} fingerprint={:?} params={:?}",
resource.r#type, resource.fingerprint, resource.params,
"type={} 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());
let mut blocked = false;
@ -40,6 +41,7 @@ pub fn parse_into_bridgelines(
addr: ip_bytes,
port: resource.port,
uid_fingerprint: resource_uid,
fingerprint: fingerprint,
info: info_bytes,
});
} else {
@ -47,6 +49,7 @@ pub fn parse_into_bridgelines(
addr: ip_bytes,
port: resource.port,
uid_fingerprint: resource_uid,
fingerprint: fingerprint,
info: info_bytes,
});
}

View File

@ -25,7 +25,10 @@ use std::convert::{TryFrom, TryInto};
use subtle::ConstantTimeEq;
/// 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
pub const MAX_BRIDGES_PER_BUCKET: usize = 3;
@ -46,10 +49,12 @@ pub struct BridgeLine {
pub port: u16,
/// fingerprint
pub uid_fingerprint: u64,
/// unhashed fingerprint (20-byte bridge ID)
pub fingerprint: [u8; 20],
/// other protocol information, including pluggable transport,
/// public key, etc.
#[serde_as(as = "[_; BRIDGE_BYTES - 26]")]
pub info: [u8; BRIDGE_BYTES - 26],
#[serde_as(as = "[_; BRIDGE_INFO_BYTES]")]
pub info: [u8; BRIDGE_INFO_BYTES],
}
/// A bucket contains MAX_BRIDGES_PER_BUCKET bridges plus the
@ -73,7 +78,8 @@ impl Default for BridgeLine {
addr: [0; 16],
port: 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[16..18].copy_from_slice(&self.port.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
}
/// Decode a BridgeLine from a byte array
@ -94,7 +101,8 @@ impl BridgeLine {
res.addr.copy_from_slice(&data[0..16]);
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.info.copy_from_slice(&data[26..]);
res.fingerprint.copy_from_slice(&data[26..46]);
res.info.copy_from_slice(&data[46..]);
res
}
/// 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
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::*;
@ -995,7 +995,7 @@ fn test_update_bridge() {
"obfs2",
"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());
@ -1003,6 +1003,7 @@ fn test_update_bridge() {
addr: bridgeline_to_update.addr,
port: bridgeline_to_update.port,
uid_fingerprint: bridgeline_to_update.uid_fingerprint,
fingerprint: bridgeline_to_update.fingerprint,
info: updated_info_bytes,
};