feat: not works now

This commit is contained in:
2022-08-23 01:24:25 +08:00
parent 391f6f3fb6
commit 9e9301d4d7
2 changed files with 27 additions and 38 deletions

View File

@@ -1,6 +1,6 @@
{ {
"listen": "127.0.0.1:2001", "listen": "127.0.0.1:2001",
"cert_pem_file": "cert.pem", "cert_pem_file": "cert.pem",
"proxy_address": "127.0.0.1:2002", "proxy_address": "47.52.7.223:1443",
"proxy_server_name": "localhost" "proxy_server_name": "localhost"
} }

View File

@@ -1,13 +1,11 @@
use std::fs; use std::fs;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::time::Duration;
use rust_util::{util_time, XResult}; use rust_util::{util_time, XResult};
use s2n_quic::Client; use s2n_quic::Client;
use s2n_quic::client::Connect; use s2n_quic::client::Connect;
use tokio::net::TcpListener; use tokio::net::TcpListener;
use tokio::sync::mpsc::channel; use tokio::sync::mpsc::channel;
use tokio::time::sleep;
use crate::config::ListenConfig; use crate::config::ListenConfig;
use crate::io_util; 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, let server_name = opt_value_result!(&listen_config.proxy_server_name,
"proxy_server_name in config is require in client mode"); "proxy_server_name in config is require in client mode");
let mut connect_count = 0_usize; loop {
're_connect: loop { let (client_stream_time, client_stream) = match inbound_stream_channel_receiver.recv().await {
let connect = Connect::new(addr).with_server_name(server_name.as_str()); Some(time_and_stream) => time_and_stream,
let mut connection = match client.connect(connect).await { None => {
Ok(connection) => connection, information!("End client to server proxy");
Err(e) => { return Ok(());
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;
} }
}; };
connect_count = 0; information!("Get connection: {} - {}", client_stream_time, client_stream.peer_addr().unwrap());
opt_result!(connection.keep_alive(true), "Keep alive connection failed: {}"); let addr = addr.clone();
let client = client.clone();
're_stream: loop { let server_name = server_name.clone();
let (client_stream_time, client_stream) = match inbound_stream_channel_receiver.recv().await { tokio::spawn(async move {
Some(time_and_stream) => time_and_stream, let connect = Connect::new(addr).with_server_name(server_name.as_str());
None => { let mut connection = match client.connect(connect).await {
information!("End client to server proxy"); Ok(connection) => connection,
return Ok(()); 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(); let time = util_time::get_current_millis();
if (time as i128 - client_stream_time as i128).abs() > 3_000 { if (time as i128 - client_stream_time as i128).abs() > 3_000 {
warning!("Connection is more than 3 second, abandon connection"); warning!("Connection is more than 3 second, abandon connection");
continue 're_stream; return;
} }
let server_stream = match connection.open_bidirectional_stream().await { let server_stream = match connection.open_bidirectional_stream().await {
Ok(stream) => stream, Ok(stream) => stream,
Err(e) => { Err(e) => {
failure!("Open stream in connection to server failed: {}", 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());
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 {
if let Err(e) = io_util::transfer_for_client_to_server(client_stream, server_stream, conn_count).await { failure!("Client - Server error: {}", e);
failure!("Client - Server error: {}", e); }
} });
});
}
} }
} }