replace temp solution with Arc<T>

This commit is contained in:
17ms 2023-04-22 01:32:52 +03:00
parent 34cf303964
commit 2349a74985
6 changed files with 172 additions and 1960 deletions

2092
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package] [package]
name = "contego" name = "contego"
description = "GUI tool for secure file transfer" description = "Dynamic CLI tool for secure file transfer"
version = "0.3.0" version = "0.3.0"
edition = "2021" edition = "2021"
@ -8,14 +8,13 @@ edition = "2021"
[dependencies] [dependencies]
tokio = { version = "1.20.4", features = ["full"] } tokio = { version = "1.20.4", features = ["full"] }
local-ip-address = "0.4.4"
rand = "0.7.0" rand = "0.7.0"
eframe = "0.21.3"
egui = "0.21.0"
x25519-dalek = "1.2.0" x25519-dalek = "1.2.0"
aes-gcm = "0.10.1" aes-gcm = "0.10.1"
base64 = "0.21.0" base64 = "0.21.0"
sha256 = "1.1.2" sha256 = "1.1.2"
inquire = "0.6.1"
ureq = "2.6.2"
[dev-dependencies] [dev-dependencies]
tokio-test = "0.4.2" tokio-test = "0.4.2"

View File

@ -23,6 +23,7 @@ pub enum Message {
ConnectionReady, ConnectionReady,
Shutdown, Shutdown,
} }
pub struct Connection<'a> { pub struct Connection<'a> {
pub reader: BufReader<ReadHalf<'a>>, pub reader: BufReader<ReadHalf<'a>>,
pub writer: BufWriter<WriteHalf<'a>>, pub writer: BufWriter<WriteHalf<'a>>,

View File

@ -27,12 +27,12 @@ impl Request {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Connector { pub struct Connector {
target_addr: SocketAddr, target_addr: SocketAddr,
access_key: &'static str, access_key: String,
output_path: PathBuf, output_path: PathBuf,
} }
impl Connector { impl Connector {
pub fn new(target_addr: SocketAddr, access_key: &'static str, output_path: PathBuf) -> Self { pub fn new(target_addr: SocketAddr, access_key: String, output_path: PathBuf) -> Self {
Self { Self {
target_addr, target_addr,
access_key, access_key,

View File

@ -2,7 +2,9 @@ use crate::{
common::{Connection, Message}, common::{Connection, Message},
comms, crypto, comms, crypto,
}; };
use std::{collections::HashMap, error::Error, net::SocketAddr, path::PathBuf, str::FromStr}; use std::{
collections::HashMap, error::Error, net::SocketAddr, path::PathBuf, str::FromStr, sync::Arc,
};
use tokio::{ use tokio::{
fs::File, fs::File,
io::AsyncReadExt, io::AsyncReadExt,
@ -10,17 +12,17 @@ use tokio::{
sync::mpsc, sync::mpsc,
}; };
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone)]
pub struct Listener { pub struct Listener {
host_addr: SocketAddr, host_addr: SocketAddr,
access_key: &'static str, access_key: String,
chunksize: usize, chunksize: usize,
} }
// TODO: impl Drop (?) // TODO: impl Drop (?)
impl Listener { impl Listener {
pub fn new(host_addr: SocketAddr, access_key: &'static str, chunksize: usize) -> Self { pub fn new(host_addr: SocketAddr, access_key: String, chunksize: usize) -> Self {
Self { Self {
host_addr, host_addr,
access_key, access_key,
@ -29,7 +31,7 @@ impl Listener {
} }
pub async fn start( pub async fn start(
self, self: Arc<Self>,
tx: mpsc::Sender<Message>, tx: mpsc::Sender<Message>,
mut kill: mpsc::Receiver<Message>, mut kill: mpsc::Receiver<Message>,
files: Vec<String>, files: Vec<String>,
@ -41,7 +43,7 @@ impl Listener {
} }
async fn listen( async fn listen(
self, self: Arc<Self>,
tx: mpsc::Sender<Message>, tx: mpsc::Sender<Message>,
files: Vec<String>, files: Vec<String>,
) -> Result<(), Box<dyn Error + Send + Sync>> { ) -> Result<(), Box<dyn Error + Send + Sync>> {
@ -52,9 +54,12 @@ impl Listener {
let (mut socket, addr) = listener.accept().await?; let (mut socket, addr) = listener.accept().await?;
tx.send(Message::ClientConnect(addr)).await?; tx.send(Message::ClientConnect(addr)).await?;
let this_tx = tx.clone(); let this_tx = tx.clone();
let this_self = Arc::clone(&self);
tokio::spawn(async move { tokio::spawn(async move {
self.connection(&mut socket, addr, this_tx, &files).await?; this_self
.connection(&mut socket, addr, this_tx, &files)
.await?;
Ok::<(), Box<dyn Error + Send + Sync>>(()) Ok::<(), Box<dyn Error + Send + Sync>>(())
}); });
} }
@ -120,7 +125,7 @@ impl Listener {
let mut index = HashMap::new(); let mut index = HashMap::new();
for path in files { for path in files {
let split: Vec<&str> = path.split('/').collect(); // TODO: different path delimiters? let split: Vec<&str> = path.split('/').collect();
let name = split[split.len() - 1].to_string(); let name = split[split.len() - 1].to_string();
let handle = File::open(PathBuf::from_str(path)?).await?; let handle = File::open(PathBuf::from_str(path)?).await?;
let size = handle.metadata().await?.len(); let size = handle.metadata().await?.len();

View File

@ -6,6 +6,7 @@ use std::{
io::{BufWriter, Write}, io::{BufWriter, Write},
net::SocketAddr, net::SocketAddr,
path::PathBuf, path::PathBuf,
sync::Arc,
thread, thread,
}; };
use tokio::sync::mpsc; use tokio::sync::mpsc;
@ -39,8 +40,8 @@ fn filesync_signals() {
let (client_tx, mut local_client_rx) = mpsc::channel::<Message>(10); let (client_tx, mut local_client_rx) = mpsc::channel::<Message>(10);
let server_handle = thread::spawn(move || { let server_handle = thread::spawn(move || {
let listener = Listener::new(server_addr, "xyz", 8192usize); let listener = Listener::new(server_addr, String::from("xyz"), 8192usize);
block_on(listener.start(server_tx, server_rx, paths)).unwrap(); block_on(Arc::new(listener).start(server_tx, server_rx, paths)).unwrap();
}); });
let server_channel_handle = thread::spawn(move || { let server_channel_handle = thread::spawn(move || {
@ -52,7 +53,7 @@ fn filesync_signals() {
let client_handle = thread::spawn(move || { let client_handle = thread::spawn(move || {
let output_path = output_path.clone(); let output_path = output_path.clone();
let connector = Connector::new(server_addr, "xyz", output_path); let connector = Connector::new(server_addr, String::from("xyz"), output_path);
block_on(connector.connect(client_tx, client_rx)).unwrap() block_on(connector.connect(client_tx, client_rx)).unwrap()
}); });