feat: work in process

This commit is contained in:
2022-08-21 11:06:11 +08:00
parent 21c857bdf4
commit 4723298aec
7 changed files with 269 additions and 19 deletions

View File

@@ -4,12 +4,15 @@ use rust_util::XResult;
use s2n_quic::Server;
use crate::config::ListenConfig;
use crate::io_util;
async fn run(listen_config: &ListenConfig) -> XResult<()> {
pub async fn run(listen_config: &ListenConfig) -> XResult<()> {
let cert_pem = opt_result!(fs::read_to_string(&listen_config.cert_pem_file),
"Read cert pem file: {}, failed: {}", &listen_config.key_pem_file);
let key_pem = opt_result!(fs::read_to_string(&listen_config.key_pem_file),
"Read key pem file :{}, failed: {}", &listen_config.key_pem_file);
"Read cert pem file: {}, failed: {}", &listen_config.cert_pem_file);
let key_pem_file = opt_value_result!(&listen_config.key_pem_file,
"key_pem_file in config is required in server mode");
let key_pem = opt_result!(fs::read_to_string(&key_pem_file),
"Read key pem file :{}, failed: {}", &key_pem_file);
let mut server = Server::builder()
.with_tls((cert_pem.as_str(), key_pem.as_str()))?
@@ -17,18 +20,18 @@ async fn run(listen_config: &ListenConfig) -> XResult<()> {
.start()?;
while let Some(mut connection) = server.accept().await {
// spawn a new task for the connection
let proxy_address = listen_config.proxy_address.clone();
tokio::spawn(async move {
information!("Connection accepted from {:?}", connection.remote_addr());
while let Ok(Some(mut stream)) = connection.accept_bidirectional_stream().await {
while let Ok(Some(stream)) = connection.accept_bidirectional_stream().await {
// spawn a new task for the stream
let proxy_address = proxy_address.clone();
tokio::spawn(async move {
information!("Stream opened from {:?}", stream.connection().remote_addr());
// echo any data back to the stream
// while let Ok(Some(data)) = stream.receive().await {
// stream.send(data).await.expect("stream should be open");
// }
// println!("Stream closed from {:?}", stream.connection().remote_addr());
// TODO ...
let conn_count = format!("{}", 1);
if let Err(e) = io_util::transfer_for_server_to_remote(stream, proxy_address, conn_count).await {
failure!("Server - Client error: {}", e);
}
});
}
println!("Connection closed from {:?}", connection.remote_addr());
@@ -36,4 +39,4 @@ async fn run(listen_config: &ListenConfig) -> XResult<()> {
}
Ok(())
}
}