Skip to content

Commit

Permalink
chore: Remove unused imports & fastwebsockets lib
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Jun 20, 2024
1 parent d2aca66 commit 670e8ef
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 147 deletions.
26 changes: 0 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion iroh-blobs/src/downloader/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
5 changes: 1 addition & 4 deletions iroh-blobs/src/downloader/test/dialer.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
1 change: 0 additions & 1 deletion iroh-blobs/src/downloader/test/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use futures_lite::{future::Boxed as BoxFuture, FutureExt};
use parking_lot::RwLock;
use std::{sync::Arc, time::Duration};

use super::*;

Expand Down
1 change: 0 additions & 1 deletion iroh-blobs/src/store/bao_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion iroh-cli/src/commands/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
1 change: 0 additions & 1 deletion iroh-net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
41 changes: 4 additions & 37 deletions iroh-net/src/relay/client.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
//! based on tailscale/derp/derp_client.go
use std::marker::PhantomData;
use std::net::SocketAddr;
use std::pin::Pin;
use std::sync::Arc;
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;
Expand All @@ -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);
Expand Down Expand Up @@ -493,35 +489,6 @@ pub enum ReceivedMessage {
},
}

pub(crate) async fn send_packet_ws<S: AsyncWrite + Unpin>(
writer: &mut WebSocketWrite<S>,
rate_limiter: &Option<RateLimiter>,
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<S: Sink<Frame, Error = std::io::Error> + Unpin>(
mut writer: S,
rate_limiter: &Option<RateLimiter>,
Expand Down
12 changes: 6 additions & 6 deletions iroh-net/src/relay/client_conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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,
Expand Down Expand Up @@ -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<()> {
Expand Down
53 changes: 1 addition & 52 deletions iroh-net/src/relay/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<S: AsyncWrite + Unpin>(
writer: &mut WebSocketWrite<S>,
frame: Frame,
timeout: Option<Duration>,
) -> 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<S: Sink<Frame, Error = std::io::Error> + Unpin>(
Expand Down Expand Up @@ -176,33 +152,6 @@ pub(crate) async fn send_client_key<S: Sink<Frame, Error = std::io::Error> + 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<S: AsyncWrite + Unpin>(
writer: &mut WebSocketWrite<S>,
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<S: Stream<Item = anyhow::Result<Frame>> + Unpin>(
Expand Down
5 changes: 1 addition & 4 deletions iroh-net/src/relay/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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");
}
Expand Down
18 changes: 5 additions & 13 deletions iroh-net/src/relay/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@ 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};
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};

Expand Down Expand Up @@ -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};

Expand Down

0 comments on commit 670e8ef

Please sign in to comment.