Skip to content

Commit

Permalink
Added simplesocks
Browse files Browse the repository at this point in the history
I'm adding this to debug/develop programs, don't use.
  • Loading branch information
eycorsican committed Sep 4, 2024
1 parent d4cb1c9 commit 1c68528
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 0 deletions.
2 changes: 2 additions & 0 deletions leaf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ all-endpoints = [
"inbound-http",
"inbound-shadowsocks",
"inbound-socks",
"inbound-simplesocks",
"inbound-tun",
"inbound-cat",
# outbounds
Expand Down Expand Up @@ -118,6 +119,7 @@ outbound-vmess = ["lz_fnv", "cfb-mode", "hmac", "aes", "sha3", "digest", "uuid",
inbound-trojan = ["sha2", "hex"]
inbound-shadowsocks = ["hkdf", "sha-1", "md-5", "tokio-util"]
inbound-socks = []
inbound-simplesocks = []
inbound-http = ["http"]
inbound-tun = ["tun", "netstack-lwip", "pnet_datalink"]
inbound-ws = ["tungstenite", "tokio-tungstenite", "url", "http"]
Expand Down
13 changes: 13 additions & 0 deletions leaf/src/app/inbound/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use crate::proxy::http;
use crate::proxy::quic;
#[cfg(feature = "inbound-shadowsocks")]
use crate::proxy::shadowsocks;
#[cfg(feature = "inbound-simplesocks")]
use crate::proxy::simplesocks;
#[cfg(feature = "inbound-socks")]
use crate::proxy::socks;
#[cfg(feature = "inbound-tls")]
Expand Down Expand Up @@ -86,6 +88,17 @@ impl InboundManager {
));
handlers.insert(tag.clone(), handler);
}
#[cfg(feature = "inbound-simplesocks")]
"simplesocks" => {
let stream = Arc::new(simplesocks::inbound::StreamHandler);
let datagram = Arc::new(simplesocks::inbound::DatagramHandler);
let handler = Arc::new(proxy::inbound::Handler::new(
tag.clone(),
Some(stream),
Some(datagram),
));
handlers.insert(tag.clone(), handler);
}
#[cfg(feature = "inbound-http")]
"http" => {
let stream = Arc::new(http::inbound::StreamHandler);
Expand Down
20 changes: 20 additions & 0 deletions leaf/src/config/conf/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub struct General {
pub http_port: Option<u16>,
pub socks_interface: Option<String>,
pub socks_port: Option<u16>,
pub simplesocks_interface: Option<String>,
pub simplesocks_port: Option<u16>,
pub api_interface: Option<String>,
pub api_port: Option<u16>,
pub routing_domain_resolve: Option<bool>,
Expand Down Expand Up @@ -331,6 +333,12 @@ pub fn from_lines(lines: Vec<io::Result<String>>) -> Result<Config> {
"socks-port" => {
general.socks_port = get_value::<u16>(parts[1]);
}
"simplesocks-interface" => {
general.simplesocks_interface = get_string(parts[1]);
}
"simplesocks-port" => {
general.simplesocks_port = get_value::<u16>(parts[1]);
}
"api-interface" => {
general.api_interface = get_string(parts[1]);
}
Expand Down Expand Up @@ -775,6 +783,18 @@ pub fn to_internal(conf: &mut Config) -> Result<internal::Config> {
inbound.port = ext_general.socks_port.unwrap() as u32;
inbounds.push(inbound);
}
if ext_general.simplesocks_interface.is_some() && ext_general.simplesocks_port.is_some() {
let mut inbound = internal::Inbound::new();
inbound.protocol = "simplesocks".to_string();
inbound.tag = "simplesocks".to_string();
inbound.address = ext_general
.simplesocks_interface
.as_ref()
.unwrap()
.to_string();
inbound.port = ext_general.simplesocks_port.unwrap() as u32;
inbounds.push(inbound);
}

if ext_general.tun_fd.is_some()
|| ext_general.tun_auto.is_some()
Expand Down
2 changes: 2 additions & 0 deletions leaf/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ pub mod redirect;
pub mod select;
#[cfg(any(feature = "inbound-shadowsocks", feature = "outbound-shadowsocks"))]
pub mod shadowsocks;
#[cfg(any(feature = "inbound-simplesocks"))]
pub mod simplesocks;
#[cfg(any(feature = "inbound-socks", feature = "outbound-socks"))]
pub mod socks;
#[cfg(feature = "outbound-static")]
Expand Down
89 changes: 89 additions & 0 deletions leaf/src/proxy/simplesocks/inbound/datagram.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::convert::TryFrom;
use std::io;
use std::net::SocketAddr;

use anyhow::anyhow;
use async_trait::async_trait;
use bytes::{BufMut, BytesMut};

use crate::{
proxy::*,
session::{DatagramSource, SocksAddr, SocksAddrWireType},
};

pub struct Handler;

#[async_trait]
impl InboundDatagramHandler for Handler {
async fn handle<'a>(&'a self, socket: AnyInboundDatagram) -> io::Result<AnyInboundTransport> {
Ok(InboundTransport::Datagram(
Box::new(Datagram { socket }),
None,
))
}
}

pub struct Datagram {
socket: Box<dyn InboundDatagram>,
}

impl InboundDatagram for Datagram {
fn split(
self: Box<Self>,
) -> (
Box<dyn InboundDatagramRecvHalf>,
Box<dyn InboundDatagramSendHalf>,
) {
let (rh, sh) = self.socket.split();
(
Box::new(DatagramRecvHalf(rh)),
Box::new(DatagramSendHalf(sh)),
)
}

fn into_std(self: Box<Self>) -> io::Result<std::net::UdpSocket> {
self.socket.into_std()
}
}

pub struct DatagramRecvHalf(Box<dyn InboundDatagramRecvHalf>);

#[async_trait]
impl InboundDatagramRecvHalf for DatagramRecvHalf {
async fn recv_from(
&mut self,
buf: &mut [u8],
) -> ProxyResult<(usize, DatagramSource, SocksAddr)> {
let mut recv_buf = vec![0u8; buf.len()];
let (n, src_addr, _) = self.0.recv_from(&mut recv_buf).await?;
let dst_addr = SocksAddr::try_from((&recv_buf[0..], SocksAddrWireType::PortLast))
.map_err(|e| ProxyError::DatagramWarn(anyhow!("Parse target address failed: {}", e)))?;
let header_size = dst_addr.size();
let payload_size = n - header_size;
assert!(buf.len() >= payload_size);
(&mut buf[..payload_size])
.copy_from_slice(&recv_buf[header_size..header_size + payload_size]);
Ok((payload_size, src_addr, dst_addr))
}
}

pub struct DatagramSendHalf(Box<dyn InboundDatagramSendHalf>);

#[async_trait]
impl InboundDatagramSendHalf for DatagramSendHalf {
async fn send_to(
&mut self,
buf: &[u8],
src_addr: &SocksAddr,
dst_addr: &SocketAddr,
) -> io::Result<usize> {
let mut send_buf = BytesMut::new();
src_addr.write_buf(&mut send_buf, SocksAddrWireType::PortLast);
send_buf.put_slice(buf);
self.0.send_to(&send_buf[..], src_addr, dst_addr).await
}

async fn close(&mut self) -> io::Result<()> {
self.0.close().await
}
}
5 changes: 5 additions & 0 deletions leaf/src/proxy/simplesocks/inbound/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod datagram;
mod stream;

pub use datagram::Handler as DatagramHandler;
pub use stream::Handler as StreamHandler;
27 changes: 27 additions & 0 deletions leaf/src/proxy/simplesocks/inbound/stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::io;

Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-macos (aarch64-apple-darwin)

unused import: `std::io`

Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest)

unused import: `std::io`

Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

unused import: `std::io`

Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-macos (x86_64-apple-darwin)

unused import: `std::io`

Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (x86_64-pc-windows-gnu)

unused import: `std::io`

Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (x86_64-unknown-linux-musl)

unused import: `std::io`

Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (armv7-unknown-linux-musleabihf)

unused import: `std::io`

Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (i686-unknown-linux-musl)

unused import: `std::io`

Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (aarch64-unknown-linux-musl)

unused import: `std::io`

Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (arm-unknown-linux-musleabi)

unused import: `std::io`

Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-lib-android

unused import: `std::io`

Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-lib-apple

unused import: `std::io`

use async_trait::async_trait;
use bytes::{BufMut, BytesMut};

Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-macos (aarch64-apple-darwin)

unused import: `BufMut`

Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest)

