2022-02-17 02:04:18 +00:00
|
|
|
use crate::conf::BUFFER_SIZE;
|
2022-08-30 03:33:50 +01:00
|
|
|
use crate::crypto::Crypt;
|
2022-02-12 19:59:04 +00:00
|
|
|
use crate::file::{to_size_string, FileHandle, FileInfo};
|
2022-08-30 02:21:48 +01:00
|
|
|
use crate::handshake::Handshake;
|
2022-02-13 22:45:58 +00:00
|
|
|
use crate::message::{
|
|
|
|
EncryptedMessage, FileNegotiationPayload, FileTransferPayload, Message, MessageStream,
|
|
|
|
};
|
2022-02-06 19:04:36 +00:00
|
|
|
|
2022-02-12 19:59:04 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use bytes::{Bytes, BytesMut};
|
|
|
|
use futures::future::try_join_all;
|
2022-02-06 19:04:36 +00:00
|
|
|
use futures::prelude::*;
|
2022-02-15 04:01:30 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::ffi::OsStr;
|
2022-02-07 00:54:36 +00:00
|
|
|
use std::path::PathBuf;
|
2022-02-15 04:01:30 +00:00
|
|
|
use tokio::fs::File;
|
|
|
|
use tokio::io::{self, AsyncReadExt, AsyncWriteExt};
|
2022-02-06 19:04:36 +00:00
|
|
|
use tokio::net::TcpStream;
|
2022-02-13 22:45:58 +00:00
|
|
|
use tokio::sync::mpsc;
|
2022-02-12 19:59:04 +00:00
|
|
|
use tokio_util::codec::{FramedRead, LinesCodec};
|
2022-02-06 19:04:36 +00:00
|
|
|
|
2022-02-10 19:52:28 +00:00
|
|
|
pub async fn send(file_paths: &Vec<PathBuf>, password: &String) -> Result<()> {
|
2022-02-12 19:59:04 +00:00
|
|
|
// Fail early if there are problems generating file handles
|
|
|
|
let handles = get_file_handles(file_paths).await?;
|
2022-02-12 17:18:24 +00:00
|
|
|
|
2022-02-12 19:59:04 +00:00
|
|
|
// Establish connection to server
|
2022-02-10 19:52:28 +00:00
|
|
|
let socket = TcpStream::connect("127.0.0.1:8080").await?;
|
2022-02-12 01:50:14 +00:00
|
|
|
|
2022-08-30 02:21:48 +01:00
|
|
|
let (handshake, s1) = Handshake::from_password(password);
|
2022-08-30 03:33:50 +01:00
|
|
|
// Complete handshake, returning key used for encryption
|
|
|
|
let (socket, key) = handshake.negotiate(socket, s1).await?;
|
2022-08-30 02:21:48 +01:00
|
|
|
|
2022-08-29 19:01:56 +01:00
|
|
|
let mut stream = Message::to_stream(socket);
|
2022-08-30 03:33:50 +01:00
|
|
|
let crypt = Crypt::new(&key);
|
2022-02-12 19:59:04 +00:00
|
|
|
// Complete file negotiation
|
2022-08-30 03:33:50 +01:00
|
|
|
let handles = negotiate_files_up(handles, &mut stream, &crypt).await?;
|
2022-02-12 19:59:04 +00:00
|
|
|
|
|
|
|
// Upload negotiated files
|
2022-08-30 03:33:50 +01:00
|
|
|
upload_encrypted_files(&mut stream, handles, &crypt).await?;
|
|
|
|
println!("Done uploading.");
|
2022-02-12 19:59:04 +00:00
|
|
|
|
|
|
|
// Exit
|
|
|
|
Ok(())
|
2022-02-10 19:52:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn receive(password: &String) -> Result<()> {
|
|
|
|
let socket = TcpStream::connect("127.0.0.1:8080").await?;
|
2022-08-30 02:21:48 +01:00
|
|
|
let (handshake, s1) = Handshake::from_password(password);
|
2022-08-30 03:33:50 +01:00
|
|
|
let (socket, key) = handshake.negotiate(socket, s1).await?;
|
2022-08-29 19:01:56 +01:00
|
|
|
let mut stream = Message::to_stream(socket);
|
2022-08-30 03:33:50 +01:00
|
|
|
let crypt = Crypt::new(&key);
|
|
|
|
let files = negotiate_files_down(&mut stream, &crypt).await?;
|
2022-02-12 19:59:04 +00:00
|
|
|
|
2022-08-30 03:33:50 +01:00
|
|
|
download_files(files, &mut stream, &crypt).await?;
|
2022-02-10 19:52:28 +00:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:59:04 +00:00
|
|
|
pub async fn get_file_handles(file_paths: &Vec<PathBuf>) -> Result<Vec<FileHandle>> {
|
|
|
|
let tasks = file_paths
|
|
|
|
.into_iter()
|
|
|
|
.map(|path| FileHandle::new(path.to_path_buf()));
|
|
|
|
let handles = try_join_all(tasks).await?;
|
|
|
|
Ok(handles)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn negotiate_files_up(
|
|
|
|
file_handles: Vec<FileHandle>,
|
|
|
|
stream: &mut MessageStream,
|
2022-08-30 03:33:50 +01:00
|
|
|
crypt: &Crypt,
|
2022-02-12 19:59:04 +00:00
|
|
|
) -> Result<Vec<FileHandle>> {
|
|
|
|
let files = file_handles.iter().map(|fh| fh.to_file_info()).collect();
|
|
|
|
let msg = EncryptedMessage::FileNegotiationMessage(FileNegotiationPayload { files });
|
2022-08-30 03:33:50 +01:00
|
|
|
let server_msg = msg.to_encrypted_message(crypt)?;
|
2022-08-29 21:14:30 +01:00
|
|
|
println!("server_msg encrypted: {:?}", server_msg);
|
2022-02-12 19:59:04 +00:00
|
|
|
stream.send(server_msg).await?;
|
|
|
|
let reply_payload = match stream.next().await {
|
|
|
|
Some(Ok(msg)) => match msg {
|
|
|
|
Message::EncryptedMessage(response) => response,
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
return Err(anyhow!("No response to negotiation message"));
|
|
|
|
}
|
|
|
|
};
|
2022-08-30 03:33:50 +01:00
|
|
|
let plaintext_reply = EncryptedMessage::from_encrypted_message(crypt, &reply_payload)?;
|
2022-02-12 19:59:04 +00:00
|
|
|
let requested_paths: Vec<PathBuf> = match plaintext_reply {
|
|
|
|
EncryptedMessage::FileNegotiationMessage(fnm) => {
|
|
|
|
fnm.files.into_iter().map(|f| f.path).collect()
|
|
|
|
}
|
|
|
|
_ => return Err(anyhow!("Expecting file negotiation message back")),
|
|
|
|
};
|
|
|
|
Ok(file_handles
|
|
|
|
.into_iter()
|
|
|
|
.filter(|fh| requested_paths.contains(&fh.path))
|
|
|
|
.collect())
|
|
|
|
}
|
|
|
|
|
2022-02-15 04:01:30 +00:00
|
|
|
pub async fn negotiate_files_down(
|
|
|
|
stream: &mut MessageStream,
|
2022-08-30 03:33:50 +01:00
|
|
|
crypt: &Crypt,
|
2022-02-17 02:04:18 +00:00
|
|
|
) -> Result<Vec<FileInfo>> {
|
2022-02-12 19:59:04 +00:00
|
|
|
let file_offer = match stream.next().await {
|
|
|
|
Some(Ok(msg)) => match msg {
|
|
|
|
Message::EncryptedMessage(response) => response,
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
return Err(anyhow!("No response to negotiation message"));
|
|
|
|
}
|
|
|
|
};
|
2022-08-30 03:33:50 +01:00
|
|
|
let plaintext_offer = EncryptedMessage::from_encrypted_message(crypt, &file_offer)?;
|
2022-02-12 19:59:04 +00:00
|
|
|
let requested_infos: Vec<FileInfo> = match plaintext_offer {
|
|
|
|
EncryptedMessage::FileNegotiationMessage(fnm) => fnm.files,
|
|
|
|
_ => return Err(anyhow!("Expecting file negotiation message back")),
|
|
|
|
};
|
|
|
|
let mut stdin = FramedRead::new(io::stdin(), LinesCodec::new());
|
|
|
|
let mut files = vec![];
|
2022-02-15 04:01:30 +00:00
|
|
|
for file_info in requested_infos.into_iter() {
|
|
|
|
let mut reply = prompt_user_input(&mut stdin, &file_info).await;
|
2022-02-12 19:59:04 +00:00
|
|
|
while reply.is_none() {
|
2022-02-15 04:01:30 +00:00
|
|
|
reply = prompt_user_input(&mut stdin, &file_info).await;
|
2022-02-12 19:59:04 +00:00
|
|
|
}
|
|
|
|
match reply {
|
2022-02-15 04:01:30 +00:00
|
|
|
Some(true) => files.push(file_info),
|
2022-02-12 19:59:04 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2022-02-15 04:01:30 +00:00
|
|
|
let msg = EncryptedMessage::FileNegotiationMessage(FileNegotiationPayload {
|
|
|
|
files: files.clone(),
|
|
|
|
});
|
2022-08-30 03:33:50 +01:00
|
|
|
let server_msg = msg.to_encrypted_message(crypt)?;
|
2022-02-12 19:59:04 +00:00
|
|
|
stream.send(server_msg).await?;
|
2022-02-15 04:01:30 +00:00
|
|
|
Ok(files)
|
2022-02-12 19:59:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 19:52:28 +00:00
|
|
|
pub async fn upload_encrypted_files(
|
|
|
|
stream: &mut MessageStream,
|
2022-02-13 22:45:58 +00:00
|
|
|
handles: Vec<FileHandle>,
|
2022-08-30 03:33:50 +01:00
|
|
|
cipher: &Crypt,
|
2022-02-10 19:52:28 +00:00
|
|
|
) -> Result<()> {
|
2022-02-13 22:45:58 +00:00
|
|
|
let (tx, mut rx) = mpsc::unbounded_channel::<EncryptedMessage>();
|
|
|
|
for mut handle in handles {
|
|
|
|
let txc = tx.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
let _ = enqueue_file_chunks(&mut handle, txc).await;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
Some(msg) = rx.recv() => {
|
2022-02-17 02:04:18 +00:00
|
|
|
// println!("message received to client.rx {:?}", msg);
|
2022-02-13 22:45:58 +00:00
|
|
|
let x = msg.to_encrypted_message(cipher)?;
|
|
|
|
stream.send(x).await?
|
|
|
|
}
|
2022-02-16 01:55:33 +00:00
|
|
|
else => {
|
|
|
|
println!("breaking");
|
|
|
|
break
|
|
|
|
},
|
2022-02-13 22:45:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-02-17 02:04:18 +00:00
|
|
|
|
2022-02-13 22:45:58 +00:00
|
|
|
pub async fn enqueue_file_chunks(
|
|
|
|
fh: &mut FileHandle,
|
|
|
|
tx: mpsc::UnboundedSender<EncryptedMessage>,
|
|
|
|
) -> Result<()> {
|
2022-02-15 04:01:30 +00:00
|
|
|
let mut chunk_num = 0;
|
2022-02-16 01:55:33 +00:00
|
|
|
let mut bytes_read = 1;
|
|
|
|
while bytes_read != 0 {
|
|
|
|
let mut buf = BytesMut::with_capacity(BUFFER_SIZE);
|
|
|
|
bytes_read = fh.file.read_buf(&mut buf).await?;
|
2022-02-17 02:04:18 +00:00
|
|
|
// println!("Bytes_read: {:?}, The bytes: {:?}", bytes_read, &buf[..]);
|
2022-02-16 01:55:33 +00:00
|
|
|
if bytes_read != 0 {
|
|
|
|
let chunk = buf.freeze();
|
|
|
|
let file_info = fh.to_file_info();
|
|
|
|
let ftp = EncryptedMessage::FileTransferMessage(FileTransferPayload {
|
|
|
|
chunk,
|
|
|
|
chunk_num,
|
|
|
|
file_info,
|
|
|
|
});
|
|
|
|
tx.send(ftp)?;
|
|
|
|
chunk_num += 1;
|
|
|
|
}
|
2022-02-15 04:01:30 +00:00
|
|
|
}
|
2022-02-13 22:45:58 +00:00
|
|
|
|
|
|
|
Ok(())
|
2022-02-06 19:04:36 +00:00
|
|
|
}
|
2022-02-12 19:59:04 +00:00
|
|
|
|
2022-02-15 04:01:30 +00:00
|
|
|
pub async fn download_files(
|
|
|
|
file_infos: Vec<FileInfo>,
|
|
|
|
stream: &mut MessageStream,
|
2022-08-30 03:33:50 +01:00
|
|
|
cipher: &Crypt,
|
2022-02-15 04:01:30 +00:00
|
|
|
) -> Result<()> {
|
|
|
|
// for each file_info
|
2022-02-16 01:55:33 +00:00
|
|
|
let mut info_handles: HashMap<PathBuf, mpsc::UnboundedSender<(u64, Bytes)>> = HashMap::new();
|
|
|
|
for fi in file_infos {
|
|
|
|
let (tx, rx) = mpsc::unbounded_channel::<(u64, Bytes)>();
|
|
|
|
let path = fi.path.clone();
|
|
|
|
tokio::spawn(async move { download_file(fi, rx).await });
|
|
|
|
info_handles.insert(path, tx);
|
|
|
|
}
|
2022-02-15 04:01:30 +00:00
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
result = stream.next() => match result {
|
|
|
|
Some(Ok(Message::EncryptedMessage(payload))) => {
|
|
|
|
let ec = EncryptedMessage::from_encrypted_message(cipher, &payload)?;
|
2022-02-17 02:04:18 +00:00
|
|
|
// println!("encrypted message received! {:?}", ec);
|
2022-02-15 04:01:30 +00:00
|
|
|
match ec {
|
|
|
|
EncryptedMessage::FileTransferMessage(payload) => {
|
2022-08-30 03:33:50 +01:00
|
|
|
// println!("matched file transfer message");
|
2022-02-15 04:01:30 +00:00
|
|
|
if let Some(tx) = info_handles.get(&payload.file_info.path) {
|
2022-08-30 03:33:50 +01:00
|
|
|
// println!("matched on filetype, sending to tx");
|
2022-02-15 04:01:30 +00:00
|
|
|
tx.send((payload.chunk_num, payload.chunk))?
|
|
|
|
};
|
|
|
|
},
|
|
|
|
_ => {println!("wrong msg")}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(Err(e)) => {
|
|
|
|
println!("Error {:?}", e);
|
|
|
|
}
|
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn download_file(
|
|
|
|
file_info: FileInfo,
|
|
|
|
rx: mpsc::UnboundedReceiver<(u64, Bytes)>,
|
|
|
|
) -> Result<()> {
|
2022-02-16 01:55:33 +00:00
|
|
|
println!("in download file");
|
2022-02-15 04:01:30 +00:00
|
|
|
let mut rx = rx;
|
|
|
|
let filename = match file_info.path.file_name() {
|
2022-02-16 01:55:33 +00:00
|
|
|
Some(f) => {
|
|
|
|
println!("matched filename");
|
|
|
|
f
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
println!("didnt match filename");
|
|
|
|
OsStr::new("random.txt")
|
|
|
|
}
|
2022-02-15 04:01:30 +00:00
|
|
|
};
|
2022-02-16 01:55:33 +00:00
|
|
|
println!("trying to create file...filename={:?}", filename);
|
|
|
|
let mut file = File::create(filename).await?;
|
|
|
|
println!("file created ok! filename={:?}", filename);
|
2022-02-17 02:04:18 +00:00
|
|
|
while let Some((_chunk_num, chunk)) = rx.recv().await {
|
|
|
|
// println!("rx got message! chunk={:?}", chunk);
|
2022-02-15 04:01:30 +00:00
|
|
|
file.write_all(&chunk).await?;
|
|
|
|
}
|
2022-02-16 01:55:33 +00:00
|
|
|
println!("done receiving messages");
|
2022-02-15 04:01:30 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:59:04 +00:00
|
|
|
pub async fn prompt_user_input(
|
|
|
|
stdin: &mut FramedRead<io::Stdin, LinesCodec>,
|
|
|
|
file_info: &FileInfo,
|
|
|
|
) -> Option<bool> {
|
|
|
|
let prompt_name = file_info.path.file_name().unwrap();
|
|
|
|
println!(
|
2022-02-15 04:01:30 +00:00
|
|
|
"Accept {:?}? ({:?}). (Y/n)",
|
2022-02-12 19:59:04 +00:00
|
|
|
prompt_name,
|
|
|
|
to_size_string(file_info.size)
|
|
|
|
);
|
|
|
|
match stdin.next().await {
|
|
|
|
Some(Ok(line)) => match line.as_str() {
|
|
|
|
"" | "Y" | "y" | "yes" | "Yes" | "YES" => Some(true),
|
|
|
|
"N" | "n" | "NO" | "no" | "No" => Some(false),
|
|
|
|
_ => {
|
|
|
|
println!("Invalid input. Please enter one of the following characters: [YyNn]");
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|