Merge pull request 'Cleanup and refactor Node handling' (#4) from refactor-node into main

Reviewed-on: #4
This commit is contained in:
mirsal 2021-03-31 10:43:57 +00:00
commit 5d351ebc00
3 changed files with 99 additions and 39 deletions

View File

@ -1,10 +1,25 @@
use async_std::fs;
use async_std::path::PathBuf;
use async_trait::async_trait;
use ed25519_dalek::{Keypair, PublicKey, SecretKey};
use json::{object, JsonValue};
use rand::rngs::OsRng;
use regex::Regex;
use async_std::fs;
use async_std::path::PathBuf;
use async_trait::async_trait;
pub trait SSBPublicKey {
fn to_base64(&self) -> String;
fn from_base64(string: &str) -> Self;
}
impl SSBPublicKey for PublicKey {
fn to_base64(&self) -> String {
base64::encode(self.to_bytes())
}
fn from_base64(string: &str) -> Self {
Self::from_bytes(&base64::decode(string).unwrap()).unwrap()
}
}
#[async_trait]
pub trait SSBKeypair {
@ -16,7 +31,7 @@ pub trait SSBKeypair {
#[async_trait]
impl SSBKeypair for Keypair {
fn to_json(&self) -> JsonValue {
let pubstring = base64::encode(self.public.to_bytes());
let pubstring = self.public.to_base64();
let privstring = base64::encode([self.secret.to_bytes(), self.public.to_bytes()].concat());
object! {
curve: "ed25519",
@ -36,8 +51,7 @@ impl SSBKeypair for Keypair {
.unwrap()
.strip_suffix(".ed25519")
.unwrap();
let pubkey = base64::decode(pubkey).unwrap();
let pubkey = PublicKey::from_bytes(pubkey.as_slice()).unwrap();
let pubkey = SSBPublicKey::from_base64(pubkey);
let privkey = obj["private"]
.as_str()
@ -70,7 +84,8 @@ impl SSBKeypair for Keypair {
keys = keypair_json.pretty(2),
id = keypair_json["id"]
),
).await
)
.await
.unwrap();
keypair
}

View File

@ -1,37 +1,17 @@
use clap::{load_yaml, App};
use ed25519_dalek::Keypair;
use async_std::fs;
use async_std::net::UdpSocket;
use async_std::path::PathBuf;
use clap::{load_yaml, App};
use ed25519_dalek::Keypair;
mod keypair;
use keypair::SSBKeypair;
mod network;
use network::Peer;
type Config = toml::map::Map<String, toml::Value>;
#[derive(Debug)]
struct Host {
protocol: String,
host: String,
port: String,
pubkey: String,
}
fn parse_packet(packet: String) -> Host {
let mut packet = packet.splitn(4, ':');
let protocol = packet.next().unwrap();
let host = packet.next().unwrap();
let port = packet.next().unwrap().splitn(2, '~').next().unwrap();
let pubkey = packet.next().unwrap();
Host {
protocol: protocol.to_string(),
host: host.to_string(),
port: port.to_string(),
pubkey: pubkey.to_string(),
}
}
#[async_std::main]
async fn main() {
let options = load_yaml!("options.yaml");
@ -39,10 +19,12 @@ async fn main() {
let config_file = match options.value_of("config") {
Some(path) => PathBuf::from(path),
None => PathBuf::from(dirs::config_dir()
.unwrap()
.join("cosmoline")
.join("config.toml")),
None => PathBuf::from(
dirs::config_dir()
.unwrap()
.join("cosmoline")
.join("config.toml"),
),
};
let config = fs::read_to_string(config_file).await.unwrap();
let config: Config = toml::from_str(config.as_str()).unwrap();
@ -64,7 +46,10 @@ async fn main() {
let (amt, peer) = socket.recv_from(&mut buf).await.unwrap();
let buf = &mut buf[..amt];
let packet = String::from_utf8(buf.to_vec()).unwrap();
println!("{:?}", (peer, parse_packet(packet)));
};
println!(
"{} {}",
peer,
Peer::from_discovery_packet(&packet).to_discovery_packet()
);
}
}

60
src/network.rs Normal file
View File

@ -0,0 +1,60 @@
use async_std::net::IpAddr;
use ed25519_dalek::PublicKey;
use crate::keypair::SSBPublicKey;
enum Protocol {
Net,
Ws,
Wss,
}
pub struct Peer {
protocol: Protocol,
host: IpAddr,
port: u16,
pubkey: PublicKey,
}
impl Peer {
pub fn to_discovery_packet(&self) -> String {
let proto = match self.protocol {
Protocol::Net => "net",
Protocol::Ws => "ws",
Protocol::Wss => "wss",
};
format!(
"{}:{}:{}~shs:{}",
proto,
self.host,
self.port,
self.pubkey.to_base64()
)
}
pub fn from_discovery_packet(packet: &str) -> Self {
let mut packet = packet.splitn(4, ':');
let protocol = match packet.next().unwrap() {
"net" => Protocol::Net,
"ws" => Protocol::Ws,
"wss" => Protocol::Wss,
_ => panic!("unknown protocol"),
};
let host = IpAddr::V4(packet.next().unwrap().parse().unwrap());
let port = packet
.next()
.unwrap()
.splitn(2, '~')
.next()
.unwrap()
.parse()
.unwrap();
let pubkey = SSBPublicKey::from_base64(packet.next().unwrap());
Peer {
protocol,
host,
port,
pubkey,
}
}
}