2021-03-20 19:35:13 +01:00
|
|
|
use clap::{load_yaml, App};
|
2021-03-25 07:03:41 +01:00
|
|
|
use ed25519_dalek::Keypair;
|
2021-03-20 19:35:13 +01:00
|
|
|
|
2021-03-28 10:18:14 +02:00
|
|
|
use async_std::fs;
|
|
|
|
use async_std::net::UdpSocket;
|
|
|
|
use async_std::path::PathBuf;
|
2021-03-20 19:35:13 +01:00
|
|
|
|
2021-03-25 07:03:41 +01:00
|
|
|
mod keypair;
|
2021-03-22 06:50:42 +01:00
|
|
|
|
2021-03-25 07:03:41 +01:00
|
|
|
use keypair::SSBKeypair;
|
2021-03-20 19:35:13 +01:00
|
|
|
|
2021-03-25 06:40:21 +01:00
|
|
|
type Config = toml::map::Map<String, toml::Value>;
|
|
|
|
|
2021-03-28 07:33:53 +02:00
|
|
|
#[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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 10:18:14 +02:00
|
|
|
#[async_std::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
2021-03-20 19:35:13 +01:00
|
|
|
let options = load_yaml!("options.yaml");
|
2021-03-25 06:44:00 +01:00
|
|
|
let options = App::from(options).get_matches();
|
2021-03-20 19:35:13 +01:00
|
|
|
|
2021-03-25 06:44:00 +01:00
|
|
|
let config_file = match options.value_of("config") {
|
2021-03-20 19:35:13 +01:00
|
|
|
Some(path) => PathBuf::from(path),
|
2021-03-28 10:18:14 +02:00
|
|
|
None => PathBuf::from(dirs::config_dir()
|
2021-03-20 19:35:13 +01:00
|
|
|
.unwrap()
|
|
|
|
.join("cosmoline")
|
2021-03-28 10:18:14 +02:00
|
|
|
.join("config.toml")),
|
2021-03-20 19:35:13 +01:00
|
|
|
};
|
2021-03-28 10:18:14 +02:00
|
|
|
let config = fs::read_to_string(config_file).await?;
|
2021-03-25 08:01:49 +01:00
|
|
|
let config: Config = toml::from_str(config.as_str()).unwrap();
|
2021-03-20 19:35:13 +01:00
|
|
|
|
2021-03-25 06:44:00 +01:00
|
|
|
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()),
|
2021-03-28 10:18:14 +02:00
|
|
|
None => PathBuf::from(dirs::home_dir().unwrap().join(".cosmoline")),
|
2021-03-25 06:55:00 +01:00
|
|
|
},
|
2021-03-20 19:35:13 +01:00
|
|
|
};
|
2021-03-28 10:18:14 +02: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));
|
2021-03-28 07:33:53 +02:00
|
|
|
|
2021-03-28 10:18:14 +02:00
|
|
|
let socket = UdpSocket::bind("0.0.0.0:8008").await?;
|
|
|
|
let mut buf = [0u8; 1024];
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let (amt, peer) = socket.recv_from(&mut buf).await?;
|
|
|
|
let buf = &mut buf[..amt];
|
|
|
|
let packet = String::from_utf8(buf.to_vec()).unwrap();
|
|
|
|
println!("{:?}", (peer, parse_packet(packet)));
|
|
|
|
}
|
|
|
|
|
2021-03-19 20:29:46 +01:00
|
|
|
}
|