mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-01 02:05:30 +00:00
aquatic_ws: add more protocol fixes and info; small handler fix
This commit is contained in:
parent
b2b4f5bb89
commit
1bb9caa8d1
2 changed files with 69 additions and 44 deletions
|
|
@ -32,15 +32,27 @@ pub fn run_request_worker(
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: lock torrent mutex
|
||||||
|
|
||||||
|
// This should be separate function. Possibly requests should be separated
|
||||||
|
// above and one announce and one scrape handler used
|
||||||
for (meta, in_message) in in_messages.drain(..){
|
for (meta, in_message) in in_messages.drain(..){
|
||||||
|
// announce requests:
|
||||||
|
// if offers are set, fetch same number of peers, send offers to all of them
|
||||||
|
// if answer is set, fetch that peer, send answer to it
|
||||||
|
// finally, return announce response, seemingly
|
||||||
|
|
||||||
|
// scrape: just fetch stats for all info_hashes sent
|
||||||
|
|
||||||
let out_message = OutMessage::ScrapeResponse(ScrapeResponse {
|
let out_message = OutMessage::ScrapeResponse(ScrapeResponse {
|
||||||
files: HashMap::new(),
|
files: HashMap::new(),
|
||||||
flags: HashMap::new(),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
out_messages.push((meta, out_message));
|
out_messages.push((meta, out_message));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: unlock torrent mutex
|
||||||
|
|
||||||
for (meta, out_message) in out_messages.drain(..){
|
for (meta, out_message) in out_messages.drain(..){
|
||||||
out_message_sender.send(meta, out_message);
|
out_message_sender.send(meta, out_message);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,29 @@
|
||||||
use std::net::IpAddr;
|
|
||||||
|
|
||||||
use hashbrown::HashMap;
|
use hashbrown::HashMap;
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
|
||||||
|
|
||||||
|
/// FIXME: This is really just string with 20 length
|
||||||
#[derive(Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[serde(transparent)]
|
||||||
pub struct PeerId(pub [u8; 20]);
|
pub struct PeerId(pub [u8; 20]);
|
||||||
|
|
||||||
|
|
||||||
|
/// FIXME: This is really just string with 20 length
|
||||||
#[derive(Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[serde(transparent)]
|
||||||
pub struct InfoHash(pub [u8; 20]);
|
pub struct InfoHash(pub [u8; 20]);
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize)]
|
/// FIXME: This is really just string with 20 length
|
||||||
pub struct ResponsePeer {
|
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub peer_id: PeerId,
|
#[serde(transparent)]
|
||||||
pub ip: IpAddr, // From src socket addr
|
pub struct OfferId(pub [u8; 20]);
|
||||||
pub port: u16, // From src port
|
|
||||||
pub complete: bool, // bytes_left == 0
|
|
||||||
}
|
/// Some kind of nested structure from https://www.npmjs.com/package/simple-peer
|
||||||
|
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[serde(transparent)]
|
||||||
|
pub struct JsonValue(pub ::serde_json::Value);
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Deserialize)]
|
#[derive(Clone, Deserialize)]
|
||||||
|
|
@ -43,10 +48,14 @@ impl Default for AnnounceEvent {
|
||||||
/// action = "announce"
|
/// action = "announce"
|
||||||
#[derive(Clone, Serialize)]
|
#[derive(Clone, Serialize)]
|
||||||
pub struct MiddlemanOfferToPeer {
|
pub struct MiddlemanOfferToPeer {
|
||||||
pub peer_id: PeerId, // Peer id of peer sending offer
|
/// Peer id of peer sending offer
|
||||||
|
/// Note: if equal to client peer_id, client ignores offer
|
||||||
|
pub peer_id: PeerId,
|
||||||
pub info_hash: InfoHash,
|
pub info_hash: InfoHash,
|
||||||
pub offer: (), // Gets copied from AnnounceRequestOffer
|
/// Gets copied from AnnounceRequestOffer
|
||||||
pub offer_id: (), // Gets copied from AnnounceRequestOffer
|
pub offer: JsonValue,
|
||||||
|
/// Gets copied from AnnounceRequestOffer
|
||||||
|
pub offer_id: OfferId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -55,56 +64,62 @@ pub struct MiddlemanOfferToPeer {
|
||||||
/// Action field should be 'announce'
|
/// Action field should be 'announce'
|
||||||
#[derive(Clone, Serialize)]
|
#[derive(Clone, Serialize)]
|
||||||
pub struct MiddlemanAnswerToPeer {
|
pub struct MiddlemanAnswerToPeer {
|
||||||
|
/// Note: if equal to client peer_id, client ignores answer
|
||||||
pub peer_id: PeerId,
|
pub peer_id: PeerId,
|
||||||
pub info_hash: InfoHash,
|
pub info_hash: InfoHash,
|
||||||
pub answer: bool,
|
pub answer: JsonValue,
|
||||||
pub offer_id: (),
|
pub offer_id: OfferId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Element of AnnounceRequest.offers
|
/// Element of AnnounceRequest.offers
|
||||||
#[derive(Clone, Deserialize)]
|
#[derive(Clone, Deserialize)]
|
||||||
pub struct AnnounceRequestOffer {
|
pub struct AnnounceRequestOffer {
|
||||||
pub offer: (), // TODO: Check client for what this is
|
pub offer: JsonValue,
|
||||||
pub offer_id: (), // TODO: Check client for what this is
|
pub offer_id: OfferId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Deserialize)]
|
#[derive(Clone, Deserialize)]
|
||||||
pub struct AnnounceRequest {
|
pub struct AnnounceRequest {
|
||||||
pub info_hash: InfoHash, // FIXME: I think these are actually really just strings with 20 len, same with peer id
|
pub info_hash: InfoHash,
|
||||||
pub peer_id: PeerId,
|
pub peer_id: PeerId,
|
||||||
|
/// Just called "left" in protocol
|
||||||
#[serde(rename = "left")]
|
#[serde(rename = "left")]
|
||||||
pub bytes_left: bool, // Just called "left" in protocol
|
pub bytes_left: bool,
|
||||||
|
/// Can be empty. Then, default is "update"
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub event: AnnounceEvent, // Can be empty? Then, default is "update"
|
pub event: AnnounceEvent,
|
||||||
|
|
||||||
// Length of this is number of peers wanted?
|
/// Only when this is an array offers are sent to random peers
|
||||||
// Only when this is an array offers are sent
|
/// 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>>,
|
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 false, send response before sending offers (or possibly "skip sending update back"?)
|
/// If empty, send response before sending offers (or possibly "skip sending update back"?)
|
||||||
/// If true, send MiddlemanAnswerToPeer to peer with "to_peer_id" as peer_id.
|
/// Else, send MiddlemanAnswerToPeer to peer with "to_peer_id" as peer_id.
|
||||||
#[serde(default)]
|
/// I think using Option is good, it seems like this isn't always set
|
||||||
pub answer: bool,
|
/// (same as `offers`)
|
||||||
pub to_peer_id: Option<PeerId>, // Only parsed to hex if answer == true, probably undefined otherwise
|
pub answer: Option<JsonValue>,
|
||||||
|
/// Only parsed to hex if answer == true, probably undefined otherwise
|
||||||
|
pub to_peer_id: Option<PeerId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize)]
|
#[derive(Clone, Serialize)]
|
||||||
pub struct AnnounceResponse {
|
pub struct AnnounceResponse {
|
||||||
pub info_hash: InfoHash,
|
pub info_hash: InfoHash,
|
||||||
|
/// Client checks if this is null, not clear why
|
||||||
pub complete: usize,
|
pub complete: usize,
|
||||||
pub incomplete: usize,
|
pub incomplete: usize,
|
||||||
// I suspect receivers don't care about this and rely on offers instead??
|
|
||||||
// Also, what does it contain, exacly (not certain that it is ResponsePeer?)
|
|
||||||
pub peers: Vec<ResponsePeer>,
|
|
||||||
#[serde(rename = "interval")]
|
#[serde(rename = "interval")]
|
||||||
pub announce_interval: usize, // Default 2 min probably
|
pub announce_interval: usize, // Default 2 min probably
|
||||||
|
|
||||||
// Sent to "to_peer_id" peer (?? or did I put this into MiddlemanAnswerToPeer instead?)
|
|
||||||
// pub offer_id: (),
|
|
||||||
// pub answer: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -127,8 +142,9 @@ pub struct ScrapeStatistics {
|
||||||
|
|
||||||
#[derive(Clone, Serialize)]
|
#[derive(Clone, Serialize)]
|
||||||
pub struct ScrapeResponse {
|
pub struct ScrapeResponse {
|
||||||
pub files: HashMap<InfoHash, ScrapeStatistics>, // InfoHash to Scrape stats
|
pub files: HashMap<InfoHash, ScrapeStatistics>,
|
||||||
pub flags: HashMap<String, usize>,
|
// Looks like `flags` field is ignored in reference client
|
||||||
|
// pub flags: HashMap<String, usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -140,6 +156,7 @@ pub enum Action {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Helper for serializing and deserializing messages
|
||||||
#[derive(Clone, Serialize, Deserialize)]
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
struct ActionWrapper<T> {
|
struct ActionWrapper<T> {
|
||||||
pub action: Action,
|
pub action: Action,
|
||||||
|
|
@ -184,18 +201,14 @@ impl InMessage {
|
||||||
|
|
||||||
let res: Result<ActionWrapper<AnnounceRequest>, _> = serde_json::from_str(&text);
|
let res: Result<ActionWrapper<AnnounceRequest>, _> = serde_json::from_str(&text);
|
||||||
|
|
||||||
if let Ok(wrapper) = res {
|
if let Ok(ActionWrapper { action: Action::Announce, inner }) = res {
|
||||||
if let Action::Announce = wrapper.action {
|
return Some(InMessage::AnnounceRequest(inner));
|
||||||
return Some(InMessage::AnnounceRequest(wrapper.inner));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let res: Result<ActionWrapper<ScrapeRequest>, _> = serde_json::from_str(&text);
|
let res: Result<ActionWrapper<ScrapeRequest>, _> = serde_json::from_str(&text);
|
||||||
|
|
||||||
if let Ok(wrapper) = res {
|
if let Ok(ActionWrapper { action: Action::Scrape, inner }) = res {
|
||||||
if let Action::Scrape = wrapper.action {
|
return Some(InMessage::ScrapeRequest(inner));
|
||||||
return Some(InMessage::ScrapeRequest(wrapper.inner));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue