forked from ryzokuken/cosmoline
Refactor peer discovery
* Move the discovery beacon codec to the discovery module * Move discovery::send and discovery::recv to static methods * Build the mock local Peer instance outside of discovery::send
This commit is contained in:
parent
44f0d75d41
commit
732cc8fd62
141
src/discovery.rs
141
src/discovery.rs
|
@ -1,4 +1,4 @@
|
|||
use async_std::net::UdpSocket;
|
||||
use async_std::net::{UdpSocket, IpAddr};
|
||||
use async_std::sync::Arc;
|
||||
use async_std::task;
|
||||
use ed25519_dalek::PublicKey;
|
||||
|
@ -6,46 +6,119 @@ use ed25519_dalek::PublicKey;
|
|||
use std::collections::HashSet;
|
||||
|
||||
use crate::keypair::SSBPublicKey;
|
||||
use crate::peer::{Address, Handshake, Peer, Protocol};
|
||||
use crate::peer::{Peer, Address, Protocol, Handshake};
|
||||
|
||||
pub async fn recv(sender: async_std::channel::Sender<Peer>, pubkey: Arc<PublicKey>) {
|
||||
let socket = UdpSocket::bind(":::8008").await.unwrap();
|
||||
let mut buf = [0u8; 1024];
|
||||
let mut old = HashSet::new();
|
||||
pub struct Server;
|
||||
|
||||
loop {
|
||||
let (amt, _) = socket.recv_from(&mut buf).await.unwrap();
|
||||
let buf = &mut buf[..amt];
|
||||
let packet = String::from_utf8(buf.to_vec()).unwrap();
|
||||
log::debug!("Recieved discovery beacon: {}", packet);
|
||||
let peer = Peer::from_discovery_packet(packet.as_str());
|
||||
if peer.key.to_base64() != pubkey.to_base64() && !old.contains(&peer) {
|
||||
old.insert(peer.clone());
|
||||
sender.send(peer).await.unwrap();
|
||||
impl Server {
|
||||
|
||||
pub async fn recv(sender: async_std::channel::Sender<Peer>, pubkey: Arc<PublicKey>) {
|
||||
let socket = UdpSocket::bind(":::8008").await.unwrap();
|
||||
let mut buf = [0u8; 1024];
|
||||
let mut old = HashSet::new();
|
||||
|
||||
loop {
|
||||
let (amt, _) = socket.recv_from(&mut buf).await.unwrap();
|
||||
let buf = &mut buf[..amt];
|
||||
let packet = String::from_utf8(buf.to_vec()).unwrap();
|
||||
log::debug!("Recieved discovery beacon: {}", packet);
|
||||
let peer = Codec::decode(packet.as_str());
|
||||
|
||||
if peer.key.to_base64() != pubkey.to_base64() && !old.contains(&peer) {
|
||||
old.insert(peer.clone());
|
||||
sender.send(peer).await.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send(peer: Arc<Peer>) {
|
||||
let socket = UdpSocket::bind(":::0").await.unwrap();
|
||||
let msg = Codec::encode(Arc::try_unwrap(peer).unwrap());
|
||||
let buf = msg.as_bytes();
|
||||
|
||||
socket.set_broadcast(true).unwrap();
|
||||
|
||||
loop {
|
||||
log::debug!("Sending discovery beacon");
|
||||
socket.send_to(&buf, "255.255.255.255:8008").await.unwrap();
|
||||
socket.send_to(&buf, "[FF02::1]:8008").await.unwrap();
|
||||
task::sleep(std::time::Duration::from_secs(1)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send(pubkey: Arc<String>) {
|
||||
let socket = UdpSocket::bind(":::0").await.unwrap();
|
||||
let msg = Peer::new(
|
||||
Vec::from([Address::new(
|
||||
Protocol::Net,
|
||||
"1.2.3.4".parse().unwrap(),
|
||||
8023,
|
||||
Handshake::Shs,
|
||||
)]),
|
||||
PublicKey::from_base64(pubkey.as_str()),
|
||||
)
|
||||
.to_discovery_packet();
|
||||
let buf = msg.as_bytes();
|
||||
pub struct Codec;
|
||||
|
||||
socket.set_broadcast(true).unwrap();
|
||||
impl Codec {
|
||||
// TODO: do this properly
|
||||
pub fn encode(peer: Peer) -> String {
|
||||
peer.addresses
|
||||
.iter()
|
||||
.map(|address| {
|
||||
let proto = match address.protocol {
|
||||
Protocol::Net => "net",
|
||||
Protocol::Ws => "ws",
|
||||
Protocol::Wss => "wss",
|
||||
};
|
||||
let hs = match address.handshake {
|
||||
Handshake::Shs => "shs",
|
||||
Handshake::Shs2 => "shs2",
|
||||
};
|
||||
format!(
|
||||
"{}:{}:{}~{}:{}",
|
||||
proto,
|
||||
address.host,
|
||||
address.port,
|
||||
hs,
|
||||
peer.key.to_base64(),
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join(";")
|
||||
}
|
||||
|
||||
loop {
|
||||
log::debug!("Sending discovery beacon");
|
||||
socket.send_to(&buf, "255.255.255.255:8008").await.unwrap();
|
||||
socket.send_to(&buf, "[FF02::1]:8008").await.unwrap();
|
||||
task::sleep(std::time::Duration::from_secs(1)).await;
|
||||
// TODO: do this properly
|
||||
pub fn decode(packet: &str) -> Peer {
|
||||
let mut key = Option::None;
|
||||
let addresses = packet
|
||||
.split(';')
|
||||
.map(|address| {
|
||||
let mut address = address.splitn(2, '~');
|
||||
|
||||
let mut network = address.next().unwrap().splitn(3, ':');
|
||||
let protocol = match network.next().unwrap() {
|
||||
"net" => Protocol::Net,
|
||||
"ws" => Protocol::Ws,
|
||||
"wss" => Protocol::Wss,
|
||||
_ => panic!("unknown protocol"),
|
||||
};
|
||||
let host = IpAddr::V4(network.next().unwrap().parse().unwrap());
|
||||
let port = network.next().unwrap().parse().unwrap();
|
||||
|
||||
let mut info = address.next().unwrap().splitn(2, ':');
|
||||
let handshake = match info.next().unwrap() {
|
||||
"shs" => Handshake::Shs,
|
||||
"shs2" => Handshake::Shs2,
|
||||
_ => panic!("unknown handshake"),
|
||||
};
|
||||
let pubkey = SSBPublicKey::from_base64(info.next().unwrap());
|
||||
if key == Option::None {
|
||||
key = Some(pubkey);
|
||||
} else if key.unwrap() != pubkey {
|
||||
panic!("unexpected pubkey");
|
||||
}
|
||||
|
||||
Address {
|
||||
protocol,
|
||||
host,
|
||||
port,
|
||||
handshake,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Peer {
|
||||
addresses,
|
||||
key: key.unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -2,12 +2,13 @@ use async_std::path::PathBuf;
|
|||
use async_std::sync::Arc;
|
||||
use async_std::{fs, task};
|
||||
use clap::{load_yaml, App};
|
||||
use ed25519_dalek::Keypair;
|
||||
use ed25519_dalek::{Keypair, PublicKey};
|
||||
|
||||
mod discovery;
|
||||
mod keypair;
|
||||
mod peer;
|
||||
|
||||
use peer::{Peer, Address, Protocol, Handshake};
|
||||
use keypair::{SSBKeypair, SSBPublicKey};
|
||||
|
||||
#[async_std::main]
|
||||
|
@ -36,12 +37,21 @@ async fn main() {
|
|||
},
|
||||
};
|
||||
let keypair = Keypair::read_or_generate(path.join("secret")).await;
|
||||
let peer = Peer::new(
|
||||
Vec::from([Address::new(
|
||||
Protocol::Net,
|
||||
"1.2.3.4".parse().unwrap(),
|
||||
8023,
|
||||
Handshake::Shs,
|
||||
)]),
|
||||
PublicKey::from_base64(keypair.public.to_base64().as_str()),
|
||||
);
|
||||
|
||||
let (psend, precv) = async_std::channel::unbounded();
|
||||
task::spawn(discovery::recv(psend, Arc::new(keypair.public)));
|
||||
task::spawn(discovery::send(Arc::new(keypair.public.to_base64())));
|
||||
task::spawn(discovery::Server::recv(psend, Arc::new(keypair.public)));
|
||||
task::spawn(discovery::Server::send(Arc::new(peer)));
|
||||
|
||||
while let Ok(peer) = precv.recv().await {
|
||||
log::info!("New peer found: {}", peer.to_discovery_packet());
|
||||
log::info!("New peer found: {}", discovery::Codec::encode(peer));
|
||||
};
|
||||
}
|
||||
|
|
88
src/peer.rs
88
src/peer.rs
|
@ -3,9 +3,8 @@ use ed25519_dalek::PublicKey;
|
|||
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
use crate::keypair::SSBPublicKey;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Debug)]
|
||||
pub enum Protocol {
|
||||
Net,
|
||||
Ws,
|
||||
|
@ -13,17 +12,19 @@ pub enum Protocol {
|
|||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Debug)]
|
||||
pub enum Handshake {
|
||||
Shs,
|
||||
Shs2,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Debug)]
|
||||
pub struct Address {
|
||||
protocol: Protocol,
|
||||
host: IpAddr,
|
||||
port: u16,
|
||||
handshake: Handshake,
|
||||
pub protocol: Protocol,
|
||||
pub host: IpAddr,
|
||||
pub port: u16,
|
||||
pub handshake: Handshake,
|
||||
}
|
||||
|
||||
impl Address {
|
||||
|
@ -38,8 +39,9 @@ impl Address {
|
|||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Debug)]
|
||||
pub struct Peer {
|
||||
addresses: Vec<Address>,
|
||||
pub addresses: Vec<Address>,
|
||||
pub key: PublicKey,
|
||||
}
|
||||
|
||||
|
@ -47,78 +49,6 @@ impl Peer {
|
|||
pub fn new(addresses: Vec<Address>, key: PublicKey) -> Self {
|
||||
Self { addresses, key }
|
||||
}
|
||||
|
||||
// TODO: do this properly
|
||||
pub fn to_discovery_packet(&self) -> String {
|
||||
self.addresses
|
||||
.iter()
|
||||
.map(|address| {
|
||||
let proto = match address.protocol {
|
||||
Protocol::Net => "net",
|
||||
Protocol::Ws => "ws",
|
||||
Protocol::Wss => "wss",
|
||||
};
|
||||
let hs = match address.handshake {
|
||||
Handshake::Shs => "shs",
|
||||
Handshake::Shs2 => "shs2",
|
||||
};
|
||||
format!(
|
||||
"{}:{}:{}~{}:{}",
|
||||
proto,
|
||||
address.host,
|
||||
address.port,
|
||||
hs,
|
||||
self.key.to_base64(),
|
||||
)
|
||||
})
|
||||
.collect::<Vec<String>>()
|
||||
.join(";")
|
||||
}
|
||||
|
||||
// TODO: do this properly
|
||||
pub fn from_discovery_packet(packet: &str) -> Self {
|
||||
let mut key = Option::None;
|
||||
let addresses = packet
|
||||
.split(';')
|
||||
.map(|address| {
|
||||
let mut address = address.splitn(2, '~');
|
||||
|
||||
let mut network = address.next().unwrap().splitn(3, ':');
|
||||
let protocol = match network.next().unwrap() {
|
||||
"net" => Protocol::Net,
|
||||
"ws" => Protocol::Ws,
|
||||
"wss" => Protocol::Wss,
|
||||
_ => panic!("unknown protocol"),
|
||||
};
|
||||
let host = IpAddr::V4(network.next().unwrap().parse().unwrap());
|
||||
let port = network.next().unwrap().parse().unwrap();
|
||||
|
||||
let mut info = address.next().unwrap().splitn(2, ':');
|
||||
let handshake = match info.next().unwrap() {
|
||||
"shs" => Handshake::Shs,
|
||||
"shs2" => Handshake::Shs2,
|
||||
_ => panic!("unknown handshake"),
|
||||
};
|
||||
let pubkey = SSBPublicKey::from_base64(info.next().unwrap());
|
||||
if key == Option::None {
|
||||
key = Some(pubkey);
|
||||
} else if key.unwrap() != pubkey {
|
||||
panic!("unexpected pubkey");
|
||||
}
|
||||
|
||||
Address {
|
||||
protocol,
|
||||
host,
|
||||
port,
|
||||
handshake,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
Peer {
|
||||
addresses,
|
||||
key: key.unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for Peer {
|
||||
|
|
Loading…
Reference in New Issue