Proxy server
The kftray server executable is an in-cluster relay that moves application traffic between a kftray client and a remote network target. It supports direct TCP and UDP forwarding plus a reverse HTTP mode that carries web requests and upgraded byte streams through a WebSocket tunnel. See Service integrations for the wider service context and MCP service for a related service interface.
Server lifecycle
The server starts from a small configuration supplied through environment variables. It selects one proxy mode, runs the matching handler, and stops all network work when it receives Ctrl+C or SIGTERM.
flowchart LR
E[Environment settings] --> C[Proxy configuration]
C --> S[Proxy server]
S --> T[TCP forwarding]
S --> U[UDP-over-TCP forwarding]
S --> R[Reverse HTTP relay]
X[Ctrl+C or SIGTERM] --> S
S --> G[Graceful shutdown]
The top-level server keeps one shared shutdown signal for its selected handler. After Ctrl+C or SIGTERM, it gives the handler up to five seconds to stop before it exits. Reverse HTTP starts its HTTP and WebSocket listeners as one coordinated operation, so a failure in either listener ends that mode and releases the other listener.
References: crates/…/main.rs• L126-180 crates/…/mod.rs• L1-12 crates/…/server.rs• L18-61 crates/…/reverse.rs• L30-59
Configuration model
The proxy configuration describes both sides of a relay: the remote target and the local listener. The server keeps the original host name for target connections and may also keep a resolved address as a first connection attempt.
| Setting | Purpose |
|---|---|
REMOTE_ADDRESS | Remote host name, IP address, or URL host |
REMOTE_PORT | Port of the remote target |
LOCAL_PORT | Port exposed by the server for TCP or UDP-over-TCP clients |
PROXY_TYPE | tcp, udp, or reverse_http |
HTTP_PORT | HTTP listener for reverse HTTP; defaults to 8080 |
WEBSOCKET_PORT | WebSocket tunnel listener for reverse HTTP; defaults to 9999 |
The first four settings are required for every mode. Reverse HTTP uses the two extra ports because it has one listener for incoming application traffic and another for the connected client tunnel. A missing value, invalid port, or unknown mode prevents startup.
References: crates/…/main.rs• L26-123 crates/…/config.rs• L1-19 crates/…/config.rs• L73-114
Request and tunnel model
The reverse HTTP path turns local HTTP requests into messages for the connected kftray client. Each request has an ID so its response can return to the correct local connection, and each upgraded connection gets a stream ID for later byte traffic.
sequenceDiagram
participant N as Network client
participant H as HTTP relay
participant W as WebSocket tunnel
participant A as kftray client
participant P as Local app
N->>H: HTTP request
H->>W: HttpRequest with request ID
W->>A: Tunnel message
A->>P: Request to local app
P-->>A: Response
A-->>W: HttpResponse with request ID
W-->>H: Matched response
H-->>N: HTTP response
The shared tunnel message model carries HTTP method, path, headers, body, status, and response data. It also carries stream open, data, end, close, and credit messages for upgraded connections and raw TCP mode. A bounded credit window keeps a fast sender from growing unbounded in-memory data while the other side is slow. The server gives each request a 30-second response limit and returns tunnel-closed errors to requests that are still waiting when the active WebSocket connection ends.
References: crates/…/tunnel_protocol.rs• L23-88 crates/…/reverse_http.rs• L136-254 crates/…/websocket_server.rs• L75-179 crates/…/websocket_server.rs• L237-498 crates/…/websocket_server.rs• L500-579
Tunnel compatibility
The WebSocket handshake declares whether the client wants raw TCP and whether it supports half-close behavior. The relay reports TCP mode with /tcp and x-kftray-tunnel-mode: tcp; without that request it keeps the HTTP request-and-response mode.
The message flags also let peers agree on upgraded connections and flow control. If an upgraded response does not confirm flow control, the relay returns 502 Bad Gateway instead of moving unbounded stream data. A new client connection replaces the active tunnel, while a later close from the old connection does not remove the replacement.
References: crates/…/tunnel_protocol.rs• L10-21 crates/…/tunnel_protocol.rs• L56-80 crates/…/websocket_server.rs• L253-285 crates/…/websocket_server.rs• L463-495 crates/…/websocket_server.rs• L617-639 crates/…/reverse_http.rs• L181-228
TCP forwarding
The TCP proxy exposes a TCP listener and creates one remote TCP connection for each accepted client connection. It then copies bytes in both directions until one side closes or the connection fails.
This mode is suited to stream-based services such as databases. Each target connection has a five-second connection limit, uses the resolved address first when one is available, and falls back to the configured host name when that attempt fails. The target connection also uses TCP keepalive to detect dead peers.
References: crates/…/tcp.rs• L38-101 crates/…/tcp.rs• L103-149 crates/…/tcp.rs• L152-201 examples/proxy-tcp.json• L1-11
UDP forwarding
The UDP proxy presents a TCP listener to the client and uses a separate connected UDP socket for the remote target. Each client datagram travels as a length-prefixed frame, then each target reply returns in the same framed form.
This design gives the client a reliable stream through which it can send and receive UDP datagrams. The relay accepts multiple datagrams without waiting for a reply and allows later replies to arrive without a new client datagram. It rejects payloads larger than the maximum UDP payload size.
References: crates/…/udp.rs• L39-73 crates/…/udp.rs• L75-167 crates/…/udp.rs• L170-217 examples/proxy-udp.json• L1-11
Reverse HTTP routing
Reverse HTTP combines an HTTP listener with a WebSocket tunnel listener. A connected client supplies the path to a local app, so network callers reach that app through the relay without the relay opening a direct target connection.
The reverse handler starts both listeners together and treats them as one proxy mode. The HTTP side rejects requests when no tunnel is active. For normal requests it forwards the method, path, headers, and body, then rebuilds the returned status, headers, and body. For protocol upgrades it keeps the HTTP connection as a byte stream after the client confirms support.
When the client requests raw TCP mode, the HTTP port changes role: each incoming connection becomes a new tunnel stream. The relay sends StreamOpen, and the client opens its own local connection for that stream. Stream data uses the shared credit window, so one stalled stream does not remove the limit for other traffic.
References: crates/…/reverse.rs• L30-59 crates/…/reverse_http.rs• L62-133 crates/…/reverse_http.rs• L136-254 crates/…/reverse_http.rs• L276-332 crates/…/websocket_server.rs• L500-614
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the environment names and accepted proxy type values aligned between main.rs, deployment settings, and client-created relay configurations.
- Keep tunnel message fields, mode headers, and stream lifecycle messages compatible with the kftray client.
- Preserve request IDs and stream IDs across the HTTP relay and WebSocket server. A mismatch can leave a caller waiting for a response.
- Keep flow-control flags and credit behavior aligned on both sides before changing upgraded or raw TCP traffic.
- Keep pending request cleanup tied to the active tunnel, so an old WebSocket close cannot clear a replacement or leave callers waiting.
- Keep the proxy handler registry in crates/…/mod.rs aligned with the modes accepted by main.rs and selected by server.rs.
- Keep HTTP and WebSocket startup and shutdown paths coordinated so a reverse proxy listener does not outlive its paired listener or the server process.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Required environment setting is missing or invalid | The executable does not start | Configuration parsing returns an error before the server runs |
| A selected proxy listener cannot bind its port | Startup fails; in reverse HTTP, the other listener is also released | The selected handler returns the bind error, and reverse mode joins both listener tasks |
| A TCP target cannot connect within the connection limit | The client connection fails instead of reaching the target | The proxy limits the attempt and falls back from a resolved address to the configured host name |
| Reverse HTTP receives a request with no active tunnel | The caller receives 503 Service Unavailable | The HTTP path checks tunnel state before forwarding |
| A request waits longer than the tunnel response limit | The request fails and its pending entry is removed | The tunnel applies a 30-second request timeout |
| The active WebSocket closes while requests are pending | Waiting callers receive tunnel-closed errors | Tunnel shutdown drains the pending response map |
| Upgraded traffic lacks flow-control support | The caller receives 502 Bad Gateway | The relay requires an upgraded response with flow-control confirmation |
| UDP frame exceeds the maximum payload | The frame is rejected and a zero-size error frame is returned | The relay validates the length before reading the payload |
| WebSocket pong does not arrive in time | The active tunnel closes and pending requests receive errors | WebSocket keepalive detects missed pong messages |
Key files
| File | Why to read it |
|---|---|
| crates/…/main.rs | Process entry point, environment parsing, mode selection, and shutdown |
| crates/…/mod.rs | Proxy component boundary and handler module set |
| crates/…/config.rs | Shared proxy configuration and supported modes |
| crates/…/server.rs | Handler selection and common lifecycle |
| crates/…/tcp.rs | Direct TCP forwarding, target connection fallback, and keepalive behavior |
| crates/…/udp.rs | UDP-over-TCP framing and forwarding behavior |
| crates/…/reverse.rs | Reverse HTTP task assembly and coordinated shutdown |
| crates/…/reverse_http.rs | HTTP request routing, raw TCP mode, and upgraded byte streams |
| crates/…/websocket_server.rs | WebSocket tunnel state, request matching, stream routing, keepalive, and flow control |
| crates/…/tunnel_protocol.rs | Wire-level tunnel message and compatibility model |
| examples/proxy-tcp.json | Example client configuration for TCP proxying |
| examples/proxy-udp.json | Example client configuration for UDP proxying |