Skip to content

Commit

Permalink
feat(udp): support udp over shadowsocks (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibigbug authored Sep 11, 2024
1 parent bb415ea commit 09dabbc
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 78 deletions.
86 changes: 35 additions & 51 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion clash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ repository = { workspace = true }
version = { workspace = true }
edition = { workspace = true }

[features]
default = []
shadowsocks = ["clash_lib/shadowsocks"]
tuic = ["clash_lib/tuic"]
tracing = ["clash_lib/tracing"]
bench = ["clash_lib/bench"]
onion = ["clash_lib/onion"]

[dependencies]
clap = { version = "4.5.17", features = ["derive"] }

clash_lib = { path = "../clash_lib", version = "*" }
clash_lib = { path = "../clash_lib", version = "*", default-features = false }
6 changes: 4 additions & 2 deletions clash/tests/data/config/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ socks-port: 8889
mixed-port: 8899

tun:
enable: true
enable: false
device-id: "dev://utun1989"
route-all: false
gateway: "198.19.0.1/32"
Expand Down Expand Up @@ -87,7 +87,7 @@ proxy-groups:
# - "h2-vmess"
# - "tls-vmess"
# - "grpc-vmess"
# - "ss-simple"
- "ss-simple"
# - "trojan"
# - "auto"
# - "fallback-auto"
Expand Down Expand Up @@ -283,6 +283,8 @@ rule-providers:
behavior: domain

rules:
- IP-CIDR,1.1.1.1/32,udp-relay
- IP-CIDR,8.8.8.8/32,udp-relay
- DOMAIN,google.com,ws-vmess
- DOMAIN-KEYWORD,httpbin,trojan-grpc
- DOMAIN,ipinfo.io,DIRECT
Expand Down
4 changes: 2 additions & 2 deletions clash_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ default = []
shadowsocks = ["dep:shadowsocks"]
tuic = ["dep:tuic", "dep:tuic-quinn", "dep:quinn", "dep:register-count"]
tracing = []
bench = ["criterion"]
bench = ["dep:criterion"]
onion = ["arti-client/onion-service-client"]

[dependencies]
Expand Down Expand Up @@ -112,7 +112,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-oslog = { branch = "main", git = "https://github.com/Absolucy/tracing-oslog.git" }
tracing-appender = "0.2"

shadowsocks = { version = "1", optional = true, features=["aead-cipher-2022","stream-cipher"] }
shadowsocks = { git = "https://github.com/Watfaq/shadowsocks-rust", rev = "c6cb7fd906fe9f4126f724ae252f8a67cc1926b1", optional = true, features=["aead-cipher-2022","stream-cipher"] }
maxminddb = "0.24"
public-suffix = "0.1"
murmur3 = "0.5"
Expand Down
9 changes: 5 additions & 4 deletions clash_lib/src/proxy/datagram.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
app::dns::ThreadSafeDNSResolver,
common::errors::new_io_error,
proxy::{socks::Socks5UDPCodec, AnyOutboundDatagram, InboundDatagram},
session::SocksAddr,
};
Expand Down Expand Up @@ -242,10 +243,10 @@ impl Sink<UdpPacket> for OutboundDatagramImpl {
let res = if wrote_all {
Ok(())
} else {
Err(io::Error::new(
io::ErrorKind::Other,
"failed to write entire datagram",
))
Err(new_io_error(format!(
"failed to send all data, only sent {} bytes",
n
)))
};
Poll::Ready(res)
} else {
Expand Down
4 changes: 2 additions & 2 deletions clash_lib/src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ pub type AnyInboundDatagram =
Box<dyn InboundDatagram<UdpPacket, Error = io::Error, Item = UdpPacket>>;

pub trait OutboundDatagram<Item>:
Stream<Item = Item> + Sink<Item, Error = io::Error> + Send + Sync + Unpin
Stream<Item = Item> + Sink<Item, Error = io::Error> + Send + Sync + Unpin + 'static
{
}

impl<T, U> OutboundDatagram<U> for T where
T: Stream<Item = U> + Sink<U, Error = io::Error> + Send + Sync + Unpin
T: Stream<Item = U> + Sink<U, Error = io::Error> + Send + Sync + Unpin + 'static
{
}

Expand Down
10 changes: 6 additions & 4 deletions clash_lib/src/proxy/relay/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ impl OutboundHandler for Handler {
connector =
Box::new(ProxyConnector::new(proxy.clone(), connector));
}

debug!("relay `{}` via proxy `{}`", self.name(), last[0].name());
let d = last[0]
.connect_datagram_with_connector(
sess,
Expand Down Expand Up @@ -228,7 +230,7 @@ mod tests {

#[tokio::test]
#[serial_test::serial]
async fn test_relay_1_tcp() -> anyhow::Result<()> {
async fn test_relay_1() -> anyhow::Result<()> {
let ss_opts = crate::proxy::shadowsocks::HandlerOptions {
name: "test-ss".to_owned(),
common_opts: Default::default(),
Expand Down Expand Up @@ -259,14 +261,14 @@ mod tests {
run_test_suites_and_cleanup(
handler,
get_ss_runner(port).await?,
Suite::tcp_tests(),
Suite::all(),
)
.await
}

#[tokio::test]
#[serial_test::serial]
async fn test_relay_2_tcp() -> anyhow::Result<()> {
async fn test_relay_2() -> anyhow::Result<()> {
let ss_opts = crate::proxy::shadowsocks::HandlerOptions {
name: "test-ss".to_owned(),
common_opts: Default::default(),
Expand Down Expand Up @@ -298,7 +300,7 @@ mod tests {
run_test_suites_and_cleanup(
handler,
get_ss_runner(port).await?,
Suite::tcp_tests(),
Suite::all(),
)
.await
}
Expand Down
Loading

0 comments on commit 09dabbc

Please sign in to comment.