@@ -477,7 +477,6 @@ dependencies = [ | |||||
name = "msg" | name = "msg" | ||||
version = "0.1.0" | version = "0.1.0" | ||||
dependencies = [ | dependencies = [ | ||||
"anyhow", | |||||
"async-std", | "async-std", | ||||
"chrono", | "chrono", | ||||
"futures", | "futures", | ||||
@@ -488,6 +487,7 @@ dependencies = [ | |||||
"serde", | "serde", | ||||
"serde_json", | "serde_json", | ||||
"structopt", | "structopt", | ||||
"thiserror", | |||||
"toml", | "toml", | ||||
"uuid", | "uuid", | ||||
] | ] | ||||
@@ -18,4 +18,4 @@ toml = "0.5.6" | |||||
# ilmp = { git = "https://github.com/izzabelle/ilmp" } | # ilmp = { git = "https://github.com/izzabelle/ilmp" } | ||||
ilmp = { path = "../ilmp"} | ilmp = { path = "../ilmp"} | ||||
ring = "0.16.13" | ring = "0.16.13" | ||||
anyhow = "1.0.31" | |||||
thiserror = "1.0.18" |
@@ -2,19 +2,27 @@ | |||||
use crate::config::ClientConfig as Config; | use crate::config::ClientConfig as Config; | ||||
use crate::Result; | use crate::Result; | ||||
use async_std::net::TcpStream; | use async_std::net::TcpStream; | ||||
use futures::io::ReadHalf; | |||||
use futures_util::io::AsyncReadExt; | |||||
/// wraps the client | /// wraps the client | ||||
pub async fn client(port: u16) -> Result<()> { | pub async fn client(port: u16) -> Result<()> { | ||||
let _config = Config::load()?; | let _config = Config::load()?; | ||||
let mut stream = TcpStream::connect(format!("127.0.0.1:{}", &port)).await?; | let mut stream = TcpStream::connect(format!("127.0.0.1:{}", &port)).await?; | ||||
println!("connection established to: {}:{}", stream.peer_addr()?.ip(), port); | |||||
println!( | |||||
"connection established to: {}:{}", | |||||
stream.peer_addr()?.ip(), | |||||
port | |||||
); | |||||
/*let (_read, mut write) = stream.split();*/ | |||||
let message = ilmp::Message::new( | |||||
"Isabelle".to_owned(), | |||||
"oh god oh fuck this shit actually works".to_owned(), | |||||
); | |||||
ilmp::write(&mut stream, message); | |||||
let message = | |||||
ilmp::Message::new("Isabelle".to_string(), "new message protocol working".to_string()); | |||||
ilmp::write(&mut stream, message).await?; | |||||
let (read, mut write) = stream.split(); | |||||
loop {} | |||||
Ok(()) | |||||
} | } |
@@ -3,9 +3,38 @@ mod client; | |||||
mod config; | mod config; | ||||
mod server; | mod server; | ||||
use ring::{agreement, rand}; | |||||
use thiserror::Error; | |||||
// re-exports | // re-exports | ||||
pub use client::client; | pub use client::client; | ||||
pub use server::server; | pub use server::server; | ||||
// lazy idiot error/result type | // lazy idiot error/result type | ||||
pub type Result<T> = anyhow::Result<T>; | |||||
pub type Result<T> = std::result::Result<T, MsgError>; | |||||
#[derive(Debug, Error)] | |||||
pub enum MsgError { | |||||
#[error("message protocol error")] | |||||
Ilmp(#[from] ilmp::IlmpError), | |||||
#[error("std::io error")] | |||||
StdIo(#[from] std::io::Error), | |||||
#[error("toml error")] | |||||
Toml(#[from] toml::de::Error), | |||||
} | |||||
pub struct AsymmetricKeys { | |||||
pub private: agreement::EphemeralPrivateKey, | |||||
pub public: agreement::PublicKey, | |||||
} | |||||
impl AsymmetricKeys { | |||||
pub fn generate() -> AsymmetricKeys { | |||||
let rng = rand::SystemRandom::new(); | |||||
let private = agreement::EphemeralPrivateKey::generate(&agreement::X25519, &rng) | |||||
.expect("failed to create private key"); | |||||
let public = private | |||||
.compute_public_key() | |||||
.expect("failed to create public key"); | |||||
AsymmetricKeys { private, public } | |||||
} | |||||
} |
@@ -3,7 +3,10 @@ use structopt::StructOpt; | |||||
// cli options | // cli options | ||||
#[derive(StructOpt)] | #[derive(StructOpt)] | ||||
#[structopt(name = "msg", about = "I have no idea what i'm doing but this is async")] | |||||
#[structopt( | |||||
name = "msg", | |||||
about = "I have no idea what i'm doing but this is async" | |||||
)] | |||||
struct Opt { | struct Opt { | ||||
/// start the application as a server | /// start the application as a server | ||||
#[structopt(short = "s", long = "server")] | #[structopt(short = "s", long = "server")] | ||||
@@ -17,7 +20,11 @@ struct Opt { | |||||
#[async_std::main] | #[async_std::main] | ||||
async fn main() { | async fn main() { | ||||
let options = Opt::from_args(); | let options = Opt::from_args(); | ||||
let port = if let Some(port) = options.port { port } else { 1337 }; | |||||
let port = if let Some(port) = options.port { | |||||
port | |||||
} else { | |||||
1337 | |||||
}; | |||||
match options.server { | match options.server { | ||||
true => { | true => { | ||||
if let Err(err) = msg::server(port).await { | if let Err(err) = msg::server(port).await { | ||||
@@ -18,8 +18,15 @@ lazy_static! { | |||||
/// wraps the server | /// wraps the server | ||||
pub async fn server(port: u16) -> Result<()> { | pub async fn server(port: u16) -> Result<()> { | ||||
let asym_keys = crate::AsymmetricKeys::generate(); | |||||
let listener = TcpListener::bind(format!("127.0.0.1:{}", &port)).await?; | let listener = TcpListener::bind(format!("127.0.0.1:{}", &port)).await?; | ||||
println!("online as server at: {}:{}", listener.local_addr()?.ip(), port); | |||||
println!( | |||||
"online as server at: {}:{}", | |||||
listener.local_addr()?.ip(), | |||||
port | |||||
); | |||||
let mut incoming = listener.incoming(); | let mut incoming = listener.incoming(); | ||||
while let Some(stream) = incoming.next().await { | while let Some(stream) = incoming.next().await { | ||||
@@ -31,28 +38,37 @@ pub async fn server(port: u16) -> Result<()> { | |||||
let (read, write) = stream.split(); | let (read, write) = stream.split(); | ||||
let stream_id = Uuid::new_v4(); | let stream_id = Uuid::new_v4(); | ||||
WRITE_STREAMS.lock().expect("could not aqcuire lock").insert(stream_id.clone(), write); | |||||
WRITE_STREAMS | |||||
.lock() | |||||
.expect("could not aqcuire lock") | |||||
.insert(stream_id.clone(), write); | |||||
task::spawn(handle_stream(read, stream_id)); | task::spawn(handle_stream(read, stream_id)); | ||||
} | } | ||||
Ok(()) | Ok(()) | ||||
} | } | ||||
async fn handle_stream(mut stream: ReadHalf<TcpStream>, _stream_id: Uuid) -> Result<()> { | |||||
async fn handle_stream(mut stream: ReadHalf<TcpStream>, stream_id: Uuid) -> Result<()> { | |||||
loop { | loop { | ||||
let packet = ilmp::read(&mut stream).await?; | let packet = ilmp::read(&mut stream).await?; | ||||
if let Some(packet) = packet { | if let Some(packet) = packet { | ||||
let res = match packet.kind { | let res = match packet.kind { | ||||
ilmp::PacketKind::Message => ilmp::Message::from_packet(packet), | ilmp::PacketKind::Message => ilmp::Message::from_packet(packet), | ||||
_ => unimplemented!(), | |||||
}; | }; | ||||
println!("{:?}", res); | println!("{:?}", res); | ||||
} else { | |||||
// if no packet was received the stream is closed | |||||
break; | |||||
} | } | ||||
} | } | ||||
/* println!("disconnecting"); | |||||
WRITE_STREAMS.lock().expect("failed to aqcuire lock").remove(&stream_id); | |||||
Ok(())*/ | |||||
println!("stream disconnected"); | |||||
WRITE_STREAMS | |||||
.lock() | |||||
.expect("failed to aqcuire lock") | |||||
.remove(&stream_id); | |||||
Ok(()) | |||||
} | } | ||||
/*async fn relay_packet<T: Clone + Sendable>(packet: T) -> Result<()> { | /*async fn relay_packet<T: Clone + Sendable>(packet: T) -> Result<()> { | ||||