ruck/src/client.rs

62 lines
1.7 KiB
Rust
Raw Normal View History

2022-02-10 19:52:28 +00:00
use crate::crypto::handshake;
2022-02-12 17:18:24 +00:00
use crate::file::FileHandle;
2022-02-10 19:52:28 +00:00
use crate::message::{Message, MessageStream};
2022-02-06 19:04:36 +00:00
2022-02-10 19:52:28 +00:00
use anyhow::Result;
2022-02-12 01:50:14 +00:00
use blake2::{Blake2s256, Digest};
2022-02-10 19:52:28 +00:00
use bytes::{BufMut, Bytes, BytesMut};
2022-02-12 17:18:24 +00:00
use futures::future::join_all;
2022-02-06 19:04:36 +00:00
use futures::prelude::*;
2022-02-07 00:54:36 +00:00
use std::path::PathBuf;
2022-02-06 19:04:36 +00:00
use tokio::net::TcpStream;
2022-02-12 01:50:14 +00:00
fn pass_to_bytes(password: &String) -> Bytes {
let mut hasher = Blake2s256::new();
hasher.update(password.as_bytes());
let res = hasher.finalize();
BytesMut::from(&res[..]).freeze()
}
2022-02-10 19:52:28 +00:00
pub async fn send(file_paths: &Vec<PathBuf>, password: &String) -> Result<()> {
2022-02-12 17:18:24 +00:00
let tasks = file_paths
.into_iter()
.map(|path| FileHandle::new(path.to_path_buf()).map(|f| f.map(|s| s.to_file_info())));
let metadatas = join_all(tasks).await;
println!("mds: {:?}", metadatas);
return Ok(());
2022-02-10 19:52:28 +00:00
let socket = TcpStream::connect("127.0.0.1:8080").await?;
let mut stream = Message::to_stream(socket);
2022-02-12 01:50:14 +00:00
2022-02-10 19:52:28 +00:00
let (stream, key) = handshake(
&mut stream,
2022-02-12 01:50:14 +00:00
true,
2022-02-10 19:52:28 +00:00
Bytes::from(password.to_string()),
2022-02-12 01:50:14 +00:00
pass_to_bytes(password),
2022-02-10 19:52:28 +00:00
)
.await?;
2022-02-12 17:18:24 +00:00
// return upload_encrypted_files(stream, file_paths, key).await;
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?;
let mut stream = Message::to_stream(socket);
let (stream, key) = handshake(
&mut stream,
2022-02-12 01:50:14 +00:00
false,
2022-02-10 19:52:28 +00:00
Bytes::from(password.to_string()),
2022-02-12 01:50:14 +00:00
pass_to_bytes(password),
2022-02-10 19:52:28 +00:00
)
.await?;
return Ok(());
}
pub async fn upload_encrypted_files(
stream: &mut MessageStream,
file_paths: &Vec<PathBuf>,
key: Bytes,
) -> Result<()> {
return Ok(());
2022-02-06 19:04:36 +00:00
}