pick best tcp server right before connect()

This commit is contained in:
zonyitoo
2021-01-03 02:32:42 +08:00
parent 1920fd0f39
commit 0cf0cf0ab2
7 changed files with 43 additions and 43 deletions
@@ -9,7 +9,7 @@ use shadowsocks::relay::socks5::Address;
use crate::local::{
context::ServiceContext,
loadbalancing::ServerIdent,
loadbalancing::PingBalancer,
net::AutoProxyClientStream,
utils::establish_tcp_tunnel,
};
@@ -23,7 +23,7 @@ use super::{
pub struct HttpDispatcher {
context: Arc<ServiceContext>,
req: Request<Body>,
server: Arc<ServerIdent>,
balancer: PingBalancer,
client_addr: SocketAddr,
bypass_client: BypassHttpClient,
proxy_client_cache: Arc<ProxyClientCache>,
@@ -33,7 +33,7 @@ impl HttpDispatcher {
pub fn new(
context: Arc<ServiceContext>,
req: Request<Body>,
server: Arc<ServerIdent>,
balancer: PingBalancer,
client_addr: SocketAddr,
bypass_client: BypassHttpClient,
proxy_client_cache: Arc<ProxyClientCache>,
@@ -41,7 +41,7 @@ impl HttpDispatcher {
HttpDispatcher {
context,
req,
server,
balancer,
client_addr,
bypass_client,
proxy_client_cache,
@@ -89,7 +89,8 @@ impl HttpDispatcher {
// Connect to Shadowsocks' remote
//
// FIXME: What STATUS should I return for connection error?
let stream = AutoProxyClientStream::connect(self.context, self.server.as_ref(), &host).await?;
let server = self.balancer.best_tcp_server();
let stream = AutoProxyClientStream::connect(self.context, server.as_ref(), &host).await?;
debug!("CONNECT relay connected {} <-> {}", self.client_addr, host);
@@ -100,7 +101,6 @@ impl HttpDispatcher {
// `on_upgrade` future.
let req = self.req;
let client_addr = self.client_addr;
let server = self.server;
tokio::spawn(async move {
match upgrade::on(req).await {
Ok(upgraded) => {
@@ -174,7 +174,8 @@ impl HttpDispatcher {
// Keep connections for clients in ServerScore::client
//
// client instance is kept for Keep-Alive connections
let client = self.proxy_client_cache.get_connected(&self.server).await;
let server = self.balancer.best_tcp_server();
let client = self.proxy_client_cache.get_connected(&server).await;
match client.request(self.req).await {
Ok(res) => res,
@@ -61,11 +61,10 @@ impl Http {
async move {
Ok::<_, Infallible>(service_fn(move |req: Request<Body>| {
let server = balancer.best_tcp_server();
HttpDispatcher::new(
context.clone(),
req,
server,
balancer.clone(),
client_addr,
bypass_client.clone(),
proxy_client_cache.clone(),
@@ -13,7 +13,7 @@ use crate::{
config::{ClientConfig, RedirType},
local::{
context::ServiceContext,
loadbalancing::{PingBalancer, ServerIdent},
loadbalancing::PingBalancer,
net::{AutoProxyClientStream, AutoProxyIo},
redir::redir_ext::{TcpListenerRedirExt, TcpStreamRedirExt},
utils::establish_tcp_tunnel,
@@ -27,12 +27,13 @@ mod sys;
/// This method must be called after handshaking with client (for example, socks5 handshaking)
async fn establish_client_tcp_redir<'a>(
context: Arc<ServiceContext>,
server: &ServerIdent,
balancer: PingBalancer,
mut stream: TcpStream,
peer_addr: SocketAddr,
addr: &Address,
nodelay: bool,
) -> io::Result<()> {
let server = balancer.best_tcp_server();
let svr_cfg = server.server_config();
let remote = AutoProxyClientStream::connect(context, &server, addr).await?;
@@ -70,7 +71,7 @@ async fn establish_client_tcp_redir<'a>(
async fn handle_redir_client(
context: Arc<ServiceContext>,
server: &ServerIdent,
balancer: PingBalancer,
s: TcpStream,
peer_addr: SocketAddr,
daddr: SocketAddr,
@@ -90,7 +91,7 @@ async fn handle_redir_client(
// Get forward address from socket
let target_addr = Address::from(daddr);
establish_client_tcp_redir(context, server, s, peer_addr, &target_addr, nodelay).await
establish_client_tcp_redir(context, balancer, s, peer_addr, &target_addr, nodelay).await
}
pub async fn run_tcp_redir(
@@ -125,12 +126,11 @@ pub async fn run_tcp_redir(
continue;
}
};
let server = balancer.best_tcp_server();
trace!("got connection {}", peer_addr);
trace!("picked proxy server: {:?}", server.server_config());
let context = context.clone();
let balancer = balancer.clone();
tokio::spawn(async move {
let dst_addr = match socket.destination_addr(redir_ty) {
Ok(d) => d,
@@ -143,7 +143,7 @@ pub async fn run_tcp_redir(
}
};
if let Err(err) = handle_redir_client(context, &server, socket, peer_addr, dst_addr, nodelay).await {
if let Err(err) = handle_redir_client(context, balancer, socket, peer_addr, dst_addr, nodelay).await {
debug!("TCP redirect client, error: {:?}", err);
}
});
@@ -12,10 +12,7 @@ use tokio::{
use crate::{
config::{ClientConfig, Mode},
local::{
context::ServiceContext,
loadbalancing::{PingBalancer, ServerIdent},
},
local::{context::ServiceContext, loadbalancing::PingBalancer},
};
#[cfg(feature = "local-socks4")]
@@ -139,7 +136,7 @@ impl Socks {
let _ = stream.set_nodelay(true);
}
let server = balancer.best_tcp_server();
let balancer = balancer.clone();
let context = self.context.clone();
let nodelay = self.nodelay;
let udp_bind_addr = udp_bind_addr.clone();
@@ -149,7 +146,7 @@ impl Socks {
context,
udp_bind_addr,
stream,
server,
balancer,
peer_addr,
mode,
nodelay,
@@ -162,7 +159,7 @@ impl Socks {
context: Arc<ServiceContext>,
udp_bind_addr: Option<Arc<ClientConfig>>,
stream: TcpStream,
server: Arc<ServerIdent>,
balancer: PingBalancer,
peer_addr: SocketAddr,
mode: Mode,
nodelay: bool,
@@ -177,12 +174,12 @@ impl Socks {
match version_buffer[0] {
0x04 => {
let handler = Socks4TcpHandler::new(context, nodelay, server, mode);
let handler = Socks4TcpHandler::new(context, nodelay, balancer, mode);
handler.handle_socks4_client(stream, peer_addr).await
}
0x05 => {
let handler = Socks5TcpHandler::new(context, udp_bind_addr, nodelay, server, mode);
let handler = Socks5TcpHandler::new(context, udp_bind_addr, nodelay, balancer, mode);
handler.handle_socks5_client(stream, peer_addr).await
}
@@ -199,12 +196,12 @@ impl Socks {
context: Arc<ServiceContext>,
udp_bind_addr: Option<Arc<ClientConfig>>,
stream: TcpStream,
server: Arc<ServerIdent>,
balancer: PingBalancer,
peer_addr: SocketAddr,
mode: Mode,
nodelay: bool,
) -> io::Result<()> {
let handler = Socks5TcpHandler::new(context, udp_bind_addr, nodelay, server, mode);
let handler = Socks5TcpHandler::new(context, udp_bind_addr, nodelay, balancer, mode);
handler.handle_socks5_client(stream, peer_addr).await
}
@@ -16,7 +16,7 @@ use crate::{
config::Mode,
local::{
context::ServiceContext,
loadbalancing::ServerIdent,
loadbalancing::PingBalancer,
net::AutoProxyClientStream,
utils::establish_tcp_tunnel,
},
@@ -27,16 +27,16 @@ use crate::local::socks::socks4::{Address, Command, HandshakeRequest, HandshakeR
pub struct Socks4TcpHandler {
context: Arc<ServiceContext>,
nodelay: bool,
server: Arc<ServerIdent>,
balancer: PingBalancer,
mode: Mode,
}
impl Socks4TcpHandler {
pub fn new(context: Arc<ServiceContext>, nodelay: bool, server: Arc<ServerIdent>, mode: Mode) -> Socks4TcpHandler {
pub fn new(context: Arc<ServiceContext>, nodelay: bool, balancer: PingBalancer, mode: Mode) -> Socks4TcpHandler {
Socks4TcpHandler {
context,
nodelay,
server,
balancer,
mode,
}
}
@@ -82,10 +82,11 @@ impl Socks4TcpHandler {
return Ok(());
}
let svr_cfg = self.server.server_config();
let server = self.balancer.best_tcp_server();
let svr_cfg = server.server_config();
let target_addr = target_addr.into();
let mut remote = match AutoProxyClientStream::connect(self.context, &self.server, &target_addr).await {
let mut remote = match AutoProxyClientStream::connect(self.context, &server, &target_addr).await {
Ok(remote) => {
// Tell the client that we are ready
let handshake_rsp = HandshakeResponse::new(ResultCode::RequestGranted);
@@ -23,7 +23,7 @@ use crate::{
config::{ClientConfig, Mode},
local::{
context::ServiceContext,
loadbalancing::ServerIdent,
loadbalancing::PingBalancer,
net::AutoProxyClientStream,
utils::establish_tcp_tunnel,
},
@@ -34,7 +34,7 @@ pub struct Socks5TcpHandler {
context: Arc<ServiceContext>,
udp_bind_addr: Option<Arc<ClientConfig>>,
nodelay: bool,
server: Arc<ServerIdent>,
balancer: PingBalancer,
mode: Mode,
}
@@ -43,14 +43,14 @@ impl Socks5TcpHandler {
context: Arc<ServiceContext>,
udp_bind_addr: Option<Arc<ClientConfig>>,
nodelay: bool,
server: Arc<ServerIdent>,
balancer: PingBalancer,
mode: Mode,
) -> Socks5TcpHandler {
Socks5TcpHandler {
context,
udp_bind_addr,
nodelay,
server,
balancer,
mode,
}
}
@@ -131,9 +131,10 @@ impl Socks5TcpHandler {
return Ok(());
}
let svr_cfg = self.server.server_config();
let server = self.balancer.best_tcp_server();
let svr_cfg = server.server_config();
let remote = match AutoProxyClientStream::connect(self.context.clone(), &self.server, &target_addr).await {
let remote = match AutoProxyClientStream::connect(self.context.clone(), &server, &target_addr).await {
Ok(remote) => {
// Tell the client that we are ready
let header =
@@ -13,7 +13,7 @@ use crate::{
config::ClientConfig,
local::{
context::ServiceContext,
loadbalancing::{PingBalancer, ServerIdent},
loadbalancing::PingBalancer,
net::AutoProxyClientStream,
utils::establish_tcp_tunnel,
},
@@ -53,13 +53,13 @@ pub async fn run_tcp_tunnel(
let _ = stream.set_nodelay(true);
}
let server = balancer.best_tcp_server();
let balancer = balancer.clone();
let forward_addr = forward_addr.clone();
tokio::spawn(handle_tcp_client(
context.clone(),
stream,
server,
balancer,
peer_addr,
forward_addr,
nodelay,
@@ -70,11 +70,12 @@ pub async fn run_tcp_tunnel(
async fn handle_tcp_client(
context: Arc<ServiceContext>,
mut stream: TcpStream,
server: Arc<ServerIdent>,
balancer: PingBalancer,
peer_addr: SocketAddr,
forward_addr: Address,
nodelay: bool,
) -> io::Result<()> {
let server = balancer.best_tcp_server();
let svr_cfg = server.server_config();
trace!(
"establishing tcp tunnel {} <-> {} through sever {} (outbound: {})",