layer4-proxy

Crates.iolayer4-proxy
lib.rslayer4-proxy
version0.7.1
sourcesrc
created_at2024-06-20 22:42:15.130557
updated_at2024-06-26 19:44:02.743506
descriptionSimple and fast layer 4 proxy in Rust
homepagehttps://github.com/git001/layer4-proxy-rs
repositoryhttps://github.com/git001/layer4-proxy-rs
max_upload_size
id1278816
size100,271
Jacob Kiers (jacobkiers)

documentation

README

layer4-proxy

Hey, now we are on level 4!

layer4-proxy is a layer 4 proxy implemented by Rust to listen on specific ports and transfer TCP data to remote addresses (only TCP) according to the configuration.

Features

  • Listen on specific port and proxy to local or remote port
  • SNI-based rule without terminating TLS connection
  • DNS-based backend with periodic resolution
  • Use Upstream HTTP proxy with via keyword
  • Offer simple HTTP/1.1 health check
  • Lookup environment variables with in ${...}
  • Limit client connection by server maxclients

Sequence diagram

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

Installation

To gain best performance on your computer's architecture, please consider build the source code. First, you may need Rust tool chain.

$ cd layer4-proxy
$ cargo build --release

Binary file will be generated at target/release/layer4-proxy, or you can use cargo install --path . to install.

Or you can use Cargo to install layer4-proxy:

$ cargo install layer4-proxy

Or you can download binary file form the Release page.

Configuration

layer4-proxy will read yaml format configuration file from /etc/l4p/l4p.yaml, and you can set custom path to environment variable L4P_CONFIG, here is an minimal viable example:

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

There are two upstreams built in:

  • Ban, which terminates the connection immediately
  • Echo, which reflects back with the input

For detailed configuration, check this example.

Test run

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

Thanks

License

layer4-proxy is available under terms of Apache-2.0.

Commit count: 82

cargo fmt