Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
mirsal | 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::sync::Arc;
|
||||||
use async_std::task;
|
use async_std::task;
|
||||||
use ed25519_dalek::PublicKey;
|
use ed25519_dalek::PublicKey;
|
||||||
|
@ -6,46 +6,119 @@ use ed25519_dalek::PublicKey;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
use crate::keypair::SSBPublicKey;
|
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>) {
|
pub struct Server;
|
||||||
let socket = UdpSocket::bind(":::8008").await.unwrap();
|
|
||||||
let mut buf = [0u8; 1024];
|
|
||||||
let mut old = HashSet::new();
|
|
||||||
|
|
||||||
loop {
|
impl Server {
|
||||||
let (amt, _) = socket.recv_from(&mut buf).await.unwrap();
|
|
||||||
let buf = &mut buf[..amt];
|
pub async fn recv(sender: async_std::channel::Sender<Peer>, pubkey: Arc<PublicKey>) {
|
||||||
let packet = String::from_utf8(buf.to_vec()).unwrap();
|
let socket = UdpSocket::bind(":::8008").await.unwrap();
|
||||||
log::debug!("Recieved discovery beacon: {}", packet);
|
let mut buf = [0u8; 1024];
|
||||||
let peer = Peer::from_discovery_packet(packet.as_str());
|
let mut old = HashSet::new();
|
||||||
if peer.key.to_base64() != pubkey.to_base64() && !old.contains(&peer) {
|
|
||||||
old.insert(peer.clone());
|
loop {
|
||||||
sender.send(peer).await.unwrap();
|
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>) {
|
pub struct Codec;
|
||||||
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();
|
|
||||||
|
|
||||||
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 {
|
// TODO: do this properly
|
||||||
log::debug!("Sending discovery beacon");
|
pub fn decode(packet: &str) -> Peer {
|
||||||
socket.send_to(&buf, "255.255.255.255:8008").await.unwrap();
|
let mut key = Option::None;
|
||||||
socket.send_to(&buf, "[FF02::1]:8008").await.unwrap();
|
let addresses = packet
|
||||||
task::sleep(std::time::Duration::from_secs(1)).await;
|
.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::sync::Arc;
|
||||||
use async_std::{fs, task};
|
use async_std::{fs, task};
|
||||||
use clap::{load_yaml, App};
|
use clap::{load_yaml, App};
|
||||||
use ed25519_dalek::Keypair;
|
use ed25519_dalek::{Keypair, PublicKey};
|
||||||
|
|
||||||
mod discovery;
|
mod discovery;
|
||||||
mod keypair;
|
mod keypair;
|
||||||
mod peer;
|
mod peer;
|
||||||
|
|
||||||
|
use peer::{Peer, Address, Protocol, Handshake};
|
||||||
use keypair::{SSBKeypair, SSBPublicKey};
|
use keypair::{SSBKeypair, SSBPublicKey};
|
||||||
|
|
||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
|
@ -36,12 +37,21 @@ async fn main() {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
let keypair = Keypair::read_or_generate(path.join("secret")).await;
|
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();
|
let (psend, precv) = async_std::channel::unbounded();
|
||||||
task::spawn(discovery::recv(psend, Arc::new(keypair.public)));
|
task::spawn(discovery::Server::recv(psend, Arc::new(keypair.public)));
|
||||||
task::spawn(discovery::send(Arc::new(keypair.public.to_base64())));
|
task::spawn(discovery::Server::send(Arc::new(peer)));
|
||||||
|
|
||||||
while let Ok(peer) = precv.recv().await {
|
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 std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
use crate::keypair::SSBPublicKey;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum Protocol {
|
pub enum Protocol {
|
||||||
Net,
|
Net,
|
||||||
Ws,
|
Ws,
|
||||||
|
@ -13,17 +12,19 @@ pub enum Protocol {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum Handshake {
|
pub enum Handshake {
|
||||||
Shs,
|
Shs,
|
||||||
Shs2,
|
Shs2,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Address {
|
pub struct Address {
|
||||||
protocol: Protocol,
|
pub protocol: Protocol,
|
||||||
host: IpAddr,
|
pub host: IpAddr,
|
||||||
port: u16,
|
pub port: u16,
|
||||||
handshake: Handshake,
|
pub handshake: Handshake,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Address {
|
impl Address {
|
||||||
|
@ -38,8 +39,9 @@ impl Address {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Peer {
|
pub struct Peer {
|
||||||
addresses: Vec<Address>,
|
pub addresses: Vec<Address>,
|
||||||
pub key: PublicKey,
|
pub key: PublicKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,78 +49,6 @@ impl Peer {
|
||||||
pub fn new(addresses: Vec<Address>, key: PublicKey) -> Self {
|
pub fn new(addresses: Vec<Address>, key: PublicKey) -> Self {
|
||||||
Self { addresses, key }
|
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 {
|
impl Hash for Peer {
|
||||||
|
|
Loading…
Reference in New Issue