-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from rabbit-digger/feature-ss-udp
Feature ss udp
- Loading branch information
Showing
8 changed files
with
179 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use std::{ | ||
io, | ||
pin::Pin, | ||
task::{self, Poll}, | ||
}; | ||
|
||
use bytes::BytesMut; | ||
use futures::{ready, Sink, SinkExt, Stream, StreamExt}; | ||
use rd_interface::UdpSocket; | ||
use rd_std::util::forward_udp::{RawUdpSource, UdpPacket}; | ||
use shadowsocks::crypto::v1::CipherKind; | ||
use socks5_protocol::Address as S5Addr; | ||
|
||
use crate::udp::{decrypt_payload, encrypt_payload}; | ||
|
||
pub struct UdpSource { | ||
udp: UdpSocket, | ||
|
||
method: CipherKind, | ||
key: Box<[u8]>, | ||
} | ||
|
||
impl UdpSource { | ||
pub fn new(method: CipherKind, key: Box<[u8]>, udp: UdpSocket) -> UdpSource { | ||
UdpSource { udp, method, key } | ||
} | ||
} | ||
|
||
impl Stream for UdpSource { | ||
type Item = io::Result<UdpPacket>; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> { | ||
// TODO: support send_to domain | ||
let packet = loop { | ||
let (mut recv_buf, from) = match ready!(self.udp.poll_next_unpin(cx)) { | ||
Some(r) => r?, | ||
None => return Poll::Ready(None), | ||
}; | ||
let (n, addr) = decrypt_payload(self.method, &self.key, &mut recv_buf[..])?; | ||
// drop the packet if it's sent to domain silently | ||
let to = match addr { | ||
S5Addr::Domain(_, _) => continue, | ||
S5Addr::SocketAddr(s) => s, | ||
}; | ||
|
||
break UdpPacket { | ||
data: recv_buf.split_to(n).freeze(), | ||
from, | ||
to, | ||
}; | ||
}; | ||
|
||
Poll::Ready(Some(Ok(packet))) | ||
} | ||
} | ||
|
||
impl Sink<UdpPacket> for UdpSource { | ||
type Error = io::Error; | ||
|
||
fn poll_ready( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut task::Context<'_>, | ||
) -> Poll<Result<(), Self::Error>> { | ||
self.udp.poll_ready_unpin(cx) | ||
} | ||
|
||
fn start_send( | ||
mut self: Pin<&mut Self>, | ||
UdpPacket { from, to, data }: UdpPacket, | ||
) -> Result<(), Self::Error> { | ||
let mut send_buf = BytesMut::new(); | ||
|
||
encrypt_payload(self.method, &self.key, &from.into(), &data, &mut send_buf)?; | ||
|
||
self.udp.start_send_unpin((send_buf.freeze(), to.into())) | ||
} | ||
|
||
fn poll_flush( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut task::Context<'_>, | ||
) -> Poll<Result<(), Self::Error>> { | ||
self.udp.poll_flush_unpin(cx) | ||
} | ||
|
||
fn poll_close( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut task::Context<'_>, | ||
) -> Poll<Result<(), Self::Error>> { | ||
self.udp.poll_close_unpin(cx) | ||
} | ||
} | ||
|
||
impl RawUdpSource for UdpSource {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::wrapper::Cipher; | ||
|
||
use super::*; | ||
use rd_interface::{config::NetRef, IServer, IntoAddress, IntoDyn}; | ||
use rd_std::tests::{ | ||
assert_echo, assert_echo_udp, get_registry, spawn_echo_server, spawn_echo_server_udp, TestNet, | ||
}; | ||
use std::time::Duration; | ||
use tokio::time::sleep; | ||
|
||
#[test] | ||
fn test_ss_smoke() { | ||
let mut registry = get_registry(); | ||
super::init(&mut registry).unwrap(); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_ss_server_client() { | ||
let local = TestNet::new().into_dyn(); | ||
spawn_echo_server(&local, "127.0.0.1:26666").await; | ||
spawn_echo_server_udp(&local, "127.0.0.1:26666").await; | ||
|
||
let server_addr = "127.0.0.1:16666".into_address().unwrap(); | ||
let server_cfg = server::SSServerConfig { | ||
bind: server_addr.clone(), | ||
password: "password".into(), | ||
udp: true, | ||
cipher: Cipher::AES_128_GCM, | ||
}; | ||
let server = server::SSServer::new(local.clone(), local.clone(), server_cfg); | ||
tokio::spawn(async move { server.start().await }); | ||
|
||
sleep(Duration::from_secs(1)).await; | ||
|
||
let client_cfg = client::SSNetConfig { | ||
server: server_addr, | ||
password: "password".into(), | ||
udp: true, | ||
cipher: Cipher::AES_128_GCM, | ||
net: NetRef::new_with_value("local".to_string(), local.clone()), | ||
}; | ||
let client = client::SSNet::new(client_cfg).into_dyn(); | ||
|
||
assert_echo(&client, "127.0.0.1:26666").await; | ||
assert_echo_udp(&client, "127.0.0.1:26666").await; | ||
} |
Submodule rabbit-digger
updated
8 files
+1 −1 | Cargo.toml | |
+6 −0 | rd-interface/src/config/resolvable.rs | |
+1 −1 | rd-std/Cargo.toml | |
+0 −1 | rd-std/src/lib.rs | |
+5 −1 | rd-std/src/socks5/tests.rs | |
+16 −3 | rd-std/src/tests.rs | |
+3 −3 | rd-std/src/transparent/redir.rs | |
+3 −3 | rd-std/src/transparent/tproxy.rs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters