iroh_relay/server/
client.rs

1//! The server-side representation of an ongoing client relaying connection.
2
3use std::{collections::HashSet, sync::Arc, time::Duration};
4
5use iroh_base::EndpointId;
6use n0_error::{e, stack_error};
7use n0_future::{SinkExt, StreamExt};
8use rand::RngExt;
9use time::{Date, OffsetDateTime};
10use tokio::{
11    sync::{
12        mpsc::{self, error::TrySendError},
13        watch,
14    },
15    time::MissedTickBehavior,
16};
17use tokio_util::{sync::CancellationToken, task::AbortOnDropHandle};
18use tracing::{Instrument, debug, trace, warn};
19
20use crate::{
21    PingTracker,
22    defaults::timeouts::SERVER_WRITE_TIMEOUT,
23    http::ProtocolVersion,
24    protos::{
25        relay::{
26            ClientToRelayMsg, Datagrams, PER_CLIENT_SEND_QUEUE_DEPTH, PING_INTERVAL,
27            RelayToClientMsg, Status,
28        },
29        streams::BytesStreamSink,
30    },
31    server::{
32        ConnectionId, OnDisconnectGuard,
33        clients::Clients,
34        metrics::Metrics,
35        streams::{RecvError as RelayRecvError, RelayedStream, SendError as RelaySendError},
36    },
37};
38
39/// A request to write a dataframe to a Client
40#[derive(Debug, Clone)]
41pub(super) struct Packet {
42    /// The sender of the packet
43    src: EndpointId,
44    /// The data packet bytes.
45    data: Datagrams,
46}
47
48/// Configuration for a client connection.
49///
50/// Generic over the stream type to support different WebSocket implementations.
51#[derive(Debug)]
52#[non_exhaustive]
53pub struct Config<S> {
54    /// Reports the disconnect once the connection ends.
55    ///
56    /// Also the owner of this connection's [`EndpointId`] and [`ConnectionId`].
57    pub guard: OnDisconnectGuard,
58    /// The relayed stream connection
59    pub stream: RelayedStream<S>,
60    /// Write timeout for the client connection
61    pub write_timeout: Duration,
62    /// Channel capacity for internal message queues
63    pub channel_capacity: usize,
64    /// Protocol version negotiated for this client
65    pub protocol_version: ProtocolVersion,
66    /// Optional signal to inform the client when it is being rate-limited.
67    ///
68    /// When set, the connection actor notifies the client once with a
69    /// [`Status::RateLimited`] message when the rate-limited counter first
70    /// becomes non-zero.
71    pub(crate) rate_limited: Option<watch::Receiver<u64>>,
72}
73
74impl<S> Config<S> {
75    /// Creates a new config with sensible default values for `write_timeout` and `channel_capacity`.
76    ///
77    /// The endpoint and connection ids are taken from `guard`.
78    pub fn new(
79        guard: OnDisconnectGuard,
80        stream: RelayedStream<S>,
81        protocol_version: ProtocolVersion,
82    ) -> Self {
83        Self {
84            guard,
85            stream,
86            protocol_version,
87            write_timeout: SERVER_WRITE_TIMEOUT,
88            channel_capacity: PER_CLIENT_SEND_QUEUE_DEPTH,
89            rate_limited: None,
90        }
91    }
92}
93
94/// The [`Server`] side representation of a [`Client`]'s connection.
95///
96/// [`Server`]: crate::server::Server
97/// [`Client`]: crate::client::Client
98#[derive(Debug)]
99pub(super) struct Client {
100    /// Identity of the connected peer.
101    endpoint_id: EndpointId,
102    /// Connection identifier.
103    connection_id: ConnectionId,
104    /// Used to close the connection loop.
105    done: CancellationToken,
106    /// Actor handle.
107    handle: AbortOnDropHandle<()>,
108    /// Channel to send packets intended for the client.
109    packet_queue: mpsc::Sender<Packet>,
110    /// Channel to send non-packet messages to the client.
111    message_queue: mpsc::Sender<RelayToClientMsg>,
112    /// Relay protocol version negotiated for this client.
113    protocol_version: ProtocolVersion,
114}
115
116impl Client {
117    /// Creates a client from a connection & starts a read and write loop to handle io to and from
118    /// the client
119    ///
120    /// The `guard` is moved into the connection actor and reports the disconnect to access
121    /// control once the connection ends.
122    ///
123    /// Call [`Client::shutdown`] to close the read and write loops before dropping the [`Client`]
124    pub(super) fn new<S>(config: Config<S>, clients: &Clients, metrics: Arc<Metrics>) -> Client
125    where
126        S: BytesStreamSink + Send + 'static,
127    {
128        let Config {
129            guard,
130            stream,
131            write_timeout,
132            channel_capacity,
133            protocol_version,
134            rate_limited,
135        } = config;
136        let endpoint_id = guard.endpoint_id;
137        let connection_id = guard.connection_id;
138
139        let (packet_send_queue_s, packet_send_queue_r) = mpsc::channel(channel_capacity);
140        let (message_send_queue_s, message_send_queue_r) = mpsc::channel(channel_capacity);
141        let done = CancellationToken::new();
142
143        let actor = Actor {
144            stream,
145            timeout: write_timeout,
146            packet_send_queue: packet_send_queue_r,
147            message_send_queue: message_send_queue_r,
148            guard,
149            clients: clients.clone(),
150            client_counter: ClientCounter::default(),
151            ping_tracker: PingTracker::default(),
152            protocol_version,
153            rate_limited,
154            metrics,
155        };
156
157        // start io loop
158        let io_done = done.clone();
159        let handle = tokio::task::spawn(actor.run(io_done).instrument(tracing::info_span!(
160            "client-connection-actor",
161            remote_endpoint = %endpoint_id.fmt_short(),
162            connection_id = %connection_id
163        )));
164
165        Client {
166            endpoint_id,
167            connection_id,
168            handle: AbortOnDropHandle::new(handle),
169            done,
170            packet_queue: packet_send_queue_s,
171            message_queue: message_send_queue_s,
172            protocol_version,
173        }
174    }
175
176    pub(super) fn connection_id(&self) -> ConnectionId {
177        self.connection_id
178    }
179
180    /// Shutdown the reader and writer loops and closes the connection.
181    ///
182    /// Any shutdown errors will be logged as warnings.
183    pub(super) async fn shutdown(self) {
184        self.start_shutdown();
185        if let Err(e) = self.handle.await {
186            warn!(
187                remote_endpoint = %self.endpoint_id.fmt_short(),
188                "error closing actor loop: {e:#?}",
189            );
190        };
191    }
192
193    /// Starts the process of shutdown.
194    pub(super) fn start_shutdown(&self) {
195        self.done.cancel();
196    }
197
198    pub(super) fn try_send_packet(
199        &self,
200        src: EndpointId,
201        data: Datagrams,
202    ) -> Result<(), TrySendError<Packet>> {
203        self.packet_queue.try_send(Packet { src, data })
204    }
205
206    pub(super) fn try_send_peer_gone(
207        &self,
208        key: EndpointId,
209    ) -> Result<(), TrySendError<RelayToClientMsg>> {
210        self.message_queue
211            .try_send(RelayToClientMsg::EndpointGone(key))
212    }
213
214    pub(super) fn try_send_health(
215        &self,
216        status: Status,
217    ) -> Result<(), TrySendError<RelayToClientMsg>> {
218        let message = match self.protocol_version {
219            ProtocolVersion::V2 => RelayToClientMsg::Status(status),
220            ProtocolVersion::V1 => RelayToClientMsg::Health {
221                problem: status.to_string(),
222            },
223        };
224        self.message_queue.try_send(message)
225    }
226}
227
228/// Completes when `rx` observes the connection being rate-limited.
229///
230/// Pends forever when `rx` is `None`, i.e. when no rate-limit signal is configured.
231async fn rate_limited_signal(rx: &mut Option<watch::Receiver<u64>>) -> bool {
232    match rx {
233        Some(rx) => match rx.changed().await {
234            Ok(()) => *rx.borrow_and_update() > 0,
235            // The sender lives inside this actor's stream, so it cannot drop while
236            // the actor runs. Report not limited to be safe.
237            Err(_) => false,
238        },
239        None => std::future::pending().await,
240    }
241}
242
243/// Error when handling an incoming frame from a client.
244#[stack_error(derive, add_meta, from_sources)]
245#[allow(missing_docs)]
246#[non_exhaustive]
247pub enum HandleFrameError {
248    #[error(transparent)]
249    ForwardPacket { source: ForwardPacketError },
250    #[error("Stream terminated")]
251    StreamTerminated {},
252    #[error(transparent)]
253    Recv { source: RelayRecvError },
254    #[error(transparent)]
255    Send { source: WriteFrameError },
256}
257
258/// Error when writing a frame to a client.
259#[stack_error(derive, add_meta, from_sources)]
260#[allow(missing_docs)]
261#[non_exhaustive]
262pub enum WriteFrameError {
263    #[error(transparent)]
264    Stream { source: RelaySendError },
265    #[error(transparent)]
266    Timeout {
267        #[error(std_err)]
268        source: tokio::time::error::Elapsed,
269    },
270}
271
272/// Run error
273#[stack_error(derive, add_meta)]
274#[allow(missing_docs)]
275#[non_exhaustive]
276pub enum RunError {
277    #[error(transparent)]
278    ForwardPacket {
279        #[error(from)]
280        source: ForwardPacketError,
281    },
282    #[error("Flush")]
283    Flush {},
284    #[error(transparent)]
285    HandleFrame {
286        #[error(from)]
287        source: HandleFrameError,
288    },
289    #[error("Failed to send packet")]
290    PacketSend { source: WriteFrameError },
291    #[error("Handle was dropped")]
292    HandleDropped {},
293    #[error("Writing a frame failed")]
294    WriteFrame { source: WriteFrameError },
295    #[error("Tick flush")]
296    TickFlush {},
297}
298
299/// Manages all the reads and writes to this client. It periodically sends a `KEEP_ALIVE`
300/// message to the client to keep the connection alive.
301///
302/// Call `run` to manage the input and output to and from the connection and the server.
303/// Once it hits its first write error or error receiving off a channel,
304/// it errors on return.
305/// If writes do not complete in the given `timeout`, it will also error.
306///
307/// On the "write" side, the [`Actor`] can send the client:
308///  - a KEEP_ALIVE frame
309///  - a PEER_GONE frame to inform the client that a peer they have previously sent messages to
310///    is gone from the network
311///  - packets from other peers
312///
313/// On the "read" side, it can:
314///     - receive a ping and write a pong back
315///     to speak to the endpoint ID associated with that client.
316#[derive(Debug)]
317struct Actor<S> {
318    /// IO Stream to talk to the client
319    stream: RelayedStream<S>,
320    /// Maximum time we wait to complete a write to the client
321    timeout: Duration,
322    /// Receiver for packets to be sent to the client.
323    packet_send_queue: mpsc::Receiver<Packet>,
324    /// Receiver for non-packet messages to be sent to the client.
325    message_send_queue: mpsc::Receiver<RelayToClientMsg>,
326    /// Reports the disconnect to access control when dropped.
327    ///
328    /// Also the owner of this connection's [`EndpointId`] and [`ConnectionId`].
329    guard: OnDisconnectGuard,
330    /// Reference to the other connected clients.
331    clients: Clients,
332    /// Statistics about the connected clients
333    client_counter: ClientCounter,
334    ping_tracker: PingTracker,
335    /// Relay protocol version negotiated for this client.
336    protocol_version: ProtocolVersion,
337    /// Optional signal counting how often the connection has been rate-limited.
338    rate_limited: Option<watch::Receiver<u64>>,
339    metrics: Arc<Metrics>,
340}
341
342impl<S> Actor<S>
343where
344    S: BytesStreamSink,
345{
346    async fn run(mut self, done: CancellationToken) {
347        // Note the accept and disconnects metrics must be in a pair.  Technically the
348        // connection is accepted long before this in the HTTP server, but it is clearer to
349        // handle the metric here.
350        self.metrics.accepts.inc();
351        if self.client_counter.update(self.guard.endpoint_id()) {
352            self.metrics.unique_client_keys.inc();
353        }
354        match self.run_inner(done).await {
355            Err(e) => {
356                warn!("actor errored {e:#}, exiting");
357            }
358            Ok(()) => {
359                debug!("actor finished, exiting");
360            }
361        }
362
363        self.clients.unregister(self.guard, &self.metrics);
364        self.metrics.disconnects.inc();
365    }
366
367    async fn run_inner(&mut self, done: CancellationToken) -> Result<(), RunError> {
368        // Add some jitter to ping pong interactions, to avoid all pings being sent at the same time
369        let next_interval = || {
370            let random_secs = rand::rng().random_range(1..=5);
371            Duration::from_secs(random_secs) + PING_INTERVAL
372        };
373
374        let mut ping_interval = tokio::time::interval(next_interval());
375        // ticks immediately
376        ping_interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
377        ping_interval.tick().await;
378
379        let mut rate_limit_notified = false;
380
381        loop {
382            tokio::select! {
383                biased;
384
385                _ = done.cancelled() => {
386                    trace!("actor loop cancelled, exiting");
387                    // final flush
388                    self.stream.flush().await.map_err(|_| e!(RunError::Flush))?;
389                    break;
390                }
391                limited = rate_limited_signal(&mut self.rate_limited), if !rate_limit_notified => {
392                    // Notify the client once per connection.
393                    // V1 clients are not notified.
394                    rate_limit_notified = true;
395                    if limited && self.protocol_version == ProtocolVersion::V2 {
396                        debug!("connection is rate-limited, notifying client");
397                        self.write_frame(RelayToClientMsg::Status(Status::RateLimited))
398                            .await
399                            .map_err(|err| e!(RunError::WriteFrame, err))?;
400                    }
401                }
402                maybe_frame = self.stream.next() => {
403                    self
404                        .handle_frame(maybe_frame)
405                        .await?;
406                    // reset the ping interval, we just received a message
407                    ping_interval.reset();
408                }
409                // Second priority, sending regular packets
410                packet = self.packet_send_queue.recv() => {
411                    let packet = packet.ok_or_else(|| e!(RunError::HandleDropped))?;
412                    self.send_packet(packet)
413                        .await
414                        .map_err(|err| e!(RunError::PacketSend, err))?;
415                }
416                // Last priority, sending other message
417                message = self.message_send_queue.recv() => {
418                    let message = message.ok_or_else(|| e!(RunError::HandleDropped))?;
419                    trace!("send {message:?}");
420                    self.write_frame(message)
421                        .await
422                        .map_err(|err| e!(RunError::WriteFrame, err))?;
423                }
424                _ = self.ping_tracker.timeout() => {
425                    trace!("pong timed out");
426                    break;
427                }
428                _ = ping_interval.tick() => {
429                    trace!("keep alive ping");
430                    // new interval
431                    ping_interval.reset_after(next_interval());
432                    let data = self.ping_tracker.new_ping();
433                    self.write_frame(RelayToClientMsg::Ping(data))
434                        .await
435                        .map_err(|err| e!(RunError::WriteFrame, err))?;
436                }
437            }
438
439            self.stream
440                .flush()
441                .await
442                .map_err(|_| e!(RunError::TickFlush))?;
443        }
444        Ok(())
445    }
446
447    /// Writes the given frame to the connection.
448    ///
449    /// Errors if the send does not happen within the `timeout` duration
450    async fn write_frame(&mut self, frame: RelayToClientMsg) -> Result<(), WriteFrameError> {
451        tokio::time::timeout(self.timeout, self.stream.send(frame)).await??;
452        Ok(())
453    }
454
455    /// Writes contents to the client in a `RECV_PACKET` frame.
456    ///
457    /// Errors if the send does not happen within the `timeout` duration
458    /// Does not flush.
459    async fn send_raw(&mut self, packet: Packet) -> Result<(), WriteFrameError> {
460        let remote_endpoint_id = packet.src;
461        let datagrams = packet.data;
462
463        if let Ok(len) = datagrams.contents.len().try_into() {
464            self.metrics.bytes_sent.inc_by(len);
465        }
466        self.write_frame(RelayToClientMsg::Datagrams {
467            remote_endpoint_id,
468            datagrams,
469        })
470        .await
471    }
472
473    async fn send_packet(&mut self, packet: Packet) -> Result<(), WriteFrameError> {
474        trace!("send packet");
475        match self.send_raw(packet).await {
476            Ok(()) => {
477                self.metrics.send_packets_sent.inc();
478                Ok(())
479            }
480            // If a packet exceeds the max packet size, that is an issue
481            // with the sender, not the receiver. Don't drop the connection
482            // to the receiver, just drop the packet.
483            Err(WriteFrameError::Stream {
484                source: RelaySendError::ExceedsMaxPacketSize { size, .. },
485                ..
486            }) => {
487                debug!(size, "dropping packet: exceeds max packet size");
488                self.metrics.send_packets_dropped.inc();
489                Ok(())
490            }
491            Err(err) => {
492                self.metrics.send_packets_dropped.inc();
493                Err(err)
494            }
495        }
496    }
497
498    /// Handles frame read results.
499    async fn handle_frame(
500        &mut self,
501        maybe_frame: Option<Result<ClientToRelayMsg, RelayRecvError>>,
502    ) -> Result<(), HandleFrameError> {
503        trace!(?maybe_frame, "handle incoming frame");
504        let frame = match maybe_frame {
505            Some(frame) => frame?,
506            None => return Err(e!(HandleFrameError::StreamTerminated)),
507        };
508
509        match frame {
510            ClientToRelayMsg::Datagrams {
511                dst_endpoint_id: dst_key,
512                datagrams,
513            } => {
514                let packet_len = datagrams.contents.len();
515                if let Err(err @ ForwardPacketError { .. }) =
516                    self.handle_frame_send_packet(dst_key, datagrams)
517                {
518                    warn!("failed to handle send packet frame: {err:#}");
519                }
520                self.metrics.bytes_recv.inc_by(packet_len as u64);
521            }
522            ClientToRelayMsg::Ping(data) => {
523                self.metrics.got_ping.inc();
524                // TODO: add rate limiter
525                self.write_frame(RelayToClientMsg::Pong(data)).await?;
526                self.metrics.sent_pong.inc();
527            }
528            ClientToRelayMsg::Pong(data) => {
529                self.ping_tracker.pong_received(data);
530            }
531        }
532        Ok(())
533    }
534
535    fn handle_frame_send_packet(
536        &self,
537        dst: EndpointId,
538        data: Datagrams,
539    ) -> Result<(), ForwardPacketError> {
540        self.metrics.send_packets_recv.inc();
541        self.clients
542            .send_packet(dst, data, self.guard.endpoint_id(), &self.metrics)?;
543
544        Ok(())
545    }
546}
547
548#[derive(Debug)]
549pub(crate) enum SendError {
550    Full,
551    Closed,
552}
553
554/// Error returned when forwarding a packet to a client fails.
555///
556/// This error occurs when the relay server cannot deliver a packet to its intended
557/// recipient, typically due to the client's send queue being full or the client
558/// disconnecting.
559#[stack_error(derive, add_meta)]
560#[error("failed to forward packet: {reason:?}")]
561pub struct ForwardPacketError {
562    reason: SendError,
563}
564
565/// Tracks how many unique endpoints have been seen during the last day.
566#[derive(Debug)]
567struct ClientCounter {
568    clients: HashSet<EndpointId>,
569    last_clear_date: Date,
570}
571
572impl Default for ClientCounter {
573    fn default() -> Self {
574        Self {
575            clients: HashSet::new(),
576            last_clear_date: OffsetDateTime::now_utc().date(),
577        }
578    }
579}
580
581impl ClientCounter {
582    fn check_and_clear(&mut self) {
583        let today = OffsetDateTime::now_utc().date();
584        if today != self.last_clear_date {
585            self.clients.clear();
586            self.last_clear_date = today;
587        }
588    }
589
590    /// Marks this endpoint as seen, returns whether it is new today or not.
591    fn update(&mut self, client: EndpointId) -> bool {
592        self.check_and_clear();
593        self.clients.insert(client)
594    }
595}
596
597#[cfg(test)]
598mod tests {
599    use iroh_base::SecretKey;
600    use n0_error::{Result, StdResultExt, bail_any};
601    use n0_future::Stream;
602    use n0_tracing_test::traced_test;
603    use rand::SeedableRng;
604    use tracing::info;
605
606    use super::*;
607    use crate::{
608        client::conn::Conn,
609        http::ProtocolVersion,
610        protos::{
611            common::FrameType,
612            relay::{MAX_PACKET_SIZE, Status},
613            streams::WsBytesFramed,
614        },
615        server::streams::{MaybeTlsStream, RateLimited, ServerRelayedStream},
616    };
617
618    async fn recv_frame<
619        E: std::error::Error + Sync + Send + 'static,
620        S: Stream<Item = Result<RelayToClientMsg, E>> + Unpin,
621    >(
622        frame_type: FrameType,
623        mut stream: S,
624    ) -> Result<RelayToClientMsg> {
625        match stream.next().await {
626            Some(Ok(frame)) => {
627                if frame_type != frame.typ() {
628                    bail_any!(
629                        "Unexpected frame, got {:?}, but expected {:?}",
630                        frame.typ(),
631                        frame_type
632                    );
633                }
634                Ok(frame)
635            }
636            Some(Err(err)) => Err(err).anyerr(),
637            None => bail_any!("Unexpected EOF, expected frame {frame_type:?}"),
638        }
639    }
640
641    #[tokio::test]
642    #[traced_test]
643    async fn test_client_actor_basic() -> Result {
644        let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0u64);
645
646        let (send_queue_s, send_queue_r) = mpsc::channel(10);
647        let (message_s, message_r) = mpsc::channel(10);
648
649        let endpoint_id = SecretKey::from_bytes(&rng.random()).public();
650        let (io, io_rw) = tokio::io::duplex(1024);
651        let mut io_rw = Conn::test(io_rw, Default::default());
652        let stream = RelayedStream::test(io);
653
654        let clients = Clients::default();
655        let metrics = Arc::new(Metrics::default());
656        let actor = Actor {
657            stream,
658            timeout: Duration::from_secs(1),
659            packet_send_queue: send_queue_r,
660            message_send_queue: message_r,
661            guard: OnDisconnectGuard::empty(endpoint_id),
662            clients: clients.clone(),
663            client_counter: ClientCounter::default(),
664            ping_tracker: PingTracker::default(),
665            protocol_version: ProtocolVersion::V2,
666            rate_limited: None,
667            metrics,
668        };
669
670        let done = CancellationToken::new();
671        let io_done = done.clone();
672        let handle = tokio::task::spawn(async move { actor.run(io_done).await });
673
674        // Write tests
675        println!("-- write");
676        let data = b"hello world!";
677
678        // send packet
679        println!("  send packet");
680        let packet = Packet {
681            src: endpoint_id,
682            data: Datagrams::from(&data[..]),
683        };
684        send_queue_s
685            .send(packet.clone())
686            .await
687            .std_context("send")?;
688        let frame = recv_frame(FrameType::RelayToClientDatagram, &mut io_rw)
689            .await
690            .anyerr()?;
691        assert_eq!(
692            frame,
693            RelayToClientMsg::Datagrams {
694                remote_endpoint_id: endpoint_id,
695                datagrams: data.to_vec().into()
696            }
697        );
698
699        // send peer_gone
700        println!("send peer gone");
701        message_s
702            .send(RelayToClientMsg::EndpointGone(endpoint_id))
703            .await
704            .std_context("send")?;
705        let frame = recv_frame(FrameType::EndpointGone, &mut io_rw)
706            .await
707            .anyerr()?;
708        assert_eq!(frame, RelayToClientMsg::EndpointGone(endpoint_id));
709
710        // Read tests
711        println!("--read");
712
713        // send ping, expect pong
714        let data = b"pingpong";
715        io_rw.send(ClientToRelayMsg::Ping(*data)).await?;
716
717        // recv pong
718        println!(" recv pong");
719        let frame = recv_frame(FrameType::Pong, &mut io_rw).await?;
720        assert_eq!(frame, RelayToClientMsg::Pong(*data));
721
722        let target = SecretKey::from_bytes(&rng.random()).public();
723
724        // send packet
725        println!("  send packet");
726        let data = b"hello world!";
727        io_rw
728            .send(ClientToRelayMsg::Datagrams {
729                dst_endpoint_id: target,
730                datagrams: Datagrams::from(data),
731            })
732            .await
733            .std_context("send")?;
734
735        done.cancel();
736        handle.await.std_context("join")?;
737        Ok(())
738    }
739
740    fn test_client_builder(
741        key: EndpointId,
742        protocol_version: ProtocolVersion,
743    ) -> (Config<WsBytesFramed<RateLimited<MaybeTlsStream>>>, Conn) {
744        let (server, client) = tokio::io::duplex(1024);
745        let guard = OnDisconnectGuard::empty(key);
746        let mut config = Config::new(guard, ServerRelayedStream::test(server), protocol_version);
747        config.write_timeout = Duration::from_secs(1);
748        config.channel_capacity = 10;
749        (config, Conn::test(client, protocol_version))
750    }
751
752    #[tokio::test]
753    #[traced_test]
754    async fn test_client_v1_protocol() -> Result {
755        let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(42u64);
756        let a_key = SecretKey::from_bytes(&rng.random()).public();
757        let b_key = SecretKey::from_bytes(&rng.random()).public();
758
759        let (builder_a, mut a_rw) = test_client_builder(a_key, ProtocolVersion::V1);
760
761        let clients = Clients::default();
762        let metrics = Arc::new(Metrics::default());
763        clients.register(builder_a, metrics.clone());
764
765        // Verify basic packet delivery works with V1.
766        let data = b"hello world v1!";
767        clients.send_packet(a_key, Datagrams::from(&data[..]), b_key, &metrics)?;
768        let frame = recv_frame(FrameType::RelayToClientDatagram, &mut a_rw).await?;
769        assert_eq!(
770            frame,
771            RelayToClientMsg::Datagrams {
772                remote_endpoint_id: b_key,
773                datagrams: data.to_vec().into(),
774            }
775        );
776
777        clients.shutdown().await;
778        Ok(())
779    }
780
781    #[tokio::test]
782    #[traced_test]
783    async fn test_client_v2_protocol() -> Result {
784        let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(42u64);
785        let a_key = SecretKey::from_bytes(&rng.random()).public();
786        let b_key = SecretKey::from_bytes(&rng.random()).public();
787
788        let (builder_a, mut a_rw) = test_client_builder(a_key, ProtocolVersion::V2);
789
790        let clients = Clients::default();
791        let metrics = Arc::new(Metrics::default());
792        clients.register(builder_a, metrics.clone());
793
794        // Verify basic packet delivery works with V2.
795        let data = b"hello world v2!";
796        clients.send_packet(a_key, Datagrams::from(&data[..]), b_key, &metrics)?;
797        let frame = recv_frame(FrameType::RelayToClientDatagram, &mut a_rw).await?;
798        assert_eq!(
799            frame,
800            RelayToClientMsg::Datagrams {
801                remote_endpoint_id: b_key,
802                datagrams: data.to_vec().into(),
803            }
804        );
805
806        clients.shutdown().await;
807        Ok(())
808    }
809
810    /// A packet that cannot be encoded is dropped and the connection stays usable.
811    #[tokio::test]
812    #[traced_test]
813    async fn test_unsendable_packet_is_dropped() -> Result {
814        let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(42u64);
815        let a_key = SecretKey::from_bytes(&rng.random()).public();
816        let b_key = SecretKey::from_bytes(&rng.random()).public();
817
818        let (builder_a, mut a_rw) = test_client_builder(a_key, ProtocolVersion::V2);
819
820        let clients = Clients::default();
821        let metrics = Arc::new(Metrics::default());
822        clients.register(builder_a, metrics.clone());
823
824        // Does not fit into a frame once the frame header is added.
825        let too_large = vec![0u8; MAX_PACKET_SIZE];
826        clients.send_packet(a_key, Datagrams::from(&too_large[..]), b_key, &metrics)?;
827
828        // The next packet still arrives.
829        let data = b"still here";
830        clients.send_packet(a_key, Datagrams::from(&data[..]), b_key, &metrics)?;
831        let frame = recv_frame(FrameType::RelayToClientDatagram, &mut a_rw).await?;
832        assert_eq!(
833            frame,
834            RelayToClientMsg::Datagrams {
835                remote_endpoint_id: b_key,
836                datagrams: data.to_vec().into(),
837            }
838        );
839        assert_eq!(metrics.send_packets_dropped.get(), 1);
840
841        clients.shutdown().await;
842        Ok(())
843    }
844
845    /// Test for versioned protocol: v1 client should receive V1Health frame.
846    #[tokio::test]
847    #[traced_test]
848    async fn test_duplicate_endpoint_v1_receives_v1health() -> Result {
849        let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(42u64);
850        let key = SecretKey::from_bytes(&rng.random()).public();
851
852        let (builder_first, mut first_rw) = test_client_builder(key, ProtocolVersion::V1);
853
854        let clients = Clients::default();
855        let metrics = Arc::new(Metrics::default());
856        clients.register(builder_first, metrics.clone());
857
858        // Register a second client with the same endpoint ID.
859        // The first client should receive a V1Health message.
860        let (builder_second, _second_rw) = test_client_builder(key, ProtocolVersion::V1);
861        clients.register(builder_second, metrics.clone());
862
863        let frame = recv_frame(FrameType::Health, &mut first_rw).await?;
864        assert!(
865            matches!(frame, RelayToClientMsg::Health { .. }),
866            "expected V1Health frame for V1 client, got {frame:?}"
867        );
868
869        clients.shutdown().await;
870        Ok(())
871    }
872
873    /// Test for versioned protocol: v2 client should receive V2Health frame.
874    #[tokio::test]
875    #[traced_test]
876    async fn test_duplicate_endpoint_v2_receives_health() -> Result {
877        let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(42u64);
878        let key = SecretKey::from_bytes(&rng.random()).public();
879
880        let (builder_first, mut first_rw) = test_client_builder(key, ProtocolVersion::V2);
881
882        let clients = Clients::default();
883        let metrics = Arc::new(Metrics::default());
884        clients.register(builder_first, metrics.clone());
885
886        // Register a second client with the same endpoint ID.
887        // The first client should receive a Health message (V2 frame).
888        let (builder_second, _second_rw) = test_client_builder(key, ProtocolVersion::V2);
889        clients.register(builder_second, metrics.clone());
890
891        let frame = recv_frame(FrameType::Status, &mut first_rw).await?;
892        assert_eq!(
893            frame,
894            RelayToClientMsg::Status(Status::SameEndpointIdConnected)
895        );
896
897        clients.shutdown().await;
898        Ok(())
899    }
900
901    #[tokio::test(start_paused = true)]
902    #[traced_test]
903    async fn test_rate_limit() -> Result {
904        const LIMIT: u32 = 50;
905        const MAX_FRAMES: u32 = 100;
906
907        let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0u64);
908
909        // Build the rate limited stream.
910        let (io_read, io_write) = tokio::io::duplex((LIMIT * MAX_FRAMES) as _);
911        let mut frame_writer = Conn::test(io_write, Default::default());
912        // Rate limiter allowing LIMIT bytes/s
913        let (mut stream, _limited) = RelayedStream::test_limited(io_read, LIMIT / 10, LIMIT)?;
914
915        // Prepare a frame to send, assert its size.
916        let data = Datagrams::from(b"hello world!!!!!");
917        let target = SecretKey::from_bytes(&rng.random()).public();
918        let frame = ClientToRelayMsg::Datagrams {
919            dst_endpoint_id: target,
920            datagrams: data.clone(),
921        };
922        let frame_len = frame.to_bytes().len();
923        assert_eq!(frame_len, LIMIT as usize);
924
925        // Send a frame, it should arrive.
926        info!("-- send packet");
927        frame_writer.send(frame.clone()).await.std_context("send")?;
928        frame_writer.flush().await.std_context("flush")?;
929        let recv_frame = tokio::time::timeout(Duration::from_millis(500), stream.next())
930            .await
931            .expect("timeout")
932            .expect("option")
933            .expect("ok");
934        assert_eq!(recv_frame, frame);
935
936        // Next frame does not arrive.
937        info!("-- send packet");
938        frame_writer.send(frame.clone()).await.std_context("send")?;
939        frame_writer.flush().await.std_context("flush")?;
940        let res = tokio::time::timeout(Duration::from_millis(100), stream.next()).await;
941        assert!(res.is_err(), "expecting a timeout");
942        info!("-- timeout happened");
943
944        // Wait long enough.
945        info!("-- sleep");
946        tokio::time::sleep(Duration::from_secs(1)).await;
947
948        // Frame arrives.
949        let recv_frame = tokio::time::timeout(Duration::from_millis(500), stream.next())
950            .await
951            .expect("timeout")
952            .expect("option")
953            .expect("ok");
954        assert_eq!(recv_frame, frame);
955
956        Ok(())
957    }
958
959    /// A client whose connection is rate-limited receives a [`Status::RateLimited`] notice.
960    #[tokio::test(start_paused = true)]
961    #[traced_test]
962    async fn test_rate_limit_notice() -> Result {
963        const LIMIT: u32 = 50;
964
965        let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0u64);
966        let key = SecretKey::from_bytes(&rng.random()).public();
967        let target = SecretKey::from_bytes(&rng.random()).public();
968
969        let (server_io, client_io) = tokio::io::duplex(4096);
970        // Rate limiter allowing LIMIT bytes/s with a burst size below a single frame.
971        let (stream, limited) = ServerRelayedStream::test_limited(server_io, LIMIT / 10, LIMIT)?;
972        let mut config = Config::new(OnDisconnectGuard::empty(key), stream, ProtocolVersion::V2);
973        config.write_timeout = Duration::from_secs(1);
974        config.channel_capacity = 10;
975        config.rate_limited = Some(limited);
976        let mut conn = Conn::test(client_io, ProtocolVersion::V2);
977
978        let clients = Clients::default();
979        clients.register(config, Arc::new(Metrics::default()));
980
981        // A single frame larger than the burst size trips the rate limiter.
982        conn.send(ClientToRelayMsg::Datagrams {
983            dst_endpoint_id: target,
984            datagrams: Datagrams::from(b"hello world!!!!!"),
985        })
986        .await?;
987
988        let frame = recv_frame(FrameType::Status, &mut conn).await?;
989        assert_eq!(frame, RelayToClientMsg::Status(Status::RateLimited));
990
991        clients.shutdown().await;
992        Ok(())
993    }
994}