连接被对等方重置。东京,选择

发布于 2025-01-11 17:45:07 字数 1561 浏览 0 评论 0原文

我正在写socks5代理服务器。该程序正在异步运行,我尝试使用 tokio::select,但是当我想获取接收到的数据的大小时,程序由于此错误而终止:

thread 'tokio-runtime-worker' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 54, kind: ConnectionReset, message: "Connection reset by peer" }'

发生错误的函数:

async fn exchange_loop(mut client: TcpStream, address: SocketAddr, cmd: u8) {
    let mut remote = TcpStream::connect(address).await.unwrap();
    let mut buffer_client: [u8; 4096] = [0; 4096];
    let mut buffer_remote: [u8; 4096] = [0; 4096];

    loop {
        tokio::select! {
            size = client.read(&mut buffer_client) => {
                let size = size.unwrap();
                remote.write(buffer_client.as_ref()).await.unwrap();
                println!("Send from client {} => {} {} KB", client.peer_addr().unwrap(), remote.peer_addr().unwrap(), size as f32 / 1024.);
                if size <= 0 {
                    break;
                };
                buffer_client = [0; 4096];
            }
            size = remote.read(&mut buffer_remote) => {
                let size = size.unwrap();
                client.write(buffer_remote.as_ref()).await.unwrap();
                println!("Send from remote {} => {} {} KB", address, client.peer_addr().unwrap(), size as f32 / 1024.);
                if size <= 0 {
                    break;
                };
                buffer_remote = [0; 4096];
            }
        }
    }
    println!("End connection to {}", address);
}

I am writing socks5 proxy server. The program is running asynchronously and I am trying to use tokio::select, but the program terminates due to this error when I want to get the size of the received data:

thread 'tokio-runtime-worker' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 54, kind: ConnectionReset, message: "Connection reset by peer" }'

The function where the error occurs:

async fn exchange_loop(mut client: TcpStream, address: SocketAddr, cmd: u8) {
    let mut remote = TcpStream::connect(address).await.unwrap();
    let mut buffer_client: [u8; 4096] = [0; 4096];
    let mut buffer_remote: [u8; 4096] = [0; 4096];

    loop {
        tokio::select! {
            size = client.read(&mut buffer_client) => {
                let size = size.unwrap();
                remote.write(buffer_client.as_ref()).await.unwrap();
                println!("Send from client {} => {} {} KB", client.peer_addr().unwrap(), remote.peer_addr().unwrap(), size as f32 / 1024.);
                if size <= 0 {
                    break;
                };
                buffer_client = [0; 4096];
            }
            size = remote.read(&mut buffer_remote) => {
                let size = size.unwrap();
                client.write(buffer_remote.as_ref()).await.unwrap();
                println!("Send from remote {} => {} {} KB", address, client.peer_addr().unwrap(), size as f32 / 1024.);
                if size <= 0 {
                    break;
                };
                buffer_remote = [0; 4096];
            }
        }
    }
    println!("End connection to {}", address);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无名指的心愿 2025-01-18 17:45:07

您需要处理错误,如前面的注释中所指定。您需要能够从客户端所做的任何操作中恢复,例如中断连接。

如何恢复的示例:

if let Ok(actual_size) = size {
    if let Ok(_) = remote.write(buffer_client.as_ref()).await {
        println!("Send from client {} => {} {} KB", client.peer_addr().unwrap(), remote.peer_addr().unwrap(), size as f32 / 1024.);
        if size <= 0 {
            break;
        };

        buffer_client = [0; 4096];
    }
}

我输入 Ok(_) 因为我不知道您对第二次解包有何期望。

You need to handle errors, as specified in the previous comments. You need to be able to recover from whatever the client does on his side, interrupting the connection for example.

An example how to recover:

if let Ok(actual_size) = size {
    if let Ok(_) = remote.write(buffer_client.as_ref()).await {
        println!("Send from client {} => {} {} KB", client.peer_addr().unwrap(), remote.peer_addr().unwrap(), size as f32 / 1024.);
        if size <= 0 {
            break;
        };

        buffer_client = [0; 4096];
    }
}

I put Ok(_) as I do not know what you expect from the 2nd unwrap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文