Skip to content

Commit f2eb533

Browse files
committed
ICE
1 parent aa6d0e1 commit f2eb533

File tree

4 files changed

+75
-28
lines changed

4 files changed

+75
-28
lines changed

src/conn.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use crate::future::timeout;
21
use crate::metainfo::InfoHash;
32
use crate::peer::Peer;
43
use crate::torrent::TorrentFile;
54
use bencode::ValueRef;
65
use reqwest::Client;
76
use std::convert::TryInto;
8-
use tokio::io::{AsyncReadExt, AsyncWriteExt};
7+
use std::io;
8+
use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt};
99
use tokio::net::TcpStream;
1010

1111
const PROTOCOL: &[u8] = b"BitTorrent protocol";
@@ -97,6 +97,8 @@ pub struct Handshake<'a> {
9797
}
9898

9999
impl<'a> Handshake<'a> {
100+
const LEN: usize = 68;
101+
100102
pub fn new(info_hash: &'a InfoHash, peer_id: &'a str) -> Self {
101103
Self {
102104
peer_id,
@@ -105,25 +107,32 @@ impl<'a> Handshake<'a> {
105107
}
106108
}
107109

108-
pub fn as_bytes(&self) -> Vec<u8> {
109-
let mut v = vec![];
110-
v.push(19);
111-
v.extend(PROTOCOL);
112-
v.extend(&self.extensions);
113-
v.extend(self.info_hash.as_ref());
114-
v.extend(self.peer_id.as_bytes());
115-
v
116-
}
110+
pub async fn send(&self, peer: &Peer) -> crate::Result<()> {
111+
let mut tcp = TcpStream::connect(peer.addr()).await?;
112+
self.write_bytes(&mut tcp).await?;
113+
114+
let mut v = [0; Self::LEN];
115+
tcp.read_exact(&mut v).await?;
117116

118-
pub async fn send(&self, peer: &Peer, timeout_secs: u64) -> crate::Result<()> {
119-
let mut tcp = timeout(TcpStream::connect(peer.addr()), timeout_secs).await?;
120-
let msg = self.as_bytes();
121-
timeout(tcp.write_all(&msg), timeout_secs).await?;
117+
println!("{:?}", v);
118+
Ok(())
119+
}
122120

123-
let mut v = vec![];
124-
timeout(tcp.read_to_end(&mut v), timeout_secs).await?;
121+
async fn write_bytes<W>(&self, writer: &mut W) -> io::Result<()>
122+
where
123+
W: AsyncWrite + Unpin,
124+
{
125+
dbg!(&[19]);
126+
dbg!(PROTOCOL);
127+
dbg!(&self.extensions);
128+
dbg!(self.info_hash.as_ref());
129+
dbg!(self.peer_id.as_bytes());
125130

126-
println!("{:?}, {:?}", msg, v);
131+
writer.write_all(&[19]).await?;
132+
writer.write_all(PROTOCOL).await?;
133+
writer.write_all(&self.extensions).await?;
134+
writer.write_all(self.info_hash.as_ref()).await?;
135+
writer.write_all(self.peer_id.as_bytes()).await?;
127136
Ok(())
128137
}
129138
}

src/future.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::future::Future;
22

3-
pub(crate) async fn timeout<T, E, F>(future: F, timeout_secs: u64) -> crate::Result<T>
3+
pub async fn timeout<T, E, F>(future: F, timeout_secs: u64) -> crate::Result<T>
44
where
55
F: Future<Output = Result<T, E>>,
66
crate::Error: From<E>,

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub mod bitfield;
2222
pub mod client;
2323
pub mod conn;
2424
mod error;
25-
mod future;
25+
pub mod future;
2626
pub mod magnet;
2727
pub mod metainfo;
2828
pub mod peer;

src/main.rs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,46 @@
11
use btrs::conn::{announce, Handshake};
2+
use btrs::future::timeout;
23
use btrs::peer;
34
use btrs::torrent::TorrentFile;
45
use futures::stream::FuturesUnordered;
56
use futures::StreamExt;
67
use tokio::fs;
78

9+
// fn main() {
10+
// const a: &[u8] = &[
11+
// 19, 66, 105, 116, 84, 111, 114, 114, 101, 110, 116, 32, 112, 114, 111, 116, 111, 99, 111,
12+
// 108, 0, 0, 0, 0, 0, 24, 0, 5, 134, 212, 200, 0, 36, 164, 105, 190, 76, 80, 188, 90, 16, 44,
13+
// 247, 23, 128, 49, 0, 116, 45, 70, 68, 53, 49, 93, 199, 45, 80, 111, 98, 52, 83, 126, 105,
14+
// 112, 82, 100, 118, 77, 0, 0, 0, 169, 5, 255, 255, 239, 254, 255, 255, 239, 255, 223, 255,
15+
// 255, 253, 255, 255, 255, 255, 255, 247, 254, 255, 255, 255, 191, 255, 255, 255, 127, 254,
16+
// 255, 223, 255, 191, 255, 255, 255, 127, 255, 254, 255, 191, 255, 255, 127, 223, 247, 255,
17+
// 254, 255, 223, 255, 255, 255, 127, 223, 255, 255, 239, 253, 255, 255, 255, 255, 255, 255,
18+
// 255, 247, 239, 255, 255, 255, 255, 255, 255, 255, 251, 255, 254, 255, 255, 255, 255, 255,
19+
// 255, 255, 255, 239, 255, 255, 247, 255, 255, 255, 223, 255, 191, 255, 255, 255, 255, 250,
20+
// 255, 191, 255, 255, 255, 255, 239, 255, 255, 255, 255, 253, 253, 255, 255, 255, 243, 255,
21+
// 255, 255, 255, 255, 255, 255, 255, 255, 223, 255, 255, 251, 255, 255, 251, 247, 255, 255,
22+
// 255, 239, 247, 254, 255, 255, 239, 255, 255, 255, 255, 255, 255, 255, 255, 255, 223, 255,
23+
// 255, 255, 255, 255, 255, 255, 255, 255, 255, 223, 255, 255, 255, 240, 0, 0, 0, 5, 4, 0, 0,
24+
// 0, 19, 0, 0, 0, 5, 4, 0, 0, 0, 31, 0, 0, 0, 5, 4, 0, 0, 0, 51, 0, 0, 0, 5, 4, 0, 0, 0, 66,
25+
// 0, 0, 0, 5, 4, 0, 0, 0, 94, 0, 0, 0, 5, 4, 0, 0, 0, 140, 0, 0, 0, 5, 4, 0, 0, 0, 151, 0, 0,
26+
// 0, 5, 4, 0, 0, 0, 177, 0, 0, 0, 5, 4, 0, 0, 0, 208, 0, 0, 0, 5, 4, 0, 0, 0, 223, 0, 0, 0,
27+
// 5, 4, 0, 0, 0, 234, 0, 0, 0, 5, 4, 0, 0, 0, 249, 0, 0, 0, 5, 4, 0, 0, 1, 24, 0, 0, 0, 5, 4,
28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 5, 4, 0, 0, 5, 26, 0, 0, 0, 1, 1,
40+
// ];
41+
// println!("{}", unsafe { std::str::from_utf8_unchecked(a) });
42+
// }
43+
844
#[tokio::main]
945
async fn main() {
1046
open().await;
@@ -20,15 +56,17 @@ async fn open() {
2056

2157
let h = &Handshake::new(&t.info_hash, &peer_id);
2258

23-
let mut futs = FuturesUnordered::new();
24-
25-
for peer in response.peers.iter() {
26-
futs.push(async move {
27-
if let Err(e) = h.send(peer, 3).await {
28-
println!("{:?}: {:?}", peer, e);
59+
let mut futs: FuturesUnordered<_> = response
60+
.peers
61+
.iter()
62+
.map(|peer| {
63+
async move {
64+
if let Err(e) = timeout(h.send(peer), 10).await {
65+
println!("{:?}: {:?}", peer, e);
66+
}
2967
}
30-
});
31-
}
68+
})
69+
.collect();
3270

3371
while let Some(_) = futs.next().await {
3472
println!("done");

0 commit comments

Comments
 (0)