π Protocol Inspection
Description
Protocol inspection is a fundamental capability that enables proxies to:
- Multi-Protocol Support: Accept and handle different proxy protocols (HTTP, SOCKS5, etc.) over the same network interface
- Deep Packet Inspection: Examine the actual content of the traffic to determine the underlying protocol
- Protocol-Specific Handling: Apply different processing rules based on the detected protocol
- Security Analysis: Identify and potentially block malicious traffic patterns
Protocol Detection Methods
Transport Layer Detection
At the transport layer, proxies can detect protocols based on:
- Port Numbers: Common ports (80 for HTTP, 443 for HTTPS, etc.)
- Connection Patterns: Initial handshake sequences
- Protocol Signatures: Characteristic byte patterns
Application Layer Detection
For application layer protocols, detection methods include:
- Protocol Headers: Examining initial bytes for protocol-specific markers
- TLS SNI: Server Name Indication in TLS handshakes
- Protocol-Specific Patterns: Characteristic sequences in the protocol
Use Cases
Multi-Protocol Proxies
A common use case is supporting both HTTP and SOCKS5 protocols on the same port:
Multi-Protocol Proxy Flow
-------------------------
ββββββββββ ββββββββββββββββββ ββββββββββββββββββββββ
β Client ββββββββΆβ Multi-Protocol ββββββββΆβ Target Server β
ββββββββββ β Proxy β ββββββββββββββββββββββ
β ββββββββββββββββββ β
β β β
β 1. TCP Connect β β
βββββββββββββββββββΆβ β
β β β
β 2. Protocol β β
β Detection β β
βββββββββββββββββββΆβ β
β β β
β 3. Protocol- β β
β Specific β β
β Handling ββββββββββββββββββββββββββΆβ
β β β
β 4. Traffic β β
β Relay ββββββββββββββββββββββββββΆβ
MITM Protocol Inspection
MITM proxies use protocol inspection for:
- TLS Traffic: Determining if traffic is TLS-encrypted
- Protocol Selection: Choosing appropriate decryption and inspection methods
- Content Analysis: Examining the actual protocol content
MITM Protocol Inspection
------------------------
ββββββββββ ββββββββββββββββββ ββββββββββββββββββββββ
β Client ββββββββΆβ MITM Proxy ββββββββΆβ Target Server β
ββββββββββ ββββββββββββββββββ ββββββββββββββββββββββ
β β β
β 1. Initial β β
β Connection β β
βββββββββββββββββββΆβ β
β β β
β 2. Protocol β β
β Detection β β
βββββββββββββββββββΆβ β
β β β
β 3. TLS β β
β Termination β β
ββββββββββββββββββββ€ β
β β β
β 4. Protocol β β
β Inspection ββββββββββββββββββββββββββΆβ
β β β
β 5. Re-encryption β β
β & Relay ββββββββββββββββββββββββββΆβ
Implementation in Rama
Rama provides protocol inspection capabilities through its modular architecture. A key example is the SOCKS5 MITM proxy implementation socks5_connect_proxy_mitm_proxy.rs, which demonstrates how to:
- Use
PeekTlsRouterto detect TLS traffic - Route TLS traffic to a TLS-capable service
- Handle non-TLS traffic with a fallback service
This pattern allows for flexible protocol handling while maintaining clean separation of concerns. The implementation shows how to:
- Detect protocols at the transport layer
- Handle protocol-specific processing
- Integrate with MITM capabilities when needed
Some rama examples that built on top of protocol inspection:
socks5_and_http_proxy.rsis an example of such protocol inspection.This code is used to be able to support a socks5 proxy that can also be something else next to it (e.g. an http proxy).http_https_socks5_and_socks5h_connect_proxy.rsis another advanced demonstration of Ramaβs protocol inspection and routing capabilities. This example showcases how to build a single, unified proxy server that intelligently handles HTTP, HTTPS (HTTP within TLS) and SOCKS5 traffic all within the same listener, usingPeekRouters and service composition.proxy_connectivity_check.rsis not about protocol inspection but does use socks5 and http protocol inspection for various purposes, including to hijack very specific http data without forcing all socks5 proxy traffic to be http.
Performance and Security
- Detect a protocol from as few bytes as possible, and check the protocols you expect most often first.
- Bound how many bytes you peek and how long you wait for them, so a slow or silent client cannot hold a connection open in the detection phase.
- Treat peeked bytes as untrusted: validate protocol signatures, and route malformed or unknown input to a fallback service (or drop it) instead of failing the listener.