cosmoline/src/main.rs

105 lines
2.8 KiB
Rust
Raw Normal View History

use async_std::fs;
2021-03-31 08:34:50 +02:00
use async_std::net::{IpAddr, UdpSocket};
use async_std::path::PathBuf;
2021-03-31 08:34:50 +02:00
use clap::{load_yaml, App};
use ed25519_dalek::{Keypair, PublicKey};
2021-03-25 07:03:41 +01:00
mod keypair;
2021-03-31 08:34:50 +02:00
use keypair::{SSBKeypair, SSBPublicKey};
type Config = toml::map::Map<String, toml::Value>;
2021-03-31 08:34:50 +02:00
enum Protocol {
Net,
Ws,
Wss,
}
2021-03-31 08:34:50 +02:00
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 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());
Node {
protocol,
host,
port,
pubkey,
}
}
}
#[async_std::main]
async fn main() {
let options = load_yaml!("options.yaml");
let options = App::from(options).get_matches();
let config_file = match options.value_of("config") {
Some(path) => PathBuf::from(path),
2021-03-31 08:34:50 +02:00
None => PathBuf::from(
dirs::config_dir()
.unwrap()
.join("cosmoline")
.join("config.toml"),
),
};
let config = fs::read_to_string(config_file).await.unwrap();
2021-03-25 08:01:49 +01:00
let config: Config = toml::from_str(config.as_str()).unwrap();
let path = match options.value_of("path") {
Some(path) => PathBuf::from(path),
None => match config.get("path") {
Some(path) => PathBuf::from(path.as_str().unwrap()),
None => PathBuf::from(dirs::home_dir().unwrap().join(".cosmoline")),
2021-03-25 06:55:00 +01:00
},
};
let keypair = Keypair::read_or_generate(path.join("secret")).await;
2021-03-22 06:50:42 +01:00
println!("{}", keypair.to_json().pretty(2));
let socket = UdpSocket::bind("0.0.0.0:8008").await.unwrap();
let mut buf = [0u8; 1024];
loop {
let (amt, peer) = socket.recv_from(&mut buf).await.unwrap();
let buf = &mut buf[..amt];
let packet = String::from_utf8(buf.to_vec()).unwrap();
2021-03-31 08:34:50 +02:00
println!("{} {}", peer, Node::from_base64(&packet).to_base64());
}
2021-03-19 20:29:46 +01:00
}