Merge pull request 'Add logging and filter out own beacons' (#16) from log into main
Reviewed-on: #16
This commit is contained in:
commit
44f0d75d41
|
@ -314,7 +314,9 @@ dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"dirs",
|
"dirs",
|
||||||
"ed25519-dalek",
|
"ed25519-dalek",
|
||||||
|
"env_logger",
|
||||||
"json",
|
"json",
|
||||||
|
"log",
|
||||||
"rand",
|
"rand",
|
||||||
"regex",
|
"regex",
|
||||||
"toml",
|
"toml",
|
||||||
|
@ -412,6 +414,19 @@ dependencies = [
|
||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "env_logger"
|
||||||
|
version = "0.8.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f"
|
||||||
|
dependencies = [
|
||||||
|
"atty",
|
||||||
|
"humantime",
|
||||||
|
"log",
|
||||||
|
"regex",
|
||||||
|
"termcolor",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "event-listener"
|
name = "event-listener"
|
||||||
version = "2.5.1"
|
version = "2.5.1"
|
||||||
|
@ -521,6 +536,12 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "humantime"
|
||||||
|
version = "2.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indexmap"
|
name = "indexmap"
|
||||||
version = "1.6.2"
|
version = "1.6.2"
|
||||||
|
|
|
@ -16,6 +16,8 @@ json = "0.12.4"
|
||||||
base64 = "0.13.0"
|
base64 = "0.13.0"
|
||||||
regex = "1.4.5"
|
regex = "1.4.5"
|
||||||
async-trait = "0.1.7"
|
async-trait = "0.1.7"
|
||||||
|
log = "0.4.14"
|
||||||
|
env_logger = "0.8.3"
|
||||||
|
|
||||||
[dependencies.async-std]
|
[dependencies.async-std]
|
||||||
version = "1.6.2"
|
version = "1.6.2"
|
||||||
|
|
|
@ -8,7 +8,7 @@ use std::collections::HashSet;
|
||||||
use crate::keypair::SSBPublicKey;
|
use crate::keypair::SSBPublicKey;
|
||||||
use crate::peer::{Address, Handshake, Peer, Protocol};
|
use crate::peer::{Address, Handshake, Peer, Protocol};
|
||||||
|
|
||||||
pub async fn recv(sender: async_std::channel::Sender<Peer>) {
|
pub async fn recv(sender: async_std::channel::Sender<Peer>, pubkey: Arc<PublicKey>) {
|
||||||
let socket = UdpSocket::bind(":::8008").await.unwrap();
|
let socket = UdpSocket::bind(":::8008").await.unwrap();
|
||||||
let mut buf = [0u8; 1024];
|
let mut buf = [0u8; 1024];
|
||||||
let mut old = HashSet::new();
|
let mut old = HashSet::new();
|
||||||
|
@ -17,8 +17,9 @@ pub async fn recv(sender: async_std::channel::Sender<Peer>) {
|
||||||
let (amt, _) = socket.recv_from(&mut buf).await.unwrap();
|
let (amt, _) = socket.recv_from(&mut buf).await.unwrap();
|
||||||
let buf = &mut buf[..amt];
|
let buf = &mut buf[..amt];
|
||||||
let packet = String::from_utf8(buf.to_vec()).unwrap();
|
let packet = String::from_utf8(buf.to_vec()).unwrap();
|
||||||
|
log::debug!("Recieved discovery beacon: {}", packet);
|
||||||
let peer = Peer::from_discovery_packet(packet.as_str());
|
let peer = Peer::from_discovery_packet(packet.as_str());
|
||||||
if !old.contains(&peer) {
|
if peer.key.to_base64() != pubkey.to_base64() && !old.contains(&peer) {
|
||||||
old.insert(peer.clone());
|
old.insert(peer.clone());
|
||||||
sender.send(peer).await.unwrap();
|
sender.send(peer).await.unwrap();
|
||||||
}
|
}
|
||||||
|
@ -42,6 +43,7 @@ pub async fn send(pubkey: Arc<String>) {
|
||||||
socket.set_broadcast(true).unwrap();
|
socket.set_broadcast(true).unwrap();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
log::debug!("Sending discovery beacon");
|
||||||
socket.send_to(&buf, "255.255.255.255:8008").await.unwrap();
|
socket.send_to(&buf, "255.255.255.255:8008").await.unwrap();
|
||||||
socket.send_to(&buf, "[FF02::1]:8008").await.unwrap();
|
socket.send_to(&buf, "[FF02::1]:8008").await.unwrap();
|
||||||
task::sleep(std::time::Duration::from_secs(1)).await;
|
task::sleep(std::time::Duration::from_secs(1)).await;
|
||||||
|
|
|
@ -12,6 +12,7 @@ use keypair::{SSBKeypair, SSBPublicKey};
|
||||||
|
|
||||||
#[async_std::main]
|
#[async_std::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
env_logger::init();
|
||||||
let options = load_yaml!("options.yaml");
|
let options = load_yaml!("options.yaml");
|
||||||
let options = App::from(options).get_matches();
|
let options = App::from(options).get_matches();
|
||||||
|
|
||||||
|
@ -37,10 +38,10 @@ async fn main() {
|
||||||
let keypair = Keypair::read_or_generate(path.join("secret")).await;
|
let keypair = Keypair::read_or_generate(path.join("secret")).await;
|
||||||
|
|
||||||
let (psend, precv) = async_std::channel::unbounded();
|
let (psend, precv) = async_std::channel::unbounded();
|
||||||
task::spawn(async move { discovery::recv(psend).await });
|
task::spawn(discovery::recv(psend, Arc::new(keypair.public)));
|
||||||
task::spawn(discovery::send(Arc::new(keypair.public.to_base64())));
|
task::spawn(discovery::send(Arc::new(keypair.public.to_base64())));
|
||||||
|
|
||||||
while let Ok(peer) = precv.recv().await {
|
while let Ok(peer) = precv.recv().await {
|
||||||
println!("{}", peer.to_discovery_packet());
|
log::info!("New peer found: {}", peer.to_discovery_packet());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ impl Address {
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Peer {
|
pub struct Peer {
|
||||||
addresses: Vec<Address>,
|
addresses: Vec<Address>,
|
||||||
key: PublicKey,
|
pub key: PublicKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Peer {
|
impl Peer {
|
||||||
|
|
Loading…
Reference in New Issue