-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
I'm adding this to debug/develop programs, don't use.
- Loading branch information
There are no files selected for viewing
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 | ||
} | ||
} |
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; |
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 GitHub Actions / build-cli-macos (aarch64-apple-darwin)
Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / test (macos-latest)
Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / test (ubuntu-latest)
Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-macos (x86_64-apple-darwin)
Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (x86_64-pc-windows-gnu)
Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (x86_64-unknown-linux-musl)
Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (armv7-unknown-linux-musleabihf)
Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (i686-unknown-linux-musl)
Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (aarch64-unknown-linux-musl)
Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (arm-unknown-linux-musleabi)
Check warning on line 1 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-lib-android
|
||
|
||
use async_trait::async_trait; | ||
use bytes::{BufMut, BytesMut}; | ||
Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-macos (aarch64-apple-darwin)
Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / test (macos-latest)
Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / test (ubuntu-latest)
Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-macos (x86_64-apple-darwin)
Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (x86_64-pc-windows-gnu)
Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (x86_64-unknown-linux-musl)
Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (armv7-unknown-linux-musleabihf)
Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (i686-unknown-linux-musl)
Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (aarch64-unknown-linux-musl)
Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (arm-unknown-linux-musleabi)
Check warning on line 4 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-lib-android
|
||
use tokio::io::{AsyncReadExt, AsyncWriteExt}; | ||
Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-macos (aarch64-apple-darwin)
Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / test (macos-latest)
Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / test (ubuntu-latest)
Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-macos (x86_64-apple-darwin)
Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (x86_64-pc-windows-gnu)
Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (x86_64-unknown-linux-musl)
Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (armv7-unknown-linux-musleabihf)
Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (i686-unknown-linux-musl)
Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (aarch64-unknown-linux-musl)
Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (arm-unknown-linux-musleabi)
Check warning on line 5 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-lib-android
|
||
use tracing::debug; | ||
Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-macos (aarch64-apple-darwin)
Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / test (macos-latest)
Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / test (ubuntu-latest)
Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-macos (x86_64-apple-darwin)
Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (x86_64-pc-windows-gnu)
Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (x86_64-unknown-linux-musl)
Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (armv7-unknown-linux-musleabihf)
Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (i686-unknown-linux-musl)
Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (aarch64-unknown-linux-musl)
Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (arm-unknown-linux-musleabi)
Check warning on line 6 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-lib-android
|
||
|
||
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 GitHub Actions / build-cli-macos (aarch64-apple-darwin)
Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-macos (aarch64-apple-darwin)
Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / test (macos-latest)
Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / test (macos-latest)
Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-macos (x86_64-apple-darwin)
Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-macos (x86_64-apple-darwin)
Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (x86_64-pc-windows-gnu)
Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-cli-cross (x86_64-pc-windows-gnu)
Check warning on line 22 in leaf/src/proxy/simplesocks/inbound/stream.rs GitHub Actions / build-lib-apple
|
||
let destination = SocksAddr::read_from(&mut stream, SocksAddrWireType::PortLast).await?; | ||
sess.destination = destination; | ||
Ok(InboundTransport::Stream(stream, sess)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[cfg(feature = "inbound-simplesocks")] | ||
pub mod inbound; |