mirror of
https://github.com/shadowsocks/shadowsocks-rust.git
synced 2026-11-25 16:07:33 +00:00
Add bind_addr() helper method for ServerAddr
UDP relay server supports using local addr for bind()
This commit is contained in:
+18
-2
@@ -48,7 +48,7 @@ use std::{
|
||||
error,
|
||||
fmt::{self, Debug, Display, Formatter},
|
||||
fs::OpenOptions,
|
||||
io::Read,
|
||||
io::{self, Read},
|
||||
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
|
||||
option::Option,
|
||||
path::Path,
|
||||
@@ -67,7 +67,12 @@ use serde_urlencoded;
|
||||
use trust_dns_resolver::config::{NameServerConfigGroup, ResolverConfig};
|
||||
use url::{self, Url};
|
||||
|
||||
use crate::{crypto::cipher::CipherType, plugin::PluginConfig, relay::socks5::Address};
|
||||
use crate::{
|
||||
context::Context,
|
||||
crypto::cipher::CipherType,
|
||||
plugin::PluginConfig,
|
||||
relay::{dns_resolver::resolve_bind_addr, socks5::Address},
|
||||
};
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||
struct SSConfig {
|
||||
@@ -142,6 +147,17 @@ impl ServerAddr {
|
||||
ServerAddr::DomainName(_, p) => p,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert for calling `bind()`
|
||||
pub async fn bind_addr(&self, context: &Context) -> io::Result<SocketAddr> {
|
||||
match resolve_bind_addr(context, self).await {
|
||||
Ok(s) => Ok(s),
|
||||
Err(err) => {
|
||||
error!("Failed to resolve {} for bind(), error: {}", self, err);
|
||||
Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse `ServerAddr` error
|
||||
|
||||
@@ -42,7 +42,6 @@ use crate::{
|
||||
config::ServerConfig,
|
||||
context::SharedContext,
|
||||
relay::{
|
||||
dns_resolver::resolve_bind_addr,
|
||||
loadbalancing::server::{LoadBalancer, PingBalancer, PingServer, PingServerType},
|
||||
socks5::Address,
|
||||
},
|
||||
@@ -402,7 +401,7 @@ impl PingServer for ServerScore {
|
||||
/// Starts a TCP local server with HTTP proxy protocol
|
||||
pub async fn run(context: SharedContext) -> io::Result<()> {
|
||||
let local_addr = context.config().local.as_ref().expect("Missing local config");
|
||||
let bind_addr = resolve_bind_addr(&*context, local_addr).await?;
|
||||
let bind_addr = local_addr.bind_addr(&*context).await?;
|
||||
|
||||
let servers = context
|
||||
.config()
|
||||
|
||||
@@ -12,10 +12,7 @@ use tokio::{
|
||||
net::{TcpListener, TcpStream},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
context::SharedContext,
|
||||
relay::{dns_resolver::resolve_bind_addr, socks5::Address},
|
||||
};
|
||||
use crate::{context::SharedContext, relay::socks5::Address};
|
||||
|
||||
use super::{
|
||||
monitor::TcpMonStream,
|
||||
@@ -75,7 +72,7 @@ async fn handle_client(
|
||||
let bind_addr = match context.config().local {
|
||||
None => None,
|
||||
Some(ref addr) => {
|
||||
let ba = resolve_bind_addr(context, addr).await?;
|
||||
let ba = addr.bind_addr(context).await?;
|
||||
Some(ba)
|
||||
}
|
||||
};
|
||||
@@ -161,7 +158,7 @@ pub async fn run(context: SharedContext) -> io::Result<()> {
|
||||
for svr_cfg in &context.config().server {
|
||||
let mut listener = {
|
||||
let addr = svr_cfg.plugin_addr().as_ref().unwrap_or_else(|| svr_cfg.addr());
|
||||
let addr = resolve_bind_addr(&*context, addr).await?;
|
||||
let addr = addr.bind_addr(&*context).await?;
|
||||
|
||||
let listener = TcpListener::bind(&addr)
|
||||
.await
|
||||
|
||||
@@ -25,7 +25,6 @@ use crate::{
|
||||
config::ServerConfig,
|
||||
context::{Context, SharedContext},
|
||||
relay::{
|
||||
dns_resolver::resolve_bind_addr,
|
||||
loadbalancing::server::{LoadBalancer, PingBalancer, PingServer, PingServerType},
|
||||
socks5::{self, Address, HandshakeRequest, HandshakeResponse, TcpRequestHeader, TcpResponseHeader},
|
||||
},
|
||||
@@ -267,7 +266,7 @@ impl PingServer for ServerScore {
|
||||
/// Starts a TCP local server with Socks5 proxy protocol
|
||||
pub async fn run(context: SharedContext) -> io::Result<()> {
|
||||
let local_addr = context.config().local.as_ref().expect("Missing local config");
|
||||
let bind_addr = resolve_bind_addr(&*context, local_addr).await?;
|
||||
let bind_addr = local_addr.bind_addr(&*context).await?;
|
||||
|
||||
let mut listener = TcpListener::bind(&bind_addr)
|
||||
.await
|
||||
|
||||
@@ -17,7 +17,6 @@ use crate::{
|
||||
config::ServerConfig,
|
||||
context::{Context, SharedContext},
|
||||
relay::{
|
||||
dns_resolver::resolve_bind_addr,
|
||||
loadbalancing::server::{LoadBalancer, PingBalancer, PingServer, PingServerType},
|
||||
socks5::Address,
|
||||
},
|
||||
@@ -143,7 +142,7 @@ pub async fn run(context: SharedContext) -> io::Result<()> {
|
||||
);
|
||||
|
||||
let local_addr = context.config().local.as_ref().expect("Missing local config");
|
||||
let bind_addr = resolve_bind_addr(&*context, local_addr).await?;
|
||||
let bind_addr = local_addr.bind_addr(&*context).await?;
|
||||
|
||||
let mut listener = TcpListener::bind(&bind_addr)
|
||||
.await
|
||||
|
||||
@@ -21,7 +21,7 @@ use tokio::{
|
||||
use crate::{
|
||||
config::ServerConfig,
|
||||
context::{Context, SharedContext},
|
||||
relay::{dns_resolver::resolve_bind_addr, socks5::Address, utils::try_timeout},
|
||||
relay::{socks5::Address, utils::try_timeout},
|
||||
};
|
||||
|
||||
use super::{
|
||||
@@ -52,12 +52,22 @@ impl UdpAssociation {
|
||||
src_addr: SocketAddr,
|
||||
mut response_tx: mpsc::Sender<(SocketAddr, BytesMut)>,
|
||||
) -> io::Result<UdpAssociation> {
|
||||
debug!("created UDP Association for {}", src_addr);
|
||||
|
||||
// Create a socket for receiving packets
|
||||
let local_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0);
|
||||
let local_addr = match context.config().local {
|
||||
None => {
|
||||
// Let system allocate an address for us
|
||||
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0)
|
||||
}
|
||||
Some(ref addr) => {
|
||||
// Uses configured local address
|
||||
addr.bind_addr(&*context).await?
|
||||
}
|
||||
};
|
||||
let remote_udp = create_socket(&local_addr).await?;
|
||||
|
||||
let local_addr = remote_udp.local_addr().expect("Could not determine port bound to");
|
||||
debug!("Created UDP Association for {} from {}", src_addr, local_addr);
|
||||
|
||||
// Create a channel for sending packets to remote
|
||||
// FIXME: Channel size 1024?
|
||||
let (tx, mut rx) = mpsc::channel::<Vec<u8>>(1024);
|
||||
@@ -80,7 +90,7 @@ impl UdpAssociation {
|
||||
if let Err(err) =
|
||||
UdpAssociation::relay_l2r(&*context, src_addr, &mut sender, &pkt[..], timeout, &*c_svr_cfg).await
|
||||
{
|
||||
error!("failed to relay packet, {} -> ..., error: {}", src_addr, err);
|
||||
error!("Failed to relay packet, {} -> ..., error: {}", src_addr, err);
|
||||
|
||||
// FIXME: Ignore? Or how to deal with it?
|
||||
}
|
||||
@@ -97,7 +107,7 @@ impl UdpAssociation {
|
||||
match UdpAssociation::relay_r2l(src_addr, &mut receiver, &mut response_tx, &*svr_cfg).await {
|
||||
Ok(..) => {}
|
||||
Err(err) => {
|
||||
error!("failed to receive packet, {} <- .., error: {}", src_addr, err);
|
||||
error!("Failed to receive packet, {} <- .., error: {}", src_addr, err);
|
||||
|
||||
// FIXME: Don't break, or if you can find a way to drop the UdpAssociation
|
||||
// break;
|
||||
@@ -131,12 +141,12 @@ impl UdpAssociation {
|
||||
let decrypted_pkt = match decrypt_payload(svr_cfg.method(), svr_cfg.key(), pkt) {
|
||||
Ok(Some(pkt)) => pkt,
|
||||
Ok(None) => {
|
||||
error!("failed to decrypt pkt in UDP relay, packet too short");
|
||||
error!("Failed to decrypt pkt in UDP relay, packet too short");
|
||||
let err = io::Error::new(io::ErrorKind::InvalidData, "packet too short");
|
||||
return Err(err);
|
||||
}
|
||||
Err(err) => {
|
||||
error!("failed to decrypt pkt in UDP relay: {}", err);
|
||||
error!("Failed to decrypt pkt in UDP relay: {}", err);
|
||||
let err = io::Error::new(io::ErrorKind::InvalidData, "decrypt failed");
|
||||
return Err(err);
|
||||
}
|
||||
@@ -225,7 +235,7 @@ impl UdpAssociation {
|
||||
|
||||
// Send back to src_addr
|
||||
if let Err(err) = response_tx.send((src_addr, encrypt_buf)).await {
|
||||
error!("failed to send packet into response channel, error: {}", err);
|
||||
error!("Failed to send packet into response channel, error: {}", err);
|
||||
|
||||
// FIXME: What to do? Ignore?
|
||||
}
|
||||
@@ -245,7 +255,7 @@ impl UdpAssociation {
|
||||
}
|
||||
|
||||
async fn listen(context: SharedContext, svr_cfg: Arc<ServerConfig>) -> io::Result<()> {
|
||||
let listen_addr = resolve_bind_addr(&*context, svr_cfg.addr()).await?;
|
||||
let listen_addr = svr_cfg.addr().bind_addr(&*context).await?;
|
||||
|
||||
let listener = create_socket(&listen_addr).await?;
|
||||
let local_addr = listener.local_addr().expect("Could not determine port bound to");
|
||||
@@ -306,7 +316,7 @@ async fn listen(context: SharedContext, svr_cfg: Arc<ServerConfig>) -> io::Resul
|
||||
// Packet length is limited by MAXIMUM_UDP_PAYLOAD_SIZE, excess bytes will be discarded.
|
||||
let pkt = &pkt_buf[..recv_len];
|
||||
|
||||
trace!("received UDP packet from {}, length {} bytes", src, recv_len);
|
||||
trace!("Received UDP packet from {}, length {} bytes", src, recv_len);
|
||||
|
||||
if recv_len == 0 {
|
||||
// For windows, it will generate a ICMP Port Unreachable Message
|
||||
@@ -357,7 +367,7 @@ pub async fn run(context: SharedContext) -> io::Result<()> {
|
||||
|
||||
match vec_fut.into_future().await.0 {
|
||||
Some(res) => {
|
||||
error!("one of UDP servers exited unexpectly, result: {:?}", res);
|
||||
error!("One of UDP servers exited unexpectly, result: {:?}", res);
|
||||
let err = io::Error::new(io::ErrorKind::Other, "server exited unexpectly");
|
||||
Err(err)
|
||||
}
|
||||
|
||||
@@ -25,7 +25,6 @@ use crate::{
|
||||
config::{ServerAddr, ServerConfig},
|
||||
context::{Context, SharedContext},
|
||||
relay::{
|
||||
dns_resolver::resolve_bind_addr,
|
||||
loadbalancing::server::{LoadBalancer, PingBalancer, PingServer, PingServerType},
|
||||
socks5::{Address, UdpAssociateHeader},
|
||||
utils::try_timeout,
|
||||
@@ -81,12 +80,13 @@ impl UdpAssociation {
|
||||
src_addr: SocketAddr,
|
||||
mut response_tx: mpsc::Sender<(SocketAddr, Vec<u8>)>,
|
||||
) -> io::Result<UdpAssociation> {
|
||||
debug!("created UDP Association for {}", src_addr);
|
||||
|
||||
// Create a socket for receiving packets
|
||||
let local_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0);
|
||||
let remote_udp = create_socket(&local_addr).await?;
|
||||
|
||||
let local_addr = remote_udp.local_addr().expect("Could not determine port bound to");
|
||||
debug!("Created UDP Association for {} from {}", src_addr, local_addr);
|
||||
|
||||
// Create a channel for sending packets to remote
|
||||
// FIXME: Channel size 1024?
|
||||
let (tx, mut rx) = mpsc::channel::<Vec<u8>>(1024);
|
||||
@@ -111,7 +111,7 @@ impl UdpAssociation {
|
||||
if let Err(err) =
|
||||
UdpAssociation::relay_l2r(&*context, src_addr, &mut sender, &pkt[..], timeout, svr_cfg).await
|
||||
{
|
||||
error!("failed to send packet {} -> ..., error: {}", src_addr, err);
|
||||
error!("Failed to send packet {} -> ..., error: {}", src_addr, err);
|
||||
|
||||
// FIXME: Ignore? Or how to deal with it?
|
||||
}
|
||||
@@ -130,7 +130,7 @@ impl UdpAssociation {
|
||||
match UdpAssociation::relay_r2l(src_addr, &mut receiver, &mut response_tx, svr_cfg).await {
|
||||
Ok(..) => {}
|
||||
Err(err) => {
|
||||
error!("failed to receive packet, {} <- .., error: {}", src_addr, err);
|
||||
error!("Failed to receive packet, {} <- .., error: {}", src_addr, err);
|
||||
|
||||
// FIXME: Don't break, or if you can find a way to drop the UdpAssociation
|
||||
// break;
|
||||
@@ -233,7 +233,7 @@ impl UdpAssociation {
|
||||
|
||||
// Send back to src_addr
|
||||
if let Err(err) = response_tx.send((src_addr, payload)).await {
|
||||
error!("failed to send packet into response channel, error: {}", err);
|
||||
error!("Failed to send packet into response channel, error: {}", err);
|
||||
|
||||
// FIXME: What to do? Ignore?
|
||||
}
|
||||
@@ -284,7 +284,7 @@ impl PingServer for ServerScore {
|
||||
/// Starts a UDP local server
|
||||
pub async fn run(context: SharedContext) -> io::Result<()> {
|
||||
let local_addr = context.config().local.as_ref().expect("Missing local config");
|
||||
let bind_addr = resolve_bind_addr(&*context, local_addr).await?;
|
||||
let bind_addr = local_addr.bind_addr(&*context).await?;
|
||||
|
||||
let l = create_socket(&bind_addr).await?;
|
||||
let local_addr = l.local_addr().expect("Could not determine port bound to");
|
||||
@@ -350,7 +350,7 @@ pub async fn run(context: SharedContext) -> io::Result<()> {
|
||||
// Copy bytes, because udp_associate runs in another tokio Task
|
||||
let pkt = &pkt_buf[..recv_len];
|
||||
|
||||
trace!("received UDP packet from {}, length {} bytes", src, recv_len);
|
||||
trace!("Received UDP packet from {}, length {} bytes", src, recv_len);
|
||||
|
||||
if recv_len == 0 {
|
||||
// For windows, it will generate a ICMP Port Unreachable Message
|
||||
|
||||
@@ -25,7 +25,6 @@ use crate::{
|
||||
config::{ServerAddr, ServerConfig},
|
||||
context::{Context, SharedContext},
|
||||
relay::{
|
||||
dns_resolver::resolve_bind_addr,
|
||||
loadbalancing::server::{LoadBalancer, PingBalancer, PingServer, PingServerType},
|
||||
socks5::Address,
|
||||
utils::try_timeout,
|
||||
@@ -61,12 +60,13 @@ impl UdpAssociation {
|
||||
src_addr: SocketAddr,
|
||||
mut response_tx: mpsc::Sender<(SocketAddr, Vec<u8>)>,
|
||||
) -> io::Result<UdpAssociation> {
|
||||
debug!("Created UDP Association for {}", src_addr);
|
||||
|
||||
// Create a socket for receiving packets
|
||||
let local_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0);
|
||||
let remote_udp = create_socket(&local_addr).await?;
|
||||
|
||||
let local_addr = remote_udp.local_addr().expect("Could not determine port bound to");
|
||||
debug!("Created UDP Association for {} from {}", src_addr, local_addr);
|
||||
|
||||
// Create a channel for sending packets to remote
|
||||
// FIXME: Channel size 1024?
|
||||
let (tx, mut rx) = mpsc::channel::<Vec<u8>>(1024);
|
||||
@@ -256,7 +256,7 @@ impl PingServer for ServerScore {
|
||||
/// Starts a UDP local server
|
||||
pub async fn run(context: SharedContext) -> io::Result<()> {
|
||||
let local_addr = context.config().local.as_ref().expect("Missing local config");
|
||||
let bind_addr = resolve_bind_addr(&*context, local_addr).await?;
|
||||
let bind_addr = local_addr.bind_addr(&*context).await?;
|
||||
|
||||
let l = create_socket(&bind_addr).await?;
|
||||
let local_addr = l.local_addr().expect("Could not determine port bound to");
|
||||
|
||||
Reference in New Issue
Block a user