|
| 1 | +#![cfg(feature = "sv1")] |
| 2 | +use async_channel::{Receiver, Sender}; |
| 3 | +use network_helpers_sv2::sv1_connection::ConnectionSV1; |
| 4 | +use std::{collections::VecDeque, net::SocketAddr, sync::Arc, time::Duration}; |
| 5 | +use tokio::{ |
| 6 | + net::{TcpListener, TcpStream}, |
| 7 | + select, |
| 8 | + sync::Mutex, |
| 9 | + time::sleep, |
| 10 | +}; |
| 11 | + |
| 12 | +#[derive(Debug, PartialEq)] |
| 13 | +enum SnifferError { |
| 14 | + DownstreamClosed, |
| 15 | + UpstreamClosed, |
| 16 | +} |
| 17 | + |
| 18 | +/// Represents an SV1 sniffer. |
| 19 | +/// |
| 20 | +/// This struct acts as a middleman between two SV1 roles. It forwards messages from one role to |
| 21 | +/// the other and vice versa. It also provides methods to wait for specific messages to be received |
| 22 | +/// from the downstream or upstream role. |
| 23 | +#[derive(Debug, Clone)] |
| 24 | +pub struct SnifferSV1 { |
| 25 | + listening_address: SocketAddr, |
| 26 | + upstream_address: SocketAddr, |
| 27 | + messages_from_downstream_sv1: MessagesAggregatorSV1, |
| 28 | + messages_from_upstream_sv1: MessagesAggregatorSV1, |
| 29 | +} |
| 30 | + |
| 31 | +impl SnifferSV1 { |
| 32 | + /// Create a new [`SnifferSV1`] instance. |
| 33 | + /// |
| 34 | + /// The listening address is the address the sniffer will listen on for incoming connections |
| 35 | + /// from the downstream role. The upstream address is the address the sniffer will connect to |
| 36 | + /// in order to forward messages to the upstream role. |
| 37 | + pub fn new(listening_address: SocketAddr, upstream_address: SocketAddr) -> Self { |
| 38 | + Self { |
| 39 | + listening_address, |
| 40 | + upstream_address, |
| 41 | + messages_from_downstream_sv1: MessagesAggregatorSV1::new(), |
| 42 | + messages_from_upstream_sv1: MessagesAggregatorSV1::new(), |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// Start the sniffer. |
| 47 | + pub fn start(&self) { |
| 48 | + let upstream_address = self.upstream_address.clone(); |
| 49 | + let listening_address = self.listening_address.clone(); |
| 50 | + let messages_from_downstream_sv1 = self.messages_from_downstream_sv1.clone(); |
| 51 | + let messages_from_upstream_sv1 = self.messages_from_upstream_sv1.clone(); |
| 52 | + tokio::spawn(async move { |
| 53 | + let sniffer_to_upstream_stream = TcpStream::connect(upstream_address) |
| 54 | + .await |
| 55 | + .expect("Failed to connect to upstream"); |
| 56 | + let listener = TcpListener::bind(listening_address) |
| 57 | + .await |
| 58 | + .expect("Failed to listen on given address"); |
| 59 | + let (downstream_stream, _) = listener |
| 60 | + .accept() |
| 61 | + .await |
| 62 | + .expect("Failed to accept downstream connection"); |
| 63 | + let sniffer_to_upstream_connection = |
| 64 | + ConnectionSV1::new(sniffer_to_upstream_stream).await; |
| 65 | + let downstream_to_sniffer_connection = ConnectionSV1::new(downstream_stream).await; |
| 66 | + select! { |
| 67 | + _ = tokio::signal::ctrl_c() => { }, |
| 68 | + _ = Self::recv_from_down_send_to_up_sv1( |
| 69 | + downstream_to_sniffer_connection.receiver(), |
| 70 | + sniffer_to_upstream_connection.sender(), |
| 71 | + messages_from_downstream_sv1 |
| 72 | + ) => { }, |
| 73 | + _ = Self::recv_from_up_send_to_down_sv1( |
| 74 | + sniffer_to_upstream_connection.receiver(), |
| 75 | + downstream_to_sniffer_connection.sender(), |
| 76 | + messages_from_upstream_sv1 |
| 77 | + ) => { }, |
| 78 | + }; |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + /// Wait for a specific message to be received from the downstream role. |
| 83 | + pub async fn wait_for_message_from_downstream(&self, message: &str) { |
| 84 | + let now = std::time::Instant::now(); |
| 85 | + tokio::select!( |
| 86 | + _ = tokio::signal::ctrl_c() => { }, |
| 87 | + _ = async { |
| 88 | + loop { |
| 89 | + if self.messages_from_downstream_sv1.has_message(&[message]).await { |
| 90 | + break; |
| 91 | + } |
| 92 | + if now.elapsed().as_secs() > 60 { |
| 93 | + panic!( |
| 94 | + "Timeout: SV1 message {} from downstream not received", |
| 95 | + message |
| 96 | + ); |
| 97 | + } else { |
| 98 | + tokio::time::sleep(std::time::Duration::from_secs(1)).await; |
| 99 | + continue; |
| 100 | + } |
| 101 | + } |
| 102 | + } => {} |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /// Wait for a specific message to be received from the upstream role. |
| 107 | + pub async fn wait_for_message_from_upstream(&self, message: &[&str]) { |
| 108 | + let now = std::time::Instant::now(); |
| 109 | + tokio::select!( |
| 110 | + _ = tokio::signal::ctrl_c() => { }, |
| 111 | + _ = async { |
| 112 | + loop { |
| 113 | + if self.messages_from_upstream_sv1.has_message(message).await { |
| 114 | + break; |
| 115 | + } |
| 116 | + if now.elapsed().as_secs() > 60 { |
| 117 | + panic!( |
| 118 | + "Timeout: SV1 message {} from upstream not received", |
| 119 | + message.join(" ") |
| 120 | + ); |
| 121 | + } else { |
| 122 | + tokio::time::sleep(std::time::Duration::from_secs(1)).await; |
| 123 | + continue; |
| 124 | + } |
| 125 | + } |
| 126 | + } => {} |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + async fn recv_from_up_send_to_down_sv1( |
| 131 | + recv: Receiver<sv1_api::Message>, |
| 132 | + send: Sender<sv1_api::Message>, |
| 133 | + upstream_messages: MessagesAggregatorSV1, |
| 134 | + ) -> Result<(), SnifferError> { |
| 135 | + while let Ok(msg) = recv.recv().await { |
| 136 | + send.send(msg.clone()) |
| 137 | + .await |
| 138 | + .map_err(|_| SnifferError::DownstreamClosed)?; |
| 139 | + upstream_messages.add_message(msg.clone()).await; |
| 140 | + sleep(Duration::from_millis(100)).await; |
| 141 | + } |
| 142 | + Err(SnifferError::UpstreamClosed) |
| 143 | + } |
| 144 | + |
| 145 | + async fn recv_from_down_send_to_up_sv1( |
| 146 | + recv: Receiver<sv1_api::Message>, |
| 147 | + send: Sender<sv1_api::Message>, |
| 148 | + downstream_messages: MessagesAggregatorSV1, |
| 149 | + ) -> Result<(), SnifferError> { |
| 150 | + while let Ok(msg) = recv.recv().await { |
| 151 | + send.send(msg.clone()) |
| 152 | + .await |
| 153 | + .map_err(|_| SnifferError::UpstreamClosed)?; |
| 154 | + downstream_messages.add_message(msg).await; |
| 155 | + } |
| 156 | + Err(SnifferError::DownstreamClosed) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +/// Represents a SV1 message manager. |
| 161 | +/// |
| 162 | +/// This struct can be used in order to aggregate and manage SV1 messages. |
| 163 | +#[derive(Debug, Clone)] |
| 164 | +pub(crate) struct MessagesAggregatorSV1 { |
| 165 | + messages: Arc<Mutex<VecDeque<sv1_api::Message>>>, |
| 166 | +} |
| 167 | + |
| 168 | +impl MessagesAggregatorSV1 { |
| 169 | + fn new() -> Self { |
| 170 | + Self { |
| 171 | + messages: Arc::new(Mutex::new(VecDeque::new())), |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + async fn add_message(&self, message: sv1_api::Message) { |
| 176 | + let mut messages = self.messages.lock().await; |
| 177 | + messages.push_back(message); |
| 178 | + } |
| 179 | + |
| 180 | + async fn has_message(&self, expected_msg: &[&str]) -> bool { |
| 181 | + let messages = self.messages.lock().await; |
| 182 | + let ret = messages.iter().any(|msg| match msg { |
| 183 | + sv1_api::Message::StandardRequest(req) => req.method == *expected_msg.get(0).unwrap(), |
| 184 | + sv1_api::Message::Notification(notif) => notif.method == *expected_msg.get(0).unwrap(), |
| 185 | + sv1_api::Message::OkResponse(res) => { |
| 186 | + if let Ok(res) = network_helpers_sv2::serde_json::to_string(&res) { |
| 187 | + for m in expected_msg { |
| 188 | + if !res.contains(m) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + } |
| 192 | + return true; |
| 193 | + } else { |
| 194 | + false |
| 195 | + } |
| 196 | + } |
| 197 | + sv1_api::Message::ErrorResponse(res) => { |
| 198 | + res.error.clone().unwrap().message == *expected_msg.get(0).unwrap() |
| 199 | + } |
| 200 | + }); |
| 201 | + ret |
| 202 | + } |
| 203 | +} |
0 commit comments