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.)
This commit is contained in:
parent
8be534c086
commit
0cfc123011
50
src/main.rs
50
src/main.rs
|
@ -1,17 +1,48 @@
|
||||||
use async_std::fs;
|
use async_std::{fs, task};
|
||||||
use async_std::net::UdpSocket;
|
use async_std::net::UdpSocket;
|
||||||
use async_std::path::PathBuf;
|
use async_std::path::PathBuf;
|
||||||
|
use async_std::sync::Arc;
|
||||||
use clap::{load_yaml, App};
|
use clap::{load_yaml, App};
|
||||||
use ed25519_dalek::Keypair;
|
use ed25519_dalek::Keypair;
|
||||||
|
|
||||||
mod keypair;
|
mod keypair;
|
||||||
use keypair::SSBKeypair;
|
use keypair::{SSBKeypair, SSBPublicKey};
|
||||||
|
|
||||||
mod network;
|
mod network;
|
||||||
use network::Peer;
|
use network::Peer;
|
||||||
|
|
||||||
type Config = toml::map::Map<String, toml::Value>;
|
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_std::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let options = load_yaml!("options.yaml");
|
let options = load_yaml!("options.yaml");
|
||||||
|
@ -39,17 +70,8 @@ async fn main() {
|
||||||
let keypair = Keypair::read_or_generate(path.join("secret")).await;
|
let keypair = Keypair::read_or_generate(path.join("secret")).await;
|
||||||
println!("{}", keypair.to_json().pretty(2));
|
println!("{}", keypair.to_json().pretty(2));
|
||||||
|
|
||||||
let socket = UdpSocket::bind(":::8008").await.unwrap();
|
let pubkey = keypair.public.to_base64();
|
||||||
let mut buf = [0u8; 1024];
|
|
||||||
|
|
||||||
loop {
|
task::spawn(peer_discovery_recv());
|
||||||
let (amt, peer) = socket.recv_from(&mut buf).await.unwrap();
|
task::spawn(peer_discovery_send(Arc::new(pubkey))).await;
|
||||||
let buf = &mut buf[..amt];
|
|
||||||
let packet = String::from_utf8(buf.to_vec()).unwrap();
|
|
||||||
println!(
|
|
||||||
"{} {}",
|
|
||||||
peer,
|
|
||||||
Peer::from_discovery_packet(&packet).to_discovery_packet()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue