Skip to content

Commit 95a3c9c

Browse files
committed
socks5 udp converts IPv4-mapped-IPv6 to IPv4
1 parent 90ae706 commit 95a3c9c

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

crates/shadowsocks-service/src/local/net/udp/association.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,8 @@ where
475475
target_os = "watchos",
476476
target_os = "tvos",
477477
target_os = "freebsd",
478-
target_os = "dragonfly",
479-
target_os = "netbsd",
478+
// target_os = "dragonfly",
479+
// target_os = "netbsd",
480480
));
481481

482482
let socket = if UDP_SOCKET_SUPPORT_DUAL_STACK {

crates/shadowsocks-service/src/local/socks/server/socks5/udprelay.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{
44
io::{self, Cursor},
5-
net::SocketAddr,
5+
net::{IpAddr, SocketAddr},
66
sync::Arc,
77
time::Duration,
88
};
@@ -26,6 +26,7 @@ use crate::local::{
2626
context::ServiceContext,
2727
loadbalancing::PingBalancer,
2828
net::{UdpAssociationManager, UdpInboundWrite},
29+
utils::to_ipv4_mapped,
2930
};
3031

3132
#[derive(Clone)]
@@ -36,6 +37,22 @@ struct Socks5UdpInboundWriter {
3637
#[async_trait]
3738
impl UdpInboundWrite for Socks5UdpInboundWriter {
3839
async fn send_to(&self, peer_addr: SocketAddr, remote_addr: &Address, data: &[u8]) -> io::Result<()> {
40+
let remote_addr = match remote_addr {
41+
Address::SocketAddress(sa) => {
42+
// Try to convert IPv4 mapped IPv6 address if server is running on dual-stack mode
43+
let saddr = match *sa {
44+
SocketAddr::V4(..) => *sa,
45+
SocketAddr::V6(ref v6) => match to_ipv4_mapped(v6.ip()) {
46+
Some(v4) => SocketAddr::new(IpAddr::from(v4), v6.port()),
47+
None => *sa,
48+
},
49+
};
50+
51+
Address::SocketAddress(saddr)
52+
}
53+
daddr => daddr.clone(),
54+
};
55+
3956
// Reassemble packet
4057
let mut payload_buffer = BytesMut::new();
4158
let header = UdpAssociateHeader::new(0, remote_addr.clone());

crates/shadowsocks-service/src/server/udprelay.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,8 @@ impl UdpAssociationContext {
655655
target_os = "watchos",
656656
target_os = "tvos",
657657
target_os = "freebsd",
658-
target_os = "dragonfly",
659-
target_os = "netbsd",
658+
// target_os = "dragonfly",
659+
// target_os = "netbsd",
660660
));
661661

662662
let socket = if UDP_SOCKET_SUPPORT_DUAL_STACK {

0 commit comments

Comments
 (0)