Skip to content

Commit 9b3a32b

Browse files
committed
Merge branch 'improve-ephemeral-peer-error-logs'
2 parents e73a827 + 05e01e9 commit 9b3a32b

File tree

4 files changed

+119
-10
lines changed

4 files changed

+119
-10
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

talpid-tunnel-config-client/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ workspace = true
1414
log = { workspace = true }
1515
rand = "0.8"
1616
talpid-types = { path = "../talpid-types" }
17-
futures = { workspace = true }
1817
tonic = { workspace = true }
1918
tower = { workspace = true }
2019
prost = { workspace = true }

talpid-tunnel-config-client/src/lib.rs

+79-4
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ pub async fn request_ephemeral_peer(
107107
enable_post_quantum: bool,
108108
enable_daita: bool,
109109
) -> Result<EphemeralPeer, Error> {
110+
log::debug!("Connecting to relay config service at {service_address}");
110111
let client = connect_relay_config_client(service_address).await?;
112+
log::debug!("Connected to relay config service at {service_address}");
111113

112114
request_ephemeral_peer_with(
113115
client,
@@ -128,6 +130,7 @@ pub async fn request_ephemeral_peer_with(
128130
) -> Result<EphemeralPeer, Error> {
129131
let (pq_request, kem_secrets) = if enable_quantum_resistant {
130132
let (pq_request, kem_secrets) = post_quantum_secrets().await;
133+
log::debug!("Generated PQ secrets");
131134
(Some(pq_request), Some(kem_secrets))
132135
} else {
133136
(None, None)
@@ -275,20 +278,92 @@ fn xor_assign(dst: &mut [u8; 32], src: &[u8; 32]) {
275278
/// value has been speficically lowered, to avoid MTU issues. See the `socket` module.
276279
#[cfg(not(target_os = "ios"))]
277280
async fn connect_relay_config_client(ip: Ipv4Addr) -> Result<RelayConfigService, Error> {
278-
use futures::TryFutureExt;
281+
use hyper_util::rt::tokio::TokioIo;
279282

280283
let endpoint = Endpoint::from_static("tcp://0.0.0.0:0");
281284
let addr = SocketAddr::new(IpAddr::V4(ip), CONFIG_SERVICE_PORT);
282285

283286
let connection = endpoint
284287
.connect_with_connector(service_fn(move |_| async move {
285288
let sock = socket::TcpSocket::new()?;
286-
sock.connect(addr)
287-
.map_ok(hyper_util::rt::tokio::TokioIo::new)
288-
.await
289+
let stream = sock.connect(addr).await?;
290+
let sniffer = socket_sniffer::SocketSniffer {
291+
s: stream,
292+
rx_bytes: 0,
293+
tx_bytes: 0,
294+
start_time: std::time::Instant::now(),
295+
};
296+
Ok::<_, std::io::Error>(TokioIo::new(sniffer))
289297
}))
290298
.await
291299
.map_err(Error::GrpcConnectError)?;
292300

293301
Ok(RelayConfigService::new(connection))
294302
}
303+
304+
mod socket_sniffer {
305+
pub struct SocketSniffer<S> {
306+
pub s: S,
307+
pub rx_bytes: usize,
308+
pub tx_bytes: usize,
309+
pub start_time: std::time::Instant,
310+
}
311+
use std::{
312+
io,
313+
pin::Pin,
314+
task::{Context, Poll},
315+
};
316+
317+
use tokio::io::AsyncWrite;
318+
319+
use tokio::io::{AsyncRead, ReadBuf};
320+
321+
impl<S> Drop for SocketSniffer<S> {
322+
fn drop(&mut self) {
323+
let duration = self.start_time.elapsed();
324+
log::debug!(
325+
"Tunnel config client connection ended. RX: {} bytes, TX: {} bytes, duration: {} s",
326+
self.rx_bytes,
327+
self.tx_bytes,
328+
duration.as_secs()
329+
);
330+
}
331+
}
332+
333+
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncRead for SocketSniffer<S> {
334+
fn poll_read(
335+
mut self: Pin<&mut Self>,
336+
cx: &mut Context<'_>,
337+
buf: &mut ReadBuf<'_>,
338+
) -> Poll<io::Result<()>> {
339+
let initial_data = buf.filled().len();
340+
let bytes = std::task::ready!(Pin::new(&mut self.s).poll_read(cx, buf));
341+
if bytes.is_ok() {
342+
self.rx_bytes += buf.filled().len().saturating_sub(initial_data);
343+
}
344+
Poll::Ready(bytes)
345+
}
346+
}
347+
348+
impl<S: AsyncRead + AsyncWrite + Unpin> AsyncWrite for SocketSniffer<S> {
349+
fn poll_write(
350+
mut self: Pin<&mut Self>,
351+
cx: &mut Context<'_>,
352+
buf: &[u8],
353+
) -> Poll<io::Result<usize>> {
354+
let bytes = std::task::ready!(Pin::new(&mut self.s).poll_write(cx, buf));
355+
if let Ok(bytes) = bytes {
356+
self.tx_bytes += bytes;
357+
}
358+
Poll::Ready(bytes)
359+
}
360+
361+
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
362+
Pin::new(&mut self.s).poll_flush(cx)
363+
}
364+
365+
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
366+
Pin::new(&mut self.s).poll_shutdown(cx)
367+
}
368+
}
369+
}

talpid-wireguard/src/lib.rs

+40-4
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,22 @@ impl WireguardMonitor {
268268

269269
let ephemeral_obfs_sender = close_obfs_sender.clone();
270270
if config.quantum_resistant || config.daita {
271-
ephemeral::config_ephemeral_peers(
271+
if let Err(e) = ephemeral::config_ephemeral_peers(
272272
&tunnel,
273273
&mut config,
274274
args.retry_attempt,
275275
obfuscator.clone(),
276276
ephemeral_obfs_sender,
277277
)
278-
.await?;
278+
.await
279+
{
280+
// We have received a small amount of reports about ephemeral peer nogationation
281+
// timing out on Windows for 2024.9-beta1. These verbose data usage logs are
282+
// a temporary measure to help us understand the issue. They can be removed
283+
// if the issue is resolved.
284+
log_tunnel_data_usage(&config, &tunnel).await;
285+
return Err(e);
286+
}
279287

280288
let metadata = Self::tunnel_metadata(&iface_name, &config);
281289
event_hook
@@ -464,15 +472,23 @@ impl WireguardMonitor {
464472
if should_negotiate_ephemeral_peer {
465473
let ephemeral_obfs_sender = close_obfs_sender.clone();
466474

467-
ephemeral::config_ephemeral_peers(
475+
if let Err(e) = ephemeral::config_ephemeral_peers(
468476
&tunnel,
469477
&mut config,
470478
args.retry_attempt,
471479
obfuscator.clone(),
472480
ephemeral_obfs_sender,
473481
args.tun_provider,
474482
)
475-
.await?;
483+
.await
484+
{
485+
// We have received a small amount of reports about ephemeral peer nogationation
486+
// timing out on Windows for 2024.9-beta1. These verbose data usage logs are
487+
// a temporary measure to help us understand the issue. They can be removed
488+
// if the issue is resolved.
489+
log_tunnel_data_usage(&config, &tunnel).await;
490+
return Err(e);
491+
}
476492

477493
let metadata = Self::tunnel_metadata(&iface_name, &config);
478494
event_hook
@@ -965,6 +981,26 @@ impl WireguardMonitor {
965981
}
966982
}
967983

984+
async fn log_tunnel_data_usage(config: &Config, tunnel: &Arc<AsyncMutex<Option<TunnelType>>>) {
985+
let tunnel = tunnel.lock().await;
986+
let Some(tunnel) = &*tunnel else { return };
987+
let Ok(tunnel_stats) = tunnel.get_tunnel_stats() else {
988+
return;
989+
};
990+
if let Some(stats) = config
991+
.exit_peer
992+
.as_ref()
993+
.map(|peer| peer.public_key.as_bytes())
994+
.and_then(|pubkey| tunnel_stats.get(pubkey))
995+
{
996+
log::warn!("Exit peer stats: {:?}", stats);
997+
};
998+
let pubkey = config.entry_peer.public_key.as_bytes();
999+
if let Some(stats) = tunnel_stats.get(pubkey) {
1000+
log::warn!("Entry peer stats: {:?}", stats);
1001+
}
1002+
}
1003+
9681004
#[derive(Debug)]
9691005
enum CloseMsg {
9701006
Stop,

0 commit comments

Comments
 (0)