forked from ryzokuken/cosmoline
src: break network into peer and discovery
This commit is contained in:
parent
500198e593
commit
808f979f06
|
@ -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<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();
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,10 +4,11 @@ use async_std::{fs, task};
|
||||||
use clap::{load_yaml, App};
|
use clap::{load_yaml, App};
|
||||||
use ed25519_dalek::Keypair;
|
use ed25519_dalek::Keypair;
|
||||||
|
|
||||||
|
mod discovery;
|
||||||
mod keypair;
|
mod keypair;
|
||||||
use keypair::{SSBKeypair, SSBPublicKey};
|
mod peer;
|
||||||
|
|
||||||
mod network;
|
use keypair::{SSBKeypair, SSBPublicKey};
|
||||||
|
|
||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
@ -38,6 +39,6 @@ async fn main() {
|
||||||
|
|
||||||
let pubkey = keypair.public.to_base64();
|
let pubkey = keypair.public.to_base64();
|
||||||
|
|
||||||
task::spawn(network::peer_discovery_recv());
|
task::spawn(discovery::recv());
|
||||||
task::spawn(network::peer_discovery_send(Arc::new(pubkey))).await;
|
task::spawn(discovery::send(Arc::new(pubkey))).await;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,20 @@
|
||||||
use async_std::net::{IpAddr, UdpSocket};
|
use async_std::net::IpAddr;
|
||||||
use async_std::sync::Arc;
|
|
||||||
use async_std::task;
|
|
||||||
use ed25519_dalek::PublicKey;
|
use ed25519_dalek::PublicKey;
|
||||||
|
|
||||||
use crate::keypair::SSBPublicKey;
|
use crate::keypair::SSBPublicKey;
|
||||||
|
|
||||||
enum Protocol {
|
pub enum Protocol {
|
||||||
Net,
|
Net,
|
||||||
Ws,
|
Ws,
|
||||||
Wss,
|
Wss,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Handshake {
|
pub enum Handshake {
|
||||||
Shs,
|
Shs,
|
||||||
Shs2,
|
Shs2,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Address {
|
pub struct Address {
|
||||||
protocol: Protocol,
|
protocol: Protocol,
|
||||||
host: IpAddr,
|
host: IpAddr,
|
||||||
port: u16,
|
port: u16,
|
||||||
|
@ -24,7 +22,7 @@ struct Address {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
Self {
|
||||||
protocol,
|
protocol,
|
||||||
host,
|
host,
|
||||||
|
@ -40,7 +38,7 @@ pub struct Peer {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Peer {
|
impl Peer {
|
||||||
fn new(addresses: Vec<Address>, key: PublicKey) -> Self {
|
pub fn new(addresses: Vec<Address>, key: PublicKey) -> Self {
|
||||||
Self { addresses, key }
|
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<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();
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue