AnyTLS v2 Protocol Specification¶
- Document: The AnyTLS Protocol Version 2
- Category: Informational
- Chinese edition: 简体中文
Status of This Memo¶
This document specifies version 2 of the AnyTLS protocol wire format and session behavior for implementers and interoperability testing. It is not an Internet Standard.
Abstract¶
AnyTLS is a multiplexed proxy protocol running over standard TLS connections. It mitigates traffic analysis using a server-updatable dynamic padding scheme, multiplexes logical streams within a single session, and falls back to standard application services to resist active probing.
1. Introduction¶
AnyTLS establishes a session over a standard TLS connection in two phases: an initial authentication exchange followed by a multiplexed session layer. To counter Deep Packet Inspection (DPI) heuristics based on early packet sizes and segmentation patterns, AnyTLS applies a dynamic paddingScheme that fragments and pads early writes and can be rolled out dynamically by the server.
2. Conventions and Terminology¶
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in BCP 14 [RFC2119] [RFC8174] when, and only when, they appear in all capitals, as shown here.
- Session: A single established TLS connection running the AnyTLS session layer.
- Stream: A multiplexed logical channel within a Session used to proxy one target connection.
- Client: The endpoint initiating the AnyTLS connection.
- Server: The endpoint receiving the connection and performing proxy forwarding.
3. Protocol Architecture¶
+-------------------------------------------------+
| User TCP / UDP Proxy |
+-------------------------------------------------+
| AnyTLS Stream Layer |
+-------------------------------------------------+
| AnyTLS Session Layer |
+-------------------------------------------------+
| Transport Layer Security (TLS) |
+-------------------------------------------------+
| Transmission Control Protocol (TCP) |
+-------------------------------------------------+
4. Authentication Phase¶
Immediately after the TLS handshake completes, the Client MUST send an authentication request.
4.1. Client Authentication Request¶
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ +
| |
+ sha256(password) (32B) +
| |
+ (8 rows total) +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| padding0 length (2B) | padding0 (var)... |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
sha256(password)(32B): SHA-256 digest of the pre-shared protocol password.padding0 length(2B): Big-endian unsigned 16-bit length ofpadding0.padding0(variable): Random padding bytes.
Note: Fixed authentication header overhead is 34 bytes, excluding variable padding.
4.2. Server Authentication Handling¶
The Server MUST read the first packet and verify the authentication request (including consuming padding0 in full):
- On success, the Server enters the Session event loop.
- On failure, the Server MUST immediately close the connection OR fall back to a standard HTTP / L7 service to defend against active probing.
5. Session Layer¶
After authentication, both endpoints exchange framed messages over the session loop.
5.1. Frame Format¶
Every frame starts with a fixed 7-byte header (offsets 0–6), immediately followed by the variable payload (starting at offset 7):
Offset: 0 1 5 7
+---------+-------------------+---------+---------------+
| command | streamId | dataLen | data (var)... |
| (1B) | (4B) | (2B) | (dataLen B) |
+---------+-------------------+---------+---------------+
command(1B): Unsigned 8-bit command identifier.streamId(4B): Big-endian unsigned 32-bit logical stream identifier.dataLen(2B): Big-endian unsigned 16-bit payload length.data(variable): Payload ofdataLenbytes.
5.2. Command Registry¶
Unless explicitly stated below, commands MUST NOT carry a non-empty data payload.
| Command Name | Value | Introduced | Direction | Description |
|---|---|---|---|---|
cmdWaste |
0 |
v1 | Both | Padding; payload MUST be read in full and silently discarded |
cmdSYN |
1 |
v1 | Client → Server | Open a new Stream |
cmdPSH |
2 |
v1 | Both | Push payload bytes to a Stream |
cmdFIN |
3 |
v1 | Both | Close a Stream (EOF marker) |
cmdSettings |
4 |
v1 | Client → Server | Client settings negotiation |
cmdAlert |
5 |
v1 | Server → Client | Fatal error alert |
cmdUpdatePaddingScheme |
6 |
v1 | Server → Client | Push updated dynamic padding scheme |
cmdSYNACK |
7 |
v2 | Server → Client | Stream open acknowledgment or error |
cmdHeartRequest |
8 |
v2 | Both | Keep-alive heartbeat request |
cmdHeartResponse |
9 |
v2 | Both | Keep-alive heartbeat response |
cmdServerSettings |
10 |
v2 | Server → Client | Server settings negotiation |
5.2.1. cmdSettings (4)¶
The Client MUST send cmdSettings immediately upon opening a new Session. If a Server receives cmdSYN before cmdSettings, it MUST reject the Session.
The payload is a UTF-8 list of key=value pairs separated by \n:
5.2.2. cmdServerSettings (10)¶
If the Client advertises v >= 2, the Server MUST reply with cmdServerSettings immediately after receiving cmdSettings:
5.2.3. cmdAlert (5)¶
Carries a UTF-8 warning message from the Server. The Client MUST read and log it, after which both endpoints MUST close the Session. A Server MAY use this command to reject outdated or non-compliant Clients.
5.2.4. cmdUpdatePaddingScheme (6)¶
If the Server observes that the Client's padding-md5 (from cmdSettings) differs from the Server's active scheme, the Server MUST send this command carrying the new scheme (Section 6).
5.2.5. cmdHeartRequest (8) and cmdHeartResponse (9)¶
Upon receiving cmdHeartRequest, an endpoint MUST reply with cmdHeartResponse. These frames detect and recover stalled tunnels.
5.3. Stream Lifecycle Commands¶
5.3.1. cmdSYN (1)¶
Requests the Server to open a new Stream. The Client MUST assign monotonically increasing streamId values within a Session.
5.3.2. cmdSYNACK (7)¶
For Clients advertising v >= 2, the Server SHOULD send cmdSYNACK with the matching streamId once the outbound TCP handshake finishes:
- An empty payload indicates a successful outbound connection;
- A non-empty payload carries an error message, upon which the Client MUST close the Stream.
5.3.3. cmdPSH (2)¶
Carries the target destination address and subsequent proxied bytes for the Stream.
5.3.4. cmdFIN (3)¶
Signals closure of the specified Stream:
- While the Session is healthy, the receiving endpoint closes its local Stream and does not need to reply with
cmdFIN; - If the Session is already closing, sending
cmdFINis unnecessary.
6. Dynamic Padding Scheme¶
The padding scheme governs how early TLS writes are fragmented and padded to obfuscate packet size distributions.
6.1. Scheme Format¶
Sent as a newline-separated directive string:
6.2. Directives and Execution Rules¶
stop: e.g.stop=8applies the padding scheme only to the first 8 writes (indices0through7).0=X-Y: Rule for packet 0 (padding0during authentication). Packet 0 is never fragmented; the Client chooses a random length in[X, Y]bytes.1and above: Rules for Session-phase writes.
Packet counting: Indices count Write() calls on the underlying TLS connection:
- Packet 1 typically holds
cmdSettings+ the first Stream'scmdSYN+cmdPSHcarrying the target address. - Packet 2 typically holds the first user payload chunk (such as the proxied TLS ClientHello).
Fragmentation and padding logic: Given a rule A-B,C-D:
- Fragment the pending write: choose the first chunk size randomly in
[A, B], the second in[C, D], and so on (measured in TLS plaintext bytes, excluding TLS record overhead). - If user data remains after all configured chunks, write the remainder natively.
- If user data is exhausted before all chunks are emitted, the sender MUST emit
cmdWasteframes (RECOMMENDED: zero-filled) to meet the required chunk sizes. - Check marker (
c): When,c,is present and no user data remains after the preceding chunk, the implementation MUST return from the write immediately and MUST NOT send further padding packets defined afterc.
6.3. Scheme Lifecycle¶
- A Client MUST store the
paddingSchemeper Server. - On its first connection to a Server, a Client MUST use the default
paddingScheme. - Upon receiving
cmdUpdatePaddingScheme, the Client MUST apply the new scheme to all subsequent Sessions opened to that Server, limiting exposure of default fingerprints to initial connections.
7. Connection Multiplexing¶
Clients MUST multiplex streams using a Session pool layered as TCP Proxy -> Stream -> Session -> TLS -> TCP.
7.1. Multiplexing Strategy¶
- Before dialing a new Session, the Client MUST check its idle Session pool.
- If idle Sessions exist, the Client MUST pick the one with the highest sequence number (
Seq). - Otherwise, the Client opens a new Session with a monotonically increasing
Seq. - When a Stream closes cleanly on a healthy Session, the Session is returned to the idle pool with its idle timestamp set to
now. - Clients SHOULD periodically (e.g., every 30 seconds) reap Sessions idle past a threshold (e.g., 60 seconds).
- Servers MAY likewise reap Sessions that observe no uplink or downlink activity for an extended period.
8. Proxy Protocol Interaction¶
8.1. TCP Proxy¶
After opening a Stream with cmdSYN, the Client MUST send the target destination encoded in SOCKS5 address format [RFC1928] inside a cmdPSH frame, after which bidirectional proxying begins.
8.2. UDP Proxy¶
UDP proxying layers sing-box udp-over-tcp v2 over a TCP Stream directed to the special domain sp.v2.udp-over-tcp.arpa.
9. Protocol Parameters¶
TLS parameters are configured externally. AnyTLS-specific parameters are:
9.1. Client Parameters¶
password(String, REQUIRED): Pre-shared authentication password.idleSessionCheckInterval(Duration, OPTIONAL): Interval for scanning the idle Session pool.idleSessionTimeout(Duration, OPTIONAL): Maximum idle time before a Session is closed.minIdleSession(Integer, OPTIONAL): Minimum number of idle Sessions kept warm in reserve.
9.2. Server Parameters¶
paddingScheme(String, OPTIONAL): Master padding scheme pushed to Clients.
10. Security Considerations¶
- Confidentiality and integrity rely entirely on the underlying TLS layer; Clients SHOULD enforce strict TLS certificate verification to prevent credential hashes from being intercepted.
- Because
sha256(password)is static across connections, strong TLS authentication and active-probing fallback behavior are essential on the server. - Implementations MUST strictly validate
dataLenand padding boundaries to prevent memory exhaustion or out-of-bounds reads.
11. References¶
- [RFC1928] Leech, M., et al., "SOCKS Protocol Version 5", RFC 1928, March 1996.
- [RFC2119] Bradner, S., "Key words for use in RFCs to Indicate Requirement Levels", BCP 14, RFC 2119, March 1997.
- [RFC8174] Leiba, B., "Ambiguity of Uppercase vs Lowercase in RFC 2119 Key Words", BCP 14, RFC 8174, May 2017.
Appendix A. Version History¶
- v1: Initial specification.
- v2 (v0.0.8, April 2025): Added
cmdSYNACKfor outbound connection status reporting and stuck-tunnel recovery,cmdHeartRequest/cmdHeartResponsekeep-alives, andcmdServerSettingsnegotiation. - v2 (v0.0.10, September 2025): Clarified
cmdFINsemantics for Stream and Session closure.