mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-03-31 17:55:36 +00:00
WIP: aquatic_ws: add half-crappy load test, fix infinite loop bug
This commit is contained in:
parent
c7be84a61e
commit
a5108f813d
17 changed files with 821 additions and 10 deletions
17
aquatic_ws_protocol/Cargo.toml
Normal file
17
aquatic_ws_protocol/Cargo.toml
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
[package]
|
||||
name = "aquatic_ws_protocol"
|
||||
version = "0.1.0"
|
||||
authors = ["Joakim Frostegård <joakim.frostegard@gmail.com>"]
|
||||
edition = "2018"
|
||||
license = "Apache-2.0"
|
||||
|
||||
[lib]
|
||||
name = "aquatic_ws_protocol"
|
||||
|
||||
[dependencies]
|
||||
hashbrown = { version = "0.8", features = ["serde"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
tungstenite = "0.11"
|
||||
|
||||
[dev-dependencies]
|
||||
300
aquatic_ws_protocol/src/lib.rs
Normal file
300
aquatic_ws_protocol/src/lib.rs
Normal file
|
|
@ -0,0 +1,300 @@
|
|||
use hashbrown::HashMap;
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
mod serde_helpers;
|
||||
|
||||
use serde_helpers::*;
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct PeerId(
|
||||
#[serde(
|
||||
deserialize_with = "deserialize_20_bytes",
|
||||
serialize_with = "serialize_20_bytes"
|
||||
)]
|
||||
pub [u8; 20]
|
||||
);
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct InfoHash(
|
||||
#[serde(
|
||||
deserialize_with = "deserialize_20_bytes",
|
||||
serialize_with = "serialize_20_bytes"
|
||||
)]
|
||||
pub [u8; 20]
|
||||
);
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct OfferId(
|
||||
#[serde(
|
||||
deserialize_with = "deserialize_20_bytes",
|
||||
serialize_with = "serialize_20_bytes"
|
||||
)]
|
||||
pub [u8; 20]
|
||||
);
|
||||
|
||||
|
||||
/// Some kind of nested structure from https://www.npmjs.com/package/simple-peer
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct JsonValue(pub ::serde_json::Value);
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum AnnounceEvent {
|
||||
Started,
|
||||
Stopped,
|
||||
Completed,
|
||||
Update
|
||||
}
|
||||
|
||||
|
||||
impl Default for AnnounceEvent {
|
||||
fn default() -> Self {
|
||||
Self::Update
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Apparently, these are sent to a number of peers when they are set
|
||||
/// in an AnnounceRequest
|
||||
/// action = "announce"
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct MiddlemanOfferToPeer {
|
||||
/// Peer id of peer sending offer
|
||||
/// Note: if equal to client peer_id, client ignores offer
|
||||
pub peer_id: PeerId,
|
||||
pub info_hash: InfoHash,
|
||||
/// Gets copied from AnnounceRequestOffer
|
||||
pub offer: JsonValue,
|
||||
/// Gets copied from AnnounceRequestOffer
|
||||
pub offer_id: OfferId,
|
||||
}
|
||||
|
||||
|
||||
/// If announce request has answer = true, send this to peer with
|
||||
/// peer id == "to_peer_id" field
|
||||
/// Action field should be 'announce'
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct MiddlemanAnswerToPeer {
|
||||
/// Note: if equal to client peer_id, client ignores answer
|
||||
pub peer_id: PeerId,
|
||||
pub info_hash: InfoHash,
|
||||
pub answer: JsonValue,
|
||||
pub offer_id: OfferId,
|
||||
}
|
||||
|
||||
|
||||
/// Element of AnnounceRequest.offers
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AnnounceRequestOffer {
|
||||
pub offer: JsonValue,
|
||||
pub offer_id: OfferId,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AnnounceRequest {
|
||||
pub info_hash: InfoHash,
|
||||
pub peer_id: PeerId,
|
||||
/// Just called "left" in protocol. Is set to None in some cases, such as
|
||||
/// when opening a magnet link
|
||||
#[serde(rename = "left")]
|
||||
pub bytes_left: Option<usize>,
|
||||
/// Can be empty. Then, default is "update"
|
||||
#[serde(default)]
|
||||
pub event: AnnounceEvent,
|
||||
|
||||
/// Only when this is an array offers are sent to random peers
|
||||
/// Length of this is number of peers wanted?
|
||||
/// Max length of this is 10 in reference client code
|
||||
/// Not sent when announce event is stopped or completed
|
||||
pub offers: Option<Vec<AnnounceRequestOffer>>,
|
||||
/// Seems to only get sent by client when sending offers, and is also same
|
||||
/// as length of offers vector (or at least never less)
|
||||
/// Max length of this is 10 in reference client code
|
||||
/// Could probably be ignored, `offers.len()` should provide needed info
|
||||
pub numwant: Option<usize>,
|
||||
|
||||
/// If empty, send response before sending offers (or possibly "skip sending update back"?)
|
||||
/// Else, send MiddlemanAnswerToPeer to peer with "to_peer_id" as peer_id.
|
||||
/// I think using Option is good, it seems like this isn't always set
|
||||
/// (same as `offers`)
|
||||
pub answer: Option<JsonValue>,
|
||||
/// Likely undefined if !(answer == true)
|
||||
pub to_peer_id: Option<PeerId>,
|
||||
/// Sent if answer is set
|
||||
pub offer_id: Option<OfferId>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct AnnounceResponse {
|
||||
pub info_hash: InfoHash,
|
||||
/// Client checks if this is null, not clear why
|
||||
pub complete: usize,
|
||||
pub incomplete: usize,
|
||||
#[serde(rename = "interval")]
|
||||
pub announce_interval: usize, // Default 2 min probably
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ScrapeRequest {
|
||||
// If omitted, scrape for all torrents, apparently
|
||||
// There is some kind of parsing here too which accepts a single info hash
|
||||
// and puts it into a vector
|
||||
#[serde(
|
||||
rename = "info_hash",
|
||||
deserialize_with = "deserialize_info_hashes",
|
||||
default
|
||||
)]
|
||||
pub info_hashes: Vec<InfoHash>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ScrapeStatistics {
|
||||
pub complete: usize,
|
||||
pub incomplete: usize,
|
||||
pub downloaded: usize,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ScrapeResponse {
|
||||
pub files: HashMap<InfoHash, ScrapeStatistics>,
|
||||
// Looks like `flags` field is ignored in reference client
|
||||
// pub flags: HashMap<String, usize>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Action {
|
||||
Announce,
|
||||
Scrape
|
||||
}
|
||||
|
||||
|
||||
/// Helper for serializing and deserializing messages
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct ActionWrapper<T> {
|
||||
pub action: Action,
|
||||
#[serde(flatten)]
|
||||
pub inner: T,
|
||||
}
|
||||
|
||||
|
||||
impl <T>ActionWrapper<T> {
|
||||
#[inline]
|
||||
pub fn announce(t: T) -> Self {
|
||||
Self {
|
||||
action: Action::Announce,
|
||||
inner: t
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn scrape(t: T) -> Self {
|
||||
Self {
|
||||
action: Action::Scrape,
|
||||
inner: t
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum InMessage {
|
||||
AnnounceRequest(AnnounceRequest),
|
||||
ScrapeRequest(ScrapeRequest),
|
||||
}
|
||||
|
||||
|
||||
impl InMessage {
|
||||
/// Try parsing as announce request first. If that fails, try parsing as
|
||||
/// scrape request, or return None
|
||||
#[inline]
|
||||
pub fn from_ws_message(ws_message: tungstenite::Message) -> Option<Self> {
|
||||
use tungstenite::Message::{Text, Binary};
|
||||
|
||||
let text = match ws_message {
|
||||
Text(text) => Some(text),
|
||||
Binary(bytes) => String::from_utf8(bytes).ok(),
|
||||
_ => None
|
||||
}?;
|
||||
|
||||
let res: Result<ActionWrapper<AnnounceRequest>, _> = serde_json::from_str(&text);
|
||||
|
||||
if let Ok(ActionWrapper { action: Action::Announce, inner }) = res {
|
||||
return Some(InMessage::AnnounceRequest(inner));
|
||||
}
|
||||
|
||||
let res: Result<ActionWrapper<ScrapeRequest>, _> = serde_json::from_str(&text);
|
||||
|
||||
if let Ok(ActionWrapper { action: Action::Scrape, inner }) = res {
|
||||
return Some(InMessage::ScrapeRequest(inner));
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn to_ws_message(&self) -> ::tungstenite::Message {
|
||||
let text = match self {
|
||||
InMessage::AnnounceRequest(r) => {
|
||||
serde_json::to_string(&ActionWrapper::announce(r)).unwrap()
|
||||
},
|
||||
InMessage::ScrapeRequest(r) => {
|
||||
serde_json::to_string(&ActionWrapper::scrape(r)).unwrap()
|
||||
},
|
||||
};
|
||||
|
||||
::tungstenite::Message::from(text)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum OutMessage {
|
||||
AnnounceResponse(AnnounceResponse),
|
||||
ScrapeResponse(ScrapeResponse),
|
||||
Offer(MiddlemanOfferToPeer),
|
||||
Answer(MiddlemanAnswerToPeer),
|
||||
}
|
||||
|
||||
|
||||
impl OutMessage {
|
||||
#[inline]
|
||||
pub fn to_ws_message(self) -> tungstenite::Message {
|
||||
let json = match self {
|
||||
Self::AnnounceResponse(message) => {
|
||||
serde_json::to_string(
|
||||
&ActionWrapper::announce(message)
|
||||
).unwrap()
|
||||
},
|
||||
Self::Offer(message) => {
|
||||
serde_json::to_string(
|
||||
&ActionWrapper::announce(message)
|
||||
).unwrap()
|
||||
},
|
||||
Self::Answer(message) => {
|
||||
serde_json::to_string(
|
||||
&ActionWrapper::announce(message)
|
||||
).unwrap()
|
||||
},
|
||||
Self::ScrapeResponse(message) => {
|
||||
serde_json::to_string(
|
||||
&ActionWrapper::scrape(message)
|
||||
).unwrap()
|
||||
},
|
||||
};
|
||||
|
||||
tungstenite::Message::from(json)
|
||||
}
|
||||
}
|
||||
247
aquatic_ws_protocol/src/serde_helpers.rs
Normal file
247
aquatic_ws_protocol/src/serde_helpers.rs
Normal file
|
|
@ -0,0 +1,247 @@
|
|||
use serde::{Serializer, Deserializer, de::{Visitor, SeqAccess}};
|
||||
|
||||
use super::InfoHash;
|
||||
|
||||
|
||||
pub fn serialize_20_bytes<S>(
|
||||
data: &[u8; 20],
|
||||
serializer: S
|
||||
) -> Result<S::Ok, S::Error> where S: Serializer {
|
||||
let text: String = data.iter().map(|byte| *byte as char).collect();
|
||||
|
||||
serializer.serialize_str(&text)
|
||||
}
|
||||
|
||||
|
||||
struct TwentyByteVisitor;
|
||||
|
||||
impl<'de> Visitor<'de> for TwentyByteVisitor {
|
||||
type Value = [u8; 20];
|
||||
|
||||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
formatter.write_str("string consisting of 20 bytes")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||
where E: ::serde::de::Error,
|
||||
{
|
||||
// Value is encoded in nodejs reference client something as follows:
|
||||
// ```
|
||||
// var infoHash = 'abcd..'; // 40 hexadecimals
|
||||
// Buffer.from(infoHash, 'hex').toString('binary');
|
||||
// ```
|
||||
// As I understand it:
|
||||
// - the code above produces a UTF16 string of 20 chars, each having
|
||||
// only the "low byte" set (e.g., numeric value ranges from 0-255)
|
||||
// - serde_json decodes this to string of 20 chars (tested), each in
|
||||
// the aforementioned range (tested), so the bytes can be extracted
|
||||
// by casting each char to u8.
|
||||
|
||||
let mut arr = [0u8; 20];
|
||||
let mut char_iter = value.chars();
|
||||
|
||||
for a in arr.iter_mut(){
|
||||
if let Some(c) = char_iter.next(){
|
||||
if c as u32 > 255 {
|
||||
return Err(E::custom(format!(
|
||||
"character not in single byte range: {:#?}",
|
||||
c
|
||||
)));
|
||||
}
|
||||
|
||||
*a = c as u8;
|
||||
} else {
|
||||
return Err(E::custom(format!("not 20 bytes: {:#?}", value)));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(arr)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
pub fn deserialize_20_bytes<'de, D>(
|
||||
deserializer: D
|
||||
) -> Result<[u8; 20], D::Error>
|
||||
where D: Deserializer<'de>
|
||||
{
|
||||
deserializer.deserialize_any(TwentyByteVisitor)
|
||||
}
|
||||
|
||||
|
||||
pub struct InfoHashVecVisitor;
|
||||
|
||||
|
||||
impl<'de> Visitor<'de> for InfoHashVecVisitor {
|
||||
type Value = Vec<InfoHash>;
|
||||
|
||||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
formatter.write_str("string or array of strings consisting of 20 bytes")
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
|
||||
where E: ::serde::de::Error,
|
||||
{
|
||||
match TwentyByteVisitor::visit_str::<E>(TwentyByteVisitor, value){
|
||||
Ok(arr) => Ok(vec![InfoHash(arr)]),
|
||||
Err(err) => Err(E::custom(format!("got string, but {}", err)))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
||||
where A: SeqAccess<'de>
|
||||
{
|
||||
let mut info_hashes: Self::Value = Vec::new();
|
||||
|
||||
while let Ok(Some(value)) = seq.next_element::<&str>(){
|
||||
let arr = TwentyByteVisitor::visit_str(
|
||||
TwentyByteVisitor, value
|
||||
)?;
|
||||
|
||||
info_hashes.push(InfoHash(arr));
|
||||
}
|
||||
|
||||
Ok(info_hashes)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_none<E>(self) -> Result<Self::Value, E>
|
||||
where E: ::serde::de::Error
|
||||
{
|
||||
Ok(vec![])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Empty vector is returned if value is null or any invalid info hash
|
||||
/// is present
|
||||
#[inline]
|
||||
pub fn deserialize_info_hashes<'de, D>(
|
||||
deserializer: D
|
||||
) -> Result<Vec<InfoHash>, D::Error>
|
||||
where D: Deserializer<'de>,
|
||||
{
|
||||
Ok(deserializer.deserialize_any(InfoHashVecVisitor).unwrap_or_default())
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde::Deserialize;
|
||||
|
||||
use super::*;
|
||||
|
||||
fn info_hash_from_bytes(bytes: &[u8]) -> InfoHash {
|
||||
let mut arr = [0u8; 20];
|
||||
|
||||
assert!(bytes.len() == 20);
|
||||
|
||||
arr.copy_from_slice(&bytes[..]);
|
||||
|
||||
InfoHash(arr)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_20_bytes(){
|
||||
let input = r#""aaaabbbbccccddddeeee""#;
|
||||
|
||||
let expected = info_hash_from_bytes(b"aaaabbbbccccddddeeee");
|
||||
let observed: InfoHash = serde_json::from_str(input).unwrap();
|
||||
|
||||
assert_eq!(observed, expected);
|
||||
|
||||
let input = r#""aaaabbbbccccddddeee""#;
|
||||
let res_info_hash: Result<InfoHash, _> = serde_json::from_str(input);
|
||||
|
||||
assert!(res_info_hash.is_err());
|
||||
|
||||
let input = r#""aaaabbbbccccddddeee𝕊""#;
|
||||
let res_info_hash: Result<InfoHash, _> = serde_json::from_str(input);
|
||||
|
||||
assert!(res_info_hash.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_serde_20_bytes(){
|
||||
let info_hash = info_hash_from_bytes(b"aaaabbbbccccddddeeee");
|
||||
|
||||
let out = serde_json::to_string(&info_hash).unwrap();
|
||||
let info_hash_2 = serde_json::from_str(&out).unwrap();
|
||||
|
||||
assert_eq!(info_hash, info_hash_2);
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Deserialize)]
|
||||
struct Test {
|
||||
#[serde(deserialize_with = "deserialize_info_hashes", default)]
|
||||
info_hashes: Vec<InfoHash>,
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_info_hashes_vec(){
|
||||
let input = r#"{
|
||||
"info_hashes": ["aaaabbbbccccddddeeee", "aaaabbbbccccddddeeee"]
|
||||
}"#;
|
||||
|
||||
let expected = Test {
|
||||
info_hashes: vec![
|
||||
info_hash_from_bytes(b"aaaabbbbccccddddeeee"),
|
||||
info_hash_from_bytes(b"aaaabbbbccccddddeeee"),
|
||||
]
|
||||
};
|
||||
|
||||
let observed: Test = serde_json::from_str(input).unwrap();
|
||||
|
||||
assert_eq!(observed, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_info_hashes_str(){
|
||||
let input = r#"{
|
||||
"info_hashes": "aaaabbbbccccddddeeee"
|
||||
}"#;
|
||||
|
||||
let expected = Test {
|
||||
info_hashes: vec![
|
||||
info_hash_from_bytes(b"aaaabbbbccccddddeeee"),
|
||||
]
|
||||
};
|
||||
|
||||
let observed: Test = serde_json::from_str(input).unwrap();
|
||||
|
||||
assert_eq!(observed, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_info_hashes_null(){
|
||||
let input = r#"{
|
||||
"info_hashes": null
|
||||
}"#;
|
||||
|
||||
let expected = Test {
|
||||
info_hashes: vec![]
|
||||
};
|
||||
|
||||
let observed: Test = serde_json::from_str(input).unwrap();
|
||||
|
||||
assert_eq!(observed, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_info_hashes_missing(){
|
||||
let input = r#"{}"#;
|
||||
|
||||
let expected = Test {
|
||||
info_hashes: vec![]
|
||||
};
|
||||
|
||||
let observed: Test = serde_json::from_str(input).unwrap();
|
||||
|
||||
assert_eq!(observed, expected);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue