Skip to content

Commit

Permalink
Don't allow peers to connect with the regular server
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Sep 14, 2024
1 parent f49ee48 commit 91cf3c2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This file lists the changes that have occurred since January 2024 in the project
* Hide advanced parameters in GUI
* Add a reset parameters button in GUI
* Add an option to measure latency-only for the client in the GUI
* Don't allow peers to connect with the regular server

## 0.2 - 2024-08-29

Expand Down
21 changes: 14 additions & 7 deletions src/crusader-lib/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ pub async fn locate(peer_server: bool) -> Result<Server, anyhow::Error> {
})
}

pub fn serve(state: Arc<State>, port: u16, peer_server: bool) -> Result<(), anyhow::Error> {
pub fn serve(state: Arc<State>, port: u16) -> Result<(), anyhow::Error> {
async fn handle_packet(
port: u16,
peer_server: bool,
Expand Down Expand Up @@ -253,12 +253,19 @@ pub fn serve(state: Arc<State>, port: u16, peer_server: bool) -> Result<(), anyh
let mut buf = [0; 1500];
loop {
if let Ok((len, src)) = socket.recv_from(&mut buf).await {
handle_packet(port, peer_server, &hostname, &buf[..len], &socket, src)
.await
.map_err(|error| {
(state.msg)(&format!("Unable to handle discovery packet: {:?}", error));
})
.ok();
handle_packet(
port,
state.peer_server,
&hostname,
&buf[..len],
&socket,
src,
)
.await
.map_err(|error| {
(state.msg)(&format!("Unable to handle discovery packet: {:?}", error));
})
.ok();
}
}
});
Expand Down
10 changes: 7 additions & 3 deletions src/crusader-lib/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
common::{hello, measure_latency, ping_recv, ping_send, TestState},
protocol::{codec, receive, send, ClientMessage, RawLatency, ServerMessage},
};
use anyhow::bail;
use anyhow::{bail, Context};
use std::{
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
sync::Arc,
Expand All @@ -34,7 +34,9 @@ pub struct Peer {
#[cfg(feature = "client")]
impl Peer {
pub async fn start(&mut self) -> Result<(), anyhow::Error> {
let reply: ServerMessage = receive(&mut self.rx).await?;
let reply: ServerMessage = receive(&mut self.rx)
.await
.context("Peer failed to get ready")?;
match reply {
ServerMessage::PeerReady { server_latency } => {
(self.msg)(&format!(
Expand Down Expand Up @@ -115,7 +117,9 @@ pub async fn connect_to_peer(
)
.await?;

let reply: ServerMessage = receive(&mut control_rx).await?;
let reply: ServerMessage = receive(&mut control_rx)
.await
.context("Failed to create peer")?;
match reply {
ServerMessage::NewPeer => (),
_ => bail!("Unexpected message {:?}", reply),
Expand Down
7 changes: 6 additions & 1 deletion src/crusader-lib/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ pub(crate) struct State {
clients: Mutex<Vec<Option<Arc<Client>>>>,
pong_servers: Mutex<HashMap<SocketAddr, Arc<Pong>>>,
pub(crate) msg: Box<dyn Fn(&str) + Send + Sync>,
pub(crate) peer_server: bool,
}

fn ip_to_ipv6_mapped(ip: IpAddr) -> Ipv6Addr {
Expand Down Expand Up @@ -160,6 +161,9 @@ async fn client(state: Arc<State>, stream: TcpStream) -> Result<(), anyhow::Erro
ping_interval,
estimated_duration,
} => {
if !state.peer_server {
bail!("Server not accepting peers")
}
(state.msg)(&format!(
"Serving as peer for {}, version {}",
addr, hello.version
Expand Down Expand Up @@ -631,6 +635,7 @@ async fn serve_async(
clients: Mutex::new((0..SLOTS).map(|_| None).collect()),
pong_servers: Default::default(),
msg,
peer_server,
});

let v6 = Socket::new(Domain::IPV6, socket2::Type::STREAM, Some(Protocol::TCP))?;
Expand All @@ -655,7 +660,7 @@ async fn serve_async(
task::spawn(listen(state.clone(), v6));
task::spawn(listen(state.clone(), v4));

if let Err(error) = discovery::serve(state.clone(), port, peer_server) {
if let Err(error) = discovery::serve(state.clone(), port) {
(state.msg)(&format!("Failed to run discovery: {:?}", error));
}

Expand Down

0 comments on commit 91cf3c2

Please sign in to comment.