unused import: `BufMut`

Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

unused import: `BufMut`

Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-macos (x86_64-apple-darwin)

unused import: `BufMut`

Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (x86_64-pc-windows-gnu)

unused import: `BufMut`

Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (x86_64-unknown-linux-musl)

unused import: `BufMut`

Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (armv7-unknown-linux-musleabihf)

unused import: `BufMut`

Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (i686-unknown-linux-musl)

unused import: `BufMut`

Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (aarch64-unknown-linux-musl)

unused import: `BufMut`

Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (arm-unknown-linux-musleabi)

unused import: `BufMut`

Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-lib-android

unused import: `BufMut`

Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-lib-apple

unused import: `BufMut`
use tokio::io::{AsyncReadExt, AsyncWriteExt};

Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-macos (aarch64-apple-darwin)

unused imports: `AsyncReadExt` and `AsyncWriteExt`

Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest)

unused imports: `AsyncReadExt` and `AsyncWriteExt`

Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

unused imports: `AsyncReadExt` and `AsyncWriteExt`

Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-macos (x86_64-apple-darwin)

unused imports: `AsyncReadExt` and `AsyncWriteExt`

Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (x86_64-pc-windows-gnu)

unused imports: `AsyncReadExt` and `AsyncWriteExt`

Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (x86_64-unknown-linux-musl)

unused imports: `AsyncReadExt` and `AsyncWriteExt`

Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (armv7-unknown-linux-musleabihf)

