src: improve Node handling

This commit is contained in:
Ujjwal Sharma 2021-03-31 12:04:50 +05:30
parent d46faad53e
commit 4b2a20c581
Signed by: ryzokuken
GPG key ID: 460B292812C67D9F

View file

@ -1,34 +1,67 @@
use clap::{load_yaml, App};
use ed25519_dalek::Keypair;
use async_std::fs; use async_std::fs;
use async_std::net::UdpSocket; use async_std::net::{IpAddr, UdpSocket};
use async_std::path::PathBuf; use async_std::path::PathBuf;
use clap::{load_yaml, App};
use ed25519_dalek::{Keypair, PublicKey};
mod keypair; mod keypair;
use keypair::{SSBKeypair, SSBPublicKey};
use keypair::SSBKeypair;
type Config = toml::map::Map<String, toml::Value>; type Config = toml::map::Map<String, toml::Value>;
#[derive(Debug)] enum Protocol {
struct Host { Net,
protocol: String, Ws,
host: String, Wss,
port: String,
pubkey: String,
} }
fn parse_packet(packet: String) -> Host { struct Node {
protocol: Protocol,
host: IpAddr,
port: u16,
pubkey: PublicKey,
}
impl Node {
fn to_base64(&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()
)
}
fn from_base64(packet: &str) -> Self {
let mut packet = packet.splitn(4, ':'); let mut packet = packet.splitn(4, ':');
let protocol = packet.next().unwrap(); let protocol = match packet.next().unwrap() {
let host = packet.next().unwrap(); "net" => Protocol::Net,
let port = packet.next().unwrap().splitn(2, '~').next().unwrap(); "ws" => Protocol::Ws,
let pubkey = packet.next().unwrap(); "wss" => Protocol::Wss,
Host { _ => panic!("unknown protocol"),
protocol: protocol.to_string(), };
host: host.to_string(), let host = IpAddr::V4(packet.next().unwrap().parse().unwrap());
port: port.to_string(), let port = packet
pubkey: pubkey.to_string(), .next()
.unwrap()
.splitn(2, '~')
.next()
.unwrap()
.parse()
.unwrap();
let pubkey = SSBPublicKey::from_base64(packet.next().unwrap());
Node {
protocol,
host,
port,
pubkey,
}
} }
} }
@ -39,10 +72,12 @@ async fn main() {
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 => PathBuf::from(dirs::config_dir() None => PathBuf::from(
dirs::config_dir()
.unwrap() .unwrap()
.join("cosmoline") .join("cosmoline")
.join("config.toml")), .join("config.toml"),
),
}; };
let config = fs::read_to_string(config_file).await.unwrap(); let config = fs::read_to_string(config_file).await.unwrap();
let config: Config = toml::from_str(config.as_str()).unwrap(); let config: Config = toml::from_str(config.as_str()).unwrap();
@ -64,7 +99,6 @@ async fn main() {
let (amt, peer) = socket.recv_from(&mut buf).await.unwrap(); let (amt, peer) = socket.recv_from(&mut buf).await.unwrap();
let buf = &mut buf[..amt]; let buf = &mut buf[..amt];
let packet = String::from_utf8(buf.to_vec()).unwrap(); let packet = String::from_utf8(buf.to_vec()).unwrap();
println!("{:?}", (peer, parse_packet(packet))); println!("{} {}", peer, Node::from_base64(&packet).to_base64());
}; }
} }