global buf size mtu based config and protocol rename

This commit is contained in:
2026-03-25 16:03:11 +07:00
parent 743c6bf05c
commit a213c01158
30 changed files with 102 additions and 92 deletions
+2 -2
View File
@@ -9,8 +9,8 @@ use crate::{
net::engine::{EngineBuilder, EngineConfig}, net::engine::{EngineBuilder, EngineConfig},
tun::routing::reset_platform_routing, tun::routing::reset_platform_routing,
}; };
use netrunner_core::proxy::connection::connection::ConnectionRole; use netrunner_core::net::connection::connection::ConnectionRole;
use netrunner_core::proxy::network::Network; use netrunner_core::net::network::Network;
use netrunner_logger::{error, info}; use netrunner_logger::{error, info};
use std::sync::{Arc, OnceLock}; use std::sync::{Arc, OnceLock};
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
+2 -2
View File
@@ -7,8 +7,8 @@ use crate::tun::{routing::reset_platform_routing, tun::Tun};
use net::engine::{EngineBuilder, EngineConfig}; use net::engine::{EngineBuilder, EngineConfig};
// Импортируем компоненты локального прокси // Импортируем компоненты локального прокси
use netrunner_core::proxy::connection::connection::ConnectionRole; use netrunner_core::net::connection::connection::ConnectionRole;
use netrunner_core::proxy::network::Network; use netrunner_core::net::network::Network;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
#[tokio::main] #[tokio::main]
+5 -3
View File
@@ -1,5 +1,5 @@
use crate::net::CHANNEL_CAPACITY;
use bytes::{Buf, Bytes, BytesMut}; use bytes::{Buf, Bytes, BytesMut};
use netrunner_core::net::network::NetworkConfig;
use smoltcp::{ use smoltcp::{
iface::SocketHandle, iface::SocketHandle,
socket::{tcp, udp}, socket::{tcp, udp},
@@ -28,8 +28,10 @@ pub struct ConnectionCore {
impl ConnectionCore { impl ConnectionCore {
pub fn new(handle: SocketHandle) -> (Self, mpsc::Receiver<Bytes>, mpsc::Sender<Bytes>) { pub fn new(handle: SocketHandle) -> (Self, mpsc::Receiver<Bytes>, mpsc::Sender<Bytes>) {
trace!(%handle, "Creating ConnectionCore channels"); trace!(%handle, "Creating ConnectionCore channels");
let (tx_to_net, rx_from_smol) = mpsc::channel::<Bytes>(CHANNEL_CAPACITY); let (tx_to_net, rx_from_smol) =
let (tx_to_smol, rx_from_net) = mpsc::channel::<Bytes>(CHANNEL_CAPACITY); mpsc::channel::<Bytes>(NetworkConfig::global().channel_capacity);
let (tx_to_smol, rx_from_net) =
mpsc::channel::<Bytes>(NetworkConfig::global().channel_capacity);
let core = Self { let core = Self {
handle, handle,
+1 -1
View File
@@ -1,5 +1,5 @@
use bytes::Bytes; use bytes::Bytes;
use netrunner_core::protocol::codec::socks::TargetAddress; use netrunner_core::nrxp::codec::socks::TargetAddress;
use netrunner_logger::{debug, error, info, trace, warn}; use netrunner_logger::{debug, error, info, trace, warn};
use smoltcp::{ use smoltcp::{
iface::{SocketHandle, SocketSet}, iface::{SocketHandle, SocketSet},
+2 -2
View File
@@ -1,5 +1,5 @@
use netrunner_core::proxy::connection::connection::ClientHandler; use netrunner_core::net::connection::connection::ClientHandler;
use netrunner_core::proxy::connection::muxer::Muxer; use netrunner_core::net::connection::muxer::Muxer;
use smoltcp::iface::PollResult; use smoltcp::iface::PollResult;
use smoltcp::time::Instant; use smoltcp::time::Instant;
use smoltcp::wire::{IpAddress, IpCidr}; use smoltcp::wire::{IpAddress, IpCidr};
-2
View File
@@ -3,5 +3,3 @@ pub mod connection_manager;
pub mod dns; pub mod dns;
pub mod engine; pub mod engine;
pub mod ip_store; pub mod ip_store;
pub const CHANNEL_CAPACITY: usize = 16;
+2 -2
View File
@@ -1,5 +1,5 @@
mod crypto; mod crypto;
pub mod protocol; pub mod nrxp;
pub mod proxy; pub mod net;
mod tlseng; mod tlseng;
mod utils; mod utils;
@@ -1,6 +1,6 @@
use crate::protocol::codec::frame::FrameType; use crate::nrxp::codec::frame::FrameType;
use crate::proxy::connection::muxer::{MuxMessage, Muxer}; use crate::net::connection::muxer::{MuxMessage, Muxer};
use crate::proxy::connection::{TCP_BUF_SIZE, UDP_BUF_SIZE}; use crate::net::network::NetworkConfig;
use bytes::{Bytes, BytesMut}; use bytes::{Bytes, BytesMut};
use netrunner_logger::{debug, error}; use netrunner_logger::{debug, error};
use tokio::net::UdpSocket; use tokio::net::UdpSocket;
@@ -16,7 +16,7 @@ pub async fn run_tcp_bridge<R, W>(
R: tokio::io::AsyncReadExt + Unpin, R: tokio::io::AsyncReadExt + Unpin,
W: tokio::io::AsyncWriteExt + Unpin, W: tokio::io::AsyncWriteExt + Unpin,
{ {
let mut buf = BytesMut::with_capacity(TCP_BUF_SIZE); let mut buf = BytesMut::with_capacity(NetworkConfig::global().tcp_buffer_size);
loop { loop {
tokio::select! { tokio::select! {
@@ -78,7 +78,7 @@ pub async fn run_udp_bridge(
muxer: Muxer, muxer: Muxer,
mut v_rx: mpsc::Receiver<Bytes>, mut v_rx: mpsc::Receiver<Bytes>,
) { ) {
let mut buf = BytesMut::with_capacity(UDP_BUF_SIZE); let mut buf = BytesMut::with_capacity(NetworkConfig::global().udp_buffer_size);
loop { loop {
tokio::select! { tokio::select! {
@@ -1,5 +1,5 @@
use crate::{ use crate::{
protocol::{ nrxp::{
codec::{ codec::{
codec::Codec, codec::Codec,
frame::FrameType, frame::FrameType,
@@ -8,9 +8,11 @@ use crate::{
errors::ErrorAction, errors::ErrorAction,
parser::parser::Parser, parser::parser::Parser,
}, },
proxy::connection::{ net::{
connection::{
bridge::run_tcp_bridge, engine::TunnelEngine, handler::StreamHandler, muxer::Muxer, bridge::run_tcp_bridge, engine::TunnelEngine, handler::StreamHandler, muxer::Muxer,
MESSAGE_CHANNEL_SIZE, TCP_BUF_SIZE, },
network::NetworkConfig,
}, },
tlseng::profile::BrowserProfile, tlseng::profile::BrowserProfile,
}; };
@@ -50,7 +52,7 @@ impl Connection {
Self { Self {
inbound, inbound,
outbound, outbound,
read_buf: BytesMut::with_capacity(TCP_BUF_SIZE), read_buf: BytesMut::with_capacity(NetworkConfig::global().tcp_buffer_size),
codec: Codec::new(init), codec: Codec::new(init),
} }
} }
@@ -59,7 +61,7 @@ impl Connection {
Self { Self {
inbound, inbound,
outbound, outbound,
read_buf: BytesMut::with_capacity(TCP_BUF_SIZE), read_buf: BytesMut::with_capacity(NetworkConfig::global().tcp_buffer_size),
codec: Codec::new(false), codec: Codec::new(false),
} }
} }
@@ -132,8 +134,8 @@ impl ClientHandler {
} }
} }
let (control_tx, control_rx) = mpsc::channel(MESSAGE_CHANNEL_SIZE * 4); let (control_tx, control_rx) = mpsc::channel(NetworkConfig::global().channel_capacity);
let (data_tx, data_rx) = mpsc::channel(MESSAGE_CHANNEL_SIZE * 4); let (data_tx, data_rx) = mpsc::channel(NetworkConfig::global().channel_capacity);
let muxer = Muxer::new(control_tx, data_tx, true); let muxer = Muxer::new(control_tx, data_tx, true);
@@ -199,7 +201,8 @@ impl TunnelHandler for ClientHandler {
target, target,
} => { } => {
let stream_id = self.muxer.next_id(); let stream_id = self.muxer.next_id();
let (v_tx, mut v_rx) = mpsc::channel::<bytes::Bytes>(TCP_BUF_SIZE); let (v_tx, mut v_rx) =
mpsc::channel::<bytes::Bytes>(NetworkConfig::global().tcp_buffer_size);
self.muxer.register_stream(stream_id, v_tx); self.muxer.register_stream(stream_id, v_tx);
self.muxer self.muxer
@@ -304,8 +307,8 @@ impl TunnelHandler for ServerHandler {
async fn run(mut self) -> Result<(), String> { async fn run(mut self) -> Result<(), String> {
info!("Acting as TLS Server with Stealth Fallback"); info!("Acting as TLS Server with Stealth Fallback");
let (control_tx, control_rx) = mpsc::channel(MESSAGE_CHANNEL_SIZE); let (control_tx, control_rx) = mpsc::channel(NetworkConfig::global().channel_capacity);
let (data_tx, data_rx) = mpsc::channel(MESSAGE_CHANNEL_SIZE); let (data_tx, data_rx) = mpsc::channel(NetworkConfig::global().channel_capacity);
let muxer = Muxer::new(control_tx, data_tx, false); let muxer = Muxer::new(control_tx, data_tx, false);
let handshake_timeout = std::time::Duration::from_secs(1); let handshake_timeout = std::time::Duration::from_secs(1);
@@ -10,11 +10,11 @@ use tokio::{
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use crate::{ use crate::{
protocol::{ net::connection::{handler::StreamHandler, muxer::MuxMessage},
nrxp::{
codec::{codec::Codec, frame::FrameType}, codec::{codec::Codec, frame::FrameType},
errors::ErrorAction, errors::ErrorAction,
}, },
proxy::connection::{handler::StreamHandler, muxer::MuxMessage},
}; };
pub struct TunnelEngine { pub struct TunnelEngine {
@@ -2,15 +2,18 @@ use bytes::{Bytes, BytesMut};
use netrunner_logger::{debug, error, info}; use netrunner_logger::{debug, error, info};
use crate::{ use crate::{
protocol::codec::{ net::{
frame::{Frame, FrameType}, connection::{
socks::SocksReply,
},
proxy::connection::{
bridge::{run_tcp_bridge, run_udp_bridge}, bridge::{run_tcp_bridge, run_udp_bridge},
connection::ConnectionRole, connection::ConnectionRole,
muxer::Muxer, muxer::Muxer,
}, },
network::NetworkConfig,
},
nrxp::codec::{
frame::{Frame, FrameType},
socks::SocksReply,
},
}; };
pub struct StreamHandler { pub struct StreamHandler {
@@ -41,7 +44,7 @@ impl StreamHandler {
let target_str = String::from_utf8_lossy(&payload).to_string(); let target_str = String::from_utf8_lossy(&payload).to_string();
let muxer = self.muxer.clone(); let muxer = self.muxer.clone();
let (v_tx, v_rx) = tokio::sync::mpsc::channel(512); let (v_tx, v_rx) = tokio::sync::mpsc::channel(NetworkConfig::global().channel_capacity);
muxer.register_stream(stream_id, v_tx); muxer.register_stream(stream_id, v_tx);
tokio::spawn(async move { tokio::spawn(async move {
@@ -95,7 +98,7 @@ impl StreamHandler {
let target_str = String::from_utf8_lossy(&payload).to_string(); let target_str = String::from_utf8_lossy(&payload).to_string();
let muxer = self.muxer.clone(); let muxer = self.muxer.clone();
let (v_tx, v_rx) = tokio::sync::mpsc::channel(512); let (v_tx, v_rx) = tokio::sync::mpsc::channel(NetworkConfig::global().channel_capacity);
muxer.register_stream(stream_id, v_tx); muxer.register_stream(stream_id, v_tx);
tokio::spawn(async move { tokio::spawn(async move {
@@ -4,6 +4,4 @@ pub mod engine;
pub mod handler; pub mod handler;
pub mod muxer; pub mod muxer;
pub const TCP_BUF_SIZE: usize = 1024 * 32;
pub const UDP_BUF_SIZE: usize = 1024 * 8;
pub const MESSAGE_CHANNEL_SIZE: usize = 4; pub const MESSAGE_CHANNEL_SIZE: usize = 4;
@@ -1,4 +1,4 @@
use crate::protocol::codec::frame::FrameType; use crate::nrxp::codec::frame::FrameType;
use bytes::Bytes; use bytes::Bytes;
use dashmap::DashMap; use dashmap::DashMap;
use std::sync::atomic::{AtomicU32, Ordering}; use std::sync::atomic::{AtomicU32, Ordering};
@@ -1,6 +1,8 @@
use std::sync::OnceLock;
use crate::{ use crate::{
protocol::codec::{frame::FRAME_HEADER_SIZE, MAX_PADDING_SIZE}, nrxp::codec::{frame::FRAME_HEADER_SIZE, MAX_PADDING_SIZE},
proxy::connection::connection::{ net::connection::connection::{
ClientHandler, Connection, ConnectionRole, ServerHandler, TunnelHandler, ClientHandler, Connection, ConnectionRole, ServerHandler, TunnelHandler,
}, },
}; };
@@ -32,7 +34,7 @@ impl Network {
pub async fn run(&self, token: CancellationToken) { pub async fn run(&self, token: CancellationToken) {
let addr = format!("{}:{}", self.host, self.port); let addr = format!("{}:{}", self.host, self.port);
NetworkConfig::init_global(1350);
match self.role { match self.role {
ConnectionRole::Client => { ConnectionRole::Client => {
info!("Starting Client mode"); info!("Starting Client mode");
@@ -90,16 +92,14 @@ impl Network {
} }
} }
pub const IP_UDP_OVERHEAD: usize = 28; pub static GLOBAL_NET_CONFIG: OnceLock<NetworkConfig> = OnceLock::new();
pub struct NetworkConfig { pub struct NetworkConfig {
pub mtu: usize, pub mtu: usize,
pub max_wire_frame_size: usize, pub max_wire_frame_size: usize,
pub safe_payload_size: usize, pub safe_payload_size: usize,
pub tcp_rx_buffer_size: usize, pub tcp_buffer_size: usize,
pub tcp_tx_buffer_size: usize, pub udp_buffer_size: usize,
pub udp_rx_buffer_size: usize,
pub udp_tx_buffer_size: usize,
pub channel_capacity: usize, pub channel_capacity: usize,
} }
@@ -132,11 +132,21 @@ impl NetworkConfig {
mtu: system_mtu, mtu: system_mtu,
max_wire_frame_size: max_wire_frame, max_wire_frame_size: max_wire_frame,
safe_payload_size: safe_payload, safe_payload_size: safe_payload,
tcp_rx_buffer_size: tcp_buffer, tcp_buffer_size: tcp_buffer,
tcp_tx_buffer_size: tcp_buffer, udp_buffer_size: udp_buffer,
udp_rx_buffer_size: udp_buffer,
udp_tx_buffer_size: udp_buffer,
channel_capacity: channel_cap, channel_capacity: channel_cap,
} }
} }
pub fn init_global(system_mtu: usize) {
let config = Self::new(system_mtu);
if GLOBAL_NET_CONFIG.set(config).is_err() {
netrunner_logger::warn!("Global network config was already initialized!");
}
}
pub fn global() -> &'static Self {
GLOBAL_NET_CONFIG
.get()
.expect("Global NetworkConfig is not initialized! Call init_global() first.")
}
} }
View File
@@ -1,6 +1,6 @@
use crate::crypto::session::SessionKeys; use crate::crypto::session::SessionKeys;
use crate::protocol::errors::{ErrorAction, ErrorStage, TlsError}; use crate::nrxp::errors::{ErrorAction, ErrorStage, TlsError};
use crate::protocol::parser::parser::Parser; use crate::nrxp::parser::parser::Parser;
use crate::tlseng::extension::ExtensionStack; use crate::tlseng::extension::ExtensionStack;
use crate::tlseng::handshake::{ClientHello, HelloHeader, ServerHello}; use crate::tlseng::handshake::{ClientHello, HelloHeader, ServerHello};
use crate::tlseng::profile::{BrowserProfile, ServerProfile}; use crate::tlseng::profile::{BrowserProfile, ServerProfile};
@@ -3,11 +3,10 @@ use bytes::{Bytes, BytesMut};
use crate::crypto::aead::AeadPacker; use crate::crypto::aead::AeadPacker;
use crate::crypto::chacha::ChaChaCipher; use crate::crypto::chacha::ChaChaCipher;
use crate::crypto::session::SessionKeys; use crate::crypto::session::SessionKeys;
use crate::protocol::codec::bridge::TlsBridge; use crate::nrxp::codec::bridge::TlsBridge;
use crate::protocol::codec::frame::{Frame, FrameHeader, FrameType}; use crate::nrxp::codec::frame::{Frame, FrameHeader, FrameType, Padding};
use crate::protocol::codec::padding::Padding; use crate::nrxp::errors::{ErrorAction, ErrorStage, TlsError};
use crate::protocol::errors::{ErrorAction, ErrorStage, TlsError}; use crate::nrxp::parser::parser::Parser;
use crate::protocol::parser::parser::Parser;
use crate::tlseng::profile::{BrowserProfile, ServerProfile}; use crate::tlseng::profile::{BrowserProfile, ServerProfile};
pub struct Codec { pub struct Codec {
@@ -1,6 +1,27 @@
use bytes::{BufMut, Bytes, BytesMut}; use bytes::{BufMut, Bytes, BytesMut};
use crate::protocol::codec::padding::Padding; use rand::Rng;
use crate::nrxp::codec::MAX_PADDING_SIZE;
pub struct Padding {
pub len: u16,
pub data: Bytes,
}
impl Padding {
pub fn generate_padding() -> Padding {
let mut rng = rand::rng();
let random_u32: u32 = rng.next_u32();
let padding_len: u16 = (random_u32 % MAX_PADDING_SIZE) as u16;
let mut padding = vec![0u8; padding_len as usize];
rng.fill_bytes(&mut padding);
Padding {
len: padding_len,
data: Bytes::from(padding),
}
}
}
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum FrameType { pub enum FrameType {
@@ -1,7 +1,6 @@
mod bridge; mod bridge;
pub mod codec; pub mod codec;
pub mod frame; pub mod frame;
mod padding;
pub mod socks; pub mod socks;
pub const MAX_PADDING_SIZE: u32 = 255; pub const MAX_PADDING_SIZE: u32 = 255;
@@ -2,7 +2,7 @@ use std::fmt;
use bytes::{BufMut, BytesMut}; use bytes::{BufMut, BytesMut};
use crate::protocol::parser::parser::Parser; use crate::nrxp::parser::parser::Parser;
pub const SOCKS5_VERSION: u8 = 0x05; pub const SOCKS5_VERSION: u8 = 0x05;
pub const REPLY_SUCCESS: u8 = 0x00; pub const REPLY_SUCCESS: u8 = 0x00;
@@ -1,6 +1,6 @@
use bytes::{Buf, BytesMut}; use bytes::{Buf, BytesMut};
use crate::protocol::{ use crate::nrxp::{
codec::frame::{Frame, FrameHeader, FrameType, FRAME_HEADER_SIZE}, codec::frame::{Frame, FrameHeader, FrameType, FRAME_HEADER_SIZE},
parser::parser::Parser, parser::parser::Parser,
}; };
@@ -1,6 +1,6 @@
use bytes::{Buf, BytesMut}; use bytes::{Buf, BytesMut};
use crate::protocol::{codec::socks::*, parser::parser::Parser}; use crate::nrxp::{codec::socks::*, parser::parser::Parser};
impl Parser for SocksTarget { impl Parser for SocksTarget {
type Error = String; type Error = String;
@@ -1,5 +1,5 @@
use crate::{ use crate::{
protocol::{ nrxp::{
errors::{ErrorAction, ErrorStage, TlsError}, errors::{ErrorAction, ErrorStage, TlsError},
parser::Parser, parser::Parser,
}, },
-23
View File
@@ -1,23 +0,0 @@
use bytes::Bytes;
use rand::Rng;
use crate::protocol::codec::MAX_PADDING_SIZE;
pub struct Padding {
pub len: u16,
pub data: Bytes,
}
impl Padding {
pub fn generate_padding() -> Padding {
let mut rng = rand::rng();
let random_u32: u32 = rng.next_u32();
let padding_len: u16 = (random_u32 % MAX_PADDING_SIZE) as u16;
let mut padding = vec![0u8; padding_len as usize];
rng.fill_bytes(&mut padding);
Padding {
len: padding_len,
data: Bytes::from(padding),
}
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
use clap::Parser; use clap::Parser;
use netrunner_core::proxy::{connection::connection::ConnectionRole, network::Network}; use netrunner_core::net::{connection::connection::ConnectionRole, network::Network};
use netrunner_logger::Logger; use netrunner_logger::Logger;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]