From 670e8efcbab403cd00d1b58d5cb690ee33f173f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Thu, 20 Jun 2024 12:12:43 +0200 Subject: [PATCH] chore: Remove unused imports & fastwebsockets lib --- Cargo.lock | 26 ------------ iroh-blobs/src/downloader/test.rs | 2 +- iroh-blobs/src/downloader/test/dialer.rs | 5 +-- iroh-blobs/src/downloader/test/getter.rs | 1 - iroh-blobs/src/store/bao_file.rs | 1 - iroh-cli/src/commands/doctor.rs | 1 - iroh-net/Cargo.toml | 1 - iroh-net/src/relay/client.rs | 41 ++---------------- iroh-net/src/relay/client_conn.rs | 12 +++--- iroh-net/src/relay/codec.rs | 53 +----------------------- iroh-net/src/relay/http/client.rs | 5 +-- iroh-net/src/relay/server.rs | 18 +++----- 12 files changed, 19 insertions(+), 147 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0444500570c..3453c78204d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1504,25 +1504,6 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" -[[package]] -name = "fastwebsockets" -version = "0.7.2" -source = "git+https://github.com/denoland/fastwebsockets#efc0788ec512a2634253bf5d5d41005fa76413cd" -dependencies = [ - "base64 0.21.7", - "bytes", - "http-body-util", - "hyper 1.3.1", - "hyper-util", - "pin-project", - "rand", - "sha1", - "simdutf8", - "thiserror", - "tokio", - "utf-8", -] - [[package]] name = "fd-lock" version = "3.0.13" @@ -2839,7 +2820,6 @@ dependencies = [ "der", "derive_more", "duct", - "fastwebsockets", "flume", "futures-buffered", "futures-concurrency", @@ -5233,12 +5213,6 @@ dependencies = [ "rand_core", ] -[[package]] -name = "simdutf8" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a" - [[package]] name = "simple-dns" version = "0.6.2" diff --git a/iroh-blobs/src/downloader/test.rs b/iroh-blobs/src/downloader/test.rs index 9901cdf2e46..ec54e0ef8c9 100644 --- a/iroh-blobs/src/downloader/test.rs +++ b/iroh-blobs/src/downloader/test.rs @@ -10,7 +10,7 @@ use iroh_net::key::SecretKey; use crate::{ get::{db::BlobId, progress::TransferState}, - util::progress::{FlumeProgressSender, IdGenerator, ProgressSender}, + util::progress::{FlumeProgressSender, IdGenerator}, }; use super::*; diff --git a/iroh-blobs/src/downloader/test/dialer.rs b/iroh-blobs/src/downloader/test/dialer.rs index 89a1af69b21..4d087145fb4 100644 --- a/iroh-blobs/src/downloader/test/dialer.rs +++ b/iroh-blobs/src/downloader/test/dialer.rs @@ -1,9 +1,6 @@ //! Implementation of [`super::Dialer`] used for testing. -use std::{ - collections::HashSet, - task::{Context, Poll}, -}; +use std::task::{Context, Poll}; use parking_lot::RwLock; diff --git a/iroh-blobs/src/downloader/test/getter.rs b/iroh-blobs/src/downloader/test/getter.rs index 378d26579e8..397f1134f1a 100644 --- a/iroh-blobs/src/downloader/test/getter.rs +++ b/iroh-blobs/src/downloader/test/getter.rs @@ -2,7 +2,6 @@ use futures_lite::{future::Boxed as BoxFuture, FutureExt}; use parking_lot::RwLock; -use std::{sync::Arc, time::Duration}; use super::*; diff --git a/iroh-blobs/src/store/bao_file.rs b/iroh-blobs/src/store/bao_file.rs index 1dc2f72a366..388b85ac609 100644 --- a/iroh-blobs/src/store/bao_file.rs +++ b/iroh-blobs/src/store/bao_file.rs @@ -732,7 +732,6 @@ pub mod test_support { BlockSize, ChunkRanges, }; use futures_lite::{Stream, StreamExt}; - use iroh_base::hash::Hash; use iroh_io::AsyncStreamReader; use rand::RngCore; use range_collections::RangeSet2; diff --git a/iroh-cli/src/commands/doctor.rs b/iroh-cli/src/commands/doctor.rs index a28f749cf66..926bfc52a6c 100644 --- a/iroh-cli/src/commands/doctor.rs +++ b/iroh-cli/src/commands/doctor.rs @@ -44,7 +44,6 @@ use iroh::{ }; use portable_atomic::AtomicU64; use postcard::experimental::max_size::MaxSize; -use ratatui::backend::Backend; use serde::{Deserialize, Serialize}; use tokio::{io::AsyncWriteExt, sync}; diff --git a/iroh-net/Cargo.toml b/iroh-net/Cargo.toml index 52220a372de..ca898f73409 100644 --- a/iroh-net/Cargo.toml +++ b/iroh-net/Cargo.toml @@ -89,7 +89,6 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"], optional = tr iroh-metrics = { version = "0.18.0", path = "../iroh-metrics", default-features = false } strum = { version = "0.26.2", features = ["derive"] } tungstenite = "0.23.0" -fastwebsockets = { git = "https://github.com/denoland/fastwebsockets", revision = "efc0788", features = ["upgrade", "unstable-split"] } tokio-tungstenite = "0.23.1" tokio-tungstenite-wasm = "0.3.1" diff --git a/iroh-net/src/relay/client.rs b/iroh-net/src/relay/client.rs index 40ae791dd33..9026284aa88 100644 --- a/iroh-net/src/relay/client.rs +++ b/iroh-net/src/relay/client.rs @@ -1,5 +1,4 @@ //! based on tailscale/derp/derp_client.go -use std::marker::PhantomData; use std::net::SocketAddr; use std::pin::Pin; use std::sync::Arc; @@ -7,16 +6,14 @@ use std::task::{Context, Poll}; use std::time::Duration; use anyhow::{anyhow, bail, ensure, Result}; -use bytes::{Bytes, BytesMut}; -use fastwebsockets::{WebSocketError, WebSocketRead, WebSocketWrite}; +use bytes::Bytes; use futures_sink::Sink; use futures_util::sink::SinkExt; use futures_util::stream::{SplitSink, SplitStream}; -use futures_util::{Stream, StreamExt, TryFutureExt}; -use tokio::io::{AsyncWrite, ReadHalf, WriteHalf}; +use futures_util::{Stream, StreamExt}; use tokio::sync::mpsc; -use tokio_tungstenite_wasm::{Message, WebSocketStream}; -use tokio_util::codec::{Decoder, Encoder, FramedRead, FramedWrite}; +use tokio_tungstenite_wasm::WebSocketStream; +use tokio_util::codec::{FramedRead, FramedWrite}; use tracing::{debug, info_span, trace, Instrument}; use super::codec::PER_CLIENT_READ_QUEUE_DEPTH; @@ -30,7 +27,6 @@ use super::{ }; use crate::key::{PublicKey, SecretKey}; -use crate::relay::codec::write_frame_ws; use crate::util::AbortingJoinHandle; const CLIENT_RECV_TIMEOUT: Duration = Duration::from_secs(120); @@ -493,35 +489,6 @@ pub enum ReceivedMessage { }, } -pub(crate) async fn send_packet_ws( - writer: &mut WebSocketWrite, - rate_limiter: &Option, - dst_key: PublicKey, - packet: Bytes, -) -> Result<()> { - ensure!( - packet.len() <= MAX_PACKET_SIZE, - "packet too big: {}", - packet.len() - ); - - let frame = Frame::SendPacket { dst_key, packet }; - if let Some(rate_limiter) = rate_limiter { - if rate_limiter.check_n(frame.len()).is_err() { - tracing::warn!("dropping send: rate limit reached"); - return Ok(()); - } - } - - let mut bytes = BytesMut::new(); - DerpCodec.encode(frame, &mut bytes)?; - - let ws_frame = fastwebsockets::Frame::binary(fastwebsockets::Payload::Bytes(bytes)); - writer.write_frame(ws_frame).await?; - - Ok(()) -} - pub(crate) async fn send_packet + Unpin>( mut writer: S, rate_limiter: &Option, diff --git a/iroh-net/src/relay/client_conn.rs b/iroh-net/src/relay/client_conn.rs index 4dfb80ec0b2..05171937d17 100644 --- a/iroh-net/src/relay/client_conn.rs +++ b/iroh-net/src/relay/client_conn.rs @@ -5,10 +5,8 @@ use std::time::Duration; use anyhow::{Context, Result}; use bytes::Bytes; use futures_lite::StreamExt; -use futures_sink::Sink; -use futures_util::{SinkExt, Stream}; +use futures_util::SinkExt; use tokio::sync::mpsc; -use tokio_util::codec::Framed; use tokio_util::sync::CancellationToken; use tracing::{trace, Instrument}; @@ -17,8 +15,8 @@ use crate::{disco::looks_like_disco_wrapper, key::PublicKey}; use iroh_metrics::{inc, inc_by}; -use super::codec::{DerpCodec, Frame}; -use super::server::{MaybeTlsStream, RelayIo}; +use super::codec::Frame; +use super::server::RelayIo; use super::{ codec::{write_frame, KEEP_ALIVE}, metrics::Metrics, @@ -454,11 +452,13 @@ impl ClientConnIo { #[cfg(test)] mod tests { use crate::key::SecretKey; - use crate::relay::codec::{recv_frame, FrameType}; + use crate::relay::codec::{recv_frame, DerpCodec, FrameType}; + use crate::relay::MaybeTlsStreamServer as MaybeTlsStream; use super::*; use anyhow::bail; + use tokio_util::codec::Framed; #[tokio::test] async fn test_client_conn_io_basic() -> Result<()> { diff --git a/iroh-net/src/relay/codec.rs b/iroh-net/src/relay/codec.rs index d34a8abe7a2..5a91db6027b 100644 --- a/iroh-net/src/relay/codec.rs +++ b/iroh-net/src/relay/codec.rs @@ -2,12 +2,10 @@ use std::time::Duration; use anyhow::{bail, ensure, Context}; use bytes::{Buf, BufMut, Bytes, BytesMut}; -use fastwebsockets::WebSocketWrite; use futures_lite::{Stream, StreamExt}; use futures_sink::Sink; use futures_util::SinkExt; use iroh_base::key::{Signature, PUBLIC_KEY_LENGTH}; -use tokio::io::AsyncWrite; use tokio_util::codec::{Decoder, Encoder}; use super::types::ClientInfo; @@ -114,29 +112,7 @@ impl std::fmt::Display for FrameType { } /// Writes complete frame, errors if it is unable to write within the given `timeout`. -/// Ignores the timeout if it's `None` -/// -/// Does not flush. -pub(super) async fn write_frame_ws( - writer: &mut WebSocketWrite, - frame: Frame, - timeout: Option, -) -> anyhow::Result<()> { - let mut bytes = BytesMut::new(); - DerpCodec.encode(frame, &mut bytes)?; - let frame = fastwebsockets::Frame::binary(fastwebsockets::Payload::Bytes(bytes)); - - if let Some(duration) = timeout { - tokio::time::timeout(duration, writer.write_frame(frame)).await??; - } else { - writer.write_frame(frame).await?; - } - - Ok(()) -} - -/// Writes complete frame, errors if it is unable to write within the given `timeout`. -/// Ignores the timeout if `timeout.is_zero()` +/// Ignores the timeout if `None` /// /// Does not flush. pub(super) async fn write_frame + Unpin>( @@ -176,33 +152,6 @@ pub(crate) async fn send_client_key + Unp Ok(()) } -/// Writes a `FrameType::ClientInfo`, including the client's [`PublicKey`], -/// and the client's [`ClientInfo`], sealed using the server's [`PublicKey`]. -/// -/// Flushes after writing. -pub(crate) async fn send_client_key_ws( - writer: &mut WebSocketWrite, - client_secret_key: &SecretKey, - client_info: &ClientInfo, -) -> anyhow::Result<()> { - let msg = postcard::to_stdvec(client_info)?; - let signature = client_secret_key.sign(&msg); - let frame = Frame::ClientInfo { - client_public_key: client_secret_key.public(), - message: msg.into(), - signature, - }; - let mut bytes = BytesMut::new(); - DerpCodec.encode(frame, &mut bytes)?; - - writer - .write_frame(fastwebsockets::Frame::binary( - fastwebsockets::Payload::Bytes(bytes), - )) - .await?; - Ok(()) -} - /// Reads the `FrameType::ClientInfo` frame from the client (its proof of identity) /// upon it's initial connection. pub(super) async fn recv_client_key> + Unpin>( diff --git a/iroh-net/src/relay/http/client.rs b/iroh-net/src/relay/http/client.rs index 8cbbd982acd..07913a499dd 100644 --- a/iroh-net/src/relay/http/client.rs +++ b/iroh-net/src/relay/http/client.rs @@ -7,7 +7,6 @@ use std::time::Duration; use base64::{engine::general_purpose::URL_SAFE, Engine as _}; use bytes::Bytes; -use fastwebsockets::WebSocket; use futures_lite::future::Boxed as BoxFuture; use futures_util::StreamExt; use http_body_util::Empty; @@ -23,7 +22,6 @@ use tokio::net::TcpStream; use tokio::sync::{mpsc, oneshot}; use tokio::task::JoinSet; use tokio::time::Instant; -use tokio_tungstenite_wasm::WebSocketStream; use tokio_util::codec::{FramedRead, FramedWrite}; use tracing::{debug, error, info_span, trace, warn, Instrument}; use url::Url; @@ -33,7 +31,6 @@ use crate::key::{PublicKey, SecretKey}; use crate::relay::client::{RelayConnReader, RelayConnWriter}; use crate::relay::codec::DerpCodec; use crate::relay::http::streams::{downcast_upgrade, MaybeTlsStream}; -use crate::relay::http::WEBSOCKET_UPGRADE_PROTOCOL; use crate::relay::RelayUrl; use crate::relay::{ client::Client as RelayClient, client::ClientBuilder as RelayClientBuilder, @@ -707,7 +704,7 @@ impl Actor { .header(UPGRADE, protocol.upgrade_header()) .header( "Sec-WebSocket-Key", - fastwebsockets::handshake::generate_key(), + tungstenite::handshake::client::generate_key(), ) .header("Sec-WebSocket-Version", "13"); } diff --git a/iroh-net/src/relay/server.rs b/iroh-net/src/relay/server.rs index 875a086cebc..3a2d9f0ea9f 100644 --- a/iroh-net/src/relay/server.rs +++ b/iroh-net/src/relay/server.rs @@ -6,11 +6,9 @@ use std::task::{Context, Poll}; use std::time::Duration; use anyhow::{bail, Context as _, Result}; -use bytes::BytesMut; -use futures_lite::stream::FilterMap; use futures_sink::Sink; -use futures_util::sink::{SinkExt, SinkMapErr, With}; -use futures_util::{Stream, StreamExt, TryStreamExt}; +use futures_util::sink::SinkExt; +use futures_util::{Stream, StreamExt}; use hyper::HeaderMap; use iroh_metrics::core::UsageStatsReport; use iroh_metrics::{inc, report_usage_stats}; @@ -18,12 +16,10 @@ use tokio::io::{AsyncRead, AsyncWrite}; use tokio::sync::mpsc; use tokio::task::JoinHandle; use tokio_tungstenite::WebSocketStream; -use tokio_util::codec::{Decoder, Encoder, Framed}; -use tokio_util::either::Either; +use tokio_util::codec::Framed; use tokio_util::sync::CancellationToken; use tracing::{info_span, trace, Instrument}; use tungstenite::protocol::Role; -use tungstenite::{Message, WebSocket}; use crate::key::{PublicKey, SecretKey}; @@ -504,15 +500,11 @@ mod tests { use crate::relay::{ client::{ClientBuilder, RelayConnReader, RelayConnWriter}, - codec::{recv_frame, Frame, FrameType}, - http::{ - server::Protocol, - streams::{MaybeTlsStreamReader, MaybeTlsStreamWriter}, - }, + codec::{recv_frame, FrameType}, + http::streams::{MaybeTlsStreamReader, MaybeTlsStreamWriter}, types::ClientInfo, ReceivedMessage, }; - use fastwebsockets::WebSocketRead; use tokio_util::codec::{FramedRead, FramedWrite}; use tracing_subscriber::{prelude::*, EnvFilter};