Switch to non-blocking asynchronous I/O with async_std

* main: add a basic main loop and make the network part async
 * keypair: do not block when writing the generated keyfile
 * keypair: Read the secret file asynchronously
This commit is contained in:
mirsal 2021-03-28 08:18:14 +00:00
parent 458ff680ec
commit 3195de3d5f
2 changed files with 32 additions and 21 deletions

View File

@ -1,16 +1,21 @@
use ed25519_dalek::{Keypair, PublicKey, SecretKey}; use ed25519_dalek::{Keypair, PublicKey, SecretKey};
use json::{object, JsonValue}; use json::{object, JsonValue};
use rand::rngs::OsRng; use rand::rngs::OsRng;
use regex::Regex; use regex::Regex;
use std::path::PathBuf; use async_std::fs;
use async_std::path::PathBuf;
use async_trait::async_trait;
#[async_trait]
pub trait SSBKeypair { pub trait SSBKeypair {
fn to_json(&self) -> JsonValue; fn to_json(&self) -> JsonValue;
fn from_json(obj: JsonValue) -> Self; fn from_json(obj: JsonValue) -> Self;
fn read_or_generate(path: PathBuf) -> Self; async fn read_or_generate(path: PathBuf) -> Self;
} }
#[async_trait]
impl SSBKeypair for Keypair { impl SSBKeypair for Keypair {
fn to_json(&self) -> JsonValue { fn to_json(&self) -> JsonValue {
let pubstring = base64::encode(self.public.to_bytes()); let pubstring = base64::encode(self.public.to_bytes());
@ -50,9 +55,9 @@ impl SSBKeypair for Keypair {
} }
} }
fn read_or_generate(path: PathBuf) -> Self { async fn read_or_generate(path: PathBuf) -> Self {
if path.exists() { if path.exists().await {
let secret = std::fs::read_to_string(path).unwrap(); let secret = fs::read_to_string(path).await.unwrap();
let re = Regex::new(r"\s*#[^\n]*").unwrap(); let re = Regex::new(r"\s*#[^\n]*").unwrap();
let secret = re.replace_all(secret.as_str(), ""); let secret = re.replace_all(secret.as_str(), "");
SSBKeypair::from_json(json::parse(&secret).unwrap()) SSBKeypair::from_json(json::parse(&secret).unwrap())
@ -60,14 +65,14 @@ impl SSBKeypair for Keypair {
let mut csprng = OsRng {}; let mut csprng = OsRng {};
let keypair = Keypair::generate(&mut csprng); let keypair = Keypair::generate(&mut csprng);
let keypair_json = keypair.to_json(); let keypair_json = keypair.to_json();
std::fs::write( fs::write(
path, path,
format!( format!(
include_str!("warning.txt"), include_str!("warning.txt"),
keys = keypair_json.pretty(2), keys = keypair_json.pretty(2),
id = keypair_json["id"] id = keypair_json["id"]
), ),
) ).await
.unwrap(); .unwrap();
keypair keypair
} }

View File

@ -1,8 +1,9 @@
use clap::{load_yaml, App}; use clap::{load_yaml, App};
use ed25519_dalek::Keypair; use ed25519_dalek::Keypair;
use std::net::UdpSocket; use async_std::fs;
use std::path::PathBuf; use async_std::net::UdpSocket;
use async_std::path::PathBuf;
mod keypair; mod keypair;
@ -32,34 +33,39 @@ fn parse_packet(packet: String) -> Host {
} }
} }
fn main() { #[async_std::main]
async fn main() -> std::io::Result<()> {
let options = load_yaml!("options.yaml"); let options = load_yaml!("options.yaml");
let options = App::from(options).get_matches(); let options = App::from(options).get_matches();
let config_file = match options.value_of("config") { let config_file = match options.value_of("config") {
Some(path) => PathBuf::from(path), Some(path) => PathBuf::from(path),
None => dirs::config_dir() None => PathBuf::from(dirs::config_dir()
.unwrap() .unwrap()
.join("cosmoline") .join("cosmoline")
.join("config.toml"), .join("config.toml")),
}; };
let config = std::fs::read_to_string(config_file).unwrap(); let config = fs::read_to_string(config_file).await?;
let config: Config = toml::from_str(config.as_str()).unwrap(); let config: Config = toml::from_str(config.as_str()).unwrap();
let path = match options.value_of("path") { let path = match options.value_of("path") {
Some(path) => PathBuf::from(path), Some(path) => PathBuf::from(path),
None => match config.get("path") { None => match config.get("path") {
Some(path) => PathBuf::from(path.as_str().unwrap()), Some(path) => PathBuf::from(path.as_str().unwrap()),
None => dirs::home_dir().unwrap().join(".cosmoline"), None => PathBuf::from(dirs::home_dir().unwrap().join(".cosmoline")),
}, },
}; };
let keypair = Keypair::read_or_generate(path.join("secret")); let keypair = Keypair::read_or_generate(path.join("secret")).await;
println!("{}", keypair.to_json().pretty(2)); println!("{}", keypair.to_json().pretty(2));
let socket = UdpSocket::bind("0.0.0.0:8008").unwrap(); let socket = UdpSocket::bind("0.0.0.0:8008").await?;
let mut buf = [0; 1024]; let mut buf = [0u8; 1024];
let (amt, _) = socket.recv_from(&mut buf).unwrap();
let buf = &mut buf[..amt]; loop {
let packet = String::from_utf8(buf.to_vec()).unwrap(); let (amt, peer) = socket.recv_from(&mut buf).await?;
println!("{:?}", parse_packet(packet)); let buf = &mut buf[..amt];
let packet = String::from_utf8(buf.to_vec()).unwrap();
println!("{:?}", (peer, parse_packet(packet)));
}
} }