diff --git a/crates/shadowsocks-service/src/local/loadbalancing/server_data.rs b/crates/shadowsocks-service/src/local/loadbalancing/server_data.rs index 86c224cffef1..9a3cac8fbd34 100644 --- a/crates/shadowsocks-service/src/local/loadbalancing/server_data.rs +++ b/crates/shadowsocks-service/src/local/loadbalancing/server_data.rs @@ -2,6 +2,7 @@ use std::{ fmt::{self, Debug}, + net::SocketAddr, sync::{ atomic::{AtomicU32, Ordering}, Arc, @@ -102,7 +103,7 @@ impl ServerIdent { } if let Some(bind_local_addr) = svr_cfg.outbound_bind_addr { - connect_opts.bind_local_addr = Some(bind_local_addr); + connect_opts.bind_local_addr = Some(SocketAddr::new(bind_local_addr, 0)); } if let Some(ref bind_interface) = svr_cfg.outbound_bind_interface { diff --git a/crates/shadowsocks-service/src/local/mod.rs b/crates/shadowsocks-service/src/local/mod.rs index 7ece6e16dcee..013cda15a025 100644 --- a/crates/shadowsocks-service/src/local/mod.rs +++ b/crates/shadowsocks-service/src/local/mod.rs @@ -2,6 +2,7 @@ use std::{ io::{self, ErrorKind}, + net::SocketAddr, sync::Arc, time::Duration, }; @@ -130,7 +131,7 @@ impl Server { vpn_protect_path: config.outbound_vpn_protect_path, bind_interface: config.outbound_bind_interface, - bind_local_addr: config.outbound_bind_addr, + bind_local_addr: config.outbound_bind_addr.map(|ip| SocketAddr::new(ip, 0)), ..Default::default() }; diff --git a/crates/shadowsocks-service/src/manager/mod.rs b/crates/shadowsocks-service/src/manager/mod.rs index 94b40dadebb7..6bc98ba82739 100644 --- a/crates/shadowsocks-service/src/manager/mod.rs +++ b/crates/shadowsocks-service/src/manager/mod.rs @@ -2,7 +2,7 @@ //! //! Service for managing multiple relay servers. [Manage Multiple Users](https://github.com/shadowsocks/shadowsocks/wiki/Manage-Multiple-Users) -use std::{io, sync::Arc}; +use std::{io, net::SocketAddr, sync::Arc}; use log::trace; use shadowsocks::net::{AcceptOpts, ConnectOpts}; @@ -40,7 +40,7 @@ pub async fn run(config: Config) -> io::Result<()> { #[cfg(target_os = "android")] vpn_protect_path: config.outbound_vpn_protect_path, - bind_local_addr: config.outbound_bind_addr, + bind_local_addr: config.outbound_bind_addr.map(|ip| SocketAddr::new(ip, 0)), bind_interface: config.outbound_bind_interface, ..Default::default() diff --git a/crates/shadowsocks-service/src/server/mod.rs b/crates/shadowsocks-service/src/server/mod.rs index e019658fc04d..1bea33f30534 100644 --- a/crates/shadowsocks-service/src/server/mod.rs +++ b/crates/shadowsocks-service/src/server/mod.rs @@ -1,6 +1,6 @@ //! Shadowsocks server -use std::{io, sync::Arc, time::Duration}; +use std::{io, net::SocketAddr, sync::Arc, time::Duration}; use futures::future; use log::trace; @@ -64,7 +64,7 @@ pub async fn run(config: Config) -> io::Result<()> { #[cfg(target_os = "android")] vpn_protect_path: config.outbound_vpn_protect_path, - bind_local_addr: config.outbound_bind_addr, + bind_local_addr: config.outbound_bind_addr.map(|ip| SocketAddr::new(ip, 0)), bind_interface: config.outbound_bind_interface, ..Default::default() @@ -110,7 +110,7 @@ pub async fn run(config: Config) -> io::Result<()> { } if let Some(bind_local_addr) = inst.outbound_bind_addr { - connect_opts.bind_local_addr = Some(bind_local_addr); + connect_opts.bind_local_addr = Some(SocketAddr::new(bind_local_addr, 0)); } if let Some(bind_interface) = inst.outbound_bind_interface { diff --git a/crates/shadowsocks/src/dns_resolver/hickory_dns_resolver.rs b/crates/shadowsocks/src/dns_resolver/hickory_dns_resolver.rs index 76a499da9e46..d7062574dfbc 100644 --- a/crates/shadowsocks/src/dns_resolver/hickory_dns_resolver.rs +++ b/crates/shadowsocks/src/dns_resolver/hickory_dns_resolver.rs @@ -80,7 +80,7 @@ impl RuntimeProvider for ShadowDnsRuntimeProvider { let mut connect_opts = self.connect_opts.clone(); if let Some(bind_addr) = bind_addr { - connect_opts.bind_local_addr = Some(bind_addr.ip()); + connect_opts.bind_local_addr = Some(bind_addr); } let wait_for = wait_for.unwrap_or_else(|| Duration::from_secs(5)); diff --git a/crates/shadowsocks/src/net/option.rs b/crates/shadowsocks/src/net/option.rs index d73ea9c29182..dffea6351f4b 100644 --- a/crates/shadowsocks/src/net/option.rs +++ b/crates/shadowsocks/src/net/option.rs @@ -1,6 +1,6 @@ //! Options for connecting to remote server -use std::{net::IpAddr, time::Duration}; +use std::{net::SocketAddr, time::Duration}; /// Options for connecting to TCP remote server #[derive(Debug, Clone, Default)] @@ -60,7 +60,7 @@ pub struct ConnectOpts { /// Outbound socket binds to this IP address, mostly for choosing network interfaces /// /// It only affects sockets that trying to connect to addresses with the same family - pub bind_local_addr: Option, + pub bind_local_addr: Option, /// Outbound socket binds to interface pub bind_interface: Option, diff --git a/crates/shadowsocks/src/net/sys/mod.rs b/crates/shadowsocks/src/net/sys/mod.rs index 6540508e951c..cd6f05aebe39 100644 --- a/crates/shadowsocks/src/net/sys/mod.rs +++ b/crates/shadowsocks/src/net/sys/mod.rs @@ -1,6 +1,6 @@ use std::{ io::{self, ErrorKind}, - net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}, + net::{Ipv4Addr, Ipv6Addr, SocketAddr}, }; use cfg_if::cfg_if; @@ -23,13 +23,13 @@ cfg_if! { fn set_common_sockopt_for_connect(addr: SocketAddr, socket: &TcpSocket, opts: &ConnectOpts) -> io::Result<()> { // Binds to IP address - if let Some(ip) = opts.bind_local_addr { - match (ip, addr.ip()) { - (IpAddr::V4(..), IpAddr::V4(..)) => { - socket.bind(SocketAddr::new(ip, 0))?; + if let Some(baddr) = opts.bind_local_addr { + match (baddr, addr) { + (SocketAddr::V4(..), SocketAddr::V4(..)) => { + socket.bind(baddr)?; } - (IpAddr::V6(..), IpAddr::V6(..)) => { - socket.bind(SocketAddr::new(ip, 0))?; + (SocketAddr::V6(..), SocketAddr::V6(..)) => { + socket.bind(baddr)?; } _ => {} } diff --git a/crates/shadowsocks/src/net/sys/unix/bsd/freebsd.rs b/crates/shadowsocks/src/net/sys/unix/bsd/freebsd.rs index 812e29c3792b..46d4ab5dc848 100644 --- a/crates/shadowsocks/src/net/sys/unix/bsd/freebsd.rs +++ b/crates/shadowsocks/src/net/sys/unix/bsd/freebsd.rs @@ -1,6 +1,6 @@ use std::{ io, mem, - net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}, + net::{Ipv4Addr, Ipv6Addr, SocketAddr}, os::unix::io::{AsRawFd, RawFd}, pin::Pin, ptr, @@ -227,8 +227,8 @@ pub fn set_disable_ip_fragmentation(af: AddrFamily, socket: &S) -> i #[inline] pub async fn create_outbound_udp_socket(af: AddrFamily, config: &ConnectOpts) -> io::Result { let bind_addr = match (af, config.bind_local_addr) { - (AddrFamily::Ipv4, Some(IpAddr::V4(ip))) => SocketAddr::new(ip.into(), 0), - (AddrFamily::Ipv6, Some(IpAddr::V6(ip))) => SocketAddr::new(ip.into(), 0), + (AddrFamily::Ipv4, Some(SocketAddr::V4(addr))) => addr.into(), + (AddrFamily::Ipv6, Some(SocketAddr::V6(addr))) => addr.into(), (AddrFamily::Ipv4, ..) => SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0), (AddrFamily::Ipv6, ..) => SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0), }; diff --git a/crates/shadowsocks/src/net/sys/unix/bsd/macos.rs b/crates/shadowsocks/src/net/sys/unix/bsd/macos.rs index de3717ee0a44..b170bf1013d0 100644 --- a/crates/shadowsocks/src/net/sys/unix/bsd/macos.rs +++ b/crates/shadowsocks/src/net/sys/unix/bsd/macos.rs @@ -3,7 +3,7 @@ use std::{ collections::HashMap, io::{self, ErrorKind}, mem, - net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, TcpStream as StdTcpStream}, + net::{Ipv4Addr, Ipv6Addr, SocketAddr, TcpStream as StdTcpStream}, os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}, pin::Pin, ptr, @@ -355,8 +355,8 @@ pub fn set_disable_ip_fragmentation(af: AddrFamily, socket: &S) -> i #[inline] pub async fn create_outbound_udp_socket(af: AddrFamily, config: &ConnectOpts) -> io::Result { let bind_addr = match (af, config.bind_local_addr) { - (AddrFamily::Ipv4, Some(IpAddr::V4(ip))) => SocketAddr::new(ip.into(), 0), - (AddrFamily::Ipv6, Some(IpAddr::V6(ip))) => SocketAddr::new(ip.into(), 0), + (AddrFamily::Ipv4, Some(SocketAddr::V4(addr))) => addr.into(), + (AddrFamily::Ipv6, Some(SocketAddr::V6(addr))) => addr.into(), (AddrFamily::Ipv4, ..) => SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0), (AddrFamily::Ipv6, ..) => SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0), }; diff --git a/crates/shadowsocks/src/net/sys/unix/linux/mod.rs b/crates/shadowsocks/src/net/sys/unix/linux/mod.rs index f007255a0f67..4f52e4682e9d 100644 --- a/crates/shadowsocks/src/net/sys/unix/linux/mod.rs +++ b/crates/shadowsocks/src/net/sys/unix/linux/mod.rs @@ -1,6 +1,6 @@ use std::{ io, mem, - net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}, + net::{Ipv4Addr, Ipv6Addr, SocketAddr}, os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd}, pin::Pin, ptr, @@ -286,8 +286,8 @@ pub fn set_disable_ip_fragmentation(af: AddrFamily, socket: &S) -> i #[inline] pub async fn create_outbound_udp_socket(af: AddrFamily, config: &ConnectOpts) -> io::Result { let bind_addr = match (af, config.bind_local_addr) { - (AddrFamily::Ipv4, Some(IpAddr::V4(ip))) => SocketAddr::new(ip.into(), 0), - (AddrFamily::Ipv6, Some(IpAddr::V6(ip))) => SocketAddr::new(ip.into(), 0), + (AddrFamily::Ipv4, Some(SocketAddr::V4(addr))) => addr.into(), + (AddrFamily::Ipv6, Some(SocketAddr::V6(addr))) => addr.into(), (AddrFamily::Ipv4, ..) => SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0), (AddrFamily::Ipv6, ..) => SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0), }; diff --git a/crates/shadowsocks/src/net/sys/unix/others.rs b/crates/shadowsocks/src/net/sys/unix/others.rs index ad0b32d3304d..cb047e4e9797 100644 --- a/crates/shadowsocks/src/net/sys/unix/others.rs +++ b/crates/shadowsocks/src/net/sys/unix/others.rs @@ -1,6 +1,6 @@ use std::{ io, - net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}, + net::{Ipv4Addr, Ipv6Addr, SocketAddr}, ops::{Deref, DerefMut}, os::fd::AsRawFd, pin::Pin, @@ -81,8 +81,8 @@ pub fn set_disable_ip_fragmentation(_af: AddrFamily, _socket: &S) -> #[inline] pub async fn create_outbound_udp_socket(af: AddrFamily, config: &ConnectOpts) -> io::Result { let bind_addr = match (af, config.bind_local_addr) { - (AddrFamily::Ipv4, Some(IpAddr::V4(ip))) => SocketAddr::new(ip.into(), 0), - (AddrFamily::Ipv6, Some(IpAddr::V6(ip))) => SocketAddr::new(ip.into(), 0), + (AddrFamily::Ipv4, Some(SocketAddr::V4(addr))) => addr.into(), + (AddrFamily::Ipv6, Some(SocketAddr::V6(addr))) => addr.into(), (AddrFamily::Ipv4, ..) => SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0), (AddrFamily::Ipv6, ..) => SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0), }; diff --git a/crates/shadowsocks/src/net/sys/windows/mod.rs b/crates/shadowsocks/src/net/sys/windows/mod.rs index ca5f70f5dbb9..12e04660397a 100644 --- a/crates/shadowsocks/src/net/sys/windows/mod.rs +++ b/crates/shadowsocks/src/net/sys/windows/mod.rs @@ -467,9 +467,9 @@ pub async fn create_inbound_udp_socket(addr: &SocketAddr, ipv6_only: bool) -> io /// Create a `UdpSocket` for connecting to `addr` #[inline(always)] pub async fn create_outbound_udp_socket(af: AddrFamily, opts: &ConnectOpts) -> io::Result { - let bind_addr = match (af, opts.bind_local_addr) { - (AddrFamily::Ipv4, Some(IpAddr::V4(ip))) => SocketAddr::new(ip.into(), 0), - (AddrFamily::Ipv6, Some(IpAddr::V6(ip))) => SocketAddr::new(ip.into(), 0), + let bind_addr = match (af, config.bind_local_addr) { + (AddrFamily::Ipv4, Some(SocketAddr::V4(addr))) => addr.into(), + (AddrFamily::Ipv6, Some(SocketAddr::V6(addr))) => addr.into(), (AddrFamily::Ipv4, ..) => SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 0), (AddrFamily::Ipv6, ..) => SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0), };