unused imports: `AsyncReadExt` and `AsyncWriteExt`

Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (i686-unknown-linux-musl)

unused imports: `AsyncReadExt` and `AsyncWriteExt`

Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (aarch64-unknown-linux-musl)

unused imports: `AsyncReadExt` and `AsyncWriteExt`

Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (arm-unknown-linux-musleabi)

unused imports: `AsyncReadExt` and `AsyncWriteExt`

Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-lib-android

unused imports: `AsyncReadExt` and `AsyncWriteExt`

Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-lib-apple

unused imports: `AsyncReadExt` and `AsyncWriteExt`
use tracing::debug;

Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-macos (aarch64-apple-darwin)

unused import: `tracing::debug`

Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest)

unused import: `tracing::debug`

Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

unused import: `tracing::debug`

Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-macos (x86_64-apple-darwin)

unused import: `tracing::debug`

Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (x86_64-pc-windows-gnu)

unused import: `tracing::debug`

Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (x86_64-unknown-linux-musl)

unused import: `tracing::debug`

Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (armv7-unknown-linux-musleabihf)

unused import: `tracing::debug`

Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (i686-unknown-linux-musl)

unused import: `tracing::debug`

Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (aarch64-unknown-linux-musl)

unused import: `tracing::debug`

Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (arm-unknown-linux-musleabi)

unused import: `tracing::debug`

Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-lib-android

unused import: `tracing::debug`

Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-lib-apple

unused import: `tracing::debug`

use crate::{
proxy::*,
session::{Session, SocksAddr, SocksAddrWireType},
};

pub struct Handler;

#[async_trait]
impl InboundStreamHandler for Handler {
async fn handle<'a>(
&'a self,
mut sess: Session,
mut stream: AnyStream,
) -> std::io::Result<AnyInboundTransport> {
let mut buf = BytesMut::new();

Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-macos (aarch64-apple-darwin)

unused variable: `buf`

Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-macos (aarch64-apple-darwin)

variable does not need to be mutable

Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest)

unused variable: `buf`

Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / test (macos-latest)

variable does not need to be mutable

Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-macos (x86_64-apple-darwin)

unused variable: `buf`

Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-macos (x86_64-apple-darwin)

variable does not need to be mutable

Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (x86_64-pc-windows-gnu)

unused variable: `buf`

Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-cli-cross (x86_64-pc-windows-gnu)

variable does not need to be mutable

Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-lib-apple

unused variable: `buf`

Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs

View workflow job for this annotation

GitHub Actions / build-lib-apple

variable does not need to be mutable
let destination = SocksAddr::read_from(&mut stream, SocksAddrWireType::PortLast).await?;
sess.destination = destination;
Ok(InboundTransport::Stream(stream, sess))
}
}
2 changes: 2 additions & 0 deletions leaf/src/proxy/simplesocks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(feature = "inbound-simplesocks")]
pub mod inbound;

0 comments on commit 1c68528

Please sign in to comment.