diff --git a/client.json b/client.json index 31017b4..b7b60b2 100644 --- a/client.json +++ b/client.json @@ -1,6 +1,6 @@ { "listen": "127.0.0.1:2001", "cert_pem_file": "cert.pem", - "proxy_address": "127.0.0.1:2002", + "proxy_address": "47.52.7.223:1443", "proxy_server_name": "localhost" -} \ No newline at end of file +} diff --git a/src/client.rs b/src/client.rs index 788ee19..177ae88 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,13 +1,11 @@ use std::fs; use std::net::SocketAddr; -use std::time::Duration; use rust_util::{util_time, XResult}; use s2n_quic::Client; use s2n_quic::client::Connect; use tokio::net::TcpListener; use tokio::sync::mpsc::channel; -use tokio::time::sleep; use crate::config::ListenConfig; use crate::io_util; @@ -44,55 +42,46 @@ pub async fn run(listen_config: &ListenConfig) -> XResult<()> { let server_name = opt_value_result!(&listen_config.proxy_server_name, "proxy_server_name in config is require in client mode"); - let mut connect_count = 0_usize; - 're_connect: loop { - let connect = Connect::new(addr).with_server_name(server_name.as_str()); - let mut connection = match client.connect(connect).await { - Ok(connection) => connection, - Err(e) => { - connect_count += 1; - failure!("Connect to server failed: {}, count: {}", e, connect_count); - sleep(Duration::from_secs(match connect_count { - 0..=3 => 1, - 4..=10 => 2, - 11..=30 => 3, - _ => 5, - })).await; - continue 're_connect; + loop { + let (client_stream_time, client_stream) = match inbound_stream_channel_receiver.recv().await { + Some(time_and_stream) => time_and_stream, + None => { + information!("End client to server proxy"); + return Ok(()); } }; - connect_count = 0; - opt_result!(connection.keep_alive(true), "Keep alive connection failed: {}"); - - 're_stream: loop { - let (client_stream_time, client_stream) = match inbound_stream_channel_receiver.recv().await { - Some(time_and_stream) => time_and_stream, - None => { - information!("End client to server proxy"); - return Ok(()); + information!("Get connection: {} - {}", client_stream_time, client_stream.peer_addr().unwrap()); + let addr = addr.clone(); + let client = client.clone(); + let server_name = server_name.clone(); + tokio::spawn(async move { + let connect = Connect::new(addr).with_server_name(server_name.as_str()); + let mut connection = match client.connect(connect).await { + Ok(connection) => connection, + Err(e) => { + failure!("Connect to server failed: {}", e); + return; } }; + connection.keep_alive(true).ok(); - information!("Get connection: {} - {}", client_stream_time, client_stream.peer_addr().unwrap()); let time = util_time::get_current_millis(); if (time as i128 - client_stream_time as i128).abs() > 3_000 { warning!("Connection is more than 3 second, abandon connection"); - continue 're_stream; + return; } let server_stream = match connection.open_bidirectional_stream().await { Ok(stream) => stream, Err(e) => { failure!("Open stream in connection to server failed: {}", e); - continue 're_connect; + return; } }; - tokio::spawn(async move { - let conn_count = format!("{}", util_time::get_current_millis()); - if let Err(e) = io_util::transfer_for_client_to_server(client_stream, server_stream, conn_count).await { - failure!("Client - Server error: {}", e); - } - }); - } + let conn_count = format!("{}", util_time::get_current_millis()); + if let Err(e) = io_util::transfer_for_client_to_server(client_stream, server_stream, conn_count).await { + failure!("Client - Server error: {}", e); + } + }); } } \ No newline at end of file