diff --git a/Cargo.lock b/Cargo.lock index e70a28e..15e0249 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -571,9 +571,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.104" +version = "0.2.126" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b2f96d100e1cf1929e7719b7edb3b90ab5298072638fccd77be9ce942ecdfce" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" [[package]] name = "libsqlite3-sys" diff --git a/src/file.rs b/src/file.rs index b14e979..289281c 100644 --- a/src/file.rs +++ b/src/file.rs @@ -49,7 +49,7 @@ pub fn walk_dir(path: &Path, prefix: &str) -> Result Result<()> { bao_hash, read, written, - } = encode(&file_path, &blake3_hash.to_hex().to_string()).await?; + } = encode(&file_path, &blake3_hash.to_hex()).await?; let parent_rev = upsert_path(&file_path.to_string_lossy(), blake3_bytes)?; let mime_type = infer_mime_type(&file_path)?; diff --git a/src/lib.rs b/src/lib.rs index 39e9fca..56ee322 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,11 @@ +use std::thread; + use anyhow::Result; use log::{error, info, warn}; use tokio::signal; +use crate::net::listen; + pub mod config; pub mod db; pub mod file; @@ -109,8 +113,12 @@ pub async fn list_files(_prefix: &str, _depth: usize) -> Result<()> { pub async fn start() -> Result<()> { info!("Starting Forage node..."); + + let _net_task = thread::spawn(move || listen(Some("0.0.0.0:5000".to_string()))); signal::ctrl_c().await?; + info!("Forage shutting down .."); + Ok(()) } diff --git a/src/net.rs b/src/net.rs index 1a32748..aff8066 100644 --- a/src/net.rs +++ b/src/net.rs @@ -1 +1,45 @@ // use torut; + +use std::{ + io::Read, + net::{Shutdown, TcpListener, TcpStream}, + thread, +}; + +pub fn listen(address: Option) { + let listener = match address { + Some(addr) => TcpListener::bind(addr), + None => TcpListener::bind("0.0.0.0:5000"), + } + .unwrap(); + + let _ = listener.set_nonblocking(true); + + log::info!( + "Forage listening on port {}", + listener.local_addr().unwrap().port() + ); + for stream in listener.incoming().flatten() { + log::info!( + "Connection recieved with peer {}", + stream.peer_addr().unwrap() + ); + let _ = thread::spawn(move || handle_client(stream)); + } +} + +fn handle_client(mut stream: TcpStream) { + let mut data: Vec = Vec::new(); + while match stream.read(&mut data) { + Ok(_) => true, + Err(_) => { + log::error!( + "error occured, terminating connection with {}", + stream.peer_addr().unwrap() + ); + stream.shutdown(Shutdown::Both).unwrap(); + false + } + } {} + println!("{:?}", data); +}