Skip to content

Commit

Permalink
[Feature] Introduce handling of HTTP(S) proxy
Browse files Browse the repository at this point in the history
This adds new category of traffic for proxy, currently unstable and untested. Would be improved further.
  • Loading branch information
boris-sinyapkin committed Aug 12, 2024
1 parent 0a24d3e commit cba2468
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 179 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ rand = { version = "0.8.5" }
[dependencies]
anyhow = { version = "1.0.81" }
async-listen = { version = "0.2.1" }
async-trait = { version = "*" }
bytes = { version = "1.6.0" }
clap = { version = "4.5.3", features = ["derive"] }
cfg-if = { version = "1.0" }
Expand Down
2 changes: 1 addition & 1 deletion src/common/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ macro_rules! log_tcp_closed_conn {
macro_rules! log_tcp_established_conn {
($conn_addr:expr, $conn_label:expr) => {
info!(
"\n\n\tTCP {} connection has been OPENED: \
"\n\n\tTCP connection with {} label has been OPENED: \
\n\t\tpeer: '{}' \
\n",
$conn_label, $conn_addr,
Expand Down
14 changes: 0 additions & 14 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use anyhow::Result;
use std::fmt::Debug;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

pub mod stream;
pub mod tunnel;

pub trait LurkRequest {
Expand All @@ -14,15 +12,3 @@ pub trait LurkRequest {
pub trait LurkResponse {
async fn write_to<T: AsyncWriteExt + Unpin>(&self, stream: &mut T) -> Result<()>;
}

pub trait LurkResponseWrite {
async fn write_response<Response>(&mut self, response: Response) -> Result<()>
where
Response: LurkResponse + Debug + 'static;
}

pub trait LurkRequestRead {
async fn read_request<Request>(&mut self) -> Result<Request>
where
Request: LurkRequest + Debug + 'static;
}
72 changes: 0 additions & 72 deletions src/io/stream.rs

This file was deleted.

42 changes: 28 additions & 14 deletions src/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub mod listener {
.expect("Expect accepted connection before expired timeout")
.expect("Expect accepted TCP connection");

assert_eq!(LurkTcpConnectionLabel::SOCKS5, conn.label());
assert_eq!(LurkTcpConnectionLabel::Socks5, conn.label());
assert!(
listener.factory.get_active_tokens() <= conn_limit,
"Number of opened connections must not exceed the limit"
Expand All @@ -195,8 +195,8 @@ pub mod listener {

pub mod connection {

use crate::io::stream::{LurkStream, LurkTcpStream};
use anyhow::{bail, Result};
use async_trait::async_trait;
use std::{fmt::Display, io, net::SocketAddr};
use tokio::net::TcpStream;

Expand All @@ -209,7 +209,13 @@ pub mod connection {
#[repr(u8)]
pub enum LurkTcpConnectionLabel {
/// Traffic of TCP connection belongs to proxy SOCKS5 protocol
SOCKS5 = 0x05,
Socks5,

/// Traffic of TCP connection belongs to HTTP protocol
Http,

/// Traffic of TCP connection belongs to HTTPS protocol
HttpSecure,

/// Unknown traffic
Unknown(u8),
Expand All @@ -227,7 +233,9 @@ pub mod connection {

if peeked_bytes == 1 {
let label = match buff[0] {
0x05 => LurkTcpConnectionLabel::SOCKS5,
0x47 => LurkTcpConnectionLabel::Http,
0x43 => LurkTcpConnectionLabel::HttpSecure,
0x05 => LurkTcpConnectionLabel::Socks5,
v => LurkTcpConnectionLabel::Unknown(v),
};

Expand All @@ -241,8 +249,10 @@ pub mod connection {
impl Display for LurkTcpConnectionLabel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LurkTcpConnectionLabel::SOCKS5 => write!(f, "SOCKS5"),
LurkTcpConnectionLabel::Unknown(l) => write!(f, "Unknown TCP label {l:#04x}"),
LurkTcpConnectionLabel::Http => write!(f, "HTTP"),
LurkTcpConnectionLabel::HttpSecure => write!(f, "HTTPS"),
LurkTcpConnectionLabel::Socks5 => write!(f, "SOCKS5"),
LurkTcpConnectionLabel::Unknown(l) => write!(f, "unknown {l:#04x}"),
}
}
}
Expand Down Expand Up @@ -273,8 +283,7 @@ pub mod connection {
}

pub struct LurkTcpConnection {
/// Lurk wrapper of TcpStream
stream: LurkTcpStream,
stream: TcpStream,
/// Label describing traffic in this TCP connection
label: LurkTcpConnectionLabel,
/// Remote address that this connection is connected to
Expand All @@ -284,11 +293,11 @@ pub mod connection {
}

impl LurkTcpConnection {
fn new(tcp_stream: TcpStream, label: LurkTcpConnectionLabel) -> Result<LurkTcpConnection> {
fn new(stream: TcpStream, label: LurkTcpConnectionLabel) -> Result<LurkTcpConnection> {
Ok(LurkTcpConnection {
peer_addr: tcp_stream.peer_addr()?,
local_addr: tcp_stream.local_addr()?,
stream: LurkStream::new(tcp_stream),
peer_addr: stream.peer_addr()?,
local_addr: stream.local_addr()?,
stream,
label,
})
}
Expand All @@ -305,11 +314,16 @@ pub mod connection {
self.label
}

pub fn stream_mut(&mut self) -> &mut LurkTcpStream {
pub fn stream_mut(&mut self) -> &mut TcpStream {
&mut self.stream
}
}

#[async_trait]
pub trait LurkTcpConnectionHandler: Send {
async fn handle(&mut self, mut conn: LurkTcpConnection) -> Result<()>;
}

#[cfg(test)]
mod tests {

Expand Down Expand Up @@ -338,7 +352,7 @@ pub mod connection {
.accept()
.and_then(|(s, _)| async move {
let label = LurkTcpConnectionLabel::from_tcp_stream(&s).await.unwrap();
assert_eq!(LurkTcpConnectionLabel::SOCKS5, label);
assert_eq!(LurkTcpConnectionLabel::Socks5, label);
Ok(())
})
.await
Expand Down
Loading

0 comments on commit cba2468

Please sign in to comment.