diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..809ab65 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,32 @@ +FROM rust:1.58 as build + +# create a new empty shell project +RUN USER=root cargo new --bin ruck +WORKDIR /ruck + +# copy over your manifests +COPY ./Cargo.lock ./Cargo.lock +COPY ./Cargo.toml ./Cargo.toml + +# this build step will cache your dependencies +RUN cargo build --release + +# copy your source tree +RUN rm src/*.rs +COPY ./src ./src + +# build for release +RUN rm ./target/release/deps/ruck* +RUN cargo build --release + +# Copy the binary into a new container for a smaller docker image +FROM debian:buster-slim + +COPY --from=build ./target/release/ruck / +USER root + +ENV RUST_LOG=info +ENV RUST_BACKTRACE=full + +CMD ["/ruck", "relay"] + diff --git a/README.md b/README.md index e68c617..f4ca539 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,11 @@ - https://pdos.csail.mit.edu/papers/chord:sigcomm01/chord_sigcomm.pdf - https://pdos.csail.mit.edu/6.824/schedule.html - https://flylib.com/books/en/2.292.1.105/1/ -- https://en.wikipedia.org/wiki/Two_Generals%27_Problem \ No newline at end of file +- https://en.wikipedia.org/wiki/Two_Generals%27_Problem + +## Todo + +- [ ] Use tracing +- [ ] Add progress bars +- [ ] Exit happily when transfer is complete +- [ ] Compress files \ No newline at end of file diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..eda8514 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1 @@ +docker build -t ruck .. \ No newline at end of file diff --git a/scripts/run.sh b/scripts/run.sh new file mode 100755 index 0000000..7895090 --- /dev/null +++ b/scripts/run.sh @@ -0,0 +1 @@ +$ docker run -p 8080:3030 --rm --name ruck1 ruck \ No newline at end of file diff --git a/src/client.rs b/src/client.rs index 91ad717..60c4da0 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,3 +1,4 @@ +use crate::conf::BUFFER_SIZE; use crate::crypto::handshake; use crate::file::{to_size_string, FileHandle, FileInfo}; use crate::message::{ @@ -10,8 +11,6 @@ use blake2::{Blake2s256, Digest}; use bytes::{Bytes, BytesMut}; use futures::future::try_join_all; use futures::prelude::*; -use futures::stream::FuturesUnordered; -use futures::StreamExt; use std::collections::HashMap; use std::ffi::OsStr; use std::path::PathBuf; @@ -112,7 +111,7 @@ pub async fn negotiate_files_up( pub async fn negotiate_files_down( stream: &mut MessageStream, cipher: &Aes256Gcm, -) -> Result<(Vec)> { +) -> Result> { let file_offer = match stream.next().await { Some(Ok(msg)) => match msg { Message::EncryptedMessage(response) => response, @@ -163,7 +162,7 @@ pub async fn upload_encrypted_files( loop { tokio::select! { Some(msg) = rx.recv() => { - println!("message received to client.rx {:?}", msg); + // println!("message received to client.rx {:?}", msg); let x = msg.to_encrypted_message(cipher)?; stream.send(x).await? } @@ -175,7 +174,7 @@ pub async fn upload_encrypted_files( } Ok(()) } -const BUFFER_SIZE: usize = 1024 * 64; + pub async fn enqueue_file_chunks( fh: &mut FileHandle, tx: mpsc::UnboundedSender, @@ -185,7 +184,7 @@ pub async fn enqueue_file_chunks( while bytes_read != 0 { let mut buf = BytesMut::with_capacity(BUFFER_SIZE); bytes_read = fh.file.read_buf(&mut buf).await?; - println!("Bytes_read: {:?}, The bytes: {:?}", bytes_read, &buf[..]); + // println!("Bytes_read: {:?}, The bytes: {:?}", bytes_read, &buf[..]); if bytes_read != 0 { let chunk = buf.freeze(); let file_info = fh.to_file_info(); @@ -220,7 +219,7 @@ pub async fn download_files( result = stream.next() => match result { Some(Ok(Message::EncryptedMessage(payload))) => { let ec = EncryptedMessage::from_encrypted_message(cipher, &payload)?; - println!("encrypted message received! {:?}", ec); + // println!("encrypted message received! {:?}", ec); match ec { EncryptedMessage::FileTransferMessage(payload) => { println!("matched file transfer message"); @@ -264,8 +263,8 @@ pub async fn download_file( println!("trying to create file...filename={:?}", filename); let mut file = File::create(filename).await?; println!("file created ok! filename={:?}", filename); - while let Some((chunk_num, chunk)) = rx.recv().await { - println!("rx got message! chunk={:?}", chunk); + while let Some((_chunk_num, chunk)) = rx.recv().await { + // println!("rx got message! chunk={:?}", chunk); file.write_all(&chunk).await?; } println!("done receiving messages"); diff --git a/src/conf.rs b/src/conf.rs new file mode 100644 index 0000000..07a6f99 --- /dev/null +++ b/src/conf.rs @@ -0,0 +1 @@ +pub const BUFFER_SIZE: usize = 1024 * 1024; diff --git a/src/crypto.rs b/src/crypto.rs index 7074718..43cf8ef 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -20,7 +20,7 @@ pub async fn handshake( id, msg: Bytes::from(outbound_msg), }); - println!("client - handshake msg, {:?}", handshake_msg); + // println!("client - handshake msg, {:?}", handshake_msg); stream.send(handshake_msg).await?; let first_message = match stream.next().await { Some(Ok(msg)) => match msg { @@ -36,7 +36,7 @@ pub async fn handshake( Ok(key_bytes) => key_bytes, Err(e) => return Err(anyhow!(e.to_string())), }; - println!("Handshake successful. Key is {:?}", key); + // println!("Handshake successful. Key is {:?}", key); return Ok((stream, new_cipher(&key))); } diff --git a/src/file.rs b/src/file.rs index 362c3f2..3e03c82 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,6 +1,4 @@ -use crate::message::FileTransferPayload; - -use anyhow::{anyhow, Result}; +use anyhow::Result; use serde::{Deserialize, Serialize}; use std::fs::Metadata; use std::path::PathBuf; diff --git a/src/main.rs b/src/main.rs index d79940f..65c2518 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,11 @@ mod cli; mod client; +mod conf; mod crypto; mod file; mod message; mod server; -#[cfg(test)] -mod tests; - use clap::Parser; use cli::{Cli, Commands}; use client::{receive, send}; diff --git a/src/message.rs b/src/message.rs index 10c440f..10593a2 100644 --- a/src/message.rs +++ b/src/message.rs @@ -3,7 +3,6 @@ use crate::file::FileInfo; use aes_gcm::Aes256Gcm; // Or `Aes128Gcm` use anyhow::{anyhow, Result}; -use bincode::config; use bytes::Bytes; use serde::{Deserialize, Serialize}; use std::error::Error; diff --git a/src/server.rs b/src/server.rs index c52bc9f..0930ead 100644 --- a/src/server.rs +++ b/src/server.rs @@ -7,7 +7,7 @@ use std::collections::HashMap; use std::net::SocketAddr; use std::sync::Arc; use tokio::net::{TcpListener, TcpStream}; -use tokio::sync::{mpsc, oneshot, Mutex}; +use tokio::sync::{mpsc, Mutex}; type Tx = mpsc::UnboundedSender; type Rx = mpsc::UnboundedReceiver; @@ -121,13 +121,13 @@ pub async fn handle_connection( return Ok(()); } }; - println!("server - received msg from {:?}", addr); + // println!("server - received msg from {:?}", addr); let client = Client::new(handshake_payload.id.clone(), state.clone(), stream).await?; let mut client = Client::upgrade(client, state.clone(), handshake_payload).await?; loop { tokio::select! { Some(msg) = client.rx.recv() => { - println!("message received to client.rx {:?}", msg); + // println!("message received to client.rx {:?}", msg); client.messages.send(msg).await? } result = client.messages.next() => match result { diff --git a/src/tests.rs b/src/tests.rs deleted file mode 100644 index 50e2f2d..0000000 --- a/src/tests.rs +++ /dev/null @@ -1,11 +0,0 @@ -use crate::crypto::new_cipher; -use crate::file::FileHandle; - -use std::path::PathBuf; - -#[tokio::test] -async fn test_file_handle_nonexistent_file() { - let pb = PathBuf::new(); - let fh = FileHandle::new(pb).await; - assert!(fh.is_err()); -}