7个版本 (4个重大更新)
| 0.5.0 | 2023年12月12日 | 
|---|---|
| 0.4.0 | 2023年12月12日 | 
| 0.3.0 | 2023年12月6日 | 
| 0.2.0 | 2023年12月5日 | 
| 0.1.2 | 2023年11月30日 | 
#2286 in 网络编程
42KB
541 行
relayport-rs
快速便捷的代理TCP和UDP端口的抽象。
这个库简化了从Rust应用程序创建异步TCP/UDP代理的过程。代理数量仅受运行库的系统上可用资源的限制。此库依赖于tokio进行其运行时。
使用
要在自己的包中使用此库,请将以下内容添加到您的Cargo.toml中
[dependencies]
relayport_rs = "0.5.0"
示例
一个简单的程序,可以将Web流量代理到服务器,可能看起来像这样
use std::error::Error;
use relayport_rs::command::RelayCommand;
use relayport_rs::RelayPortError;
use relayport_rs::RelayTcpSocket;
use tokio::sync::broadcast;
#[tokio::main]
pub async fn main() -> Result<(), RelayPortError> {
    // The relay expects a broadcast channel on which to listen for shutdown commands
    let (tx, rx) = broadcast::channel(16);
    // build a relay with a listener TCP socket
    let relay = RelaySocket::build()
        .set_so_reuseaddr(true)
        .set_tcp_nodelay(true)
        .bind("127.0.0.1:8080")?
        .listen()?;
    // this will never return unless it encounters an error
    relay
        .run("127.0.0.1:80", &rx)
        .await
}
此示例代理DNS端口,使用UDP
use std::error::Error;
use relayport_rs::command::RelayCommand;
use relayport_rs::RelayPortError;
use relayport_rs::RelayUdpSocket;
use tokio::sync::broadcast;
#[tokio::main]
pub async fn main() -> Result<(), RelayPortError> {
    // The relay expects a broadcast channel on which to listen for shutdown commands
    let (tx, rx) = broadcast::channel(16);
    // build a relay with a UDP socket
    let udp_relay = RelayUdpSocket::build()
        .set_so_reuseaddr(true)
        .bind("127.0.0.1:10080")
        .await?;
    // this will never return unless it encounters an error
    udp_relay
        .run("1.1.1.1:53", &rx)
        .await
        .expect("failed to start relay");
}
先决条件
- Git源代码版本控制系统
https://git-scm.cn/book/en/v2/Getting-Started-Installing-Git
- Rust编程语言 官方安装指南
curl --proto '=https' --tlsv1.2 -sSfhttps://sh.rustup.rs | sh
为了确保正确安装,请输入以下命令并确保您获得成功输出
rustc --version
cargo --version
依赖关系
~3–13MB
~126K SLoC