一个基于 Tokio + Rust 的异步 SOCKS5 代理服务器。
- SOCKS5 协议(RFC1928)
- 支持 CONNECT
- IPv4 / IPv6 / 域名解析
- 用户名密码认证(RFC1929)
- 异步高并发(Tokio)
- 双向流量转发
- 连接数限制(Semaphore)
- 连接超时控制
- 空闲超时控制
- 活动连接数监控
- Structured logging(tracing + 文件日志轮转)
- 每日日志轮转
- Graceful shutdown(SIGINT/SIGTERM 平滑关闭)
- TCP_NODELAY 减少延迟
cargo build --release
// install from crates-io
cargo install socks5d./target/release/socks5d默认监听: 0.0.0.0:1080
./target/release/socks5d --bind 127.0.0.1:1080./target/release/socks5d --bind 0.0.0.0:1080 --username admin --password 123456./target/release/socks5d \
--bind 0.0.0.0:1080 \
--username admin \
--password 123456 \
--max-connections 1024 \
--connect-timeout 10 \
--idle-timeout 300 \
--log-dir logs \
--shutdown-timeout 30 \
--metrics-port 9090| 参数 | 默认值 | 说明 |
|---|---|---|
--bind |
0.0.0.0:1080 |
监听地址 |
--username |
- | 认证用户名 |
--password |
- | 认证密码 |
--max-connections |
1024 |
最大并发连接数 |
--connect-timeout |
10 |
连接目标服务器超时(秒) |
--idle-timeout |
300 |
空闲超时(秒),0=无限制 |
--log-dir |
logs |
日志目录 |
--shutdown-timeout |
30 |
优雅关闭等待连接结束超时(秒) |
--metrics-port |
9090 |
metrics HTTP 端口,0=禁用 |
日志输出到 stdout 和 logs/socks5d.log.{日期},格式为 JSON。
访问 http://localhost:9090 查看 metrics:
socks5d_active_connections- 当前活跃连接数socks5d_total_connections- 累计总连接数socks5d_connection_errors- 连接错误数socks5d_bytes_transferred- 累计传输字节数
METHODS: [0x00]
服务器返回: 0x00
VER | ULEN | UNAME | PLEN | PASSWD
成功: 0x01 0x00 失败: 0x01 0x01
VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT
ATYP:
- 0x01 IPv4
- 0x03 域名
- 0x04 IPv6
VER | REP | RSV | ATYP | BND.ADDR | BND.PORT
- 0x01 CONNECT
0x01 general failure 0x02 not allowed 0x03 network unreachable 0x04 host unreachable 0x05 connection refused 0x07 command not supported 0x08 address type not supported
收到 SIGINT/SIGTERM 后:
- 停止接受新连接
- 等待现有连接结束(最多
--shutdown-timeout秒) - 强制退出
curl --socks5 127.0.0.1:1080 http://example.com
带认证: curl --socks5-user admin:123456 --socks5 127.0.0.1:1080 http://example.com
仅支持 TCP CONNECT 未加密,仅用于学习/测试/内网代理