clean project from comments
This commit is contained in:
@@ -17,12 +17,9 @@ impl NonceState {
|
||||
}
|
||||
}
|
||||
|
||||
// Возвращает nonce для текущего пакета и ПЕРЕХОДИТ к следующему
|
||||
pub fn next_nonce(&mut self) -> Nonce {
|
||||
let mut iv = self.base_iv;
|
||||
// В TLS 1.3 используется Little Endian для счетчика при XOR
|
||||
// Но если ты сам пишешь протокол, BE тоже пойдет.
|
||||
// Главное — единообразие.
|
||||
|
||||
let counter_bytes = self.counter.to_be_bytes();
|
||||
|
||||
for i in 0..8 {
|
||||
@@ -54,13 +51,7 @@ impl ChaChaCipher {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_keys(
|
||||
&mut self,
|
||||
w_key: [u8; 32],
|
||||
w_iv: [u8; 12], // Write (исходящие)
|
||||
r_key: [u8; 32],
|
||||
r_iv: [u8; 12], // Read (входящие)
|
||||
) {
|
||||
pub fn set_keys(&mut self, w_key: [u8; 32], w_iv: [u8; 12], r_key: [u8; 32], r_iv: [u8; 12]) {
|
||||
self.encrypt_cipher = ChaCha20Poly1305::new(Key::from_slice(&w_key));
|
||||
self.decrypt_cipher = ChaCha20Poly1305::new(Key::from_slice(&r_key));
|
||||
|
||||
@@ -72,11 +63,10 @@ impl ChaChaCipher {
|
||||
}
|
||||
impl AeadPacker for ChaChaCipher {
|
||||
fn encrypt(&mut self, data: &mut BytesMut) -> Result<Bytes, chacha20poly1305::aead::Error> {
|
||||
// Сначала получаем текущий counter для лога (до того, как next_nonce его инкрементирует)
|
||||
let current_counter = self.encrypt_state.counter;
|
||||
let nonce = self.encrypt_state.next_nonce();
|
||||
let data_len = data.len();
|
||||
// maybe ad should be stream id
|
||||
|
||||
match self.encrypt_cipher.encrypt_in_place(&nonce, &nonce, data) {
|
||||
Ok(_) => {
|
||||
tracing::trace!(
|
||||
@@ -104,7 +94,7 @@ impl AeadPacker for ChaChaCipher {
|
||||
let current_counter = self.decrypt_state.counter;
|
||||
let nonce = self.decrypt_state.next_nonce();
|
||||
let data_len = data.len();
|
||||
// maybe ad should be stream id
|
||||
|
||||
match self.decrypt_cipher.decrypt_in_place(&nonce, &nonce, data) {
|
||||
Ok(_) => {
|
||||
tracing::trace!(
|
||||
|
||||
@@ -70,9 +70,8 @@ impl SessionKeys {
|
||||
&mut self,
|
||||
salt: [u8; 32],
|
||||
extensions: &ExtensionStack,
|
||||
is_server: bool, // true если мы Сервер (парсим ClientHello), false если Клиент
|
||||
is_server: bool,
|
||||
) -> Result<([u8; 32], [u8; 12], [u8; 32], [u8; 12]), String> {
|
||||
// Сохраняем соль от удаленной стороны
|
||||
self.salt.set_remote_salt(salt);
|
||||
|
||||
tracing::debug!(
|
||||
@@ -88,8 +87,6 @@ impl SessionKeys {
|
||||
let mut key_bytes = [0u8; 32];
|
||||
|
||||
if is_server {
|
||||
// МЫ СЕРВЕР: Парсим ClientHello KeyShare (там список)
|
||||
// Минимум: 2 (длина списка) + 2 (группа) + 2 (длина ключа) + 32 (ключ) = 38
|
||||
if dh_data.len() < 38 {
|
||||
return Err(format!(
|
||||
"Client KeyShare too short: {} bytes",
|
||||
@@ -98,7 +95,7 @@ impl SessionKeys {
|
||||
}
|
||||
|
||||
let mut found = false;
|
||||
// Ищем маркер x25519 (00 1d) и длину 32 (00 20)
|
||||
|
||||
for i in 2..=(dh_data.len() - 34) {
|
||||
if dh_data[i..i + 4] == [0x00, 0x1d, 0x00, 0x20] {
|
||||
key_bytes.copy_from_slice(&dh_data[i + 4..i + 36]);
|
||||
@@ -111,15 +108,12 @@ impl SessionKeys {
|
||||
return Err("Could not find x25519 key in ClientHello".into());
|
||||
}
|
||||
} else {
|
||||
// МЫ КЛИЕНТ: Парсим ServerHello KeyShare (там сразу группа и ключ)
|
||||
// [Group:2] [KeyLen:2] [Key:32] = 36 байт
|
||||
if dh_data.len() < 36 {
|
||||
return Err("Server KeyShare too short".into());
|
||||
}
|
||||
key_bytes.copy_from_slice(&dh_data[4..36]);
|
||||
}
|
||||
|
||||
// Проверка на "кривой" ключ
|
||||
if key_bytes.iter().all(|&x| x == 0) {
|
||||
return Err("Extracted remote public key is all ZEROS!".into());
|
||||
}
|
||||
@@ -132,7 +126,6 @@ impl SessionKeys {
|
||||
"Key exchange successful, deriving material..."
|
||||
);
|
||||
|
||||
// Вызываем генерацию, которая вернет (w_key, w_iv, r_key, r_iv)
|
||||
self.generate_keys(&public_key, is_server)
|
||||
} else {
|
||||
Err("No KeyShare extension found in handshake".into())
|
||||
@@ -168,11 +161,10 @@ impl SessionKeys {
|
||||
"HKDF expansion complete"
|
||||
);
|
||||
|
||||
// Распределяем: (write_key, write_iv, read_key, read_iv)
|
||||
if is_server {
|
||||
Ok((s_key, s_iv, c_key, c_iv)) // Сервер пишет своим, читает клиентским
|
||||
Ok((s_key, s_iv, c_key, c_iv))
|
||||
} else {
|
||||
Ok((c_key, c_iv, s_key, s_iv)) // Клиент пишет своим, читает серверным
|
||||
Ok((c_key, c_iv, s_key, s_iv))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,7 +183,6 @@ impl SessionKeys {
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
|
||||
// Генерируем на основе текущей минуты
|
||||
Self::compute_tag(&self.auth_key, now / 60)
|
||||
}
|
||||
|
||||
@@ -203,11 +194,8 @@ impl SessionKeys {
|
||||
|
||||
let current_step = now / 60;
|
||||
|
||||
// Вставляем цикл проверки расширенного окна [-2, +2]
|
||||
// Это дает запас по времени в обе стороны
|
||||
for step in (current_step.saturating_sub(2))..=(current_step.saturating_add(2)) {
|
||||
if &Self::compute_tag(&self.auth_key, step) == received_tag {
|
||||
// Если подошел не текущий, а другой шаг — логируем это для диагностики
|
||||
if step != current_step {
|
||||
tracing::debug!(expected = %current_step, matched = %step, "Auth tag valid with time offset");
|
||||
}
|
||||
@@ -215,7 +203,6 @@ impl SessionKeys {
|
||||
}
|
||||
}
|
||||
|
||||
// Если ни один не подошел — логируем для отладки
|
||||
tracing::warn!(
|
||||
current_step = %current_step,
|
||||
"AUTH MISMATCH: All tags rejected for current window"
|
||||
|
||||
@@ -5,17 +5,13 @@ pub fn logger_init() {
|
||||
eprintln!("--- [DEBUG] logger_init start ---");
|
||||
let _ = LogTracer::init();
|
||||
|
||||
// 4. Фильтр (создаем его один раз для всех)
|
||||
let filter_layer =
|
||||
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("trace"));
|
||||
|
||||
// Инициализируем базовый реестр
|
||||
let registry = tracing_subscriber::registry().with(filter_layer);
|
||||
|
||||
// 2 & 5. Собираем реестр с учетом платформы
|
||||
#[cfg(target_os = "android")]
|
||||
{
|
||||
// Слой для Android Logcat
|
||||
let android_layer =
|
||||
tracing_android::layer("NETRUNNER_RUST").expect("Failed to create android layer");
|
||||
|
||||
@@ -27,7 +23,6 @@ pub fn logger_init() {
|
||||
|
||||
#[cfg(not(target_os = "android"))]
|
||||
{
|
||||
// 3. Слой для консоли (только для Linux/десктопа)
|
||||
let fmt_layer = fmt::layer()
|
||||
.with_target(true)
|
||||
.with_line_number(true)
|
||||
|
||||
@@ -8,7 +8,6 @@ use crate::tlseng::types::{ContentType, HelloType};
|
||||
use crate::tlseng::ApplicationData;
|
||||
use bytes::{Bytes, BytesMut};
|
||||
|
||||
// --- 1. Общий интерфейс перехвата ---
|
||||
pub trait TlsInterceptor {
|
||||
type Output;
|
||||
|
||||
@@ -23,7 +22,6 @@ pub trait TlsInterceptor {
|
||||
fn handle_record(record: TlsRecord) -> Result<Option<Self::Output>, TlsError>;
|
||||
}
|
||||
|
||||
// --- 2. Обработка Handshake ---
|
||||
pub enum HandshakeMessage {
|
||||
Client {
|
||||
base: ClientHello,
|
||||
@@ -106,7 +104,6 @@ impl TlsInterceptor for HandshakeMessage {
|
||||
}
|
||||
}
|
||||
|
||||
// --- 3. Обработка Application Data ---
|
||||
impl TlsInterceptor for ApplicationData {
|
||||
type Output = ApplicationData;
|
||||
|
||||
@@ -125,12 +122,9 @@ impl TlsInterceptor for ApplicationData {
|
||||
}
|
||||
}
|
||||
|
||||
// --- 4. Высокоуровневый Bridge API ---
|
||||
pub struct TlsBridge;
|
||||
|
||||
impl TlsBridge {
|
||||
// --- Распаковка (уже была) ---
|
||||
|
||||
pub fn unpack_handshake(buffer: &mut BytesMut) -> Result<Option<HandshakeMessage>, TlsError> {
|
||||
HandshakeMessage::start_process(buffer)
|
||||
}
|
||||
@@ -139,19 +133,15 @@ impl TlsBridge {
|
||||
ApplicationData::start_process(buffer)
|
||||
}
|
||||
|
||||
// --- Запаковка (новое) ---
|
||||
|
||||
/// Создает полный TLS Record с ClientHello внутри
|
||||
pub fn wrap_client_hello(
|
||||
profile: &BrowserProfile,
|
||||
host: &str,
|
||||
public_key: &[u8; 32],
|
||||
salt: [u8; 32],
|
||||
) -> Bytes {
|
||||
ClientHello::make_client_hello(profile, host, public_key, salt) // Передаем ключ дальше
|
||||
ClientHello::make_client_hello(profile, host, public_key, salt)
|
||||
}
|
||||
|
||||
/// Создает полный TLS Record с ServerHello, базируясь на данных из HandshakeMessage::Client
|
||||
pub fn wrap_server_hello(
|
||||
client_msg: &HandshakeMessage,
|
||||
server_pub_key: &[u8],
|
||||
|
||||
@@ -32,7 +32,6 @@ impl Codec {
|
||||
) -> Result<Bytes, TlsError> {
|
||||
let pub_key = self.session_keys.ecdh.public_key.to_bytes();
|
||||
|
||||
// 2. Передаем его в мост
|
||||
Ok(TlsBridge::wrap_client_hello(
|
||||
profile,
|
||||
host,
|
||||
@@ -69,7 +68,6 @@ impl Codec {
|
||||
)
|
||||
})?;
|
||||
|
||||
// Инициализируем шифратор сервера
|
||||
self.crypto.set_keys(w_key, w_iv, r_key, r_iv);
|
||||
|
||||
Ok(server_hello_record)
|
||||
@@ -85,8 +83,6 @@ impl Codec {
|
||||
)
|
||||
})?;
|
||||
|
||||
// ОБНОВЛЕНИЕ КЛЮЧЕЙ НА КЛИЕНТЕ
|
||||
// Передаем false, так как клиент ПАРСИТ ServerHello (смещение 4 байта)
|
||||
let (w_key, w_iv, r_key, r_iv) = self
|
||||
.session_keys
|
||||
.update_keys(mes.random(), mes.extensions(), false)
|
||||
@@ -142,9 +138,6 @@ impl Codec {
|
||||
};
|
||||
let mut frame_bytes = frame.into_bytes(&tag);
|
||||
|
||||
// ВАЖНО: вызываем шифрование ОДИН РАЗ.
|
||||
// Метод encrypt возвращает Result<Bytes, chacha20poly1305::Error>
|
||||
// Мы вручную превращаем его ошибку в твой TlsError.
|
||||
let encrypted_payload = self.crypto.encrypt(&mut frame_bytes).map_err(|e| {
|
||||
tracing::error!("Encryption failed: {:?}", e);
|
||||
TlsError::new(
|
||||
@@ -154,7 +147,6 @@ impl Codec {
|
||||
)
|
||||
})?;
|
||||
|
||||
// Теперь передаем зашифрованные байты в новый метод упаковки
|
||||
Ok(TlsBridge::pack_app_data(encrypted_payload))
|
||||
}
|
||||
|
||||
@@ -168,14 +160,12 @@ impl Codec {
|
||||
}
|
||||
|
||||
pub fn inbound(&mut self, buffer: &mut BytesMut) -> Result<Option<Frame>, TlsError> {
|
||||
// 1. Проверка старых данных
|
||||
if !self.staging.is_empty() {
|
||||
if let Some(frame) = self.try_parse_frame()? {
|
||||
return Ok(Some(frame));
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Цикл обработки новых рекордов
|
||||
while let Some(app_data) = TlsBridge::unpack_app_data(buffer)? {
|
||||
let mut data_to_decrypt = BytesMut::from(app_data.payload);
|
||||
|
||||
@@ -187,7 +177,6 @@ impl Codec {
|
||||
)
|
||||
})?;
|
||||
|
||||
// --- КРИТИЧЕСКАЯ ПРОВЕРКА ТЕГА ---
|
||||
if decrypted.len() < 16 {
|
||||
return Err(TlsError::new(
|
||||
ErrorStage::Tls("Packet too short for auth"),
|
||||
@@ -199,7 +188,6 @@ impl Codec {
|
||||
let mut received_tag = [0u8; 16];
|
||||
received_tag.copy_from_slice(&decrypted[..16]);
|
||||
|
||||
// Используем метод verify_auth_tag, который мы обсуждали ранее
|
||||
if !self.session_keys.verify_auth_tag(&received_tag) {
|
||||
tracing::error!(
|
||||
expected_hash = %hex::encode(&self.session_keys.auth_key[..4]),
|
||||
@@ -208,11 +196,10 @@ impl Codec {
|
||||
);
|
||||
return Err(TlsError::new(
|
||||
ErrorStage::Tls("Auth tag mismatch"),
|
||||
ErrorAction::Drop, // Убиваем соединение
|
||||
ErrorAction::Drop,
|
||||
Bytes::new(),
|
||||
));
|
||||
}
|
||||
// ---------------------------------
|
||||
|
||||
self.staging.extend_from_slice(&decrypted);
|
||||
|
||||
@@ -223,7 +210,7 @@ impl Codec {
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
// Выносим парсинг в отдельный метод, чтобы не дублировать код
|
||||
|
||||
fn try_parse_frame(&mut self) -> Result<Option<Frame>, TlsError> {
|
||||
match Frame::parse(&mut self.staging) {
|
||||
Ok(Some(frame)) => Ok(Some(frame)),
|
||||
|
||||
@@ -27,9 +27,7 @@ impl SocksRequest {
|
||||
where
|
||||
S: tokio::io::AsyncReadExt + tokio::io::AsyncWriteExt + Unpin,
|
||||
{
|
||||
// 1. Handshake Phase
|
||||
loop {
|
||||
// Используем трейт Parser
|
||||
if let Some(req) = Self::parse(buf)? {
|
||||
if let SocksRequest::Handshake { .. } = req {
|
||||
let mut reply = BytesMut::with_capacity(2);
|
||||
@@ -44,11 +42,9 @@ impl SocksRequest {
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Connect Request Phase
|
||||
loop {
|
||||
if let Some(req) = Self::parse(buf)? {
|
||||
if let SocksRequest::Connect { command, target } = req {
|
||||
// Проверяем, что это именно CONNECT (0x01)
|
||||
if command != 0x01 {
|
||||
return Err(format!("Unsupported SOCKS command: 0x{:02X}", command));
|
||||
}
|
||||
@@ -68,14 +64,12 @@ impl SocksRequest {
|
||||
where
|
||||
S: tokio::io::AsyncReadExt + tokio::io::AsyncWriteExt + Unpin,
|
||||
{
|
||||
// 1. Отправляем Greeting (SOCKS5, 1 метод: No Auth)
|
||||
let greeting = [SOCKS5_VERSION, 0x01, 0x00];
|
||||
stream
|
||||
.write_all(&greeting)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
// 2. Читаем выбор метода (должно быть 0x05 0x00)
|
||||
let mut method_selection = [0u8; 2];
|
||||
stream
|
||||
.read_exact(&mut method_selection)
|
||||
@@ -89,11 +83,10 @@ impl SocksRequest {
|
||||
));
|
||||
}
|
||||
|
||||
// 3. Формируем CONNECT запрос
|
||||
let mut connect_req = BytesMut::with_capacity(32);
|
||||
connect_req.put_u8(SOCKS5_VERSION);
|
||||
connect_req.put_u8(0x01); // CMD: Connect
|
||||
connect_req.put_u8(0x00); // RSV
|
||||
connect_req.put_u8(0x01);
|
||||
connect_req.put_u8(0x00);
|
||||
|
||||
match target_addr {
|
||||
TargetAddress::Ipv4(ip, port) => {
|
||||
@@ -108,7 +101,7 @@ impl SocksRequest {
|
||||
}
|
||||
TargetAddress::Domain(host, port) => {
|
||||
connect_req.put_u8(ATYP_DOMAIN);
|
||||
// SOCKS5 для домена требует: [1 байт длина] + [строка]
|
||||
|
||||
let host_bytes = host.as_bytes();
|
||||
connect_req.put_u8(host_bytes.len() as u8);
|
||||
connect_req.put_slice(host_bytes);
|
||||
@@ -121,8 +114,6 @@ impl SocksRequest {
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
// 4. Читаем ответ на Connect (REP)
|
||||
// Нам нужно как минимум 4 байта, чтобы узнать статус (REPLY_SUCCESS)
|
||||
let mut reply_header = [0u8; 4];
|
||||
stream
|
||||
.read_exact(&mut reply_header)
|
||||
@@ -136,8 +127,6 @@ impl SocksRequest {
|
||||
));
|
||||
}
|
||||
|
||||
// Дочитываем оставшуюся часть адреса в ответе (BND.ADDR + BND.PORT),
|
||||
// чтобы очистить поток перед передачей данных.
|
||||
let atyp = reply_header[3];
|
||||
let remain_len = match atyp {
|
||||
ATYP_IPV4 => IPV4_SIZE + PORT_SIZE,
|
||||
@@ -174,9 +163,9 @@ pub enum SocksReply {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum TargetAddress {
|
||||
Ipv4(std::net::Ipv4Addr, u16), // Теперь 2 поля
|
||||
Domain(String, u16), // Теперь 2 поля
|
||||
Ipv6(std::net::Ipv6Addr, u16), // Теперь 2 поля
|
||||
Ipv4(std::net::Ipv4Addr, u16),
|
||||
Domain(String, u16),
|
||||
Ipv6(std::net::Ipv6Addr, u16),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -199,7 +188,7 @@ impl SocksReply {
|
||||
} => {
|
||||
buf.put_u8(SOCKS5_VERSION);
|
||||
buf.put_u8(reply_code);
|
||||
buf.put_u8(0x00); // Reserved
|
||||
buf.put_u8(0x00);
|
||||
buf.put_u8(atyp);
|
||||
buf.put_slice(&addr);
|
||||
buf.put_u16(port);
|
||||
@@ -211,18 +200,15 @@ impl SocksReply {
|
||||
impl SocksTarget {
|
||||
pub fn to_string(&self) -> String {
|
||||
match &self.addr {
|
||||
// Теперь в каждом варианте TargetAddress уже есть порт (port)
|
||||
TargetAddress::Ipv4(ip, port) => {
|
||||
format!("{}:{}", ip, port)
|
||||
}
|
||||
|
||||
TargetAddress::Ipv6(ip, port) => {
|
||||
// IPv6 адреса принято заключать в квадратные скобки при наличии порта
|
||||
format!("[{}]:{}", ip, port)
|
||||
}
|
||||
|
||||
TargetAddress::Domain(domain, port) => {
|
||||
// Вычищаем нулевые байты, если они случайно попали в строку
|
||||
let clean_domain = domain.replace('\0', "");
|
||||
format!("{}:{}", clean_domain, port)
|
||||
}
|
||||
|
||||
@@ -32,10 +32,6 @@ impl TlsError {
|
||||
}
|
||||
|
||||
fn log_error(&self) {
|
||||
// Определяем уровень логирования в зависимости от действия
|
||||
// Если мы просто ждем данные (Wait) — это не ошибка, а рабочий процесс (debug/trace)
|
||||
// Если дропаем соединение (Drop) — это серьезно (error)
|
||||
|
||||
let stage_name = match &self.stage {
|
||||
ErrorStage::Tls(_) => "TLS",
|
||||
ErrorStage::Handshake(_) => "Handshake",
|
||||
@@ -46,7 +42,6 @@ impl TlsError {
|
||||
ErrorStage::Tls(m) | ErrorStage::Handshake(m) | ErrorStage::ApplicationData(m) => m,
|
||||
};
|
||||
|
||||
// Подготавливаем превью данных (первые 8 байт в хексе)
|
||||
let data_preview = if !self.data.is_empty() {
|
||||
let limit = self.data.len().min(8);
|
||||
format!(
|
||||
@@ -60,7 +55,6 @@ impl TlsError {
|
||||
|
||||
match self.action {
|
||||
ErrorAction::Wait => {
|
||||
// Wait — это нормальное состояние асинхронного чтения
|
||||
trace!(
|
||||
stage = stage_name,
|
||||
action = ?self.action,
|
||||
|
||||
@@ -50,13 +50,10 @@ impl Parser for Frame {
|
||||
type Error = String;
|
||||
|
||||
fn can_parse(bytes: &BytesMut) -> bool {
|
||||
// 1. Сначала проверяем, есть ли хотя бы заголовок
|
||||
if bytes.len() < FRAME_HEADER_SIZE as usize {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. Извлекаем длины из заголовка (БЕЗ удаления байтов из буфера)
|
||||
// По твоей структуре: Auth(16) + Stream(4) + Type(1) = 21 байт смещения
|
||||
let p_len = u16::from_be_bytes([bytes[21], bytes[22]]) as usize;
|
||||
let pad_len = u16::from_be_bytes([bytes[23], bytes[24]]) as usize;
|
||||
|
||||
@@ -68,7 +65,6 @@ impl Parser for Frame {
|
||||
bytes.len()
|
||||
);
|
||||
|
||||
// 3. Проверяем, есть ли в буфере весь фрейм целиком
|
||||
bytes.len() >= (FRAME_HEADER_SIZE as usize + p_len + pad_len)
|
||||
}
|
||||
|
||||
@@ -77,13 +73,11 @@ impl Parser for Frame {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// Извлекаем заголовок (теперь split_to удалит эти байты из начала bytes)
|
||||
let header = FrameHeader::parse(bytes)?.ok_or("Failed to parse header")?;
|
||||
|
||||
let p_len = header.payload_len as usize;
|
||||
let pad_len = header.padding_len as usize;
|
||||
|
||||
// Теперь байты заголовка уже удалены, и в начале 'bytes' лежит Payload
|
||||
if bytes.len() < p_len + pad_len {
|
||||
return Err("Buffer corrupted: length mismatch after header parse".into());
|
||||
}
|
||||
|
||||
@@ -6,18 +6,16 @@ impl Parser for SocksTarget {
|
||||
type Error = String;
|
||||
|
||||
fn can_parse(bytes: &BytesMut) -> bool {
|
||||
// Минимальная длина SOCKS5 CONNECT (VER, CMD, RSV, ATYP) = 4 байта
|
||||
if bytes.len() < 4 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let atyp = bytes[3];
|
||||
match atyp {
|
||||
// IPv4: 4 байта IP + 2 байта порт
|
||||
ATYP_IPV4 => bytes.len() >= 4 + 4 + 2,
|
||||
// IPv6: 16 байт IP + 2 байта порт
|
||||
|
||||
ATYP_IPV6 => bytes.len() >= 4 + 16 + 2,
|
||||
// Domain: 1 байт длины + N байт домена + 2 байта порт
|
||||
|
||||
ATYP_DOMAIN => {
|
||||
if bytes.len() < 5 {
|
||||
return false;
|
||||
@@ -35,7 +33,7 @@ impl Parser for SocksTarget {
|
||||
}
|
||||
|
||||
let atyp = bytes[3];
|
||||
// Вычисляем длину (включая ATYP и порт)
|
||||
|
||||
let total_len = match atyp {
|
||||
ATYP_IPV4 => SOCKS5_MIN_HEADER + IPV4_SIZE + PORT_SIZE,
|
||||
ATYP_DOMAIN => SOCKS5_MIN_HEADER + 1 + (bytes[4] as usize) + PORT_SIZE,
|
||||
@@ -44,12 +42,12 @@ impl Parser for SocksTarget {
|
||||
};
|
||||
|
||||
let mut packet = bytes.split_to(total_len);
|
||||
packet.advance(4); // Пропускаем [VER, CMD, RSV, ATYP]
|
||||
packet.advance(4);
|
||||
|
||||
let addr = match atyp {
|
||||
ATYP_IPV4 => {
|
||||
let octets: [u8; 4] = packet.split_to(4)[..].try_into().unwrap();
|
||||
let port = packet.get_u16(); // Достаем порт сразу
|
||||
let port = packet.get_u16();
|
||||
TargetAddress::Ipv4(octets.into(), port)
|
||||
}
|
||||
ATYP_DOMAIN => {
|
||||
@@ -57,18 +55,17 @@ impl Parser for SocksTarget {
|
||||
let domain_bytes = packet.split_to(len);
|
||||
let domain = String::from_utf8(domain_bytes.to_vec())
|
||||
.map_err(|_| "Invalid UTF-8 domain".to_string())?;
|
||||
let port = packet.get_u16(); // Достаем порт сразу
|
||||
let port = packet.get_u16();
|
||||
TargetAddress::Domain(domain, port)
|
||||
}
|
||||
ATYP_IPV6 => {
|
||||
let octets: [u8; 16] = packet.split_to(16)[..].try_into().unwrap();
|
||||
let port = packet.get_u16(); // Достаем порт сразу
|
||||
let port = packet.get_u16();
|
||||
TargetAddress::Ipv6(octets.into(), port)
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
// Теперь SocksTarget — это просто оболочка над новым TargetAddress
|
||||
Ok(Some(SocksTarget { addr }))
|
||||
}
|
||||
}
|
||||
@@ -83,12 +80,10 @@ impl Parser for SocksRequest {
|
||||
|
||||
let nmethods = bytes[1] as usize;
|
||||
if bytes.len() >= 2 + nmethods {
|
||||
// Это может быть Handshake. Проверяем, не Connect ли это (мин. 6-10 байт)
|
||||
if bytes.len() >= SOCKS5_MIN_HEADER && SocksTarget::can_parse(bytes) {
|
||||
return true;
|
||||
}
|
||||
// Если для Connect данных мало или структура не совпадает,
|
||||
// но для Handshake достаточно — ок.
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -100,7 +95,6 @@ impl Parser for SocksRequest {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// 1. Пытаемся распарсить как Connect (у него строгая структура)
|
||||
if bytes.len() >= SOCKS5_MIN_HEADER && SocksTarget::can_parse(bytes) {
|
||||
let command = bytes[1];
|
||||
if let Some(target) = SocksTarget::parse(bytes)? {
|
||||
@@ -108,7 +102,6 @@ impl Parser for SocksRequest {
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Если не Connect, пробуем Handshake
|
||||
let nmethods = bytes[1] as usize;
|
||||
let total_handshake = 2 + nmethods;
|
||||
|
||||
|
||||
@@ -14,20 +14,14 @@ use crate::{
|
||||
};
|
||||
use bytes::{Buf, Bytes, BytesMut};
|
||||
|
||||
// =================================================================
|
||||
// 1. RECORD LAYER
|
||||
// =================================================================
|
||||
|
||||
impl Parser for TlsRecord {
|
||||
type Error = TlsError;
|
||||
|
||||
fn can_parse(bytes: &BytesMut) -> bool {
|
||||
// 1. Минимум 5 байт для заголовка
|
||||
if bytes.len() < 5 {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. Проверяем ContentType
|
||||
let content_type = bytes[0];
|
||||
let is_valid_type = content_type == ContentType::Handshake as u8
|
||||
|| content_type == ContentType::ApplicationData as u8
|
||||
@@ -37,21 +31,14 @@ impl Parser for TlsRecord {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 3. Извлекаем заявленную длину тела рекорда
|
||||
let record_len = u16::from_be_bytes([bytes[3], bytes[4]]) as usize;
|
||||
|
||||
// 4. Специфика TLS 1.3:
|
||||
// Если это зашифрованные данные (0x17), они ДОЛЖНЫ содержать тег (16 байт)
|
||||
// + как минимум 1 байт зашифрованного типа контента.
|
||||
if content_type == ContentType::ApplicationData as u8 {
|
||||
if record_len < 17 {
|
||||
// Если длина < 17, это либо неполный пакет, либо ошибка протокола.
|
||||
// Возвращаем false, чтобы подождать еще данных из сокета.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Ждем, пока в буфере будет заголовок + всё тело
|
||||
bytes.len() >= 5 + record_len
|
||||
}
|
||||
|
||||
@@ -60,7 +47,6 @@ impl Parser for TlsRecord {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// --- ТОЛЬКО ТЕПЕРЬ МЫ ИЗМЕНЯЕМ БУФЕР ---
|
||||
let raw_content_type = bytes.get_u8();
|
||||
let raw_version = bytes.get_u16();
|
||||
let record_len = bytes.get_u16() as usize;
|
||||
@@ -71,17 +57,12 @@ impl Parser for TlsRecord {
|
||||
let version = ProtocolVersion::try_from(raw_version)
|
||||
.map_err(|e| TlsError::new(ErrorStage::Tls(e), ErrorAction::Drop, Bytes::new()))?;
|
||||
|
||||
// Забираем ровно столько, сколько указано в заголовке
|
||||
let payload = bytes.split_to(record_len).freeze();
|
||||
|
||||
Ok(Some(TlsRecord::new(content_type, version, payload)))
|
||||
}
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// 2. PAYLOAD TYPES
|
||||
// =================================================================
|
||||
|
||||
impl Parser for ApplicationData {
|
||||
type Error = TlsError;
|
||||
|
||||
@@ -128,15 +109,11 @@ impl Parser for HelloHeader {
|
||||
}
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// 3. HELLO MESSAGES
|
||||
// =================================================================
|
||||
|
||||
impl Parser for ClientHello {
|
||||
type Error = TlsError;
|
||||
|
||||
fn can_parse(bytes: &BytesMut) -> bool {
|
||||
let mut offset = 34; // ProtocolVersion (2) + Random (32)
|
||||
let mut offset = 34;
|
||||
if bytes.len() < offset + 1 {
|
||||
return false;
|
||||
}
|
||||
@@ -165,8 +142,7 @@ impl Parser for ClientHello {
|
||||
}
|
||||
|
||||
fn parse(bytes: &mut BytesMut) -> Result<Option<Self>, Self::Error> {
|
||||
// --- ШАГ 1: Атомарная проверка всего пакета ---
|
||||
let mut offset = 34; // Version + Random
|
||||
let mut offset = 34;
|
||||
if bytes.len() < offset + 1 {
|
||||
return Ok(None);
|
||||
}
|
||||
@@ -190,13 +166,10 @@ impl Parser for ClientHello {
|
||||
offset += 2 + ext_len;
|
||||
}
|
||||
|
||||
// Если нам не хватает данных для полного ClientHello, выходим, не трогая буфер
|
||||
if bytes.len() < offset {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// --- ШАГ 2: Безопасное чтение ---
|
||||
// Изолируем ровно тот кусок, который проверили.
|
||||
let mut msg = bytes.split_to(offset);
|
||||
|
||||
let version = ProtocolVersion::try_from(msg.get_u16())
|
||||
@@ -216,7 +189,7 @@ impl Parser for ClientHello {
|
||||
}
|
||||
|
||||
let cmp_len = msg.get_u8() as usize;
|
||||
msg.advance(cmp_len); // пропускаем методы сжатия
|
||||
msg.advance(cmp_len);
|
||||
|
||||
let extensions = if msg.remaining() >= 2 {
|
||||
let ext_len = msg.get_u16() as usize;
|
||||
@@ -239,7 +212,7 @@ impl Parser for ServerHello {
|
||||
type Error = TlsError;
|
||||
|
||||
fn can_parse(bytes: &BytesMut) -> bool {
|
||||
let mut offset = 34; // ProtocolVersion (2) + Random (32)
|
||||
let mut offset = 34;
|
||||
if bytes.len() < offset + 1 {
|
||||
return false;
|
||||
}
|
||||
@@ -247,7 +220,6 @@ impl Parser for ServerHello {
|
||||
let session_id_len = bytes[offset] as usize;
|
||||
offset += 1 + session_id_len;
|
||||
|
||||
// Cipher suite (2) + Compression (1)
|
||||
offset += 3;
|
||||
|
||||
if bytes.len() >= offset + 2 {
|
||||
@@ -259,15 +231,14 @@ impl Parser for ServerHello {
|
||||
}
|
||||
|
||||
fn parse(bytes: &mut bytes::BytesMut) -> Result<Option<Self>, Self::Error> {
|
||||
// --- ШАГ 1: Атомарная проверка всего пакета ---
|
||||
let mut offset = 34; // Version + Random
|
||||
let mut offset = 34;
|
||||
if bytes.len() < offset + 1 {
|
||||
return Ok(None);
|
||||
}
|
||||
let session_id_len = bytes[offset] as usize;
|
||||
offset += 1 + session_id_len;
|
||||
|
||||
offset += 3; // Cipher Suite (2) + Compression (1)
|
||||
offset += 3;
|
||||
|
||||
if bytes.len() >= offset + 2 {
|
||||
let ext_len = u16::from_be_bytes([bytes[offset], bytes[offset + 1]]) as usize;
|
||||
@@ -278,7 +249,6 @@ impl Parser for ServerHello {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// --- ШАГ 2: Безопасное чтение ---
|
||||
let mut msg = bytes.split_to(offset);
|
||||
|
||||
let version = ProtocolVersion::try_from(msg.get_u16())
|
||||
@@ -291,7 +261,7 @@ impl Parser for ServerHello {
|
||||
let session_id = msg.split_to(sid_len).freeze();
|
||||
|
||||
let cipher_suite = msg.get_u16();
|
||||
msg.advance(1); // compression
|
||||
msg.advance(1);
|
||||
|
||||
let extensions = if msg.remaining() >= 2 {
|
||||
let ext_len = msg.get_u16() as usize;
|
||||
@@ -310,10 +280,6 @@ impl Parser for ServerHello {
|
||||
}
|
||||
}
|
||||
|
||||
// =================================================================
|
||||
// 4. EXTENSIONS
|
||||
// =================================================================
|
||||
|
||||
impl Parser for ExtensionStack {
|
||||
type Error = TlsError;
|
||||
|
||||
@@ -330,7 +296,6 @@ impl Parser for ExtensionStack {
|
||||
}
|
||||
|
||||
fn parse(bytes: &mut BytesMut) -> Result<Option<Self>, Self::Error> {
|
||||
// Проверяем на целостность всех расширений
|
||||
let mut offset = 0;
|
||||
let data_len = bytes.len();
|
||||
|
||||
@@ -339,13 +304,10 @@ impl Parser for ExtensionStack {
|
||||
offset += 4 + elen;
|
||||
}
|
||||
|
||||
// Если offset > data_len, значит кусок данных расширения отсечен, ждем еще данных
|
||||
if offset > data_len {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// Если offset < data_len, есть лишние байты (Trailing garbage). Но так как мы
|
||||
// обычно передаем сюда точный срез `extensions`, это может быть ошибкой формата.
|
||||
if offset != data_len {
|
||||
return Err(TlsError::new(
|
||||
ErrorStage::Tls("Malformed extension stack: trailing data"),
|
||||
@@ -354,7 +316,6 @@ impl Parser for ExtensionStack {
|
||||
));
|
||||
}
|
||||
|
||||
// Теперь гарантированно безопасно парсить всё до конца
|
||||
let mut extensions = Vec::new();
|
||||
while bytes.remaining() >= 4 {
|
||||
let etype = bytes.get_u16();
|
||||
|
||||
@@ -38,11 +38,11 @@ pub async fn run_proxy_bridge<R, W>(
|
||||
}
|
||||
}
|
||||
}
|
||||
// Читаем из туннеля (v_rx) -> шлем в сокет
|
||||
|
||||
maybe_data = v_rx.recv() => {
|
||||
match maybe_data {
|
||||
Some(data) => {
|
||||
if data.is_empty() { break; } // EOF от другой стороны
|
||||
if data.is_empty() { break; }
|
||||
if let Err(e) = writer.write_all(&data).await {
|
||||
error!(stream_id, error = %e, "Socket write error");
|
||||
break;
|
||||
@@ -57,7 +57,6 @@ pub async fn run_proxy_bridge<R, W>(
|
||||
}
|
||||
}
|
||||
|
||||
// Финализация (общая для всех)
|
||||
let _ = muxer
|
||||
.to_network
|
||||
.send(MuxMessage {
|
||||
|
||||
@@ -58,18 +58,18 @@ impl Connection {
|
||||
}
|
||||
}
|
||||
|
||||
/// Читает и парсит запрос SOCKS5 из входящего потока
|
||||
|
||||
async fn read_socks_request(&mut self) -> Result<SocksRequest, String> {
|
||||
loop {
|
||||
// Попытка парсинга из текущего буфера
|
||||
|
||||
match SocksRequest::parse(&mut self.read_buf) {
|
||||
Ok(Some(req)) => {
|
||||
// Используем Debug-вывод (?req), так как SocksRequest обычно Enum
|
||||
|
||||
info!(client = %self.addr, request = ?req, "SOCKS request successfully parsed");
|
||||
return Ok(req);
|
||||
}
|
||||
Ok(None) => {
|
||||
// Это не ошибка, просто данных в сокете пока меньше, чем размер структуры SOCKS
|
||||
|
||||
trace!(client = %self.addr, buffer_len = self.read_buf.len(), "SOCKS parse: need more data");
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -78,7 +78,7 @@ impl Connection {
|
||||
}
|
||||
}
|
||||
|
||||
// Чтение новых данных из сокета
|
||||
|
||||
let n = self
|
||||
.inbound
|
||||
.read_buf(&mut self.read_buf)
|
||||
@@ -97,7 +97,7 @@ impl Connection {
|
||||
}
|
||||
}
|
||||
|
||||
/// Отправляет SOCKS ответ
|
||||
|
||||
async fn send_socks_reply(&mut self, reply: SocksReply) -> Result<(), String> {
|
||||
let mut buf = BytesMut::with_capacity(24);
|
||||
debug!(client = %self.addr, reply = ?reply, "Sending SOCKS reply to client");
|
||||
@@ -124,7 +124,7 @@ impl Connection {
|
||||
pub async fn handle_socks_client(mut self, muxer: Muxer) -> Result<(), String> {
|
||||
info!("Starting SOCKS multiplexed handling");
|
||||
|
||||
// 1. SOCKS Handshake
|
||||
|
||||
debug!("Reading SOCKS handshake request");
|
||||
let _ = self.read_socks_request().await.map_err(|e| {
|
||||
error!("SOCKS handshake failed: {}", e);
|
||||
@@ -133,8 +133,8 @@ impl Connection {
|
||||
|
||||
self.send_socks_reply(SocksReply::HandshakeSelect { method: 0x00 }).await?;
|
||||
|
||||
// 2. SOCKS Connect
|
||||
// 2. SOCKS Connect - читаем, КУДА хочет браузер
|
||||
|
||||
|
||||
let req = self.read_socks_request().await?;
|
||||
let target = if let SocksRequest::Connect { target, .. } = req {
|
||||
target
|
||||
@@ -145,12 +145,12 @@ impl Connection {
|
||||
let stream_id = muxer.next_id();
|
||||
let target_str = target.to_string();
|
||||
|
||||
// --- НОВАЯ ЛОГИКА ОЖИДАНИЯ ---
|
||||
// Регистрируем временный канал, чтобы получить Connect-подтверждение от сервера
|
||||
|
||||
|
||||
let (v_tx, mut v_rx) = mpsc::channel::<Bytes>(1024);
|
||||
muxer.register_stream(stream_id, v_tx).await;
|
||||
|
||||
// Отправляем Connect-кадр на сервер
|
||||
|
||||
muxer.to_network.send(MuxMessage {
|
||||
stream_id,
|
||||
frame_type: FrameType::Connect,
|
||||
@@ -161,7 +161,7 @@ impl Connection {
|
||||
Ok(Some(data)) => data,
|
||||
_ => {
|
||||
error!(stream_id, "Server timeout or failed to send Connect confirmation");
|
||||
// Шлем браузеру ошибку, если сервер промолчал
|
||||
|
||||
self.send_socks_reply(SocksReply::ConnectResult {
|
||||
reply_code: 0x01, atyp: 0x01, addr: [0, 0, 0, 0], port: 0,
|
||||
}).await.ok();
|
||||
@@ -169,20 +169,20 @@ impl Connection {
|
||||
}
|
||||
};
|
||||
|
||||
// Проверяем код ответа (второй байт в SOCKS5)
|
||||
|
||||
if first_payload.len() >= 2 && first_payload[1] == 0x00 {
|
||||
debug!(stream_id, "Server confirmed connection, forwarding SOCKS reply to browser");
|
||||
|
||||
// ВАЖНО: Отправляем браузеру ТО, что прислал сервер (те самые 10 байт)
|
||||
// Не создаем новый SocksReply вручную, а пробрасываем байты сервера
|
||||
|
||||
|
||||
self.outbound.write_all(&first_payload).await.map_err(|e| e.to_string())?;
|
||||
} else {
|
||||
// Если сервер прислал ошибку (reply_code != 0), тоже пробрасываем её браузеру и выходим
|
||||
|
||||
self.outbound.write_all(&first_payload).await.ok();
|
||||
return Err("Server rejected connection".into());
|
||||
}
|
||||
|
||||
// 4. Разбираем self и запускаем хендлер
|
||||
|
||||
let Self { inbound: browser_in, outbound: browser_out, .. } = self;
|
||||
|
||||
let muxer_clone = muxer.clone();
|
||||
@@ -201,11 +201,11 @@ impl Connection {
|
||||
pub async fn handle_server_tunnel(mut self) -> Result<(), String> {
|
||||
info!("Acting as TLS Server, waiting for ClientHello");
|
||||
|
||||
// Создаем Muxer для сервера
|
||||
|
||||
let (mux_tx, mux_rx) = mpsc::channel(BUF_SIZE);
|
||||
let muxer = Muxer::new(mux_tx.clone(), false); // false, так как это Сервер
|
||||
let muxer = Muxer::new(mux_tx.clone(), false);
|
||||
|
||||
// 1. TLS Handshake
|
||||
|
||||
let server_hello_bytes = loop {
|
||||
match self.codec.make_server_handshake(&mut self.read_buf) {
|
||||
Ok(bytes) => {
|
||||
@@ -227,7 +227,7 @@ impl Connection {
|
||||
|
||||
let handler = std::sync::Arc::new(StreamHandler::new(muxer.clone(), ConnectionRole::Server));
|
||||
|
||||
// 2. Передача управления в TunnelEngine
|
||||
|
||||
debug!("Handover to TunnelEngine");
|
||||
let engine = TunnelEngine {
|
||||
inbound: self.inbound,
|
||||
|
||||
@@ -19,7 +19,7 @@ pub struct TunnelEngine {
|
||||
pub codec: Codec,
|
||||
pub read_buf: BytesMut,
|
||||
pub mux_rx: Receiver<MuxMessage>,
|
||||
pub handler: Arc<StreamHandler>, // Добавь это вместо прямого вызова логики
|
||||
pub handler: Arc<StreamHandler>,
|
||||
}
|
||||
|
||||
impl TunnelEngine {
|
||||
@@ -36,7 +36,7 @@ impl TunnelEngine {
|
||||
res?
|
||||
}
|
||||
|
||||
// НУЖНО ОТПРАВИТЬ В СЕТЬ (В сторону удаленного прокси)
|
||||
|
||||
Some(msg) = mux_rx.recv() => {
|
||||
Self::handle_outbound( &mut outbound, &mut codec, msg).await?;
|
||||
}
|
||||
@@ -61,20 +61,17 @@ impl TunnelEngine {
|
||||
|
||||
loop {
|
||||
match codec.inbound(read_buf) {
|
||||
// 1. Успешно достали фрейм
|
||||
Ok(Some(frame)) => {
|
||||
handler.handle(frame).await;
|
||||
}
|
||||
// 2. Данных в буфере недостаточно (нужно подождать еще)
|
||||
|
||||
Ok(None) => break,
|
||||
|
||||
// 3. Ошибка кодека
|
||||
Err(e) => {
|
||||
// Если кодек говорит "подожди", выходим из цикла парсинга
|
||||
if e.action == ErrorAction::Wait {
|
||||
break;
|
||||
}
|
||||
// Иначе — это реальная проблема (кривой TLS и т.д.)
|
||||
|
||||
error!(error = ?e, "Codec inbound failed");
|
||||
return Err(format!("Codec error: {:?}", e));
|
||||
}
|
||||
@@ -88,10 +85,8 @@ impl TunnelEngine {
|
||||
codec: &mut Codec,
|
||||
msg: MuxMessage,
|
||||
) -> Result<(), String> {
|
||||
// 1. Шифруем данные, используя только кодек
|
||||
match codec.encrypt_data(msg.stream_id, msg.frame_type, msg.data) {
|
||||
Ok(pkt) => {
|
||||
// 2. Пишем в сокет, используя только outbound
|
||||
outbound
|
||||
.write_all(&pkt)
|
||||
.await
|
||||
|
||||
@@ -9,7 +9,6 @@ use crate::{
|
||||
proxy::connection::{bridge::run_proxy_bridge, connection::ConnectionRole, muxer::Muxer},
|
||||
};
|
||||
|
||||
// proxy/connection/stream_handler.rs
|
||||
pub struct StreamHandler {
|
||||
muxer: Muxer,
|
||||
role: ConnectionRole,
|
||||
@@ -44,7 +43,6 @@ impl StreamHandler {
|
||||
|
||||
match tokio::net::TcpStream::connect(&target_str).await {
|
||||
Ok(stream) => {
|
||||
// --- ШАГ 2: ШЛЕМ ПОДТВЕРЖДЕНИЕ ---
|
||||
let mut reply_buf = BytesMut::with_capacity(10);
|
||||
let reply = SocksReply::ConnectResult {
|
||||
reply_code: 0x00,
|
||||
@@ -58,13 +56,12 @@ impl StreamHandler {
|
||||
.send_control(stream_id, FrameType::Connect, reply_buf.freeze())
|
||||
.await;
|
||||
|
||||
// --- ШАГ 3: ЗАПУСКАЕМ МОСТ ---
|
||||
let (r, w) = stream.into_split();
|
||||
run_proxy_bridge(stream_id, r, w, muxer, v_rx).await;
|
||||
}
|
||||
Err(e) => {
|
||||
error!(stream_id, error = %e, "Connection failed");
|
||||
// Если не подключились — удаляем стрим, чтобы не висел в мапе
|
||||
|
||||
muxer.remove_stream(stream_id).await;
|
||||
|
||||
let mut reply_buf = BytesMut::with_capacity(10);
|
||||
@@ -82,7 +79,6 @@ impl StreamHandler {
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// Логика для клиента (проброс ответа сервера браузеру)
|
||||
self.muxer.dispatch_to_local(stream_id, payload).await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ impl Muxer {
|
||||
|
||||
pub async fn remove_stream(&self, stream_id: u32) {
|
||||
let mut lock = self.streams.write().await;
|
||||
lock.remove(&stream_id); // Просто удаляем, если есть
|
||||
lock.remove(&stream_id);
|
||||
}
|
||||
|
||||
pub async fn send_control(
|
||||
|
||||
@@ -12,7 +12,7 @@ use tokio::{
|
||||
io::{AsyncReadExt, AsyncWriteExt},
|
||||
net::{TcpListener, TcpStream},
|
||||
};
|
||||
use tracing::{error, info, instrument}; // Импортируем макросы
|
||||
use tracing::{error, info, instrument};
|
||||
|
||||
pub struct Network {
|
||||
host: String,
|
||||
@@ -36,7 +36,6 @@ impl Network {
|
||||
}
|
||||
}
|
||||
|
||||
// Добавляем инструмент, чтобы видеть параметры запуска сети в логах
|
||||
#[instrument(skip(self), fields(role = ?self.role, port = self.port))]
|
||||
pub async fn run(&self) {
|
||||
let addr = format!("{}:{}", self.host, self.port);
|
||||
@@ -60,7 +59,6 @@ impl Network {
|
||||
if let Ok((stream, client_addr)) = listener.accept().await {
|
||||
let current_muxer = muxer.clone();
|
||||
tokio::spawn(async move {
|
||||
// Здесь мы просто создаем Connection и сразу в SOCKS
|
||||
let connection = Connection::new(stream, client_addr, false);
|
||||
let _ = connection.handle_socks_client(current_muxer).await;
|
||||
});
|
||||
@@ -69,7 +67,6 @@ impl Network {
|
||||
}
|
||||
|
||||
ConnectionRole::Server => {
|
||||
// --- ЛОГИКА СЕРВЕРА ---
|
||||
let listener = TcpListener::bind(&addr)
|
||||
.await
|
||||
.expect("Failed to bind Server port");
|
||||
@@ -78,7 +75,6 @@ impl Network {
|
||||
loop {
|
||||
if let Ok((stream, client_addr)) = listener.accept().await {
|
||||
tokio::spawn(async move {
|
||||
// Сервер использует handle_server_tunnel
|
||||
let connection = Connection::new(stream, client_addr, true);
|
||||
if let Err(e) = connection.handle_server_tunnel().await {
|
||||
error!(client = %client_addr, error = %e, "Tunnel error");
|
||||
@@ -90,21 +86,16 @@ impl Network {
|
||||
}
|
||||
}
|
||||
|
||||
/// Вспомогательный метод для Клиента: создает TLS туннель и запускает TunnelEngine
|
||||
pub async fn initialize_client_tunnel(&self) -> Result<Muxer, String> {
|
||||
let server_addr = self.remote_proxy_addr.as_ref().ok_or("No proxy addr")?;
|
||||
|
||||
// Вместо создания Connection (который нужен для обработки клиентов),
|
||||
// работаем напрямую с TcpStream для первичного TLS-хендшейка.
|
||||
let stream = TcpStream::connect(server_addr)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let (mut inbound, mut outbound) = stream.into_split();
|
||||
|
||||
// Кодек создаем «с чистого листа»
|
||||
let mut codec = crate::protocol::codec::codec::Codec::new(false);
|
||||
|
||||
// --- TLS Handshake ---
|
||||
let ch = codec
|
||||
.make_client_handshake(&BrowserProfile::CHROME_131, "google.com")
|
||||
.map_err(|e| format!("{:?}", e))?;
|
||||
@@ -112,9 +103,8 @@ impl Network {
|
||||
|
||||
let mut sh_buf = BytesMut::with_capacity(2048);
|
||||
loop {
|
||||
// Пытаемся обработать то, что уже есть в буфере
|
||||
match codec.process_handshake(&mut sh_buf) {
|
||||
Ok(_) => break, // Готово!
|
||||
Ok(_) => break,
|
||||
Err(e) if e.action == ErrorAction::Wait => {
|
||||
let n = inbound
|
||||
.read_buf(&mut sh_buf)
|
||||
@@ -128,7 +118,6 @@ impl Network {
|
||||
}
|
||||
}
|
||||
|
||||
// --- Запуск инфраструктуры ---
|
||||
let (mux_tx, mux_rx) = tokio::sync::mpsc::channel(BUF_SIZE);
|
||||
let muxer = Muxer::new(mux_tx, true);
|
||||
|
||||
@@ -141,7 +130,7 @@ impl Network {
|
||||
inbound,
|
||||
outbound,
|
||||
codec,
|
||||
read_buf: sh_buf, // Передаем остатки данных из буфера хендшейка в движок!
|
||||
read_buf: sh_buf,
|
||||
mux_rx,
|
||||
handler,
|
||||
};
|
||||
|
||||
@@ -1,22 +1,14 @@
|
||||
/// Handshake message types
|
||||
pub const HANDSHAKE_TYPE_CLIENT_HELLO: u8 = 0x01;
|
||||
pub const HANDSHAKE_TYPE_SERVER_HELLO: u8 = 0x02;
|
||||
|
||||
/// SNI (Server Name Indication) specific
|
||||
pub const TYPE_HOST_NAME: u8 = 0x00;
|
||||
|
||||
/// PSK (Pre-Shared Key) modes
|
||||
pub const PSK_DHE_KE_MODE: u8 = 0x01;
|
||||
|
||||
/// Certificate compression algorithms
|
||||
pub const CERT_COMPRESSION_BROTLI: u16 = 0x0002;
|
||||
|
||||
/// Extension internal status types
|
||||
pub const OCSP_STATUS_TYPE: u8 = 0x01;
|
||||
//pub const EC_POINT_FORMAT_UNCOMPRESSED: u8 = 0x00;
|
||||
|
||||
/// GREASE (Generate Random Extensions And Sustain Extensibility)
|
||||
/// Используется для предотвращения ошибок серверов при встрече с неизвестными ID.
|
||||
pub const GREASE_IDENTIFIERS: [u16; 16] = [
|
||||
0x0A0A, 0x1A1A, 0x2A2A, 0x3A3A, 0x4A4A, 0x5A5A, 0x6A6A, 0x7A7A, 0x8A8A, 0x9A9A, 0xAAAA, 0xBABA,
|
||||
0xCACA, 0xDADA, 0xEAEA, 0xFAFA,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use bytes::{BufMut, Bytes, BytesMut};
|
||||
use rand::RngExt;
|
||||
|
||||
// Using your provided constants and types
|
||||
use crate::tlseng::{
|
||||
consts::{
|
||||
CERT_COMPRESSION_BROTLI, GREASE_IDENTIFIERS, OCSP_STATUS_TYPE, PSK_DHE_KE_MODE,
|
||||
@@ -33,16 +32,6 @@ impl ExtensionStack {
|
||||
}
|
||||
|
||||
impl Extension {
|
||||
/// Creates a new Extension from the given parameters.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `etype`: The type of extension (e.g., EXT_SUPPORTED_VERSIONS).
|
||||
/// * `data`: The actual data being transported (e.g., a serialized list of supported versions).
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A new Extension structure with the given parameters.
|
||||
pub fn new(etype: u16, data: Bytes) -> Self {
|
||||
Self {
|
||||
etype,
|
||||
@@ -50,16 +39,7 @@ impl Extension {
|
||||
data,
|
||||
}
|
||||
}
|
||||
/// Packs an extension into a single byte array.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `etype`: The type of extension (e.g., EXT_SUPPORTED_VERSIONS).
|
||||
/// * `data`: The actual data being transported (e.g., a serialized list of supported versions).
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A single byte array containing the type and length of the extension, followed by the actual extension data.
|
||||
|
||||
pub fn pack(etype: u16, data: &[u8]) -> Bytes {
|
||||
let mut ext = BytesMut::with_capacity(4 + data.len());
|
||||
ext.put_u16(etype);
|
||||
@@ -139,9 +119,9 @@ impl ExtensionBuilder {
|
||||
|
||||
pub fn key_share(&mut self, pub_key: &[u8]) {
|
||||
let mut data = BytesMut::with_capacity(38);
|
||||
data.put_u16(34); // Total Key Share List Length
|
||||
data.put_u16(34);
|
||||
data.put_u16(TlsGroups::X25519);
|
||||
data.put_u16(32); // Public Key length
|
||||
data.put_u16(32);
|
||||
data.put_slice(pub_key);
|
||||
self.add_extension(TlsExtensions::KEY_SHARE, &data);
|
||||
}
|
||||
@@ -152,7 +132,7 @@ impl ExtensionBuilder {
|
||||
let p_bytes = proto.as_bytes();
|
||||
data.put_u8(p_bytes.len() as u8);
|
||||
data.put_slice(p_bytes);
|
||||
data.put_u16(0); // Empty settings per-protocol
|
||||
data.put_u16(0);
|
||||
}
|
||||
self.add_extension(TlsExtensions::ALPS, &data);
|
||||
}
|
||||
@@ -189,15 +169,15 @@ impl ExtensionBuilder {
|
||||
pub fn status_request(&mut self) {
|
||||
let mut data = BytesMut::with_capacity(5);
|
||||
data.put_u8(OCSP_STATUS_TYPE);
|
||||
data.put_u16(0); // responder_id_list
|
||||
data.put_u16(0); // request_extensions
|
||||
data.put_u16(0);
|
||||
data.put_u16(0);
|
||||
self.add_extension(TlsExtensions::STATUS_REQUEST, &data);
|
||||
}
|
||||
|
||||
pub fn ec_point_formats(&mut self) {
|
||||
let mut data = BytesMut::with_capacity(2);
|
||||
data.put_u8(1);
|
||||
data.put_u8(0x00); // Uncompressed
|
||||
data.put_u8(0x00);
|
||||
self.add_extension(TlsExtensions::EC_POINT_FORMATS, &data);
|
||||
}
|
||||
|
||||
@@ -263,7 +243,7 @@ impl ExtensionBuilder {
|
||||
self.add_extension(TlsExtensions::PADDING, &[]);
|
||||
}
|
||||
}
|
||||
// Обработка GREASE по маске
|
||||
|
||||
id if (id & 0x0f0f) == 0x0a0a => self.grease(),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -17,65 +17,46 @@ pub struct HelloHeader {
|
||||
}
|
||||
|
||||
pub struct ClientHello {
|
||||
/// The maximum version supported (legacy field in TLS 1.3)
|
||||
pub version: ProtocolVersion,
|
||||
/// 32 bytes of client-generated entropy
|
||||
|
||||
pub random: [u8; 32],
|
||||
/// Legacy session ID (used in TLS 1.3 for middlebox compatibility)
|
||||
|
||||
pub session_id: Bytes,
|
||||
/// List of cryptographic ciphers the client supports
|
||||
|
||||
pub cipher_suites: Vec<u16>,
|
||||
/// Opaque block of extensions generated by ExtensionBuilder
|
||||
|
||||
pub extensions: Bytes,
|
||||
}
|
||||
|
||||
impl ClientHello {
|
||||
/// Serializes the ClientHello message into its wire format.
|
||||
/// Includes the 4-byte Handshake header (Type + Length).
|
||||
pub fn serialize(&self) -> Bytes {
|
||||
let mut buf = BytesMut::with_capacity(512 + self.extensions.len());
|
||||
|
||||
// Handshake Type: 0x01 (ClientHello)
|
||||
buf.put_u8(HANDSHAKE_TYPE_CLIENT_HELLO);
|
||||
|
||||
// Handshake Length Placeholder:
|
||||
// Handshake messages use a 24-bit (3 byte) length field.
|
||||
let length_pos = buf.len();
|
||||
buf.put_bytes(0, 3);
|
||||
|
||||
// Protocol Version:
|
||||
// For TLS 1.3, this is traditionally pinned to 0x0303 (TLS 1.2)
|
||||
// to prevent middleboxes from dropping the packet.
|
||||
buf.put_u16(0x0303);
|
||||
buf.put_slice(&self.random);
|
||||
|
||||
// Legacy Session ID:
|
||||
// Formatted as Length (1 byte) + ID bytes.
|
||||
buf.put_u8(self.session_id.len() as u8);
|
||||
buf.put_slice(&self.session_id);
|
||||
|
||||
// Cipher Suites:
|
||||
// Formatted as Total Length (2 bytes) + Suite IDs (2 bytes each).
|
||||
buf.put_u16((self.cipher_suites.len() * 2) as u16);
|
||||
for &suite in &self.cipher_suites {
|
||||
buf.put_u16(suite);
|
||||
}
|
||||
|
||||
// Legacy Compression Methods:
|
||||
// Always 1 byte of length (1) followed by the 'Null' method (0x00).
|
||||
buf.put_u8(1);
|
||||
buf.put_u8(0x00);
|
||||
|
||||
// Extensions Block:
|
||||
// Formatted as Total Length (2 bytes) + Extension Data.
|
||||
buf.put_u16(self.extensions.len() as u16);
|
||||
buf.put_slice(&self.extensions);
|
||||
|
||||
// Patch the Handshake Length:
|
||||
// We calculate the length of everything after the 3-byte placeholder.
|
||||
let total_len = (buf.len() - length_pos - 3) as u32;
|
||||
let len_bytes = total_len.to_be_bytes();
|
||||
// Copy the last 3 bytes of the big-endian u32 into the placeholder.
|
||||
|
||||
buf[length_pos..length_pos + 3].copy_from_slice(&len_bytes[1..4]);
|
||||
|
||||
let ext_len = self.extensions.len();
|
||||
@@ -108,10 +89,10 @@ impl ClientHello {
|
||||
session_id.put_slice(&[0u8; 32]);
|
||||
|
||||
let client_hello = ClientHello {
|
||||
version: ProtocolVersion::Tls12, // Legacy version for compatibility
|
||||
version: ProtocolVersion::Tls12,
|
||||
random: tls_random,
|
||||
session_id: session_id.freeze(), // Standard 32-byte session ID
|
||||
cipher_suites: vec![0x1301, 0x1302, 0x1303], // TLS 1.3 suites
|
||||
session_id: session_id.freeze(),
|
||||
cipher_suites: vec![0x1301, 0x1302, 0x1303],
|
||||
extensions: extensions_bytes,
|
||||
};
|
||||
|
||||
@@ -133,7 +114,6 @@ pub struct ServerHello {
|
||||
pub extensions: BytesMut,
|
||||
}
|
||||
impl ServerHello {
|
||||
/// Динамически создает ServerHello на основе данных из ClientHello
|
||||
pub fn from_client_hello(
|
||||
client_hello: &ClientHello,
|
||||
server_public_key: &[u8],
|
||||
@@ -149,17 +129,14 @@ impl ServerHello {
|
||||
|
||||
let mut extensions = BytesMut::new();
|
||||
|
||||
// --- Extension: Supported Versions (0x002b) ---
|
||||
extensions.put_u16(0x002b);
|
||||
extensions.put_u16(2);
|
||||
extensions.put_u16(0x0304); // TLS 1.3
|
||||
extensions.put_u16(0x0304);
|
||||
|
||||
// --- Extension: Key Share (0x0033) ---
|
||||
// Структура: Type(2) + Length(2) + Group(2) + KeyLength(2) + Key(N)
|
||||
extensions.put_u16(0x0033);
|
||||
extensions.put_u16(36); // Общая длина данных расширения (2+2+32)
|
||||
extensions.put_u16(0x001d); // Named Group: x25519
|
||||
extensions.put_u16(32); // Key Length
|
||||
extensions.put_u16(36);
|
||||
extensions.put_u16(0x001d);
|
||||
extensions.put_u16(32);
|
||||
extensions.put_slice(server_public_key);
|
||||
|
||||
Self {
|
||||
@@ -171,7 +148,6 @@ impl ServerHello {
|
||||
}
|
||||
}
|
||||
|
||||
/// Оборачивает в TLS Record, принимая ключ
|
||||
pub fn make_server_hello(
|
||||
client_hello: &ClientHello,
|
||||
server_public_key: &[u8],
|
||||
@@ -188,43 +164,35 @@ impl ServerHello {
|
||||
|
||||
record.serialize()
|
||||
}
|
||||
/// Сериализация самого сообщения Handshake (Type + Len gth + Body)
|
||||
|
||||
pub fn serialize(&self) -> Bytes {
|
||||
let mut buf = BytesMut::with_capacity(256 + self.extensions.len());
|
||||
|
||||
// 1. Handshake Type: 0x02 (ServerHello)
|
||||
buf.put_u8(HANDSHAKE_TYPE_SERVER_HELLO);
|
||||
|
||||
// 2. Placeholder для длины (u24)
|
||||
let length_pos = buf.len();
|
||||
buf.put_bytes(0, 3);
|
||||
|
||||
// 3. Тело ServerHello
|
||||
buf.put_u16(0x0303); // Legacy Version
|
||||
buf.put_u16(0x0303);
|
||||
buf.put_slice(&self.random);
|
||||
|
||||
// Session ID: Length (1 byte) + Data
|
||||
buf.put_u8(self.session_id.len() as u8);
|
||||
buf.put_slice(&self.session_id);
|
||||
|
||||
// Selected Cipher Suite
|
||||
buf.put_u16(self.cipher_suite);
|
||||
|
||||
// Compression: всегда 0x00
|
||||
buf.put_u8(0x00);
|
||||
|
||||
// Extensions: Length (2 bytes) + Data
|
||||
buf.put_u16(self.extensions.len() as u16);
|
||||
buf.put_slice(&self.extensions);
|
||||
|
||||
// 4. Патчим длину Handshake сообщения (u24)
|
||||
let total_len = (buf.len() - length_pos - 3) as u32;
|
||||
let len_bytes = total_len.to_be_bytes();
|
||||
buf[length_pos..length_pos + 3].copy_from_slice(&len_bytes[1..4]);
|
||||
|
||||
let total_handshake_len = (buf.len() - length_pos - 3) as u32;
|
||||
let ext_len = self.extensions.len();
|
||||
// ИНФОРМАТИВНЫЙ ЛОГ
|
||||
|
||||
tracing::debug!(
|
||||
handshake_type = "ServerHello",
|
||||
body_len = total_handshake_len,
|
||||
@@ -233,6 +201,6 @@ impl ServerHello {
|
||||
"Serialized Handshake message"
|
||||
);
|
||||
|
||||
buf.freeze() // Превращаем BytesMut в Bytes
|
||||
buf.freeze()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,20 @@
|
||||
use crate::tlseng::types::{ExtensionOrder, TlsGroups, TlsSignatures, TlsVersions};
|
||||
|
||||
/// Represents a complete TLS fingerprint profile for a specific browser.
|
||||
///
|
||||
/// This struct contains all the necessary information to generate a TLS
|
||||
/// fingerprint for a specific browser, including the groups, signatures,
|
||||
/// delegated signatures, versions, ALPN, and extension order.
|
||||
pub struct BrowserProfile {
|
||||
/// The name of the browser profile.
|
||||
pub name: &'static str,
|
||||
|
||||
/// The groups supported by the browser.
|
||||
pub groups: TlsGroups,
|
||||
|
||||
/// The signatures supported by the browser.
|
||||
pub signatures: TlsSignatures,
|
||||
|
||||
/// The delegated signatures supported by the browser.
|
||||
pub delegated_signatures: TlsSignatures,
|
||||
|
||||
/// The versions of TLS supported by the browser.
|
||||
pub versions: TlsVersions,
|
||||
|
||||
/// The ALPN protocols supported by the browser.
|
||||
pub alpn: &'static [&'static str],
|
||||
|
||||
/// The specific order of Extension IDs (e.g., [0x0000, 0x0017, ...])
|
||||
pub extension_order: ExtensionOrder,
|
||||
|
||||
/// Whether the browser is based on Chromium.
|
||||
pub is_chromium: bool,
|
||||
}
|
||||
|
||||
@@ -45,7 +32,7 @@ impl BrowserProfile {
|
||||
|
||||
pub const EDGE: Self = Self {
|
||||
name: "Edge",
|
||||
groups: TlsGroups::CHROMIUM, // Edge использует тот же набор, что и Chrome
|
||||
groups: TlsGroups::CHROMIUM,
|
||||
signatures: TlsSignatures::BROWSER_STANDARD,
|
||||
delegated_signatures: TlsSignatures::BROWSER_STANDARD,
|
||||
versions: TlsVersions::MODERN,
|
||||
@@ -56,45 +43,31 @@ impl BrowserProfile {
|
||||
|
||||
pub const DEFAULT: Self = Self::CHROME_131;
|
||||
}
|
||||
/// Represents a TLS configuration profile for the server side.
|
||||
|
||||
pub struct ServerProfile {
|
||||
/// Имя профиля (например, "Modern-TLS-1.3-Only" или "Compatible-Nginx-Style")
|
||||
pub name: &'static str,
|
||||
|
||||
/// Поддерживаемые версии TLS. Сервер выберет высшую общую с клиентом.
|
||||
pub versions: TlsVersions,
|
||||
|
||||
/// Приоритетный список шифров (Cipher Suites).
|
||||
/// В TLS 1.3 это обычно [0x1301, 0x1302, 0x1303].
|
||||
pub cipher_suites: &'static [u16],
|
||||
|
||||
/// Группы для обмена ключами (Key Exchange Groups).
|
||||
pub groups: TlsGroups,
|
||||
|
||||
/// Поддерживаемые алгоритмы подписи для аутентификации сервера.
|
||||
pub signatures: TlsSignatures,
|
||||
|
||||
/// Протоколы ALPN, которые сервер готов подтвердить (h2, http/1.1).
|
||||
pub alpn: &'static [&'static str],
|
||||
|
||||
/// Настройки сессий
|
||||
pub session_tickets: bool,
|
||||
|
||||
/// Нужно ли форсировать порядок шифров сервера (Server Preference),
|
||||
/// игнорируя порядок предпочтений клиента.
|
||||
pub honor_cipher_order: bool,
|
||||
}
|
||||
|
||||
impl ServerProfile {
|
||||
pub const MODERN: Self = Self {
|
||||
name: "Modern-Secure",
|
||||
versions: TlsVersions::MODERN, // Допустим, у тебя есть такой хелпер
|
||||
cipher_suites: &[
|
||||
0x1301, // TLS_AES_128_GCM_SHA256
|
||||
0x1302, // TLS_AES_256_GCM_SHA384
|
||||
0x1303, // TLS_CHACHA20_POLY1305_SHA256
|
||||
],
|
||||
groups: TlsGroups::MODERN, // X25519, P-256
|
||||
versions: TlsVersions::MODERN,
|
||||
cipher_suites: &[0x1301, 0x1302, 0x1303],
|
||||
groups: TlsGroups::MODERN,
|
||||
signatures: TlsSignatures::BROWSER_STANDARD,
|
||||
alpn: &["h2", "http/1.1"],
|
||||
session_tickets: true,
|
||||
|
||||
@@ -2,32 +2,18 @@ use bytes::{BufMut, Bytes, BytesMut};
|
||||
|
||||
use crate::tlseng::types::{ContentType, ProtocolVersion};
|
||||
|
||||
/// The TLS Record Layer structure.
|
||||
/// This is the outer envelope that wraps all TLS messages sent over the wire.
|
||||
#[derive(Debug)]
|
||||
pub struct TlsRecord {
|
||||
/// The type of data contained (Handshake, ApplicationData, etc.)
|
||||
pub content_type: ContentType,
|
||||
/// The record layer version (usually 0x0301 for legacy support)
|
||||
|
||||
pub version: ProtocolVersion,
|
||||
|
||||
pub len: u16,
|
||||
/// The actual data being transported (e.g., a serialized ClientHello)
|
||||
|
||||
pub payload: Bytes,
|
||||
}
|
||||
|
||||
impl TlsRecord {
|
||||
/// Creates a new TLS Record Layer from the given parameters.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `content_type`: The type of data contained (Handshake, ApplicationData, etc.).
|
||||
/// * `version`: The record layer version (usually 0x0301 for legacy support).
|
||||
/// * `payload`: The actual data being transported (e.g., a serialized ClientHello).
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// A new TLS Record Layer structure with the given parameters.
|
||||
pub fn new(content_type: ContentType, version: ProtocolVersion, payload: Bytes) -> Self {
|
||||
Self {
|
||||
content_type,
|
||||
@@ -37,8 +23,6 @@ impl TlsRecord {
|
||||
}
|
||||
}
|
||||
|
||||
/// Serializes the Record Layer header and payload.
|
||||
/// Wire Format: [Type (1)] [Version (2)] [Length (2)] [Payload (N)]
|
||||
pub fn serialize(&self) -> Bytes {
|
||||
let mut buf = BytesMut::with_capacity(5 + self.payload.len());
|
||||
|
||||
|
||||
@@ -1,25 +1,15 @@
|
||||
/// TLS Content Types as defined in the TLS Record Protocol.
|
||||
/// These identify what is contained within the TLS Record payload.
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ContentType {
|
||||
/// Handshake messages (e.g., ClientHello, ServerHello)
|
||||
Handshake = 0x16,
|
||||
/// Encrypted application data (the actual traffic)
|
||||
|
||||
ApplicationData = 0x17,
|
||||
/// Notification messages (e.g., CloseNotify or error signals)
|
||||
|
||||
Alert = 0x15,
|
||||
}
|
||||
|
||||
impl TryFrom<u8> for ContentType {
|
||||
type Error = &'static str;
|
||||
/// Attempts to convert a given `u8` value into a `ContentType`.
|
||||
///
|
||||
/// Returns `Ok(ContentType)` if the conversion is successful, and `Err(&str)` if not.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
///
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
@@ -31,14 +21,6 @@ impl TryFrom<u8> for ContentType {
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
/// Represents known TLS protocol versions.
|
||||
///
|
||||
/// Note that TLS 1.3 often uses legacy versions in headers for compatibility.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
///
|
||||
#[repr(u16)]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum ProtocolVersion {
|
||||
@@ -50,66 +32,28 @@ pub enum ProtocolVersion {
|
||||
impl TryFrom<u16> for ProtocolVersion {
|
||||
type Error = &'static str;
|
||||
|
||||
/// Attempts to convert a given `u16` value into a `ProtocolVersion`.
|
||||
///
|
||||
/// Returns `Ok(ProtocolVersion)` if the conversion is successful, and `Err(&str)` if not.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
///
|
||||
fn try_from(value: u16) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
// TLS 1.0 (RFC 2246)
|
||||
0x0301 => Ok(ProtocolVersion::Tls10),
|
||||
// TLS 1.2 (RFC 4346)
|
||||
|
||||
0x0303 => Ok(ProtocolVersion::Tls12),
|
||||
// TLS 1.3 (draft-ietf-tls-tls13-28)
|
||||
|
||||
0x0304 => Ok(ProtocolVersion::Tls13),
|
||||
_ => Err("This is not Protocol Version"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Hello types as defined in the TLS Handshake Protocol.
|
||||
///
|
||||
/// These identify the type of the message in the TLS Handshake protocol.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
///
|
||||
/// # Notes
|
||||
///
|
||||
/// The values of these enum variants are used as the first byte of the TLS
|
||||
/// Handshake protocol message.
|
||||
///
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub enum HelloType {
|
||||
/// Client hello message type
|
||||
Client = 0x01,
|
||||
/// Server hello message type
|
||||
|
||||
Server = 0x02,
|
||||
}
|
||||
|
||||
/// Attempts to convert a given `u8` value into a `HelloType`.
|
||||
///
|
||||
/// Returns `Ok(HelloType)` if the conversion is successful, and `Err(&str)` if not.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
///
|
||||
/// # Notes
|
||||
///
|
||||
/// This function is used to convert raw bytes into a `HelloType`.
|
||||
/// It is used in the `HelloHeader` parsing process.
|
||||
impl TryFrom<u8> for HelloType {
|
||||
type Error = &'static str;
|
||||
/// Attempts to convert a given `u8` value into a `HelloType`.
|
||||
///
|
||||
/// Returns `Ok(HelloType)` if the conversion is successful, and `Err(&str)` if not.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
///
|
||||
|
||||
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
0x01 => Ok(HelloType::Client),
|
||||
@@ -119,10 +63,6 @@ impl TryFrom<u8> for HelloType {
|
||||
}
|
||||
}
|
||||
|
||||
/// A collection of supported TLS groups.
|
||||
///
|
||||
/// This is a list of 16-bit group identifiers that the client supports.
|
||||
/// The server will select one of these groups to use for the key exchange.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct TlsGroups(pub &'static [u16]);
|
||||
|
||||
@@ -131,17 +71,11 @@ impl TlsGroups {
|
||||
pub const SECP256R1: u16 = 0x0017;
|
||||
pub const SECP384R1: u16 = 0x0018;
|
||||
|
||||
/// Стандартный набор для Chrome/Edge (X25519 + P-256)
|
||||
pub const CHROMIUM: Self = Self(&[Self::X25519, Self::SECP256R1, Self::SECP384R1]);
|
||||
|
||||
/// Набор "только современные кривые"
|
||||
pub const MODERN: Self = Self(&[Self::X25519, Self::SECP256R1]);
|
||||
}
|
||||
|
||||
/// A collection of supported TLS signature algorithms.
|
||||
///
|
||||
/// This is a list of 16-bit signature algorithm identifiers that the client supports.
|
||||
/// The server will select one of these algorithms to use for the digital signature.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct TlsSignatures(pub &'static [u16]);
|
||||
|
||||
@@ -155,7 +89,6 @@ impl TlsSignatures {
|
||||
pub const RSA_PSS_RSAE_SHA512: u16 = 0x0806;
|
||||
pub const RSA_PKCS1_SHA512: u16 = 0x0601;
|
||||
|
||||
/// Типичный набор для современных браузеров
|
||||
pub const BROWSER_STANDARD: Self = Self(&[
|
||||
Self::ECDSA_SECP256R1_SHA256,
|
||||
Self::RSA_PSS_RSAE_SHA256,
|
||||
@@ -167,10 +100,6 @@ impl TlsSignatures {
|
||||
]);
|
||||
}
|
||||
|
||||
/// A collection of supported TLS protocol versions.
|
||||
///
|
||||
/// This is a list of 16-bit protocol version identifiers that the client supports.
|
||||
/// The server will select one of these versions to use for the TLS connection.
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct TlsVersions(pub &'static [u16]);
|
||||
|
||||
@@ -218,7 +147,7 @@ impl<'a> IntoIterator for &'a ExtensionOrder {
|
||||
|
||||
impl ExtensionOrder {
|
||||
pub const CHROMIUM_131: Self = Self(&[
|
||||
0xaaaa, // GREASE
|
||||
0xaaaa,
|
||||
TlsExtensions::SNI,
|
||||
TlsExtensions::EMS,
|
||||
TlsExtensions::SESSION_TICKET,
|
||||
@@ -237,7 +166,7 @@ impl ExtensionOrder {
|
||||
]);
|
||||
|
||||
pub const EDGE_130: Self = Self(&[
|
||||
0x1a1a, // GREASE
|
||||
0x1a1a,
|
||||
TlsExtensions::SNI,
|
||||
TlsExtensions::EMS,
|
||||
TlsExtensions::SESSION_TICKET,
|
||||
@@ -254,6 +183,6 @@ impl ExtensionOrder {
|
||||
TlsExtensions::SCT,
|
||||
TlsExtensions::DELEGATED_CREDENTIAL,
|
||||
TlsExtensions::PADDING,
|
||||
0x3a3a, // GREASE
|
||||
0x3a3a,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -4,50 +4,21 @@ use bytes::Buf;
|
||||
pub struct U24([u8; 3]);
|
||||
|
||||
impl U24 {
|
||||
/// Creates a new `U24` from a given `u32` value.
|
||||
///
|
||||
/// The `u32` value is converted to big-endian byte order and then split into
|
||||
/// three bytes, which are used to initialize the `U24`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
///
|
||||
pub fn from_u32(value: u32) -> Self {
|
||||
let b = value.to_be_bytes();
|
||||
U24([b[1], b[2], b[3]])
|
||||
}
|
||||
|
||||
/// Converts the `U24` into a `u32` value.
|
||||
///
|
||||
/// The `U24` is converted from big-endian byte order to a `u32` value.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
///
|
||||
pub fn to_u32(&self) -> u32 {
|
||||
u32::from_be_bytes([0, self.0[0], self.0[1], self.0[2]])
|
||||
}
|
||||
|
||||
/// Converts a slice of three bytes into a `u32` value.
|
||||
///
|
||||
/// The slice is interpreted as a big-endian byte order, and the resulting `u32` value is
|
||||
/// computed by combining the three bytes into a single 32-bit value.
|
||||
pub fn from_slice(slice: &[u8]) -> u32 {
|
||||
u32::from_be_bytes([0, slice[0], slice[1], slice[2]])
|
||||
}
|
||||
}
|
||||
|
||||
pub trait BufExt: Buf {
|
||||
/// Reads three bytes from the buffer and returns a `u32` value.
|
||||
///
|
||||
/// The bytes are read in big-endian byte order and then combined into a single `u32` value.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
///
|
||||
/// let mut buf = BytesMut::from(&[0x12, 0x34, 0x56][..]);
|
||||
/// let val = buf.get_u24();
|
||||
/// assert_eq!(val, 0x123456);
|
||||
fn get_u24(&mut self) -> u32 {
|
||||
let b1 = self.get_u8() as u32;
|
||||
let b2 = self.get_u8() as u32;
|
||||
|
||||
Reference in New Issue
Block a user