#proxy #http-proxy #listen #networking #tcp #layer #remote

已删除 layer4-proxy

Rust实现的简单快速的第四层代理

8个版本 (破坏性更新)

0.7.1 2024年6月26日
0.6.1 2024年6月26日
0.5.1 2024年6月25日
0.4.1 2024年6月25日
0.1.7 2024年6月20日

#30 in #listen

每月30次下载

Apache-2.0

49KB
1K SLoC

layer4-proxy

嘿,我们现在在第四层了!

layer4-proxy 是一个由Rust实现的第四层代理,用于监听特定端口并根据配置将TCP数据传输到远程地址(仅TCP)。

特性

  • 监听特定端口并将代理到本地或远程端口
  • 基于SNI的规则,不终止TLS连接
  • 基于DNS的后端,定期解析
  • 使用带有 via 关键字的 Upstream HTTP 代理
  • 提供简单的HTTP/1.1健康检查
  • 使用 ${...} 查找环境变量
  • 通过服务器 maxclients 限制客户端连接

序列图

sequenceDiagram

  participant CL AS Client
  participant LP AS Layer4 Proxy
  participant UP AS Upstream Proxy
  participant DS AS Destination Server

  CL->>LP: Connect to Layer4 Proxy via TCP<br/>SNI is used for routing
  LP->>UP: Connect to Upstream Proxy via TCP
  LP->>UP: Send "CONNECT" and some other<br/>Upstream Proxy header
  UP->>DS: Connect to Destination Server
  DS->>UP: Successful connection to Destination Server
  LP->>DS: Successful Proxy Tunnel
  CL->>DS: TLS Handshake with the SNI from the Client
  Note over DS,CL: TLS Traffic
  CL->>DS: Closes connection

安装

为了在您的计算机架构上获得最佳性能,请考虑构建源代码。首先,您可能需要 Rust工具链

$ cd layer4-proxy
$ cargo build --release

二进制文件将在 target/release/layer4-proxy 生成,或者您可以使用 cargo install --path . 安装。

或者您可以使用Cargo安装 layer4-proxy

$ cargo install layer4-proxy

或者您可以从发布页面下载二进制文件。

配置

layer4-proxy 将从 /etc/l4p/l4p.yaml 读取yaml格式的配置文件,并且您可以将自定义路径设置为环境变量 L4P_CONFIG,以下是一个可行的最小示例

version: 1
log: info

via: &viaanchor
  target: www.test1.com
  headers:
    Host: www.test1.com
    Proxy-Authorization: basic ${ENCODED_PW}
    Proxy-KeepAlive: true

servers:
  proxy_server:
    listen:
      - "127.0.0.1:8081"
    default: remote
    via:
      *viaanchor
  health-server:
    listen: [ "127.0.0.1:8081" ]
    default: health
    via:
      *viaanchor

upstream:
  remote: "tcp://www.remote.example.com:8082" # proxy to remote address

内置了两个上游

  • Ban,立即终止连接
  • Echo,反射回输入

有关详细配置,请参阅 此示例

测试运行

L4P_CONFIG=container-files/etc/l4p/config.yaml cargo run

谢谢

许可证

layer4-proxy 在 Apache-2.0 许可协议下可用。

依赖项

~11–21MB
~319K SLoC