Skip to content

Commit

Permalink
feat(shadowsocks): IpStackCapability check Ipv4-mapped-Ipv6 by connect()
Browse files Browse the repository at this point in the history
- ref #1543
  • Loading branch information
zonyitoo committed Jun 16, 2024
1 parent cd25d25 commit 2f2d3cd
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions crates/shadowsocks/src/net/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
io::{self, ErrorKind},
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
time::Duration,
};

use cfg_if::cfg_if;
Expand Down Expand Up @@ -167,19 +168,32 @@ static IP_STACK_CAPABILITIES: Lazy<IpStackCapabilities> = Lazy::new(|| {
}

// Check IPv4-mapped-IPv6 (127.0.0.1)
if let Ok(ipv6_socket) = Socket::new(Domain::IPV6, Type::STREAM, Some(Protocol::TCP)) {
if let Ok(..) = ipv6_socket.set_only_v6(false) {
let local_host = SockAddr::from(SocketAddr::new(Ipv4Addr::LOCALHOST.to_ipv6_mapped().into(), 0));
if let Ok(..) = ipv6_socket.bind(&local_host) {
caps.support_ipv4_mapped_ipv6 = true;
debug!("IpStackCapability support_ipv4_mapped_ipv6=true");
}
}
if let Ok(..) = check_ipv4_mapped_ipv6_capability() {

Check warning on line 171 in crates/shadowsocks/src/net/sys/mod.rs

View workflow job for this annotation

GitHub Actions / clippy macos-latest

redundant pattern matching, consider using `is_ok()`

warning: redundant pattern matching, consider using `is_ok()` --> crates/shadowsocks/src/net/sys/mod.rs:171:12 | 171 | if let Ok(..) = check_ipv4_mapped_ipv6_capability() { | -------^^^^^^-------------------------------------- help: try: `if check_ipv4_mapped_ipv6_capability().is_ok()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching

Check warning on line 171 in crates/shadowsocks/src/net/sys/mod.rs

View workflow job for this annotation

GitHub Actions / clippy ubuntu-latest

redundant pattern matching, consider using `is_ok()`

warning: redundant pattern matching, consider using `is_ok()` --> crates/shadowsocks/src/net/sys/mod.rs:171:12 | 171 | if let Ok(..) = check_ipv4_mapped_ipv6_capability() { | -------^^^^^^-------------------------------------- help: try: `if check_ipv4_mapped_ipv6_capability().is_ok()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching
caps.support_ipv4_mapped_ipv6 = true;
debug!("IpStackCapability support_ipv4_mapped_ipv6=true");
}

caps
});

fn check_ipv4_mapped_ipv6_capability() -> io::Result<()> {
let local_host = SockAddr::from(SocketAddr::new(Ipv4Addr::LOCALHOST.to_ipv6_mapped().into(), 0));

let socket1 = Socket::new(Domain::IPV6, Type::STREAM, Some(Protocol::TCP))?;
socket1.set_only_v6(false)?;
socket1.bind(&local_host)?;
socket1.listen(128)?;

let socket1_address = socket1.local_addr()?;

let socket2 = Socket::new(Domain::IPV6, Type::STREAM, Some(Protocol::TCP))?;
socket2.set_only_v6(false)?;
socket2.bind(&local_host)?;
socket2.connect_timeout(&socket1_address, Duration::from_secs(1))?;

Ok(())
}

/// Get globally probed `IpStackCapabilities`
pub fn get_ip_stack_capabilities() -> &'static IpStackCapabilities {
&IP_STACK_CAPABILITIES
Expand Down

0 comments on commit 2f2d3cd

Please sign in to comment.