diff --git a/src/discovery.rs b/src/discovery.rs new file mode 100644 index 0000000..884be2e --- /dev/null +++ b/src/discovery.rs @@ -0,0 +1,47 @@ +use async_std::net::UdpSocket; +use async_std::sync::Arc; +use async_std::task; +use ed25519_dalek::PublicKey; + +use crate::keypair::SSBPublicKey; +use crate::peer::{Address, Handshake, Peer, Protocol}; + +pub async fn 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() + ); + } +} + +pub async fn send(pubkey: Arc) { + 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(); + + loop { + println!("Sending packet: {:?}", &msg); + 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; + } +} diff --git a/src/main.rs b/src/main.rs index 2ba9dca..33d1924 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,10 +4,11 @@ use async_std::{fs, task}; use clap::{load_yaml, App}; use ed25519_dalek::Keypair; +mod discovery; mod keypair; -use keypair::{SSBKeypair, SSBPublicKey}; +mod peer; -mod network; +use keypair::{SSBKeypair, SSBPublicKey}; #[async_std::main] async fn main() { @@ -38,6 +39,6 @@ async fn main() { let pubkey = keypair.public.to_base64(); - task::spawn(network::peer_discovery_recv()); - task::spawn(network::peer_discovery_send(Arc::new(pubkey))).await; + task::spawn(discovery::recv()); + task::spawn(discovery::send(Arc::new(pubkey))).await; } diff --git a/src/network.rs b/src/peer.rs similarity index 66% rename from src/network.rs rename to src/peer.rs index dbfdc67..b45f4be 100644 --- a/src/network.rs +++ b/src/peer.rs @@ -1,22 +1,20 @@ -use async_std::net::{IpAddr, UdpSocket}; -use async_std::sync::Arc; -use async_std::task; +use async_std::net::IpAddr; use ed25519_dalek::PublicKey; use crate::keypair::SSBPublicKey; -enum Protocol { +pub enum Protocol { Net, Ws, Wss, } -enum Handshake { +pub enum Handshake { Shs, Shs2, } -struct Address { +pub struct Address { protocol: Protocol, host: IpAddr, port: u16, @@ -24,7 +22,7 @@ struct Address { } impl Address { - fn new(protocol: Protocol, host: IpAddr, port: u16, handshake: Handshake) -> Self { + pub fn new(protocol: Protocol, host: IpAddr, port: u16, handshake: Handshake) -> Self { Self { protocol, host, @@ -40,7 +38,7 @@ pub struct Peer { } impl Peer { - fn new(addresses: Vec
, key: PublicKey) -> Self { + pub fn new(addresses: Vec
, key: PublicKey) -> Self { Self { addresses, key } } @@ -116,43 +114,3 @@ impl Peer { } } } - -pub 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() - ); - } -} - -pub async fn peer_discovery_send(pubkey: Arc) { - 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(); - - loop { - println!("Sending packet: {:?}", &msg); - 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; - } -}