Compare commits

...

2 Commits

Author SHA1 Message Date
mirsal 0cfc123011 Quick and dirty peer discovery broadcast mock implementation
* Use `async_std::task` for network loops
 * Send the generated public key over ipv4 udp broadcast frames

(peer discovery is not functional yet, although multiple instances
connected to the same link can now talk to each other, so yay.)
2021-03-31 22:59:23 +00:00
mirsal 8be534c086 Listen on IPv6 for peer discovery broadcasts
The Future
2021-03-31 21:31:04 +00:00
1 changed files with 36 additions and 14 deletions

View File

@ -1,17 +1,48 @@
use async_std::fs;
use async_std::{fs, task};
use async_std::net::UdpSocket;
use async_std::path::PathBuf;
use async_std::sync::Arc;
use clap::{load_yaml, App};
use ed25519_dalek::Keypair;
mod keypair;
use keypair::SSBKeypair;
use keypair::{SSBKeypair, SSBPublicKey};
mod network;
use network::Peer;
type Config = toml::map::Map<String, toml::Value>;
async fn peer_discovery_recv() {
let socket = UdpSocket::bind(":::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();
println!(
"{} {}",
peer,
Peer::from_discovery_packet(&packet).to_discovery_packet()
);
}
}
async fn peer_discovery_send(pubkey: Arc<String>) {
let socket = UdpSocket::bind(":::0").await.unwrap();
let msg = format!("net:1.2.3.4:8023~shs:{}", &pubkey);
let buf = msg.as_bytes();
socket.set_broadcast(true).unwrap();
loop {
println!("Sending packet: {:?}", &msg);
socket.send_to(&buf, "255.255.255.255:8008").await.unwrap();
task::sleep(std::time::Duration::from_secs(1)).await;
}
}
#[async_std::main]
async fn main() {
let options = load_yaml!("options.yaml");
@ -39,17 +70,8 @@ async fn main() {
let keypair = Keypair::read_or_generate(path.join("secret")).await;
println!("{}", keypair.to_json().pretty(2));
let socket = UdpSocket::bind("0.0.0.0:8008").await.unwrap();
let mut buf = [0u8; 1024];
let pubkey = keypair.public.to_base64();
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();
println!(
"{} {}",
peer,
Peer::from_discovery_packet(&packet).to_discovery_packet()
);
}
task::spawn(peer_discovery_recv());
task::spawn(peer_discovery_send(Arc::new(pubkey))).await;
}