trying to fix udp telegram

This commit is contained in:
2026-03-26 19:55:07 +07:00
parent 64d09ee7bf
commit 3eb2df3a34
10 changed files with 65 additions and 59 deletions
+1 -1
View File
@@ -89,7 +89,7 @@ impl SessionManager {
#[cfg(target_os = "linux")]
{
config = config.with_mtu(1350);
config = config.with_mtu(1500);
}
NetworkConfig::init_global(config.mtu);
+2 -1
View File
@@ -13,6 +13,7 @@ use tokio_util::sync::CancellationToken;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
netrunner_logger::Logger::init();
netrunner_logger::Logger::global().set_level("debug");
info!("Initializing NetRunner Stack...");
let remote_address = "147.45.43.70:443".to_string();
@@ -23,7 +24,7 @@ async fn main() -> anyhow::Result<()> {
// ==================================================
let config = EngineConfig::new(&remote_address)
.with_cache_path(".")
.with_mtu(1350);
.with_mtu(1500);
// ВАЖНО: Инициализируем глобальные настройки сети (MTU, размеры буферов Muxer'а)
NetworkConfig::init_global(config.mtu);
+38 -30
View File
@@ -3,13 +3,11 @@ use netrunner_core::net::network::NetworkConfig;
use smoltcp::{
iface::SocketHandle,
socket::{tcp, udp},
time::Instant,
wire::IpEndpoint,
};
use std::time::Duration;
use tokio::{
sync::{mpsc, oneshot},
time::Instant,
};
use tokio::sync::{mpsc, oneshot};
// Добавили trace для частых логов (попакетно) и debug для состояний
use netrunner_logger::{debug, info, trace, warn};
@@ -29,9 +27,9 @@ impl ConnectionCore {
pub fn new(handle: SocketHandle) -> (Self, mpsc::Receiver<Bytes>, mpsc::Sender<Bytes>) {
trace!(%handle, "Creating ConnectionCore channels");
let (tx_to_net, rx_from_smol) =
mpsc::channel::<Bytes>(NetworkConfig::global().stream_capacity);
mpsc::channel::<Bytes>(NetworkConfig::global().tcp_stream_capacity);
let (tx_to_smol, rx_from_net) =
mpsc::channel::<Bytes>(NetworkConfig::global().stream_capacity);
mpsc::channel::<Bytes>(NetworkConfig::global().tcp_stream_capacity);
let core = Self {
handle,
@@ -247,8 +245,22 @@ impl UdpConnection {
}
pub fn tick(&mut self, socket: &mut udp::Socket) -> bool {
if self.last_activity.elapsed() > UDP_TIMEOUT {
debug!(%self.core.handle, "UDP Session closed due to {}s timeout", UDP_TIMEOUT.as_secs());
// 1. Получаем текущее время системы в миллисекундах
let now_ms = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_millis() as i64;
// 2. Считаем разницу (сколько мс прошло с последней активности)
let elapsed_ms = now_ms - self.last_activity.total_millis();
// 3. Сравниваем с UDP_TIMEOUT (переводим его тоже в мс)
if elapsed_ms > UDP_TIMEOUT.as_millis() as i64 {
debug!(
%self.core.handle,
"UDP Session closed due to {}s timeout",
UDP_TIMEOUT.as_secs()
);
socket.close();
return false;
}
@@ -286,34 +298,30 @@ impl UdpConnection {
if socket.can_send() {
if let Some(client_endpoint) = self.client_endpoint {
while let Ok(data) = self.core.rx.try_recv() {
if data.is_empty() {
debug!(%self.core.handle, "Received empty datagram (Close signal) from Muxer, closing UDP socket");
socket.close();
return false;
}
match socket.send_slice(&data, client_endpoint) {
Ok(_) => {
let proxy_endpoint = socket.endpoint();
info!(
%self.core.handle,
source = %proxy_endpoint,
target = %client_endpoint,
bytes = data.len(),
"Wrote UDP reply from Muxer back to smoltcp"
);
self.last_activity = Instant::now();
}
Err(e) => {
debug!(%self.core.handle, "Failed to send UDP datagram to smoltcp: {:?}", e);
loop {
match self.core.rx.try_recv() {
Ok(data) => match socket.send_slice(&data, client_endpoint) {
Ok(_) => {
self.last_activity = smoltcp::time::Instant::now();
}
Err(e) => {
debug!(%self.core.handle, "Failed to send UDP datagram: {:?}", e);
break;
}
},
Err(mpsc::error::TryRecvError::Empty) => {
break;
}
Err(mpsc::error::TryRecvError::Disconnected) => {
// Сервер разорвал соединение!
debug!(%self.core.handle, "Muxer channel disconnected, closing UDP socket");
socket.close();
return false;
}
}
}
}
}
true
}
}
+1 -1
View File
@@ -168,7 +168,7 @@ impl SocketFactory {
let (buf_size, packet_count) = match port {
443 => (max_buf, max_buf / payload_size), // QUIC/HTTP3 трафик
53 => (64 * 1024, (64 * 1024) / payload_size), // DNS
_ => (max_buf / 2, (max_buf / 2) / payload_size),
_ => (max_buf, max_buf / payload_size),
};
// Гарантируем, что метаданных хватит хотя бы на 10 пакетов
+1 -1
View File
@@ -282,7 +282,7 @@ impl EngineConfig {
Self {
remote_address: remote_address.into(),
cache_path: ".".to_string(),
mtu: 1350,
mtu: 1500,
setup_routing: true,
any_ip: true,
transparent_mode: true,