Skip to content

Commit 571dbf9

Browse files
committed
Debug IPv4/v6 issue with UDP client connecting on macOS
1 parent 786985f commit 571dbf9

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

examples/udp_client.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#[macro_use]
33
extern crate log;
44

5+
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
6+
57
use fast_socks5::{client::Socks5Datagram, Result};
68
use structopt::StructOpt;
79
use tokio::{
@@ -28,7 +30,7 @@ use tokio::{
2830
struct Opt {
2931
/// Socks5 server address + port, e.g. `127.0.0.1:1080`
3032
#[structopt(short, long)]
31-
pub socks_server: String,
33+
pub socks_server: SocketAddr,
3234

3335
/// Target (DNS) server address, e.g. `8.8.8.8`
3436
#[structopt(short = "a", long)]
@@ -60,18 +62,25 @@ async fn spawn_socks_client() -> Result<()> {
6062

6163
// Creating a SOCKS stream to the target address through the socks server
6264
let backing_socket = TcpStream::connect(opt.socks_server).await?;
65+
// At least on some platforms it is important to use the same protocol as the server
66+
// XXX: assumes the returned UDP proxy will have the same protocol as the socks_server
67+
let client_bind_addr = if opt.socks_server.is_ipv4() {
68+
SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0)
69+
} else {
70+
SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0)
71+
};
6372
let mut socks = match opt.username {
6473
Some(username) => {
6574
Socks5Datagram::bind_with_password(
6675
backing_socket,
67-
"[::]:0",
76+
client_bind_addr,
6877
&username,
6978
&opt.password.expect("Please fill the password"),
7079
)
7180
.await?
7281
}
7382

74-
_ => Socks5Datagram::bind(backing_socket, "[::]:0").await?,
83+
_ => Socks5Datagram::bind(backing_socket, client_bind_addr).await?,
7584
};
7685

7786
// Once socket creation is completed, can start to communicate with the server

src/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,9 @@ impl<S: AsyncRead + AsyncWrite + Unpin> Socks5Datagram<S> {
466466
.to_socket_addrs()?
467467
.next()
468468
.context("unreachable")?;
469+
info!("UdpSocket client connecting to {}", proxy_addr_resolved);
469470
out_sock.connect(proxy_addr_resolved).await?;
470-
info!("UdpSocket client connected to {}", proxy_addr_resolved);
471+
info!("UdpSocket client connected");
471472

472473
Ok(Socks5Datagram {
473474
socket: out_sock,

0 commit comments

Comments
 (0)