AnyTLS
AnyTLS is a proxy protocol that runs inside an ordinary TLS connection. Right after the TLS handshake the client sends a hash of its password once, and from then on the connection is a session: it carries streams, one per proxied connection, and a client reuses it for one stream after another. A padding scheme, which the server pushes to its clients, decides the sizes of the TLS records the client writes first. etemenanki-app speaks it in both directions since 2.6.0: an anytls inbound accepts AnyTLS clients, and an anytls outbound connects through an AnyTLS server. Both carry TCP and UDP. katana serves AnyTLS nodes since 4.9.0 (katana nodes).
The implementation speaks version 2 of the anytls-go protocol, as sing-box does, and is tested against sing-box 1.14.3 in both directions (Compatibility with sing-box).
At a glance
Section titled “At a glance”| Inbound (server) | Outbound (client) | |
|---|---|---|
| Credentials | users = [{ password = "…", email = "…" }] |
password = "…" |
| Transport | network = "tls" only |
network = "tls" only |
| REALITY, WebSocket, gRPC, a Unix socket | Refused | Refused (no Unix socket on this side) |
| TCP | Yes | Yes |
| UDP | Yes, always on: UDP-over-TCP version 2 and version 1 | Yes: UDP-over-TCP version 2, each datagram with its own address |
| Connections | One TLS connection per session, which may carry many streams | A pool of sessions; each carries one flow at a time |
| Padding scheme | Pushes padding_scheme, or the reference default |
Pads with the server’s scheme from its next session on |
A minimal server and client
Section titled “A minimal server and client”The server listens on every interface on port 443, terminates TLS with your certificate, accepts two users and sends their traffic straight out. A rule sends requests for private, loopback and link-local addresses to a blackhole; it matches only requests that name an IP address. The padding_scheme it lists is the reference default, written out so you can see its shape: leave the key out and the server pushes the same scheme.
The client runs a local SOCKS proxy and sends everything, TCP and UDP, through the server. server is the address to dial; tls.server_name is the name the server’s certificate is checked against. You can leave server_name out when server is already that name.
# An AnyTLS server on port 443 with two users, the default padding scheme and a direct exit.# Replace the certificate paths and the passwords before you use it.
[log]level = "info"
[[inbound]]tag = "anytls-in"protocol = "anytls"listen = "0.0.0.0"port = 443
# AnyTLS runs over TLS on TCP and nothing else.[inbound.stream]network = "tls"
[inbound.stream.tls]cert_file = "/etc/etemenanki/fullchain.pem"key_file = "/etc/etemenanki/privkey.pem"
[inbound.settings]users = [ { password = "replace-with-a-long-random-password", email = "alice@example.com" }, { password = "replace-with-another-long-random-password", email = "bob@example.com" },]# Leave padding_scheme out to push the reference default; this is that default, line by line.padding_scheme = [ "stop=8", "0=30-30", "1=100-400", "2=400-500,c,500-1000,c,500-1000,c,500-1000,c,500-1000", "3=9-9,500-1000", "4=500-1000", "5=500-1000", "6=500-1000", "7=500-1000",]
[[outbound]]tag = "direct"protocol = "freedom"
[[outbound]]tag = "block"protocol = "blackhole"
[route]default = "direct"
# Refuse requests for private, loopback and link-local addresses. This rule# only sees requests that name an IP: a domain is not resolved before routing.[[route.rule]]outbound = "block"cidr = [ "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "127.0.0.0/8", "169.254.0.0/16", "fc00::/7", "fe80::/10", "::1/128",]# A local SOCKS proxy that sends everything, TCP and UDP, to an AnyTLS server.# Replace the server address, the domain and the password with your server's values.
[log]level = "info"
[[inbound]]tag = "socks-in"protocol = "socks"listen = "127.0.0.1"port = 1080
[[outbound]]tag = "proxy"protocol = "anytls"server = "203.0.113.10" # the address to dialport = 443
[outbound.stream]network = "tls" # the only network AnyTLS takes
[outbound.stream.tls]server_name = "proxy.example.com" # SNI, and the name the certificate is checked against
[outbound.settings]password = "replace-with-a-long-random-password"# The session pool, in seconds; these are the defaults.idle_session_check_interval = 30idle_session_timeout = 30min_idle_session = 0
[route]default = "proxy"Check both files with etemenanki-app --test -c <file> before you start them. To generate a password, run openssl rand -base64 32 and use the output as is.
Inbound settings
Section titled “Inbound settings”An AnyTLS inbound takes the common inbound keys (tag, protocol, listen, port, stream, sniffing and the rest), described in Inbounds, plus this [inbound.settings] table:
| Key | Type | Required | Default | Description |
|---|---|---|---|---|
users | array of tables | no | [] | The accounts this inbound accepts, one table per user, written as for Trojan. sing-box's name key is not accepted; write email. |
users[].password | string | yes | — | The user's secret. A client sends SHA-256(password) right after the TLS handshake, and the server looks the digest up among its users, so use a long random value. Two users cannot share a password (users … and … present the same credential). |
users[].email | string | yes | — | The name this user is known by. Required in every entry (inbound <tag>: users[N] has no email; a user of this inbound is named by its email), and two users cannot share it (users[0] and users[1] are both named "…"). It never goes on the wire and need not be an e-mail address. |
padding_scheme | array of strings | no | — | The padding scheme the server pushes to every client whose own scheme differs, one line per entry, as sing-box writes it (["stop=8", "0=30-30", …]). The lines are joined with \n; the result must parse and be at most 2048 bytes. Leave the key out for the reference default; an empty list fails with padding scheme is empty. |
users is written exactly as for Trojan, as an inline array or as [[inbound.settings.users]] tables. etemenanki-app hashes every password once, when the config is built, and finds a client’s user with one lookup of the hash.
[inbound.stream] must say network = "tls", with tls.cert_file and tls.key_file. A redundant security = "tls" is accepted.
To add or remove a user, edit the file and let hot reload pick it up. The listener keeps running. A removed user, or a user whose password changed, loses its open sessions, every stream on them included; other users’ sessions carry on. A new padding_scheme reaches the sessions that start after the reload; open sessions keep the scheme they started with.
The padding scheme
Section titled “The padding scheme”A padding scheme is a list of key=value lines. The client counts what it sends on a session in packets. Packet 0 is the authentication. From packet 1 on, a packet is one batch of frames sent at once: on a new session, packet 1 holds the settings, the first SYN and its destination, and later packets mostly hold a stream’s data. The scheme shapes the first packets of every session:
| Line | Meaning |
|---|---|
stop=N |
Pad packets 0 to N - 1. Later packets go out as they are. |
p=items |
How packet p is cut into TLS records, one item per record, separated by commas. |
item min-max |
One record of a size drawn between min and max. The packet’s data is cut to that size, or filled up to it with padding. |
item c |
Stop the packet here if no data is left. Without c, every listed record is written, and a record after the data has run out is padding alone. |
For packet 0, only the first item counts: it is the length of the zero padding sent after the password hash. As in the reference implementations, a size is drawn from min up to max - 1, and is max only when the two are equal. Data left over after a packet’s last item goes out in one more write. A line for a packet at or past stop is accepted and never used.
This is the reference default, the scheme every client starts with (137 bytes, md5 75cff2ad89aadf5e257059ee571ebe11):
stop=80=30-301=100-4002=400-500,c,500-1000,c,500-1000,c,500-1000,c,500-10003=9-9,500-10004=500-10005=500-10006=500-10007=500-1000At the start of each session a client tells the server the md5 of the scheme it holds. When it differs from the server’s, the server pushes its own scheme, and the client uses it from its next session on. So a client meets a new scheme on its first session and pads by it from its second.
In padding_scheme you write the lines as a list of strings, as sing-box does. etemenanki-app joins them with \n and checks the result strictly. Other implementations skip a line they cannot read; etemenanki-app refuses the configuration, so that a typo stops the server instead of quietly padding less:
| Rule | Error when broken |
|---|---|
| The list is not empty | padding scheme is empty |
| The joined lines are at most 2048 bytes | padding scheme is 2485 bytes, more than 2048 |
| No line holds a carriage return | padding scheme has a carriage return; join its lines with \n |
Every line is key=value; an empty entry is not |
padding scheme line 2 is not key=value |
A key is stop or a packet number, in plain decimal without sign or leading zero |
padding scheme line 2: the key is neither stop nor a packet number |
| No key appears twice | padding scheme line 3 repeats a key |
stop is a number |
padding scheme line 1: stop is not a number |
Each item is c or min-max with 1 <= min <= max <= 1048576 |
padding scheme line 2: an item is neither c nor min-max with 1 <= min <= max <= 1048576 |
| Packet 0’s sizes are at most 65535 | padding scheme line 2: packet 0 is the authentication's padding, at most 65535 bytes |
There is a stop line |
padding scheme has no stop |
Line numbers count from 1, in the order of the list.
Outbound settings
Section titled “Outbound settings”An AnyTLS outbound needs the common outbound keys server and port, and an [outbound.stream] block with network = "tls"; see Outbounds. The certificate is checked against the system roots by default. Set tls.ca_file to trust a private CA instead, or tls.allow_insecure = true to skip the check; the two cannot be combined. The Transports page covers every TLS key. Its [outbound.settings] table:
| Key | Type | Required | Default | Description |
|---|---|---|---|---|
password | string | yes | — | The password the server knows you by. The outbound sends SHA-256(password) once per session, right after the TLS handshake. Without it the outbound fails with an invalid settings: missing field error that names password; an empty one fails with anytls password must not be empty. |
client | string | no | "" | What the client setting announces to the server at the start of each session. Empty, the default, names no client software. |
idle_session_check_interval | u64 | no | 30 | Seconds between two checks of the idle sessions in the pool. A whole number of seconds, not a duration string: "30s" fails. A value of 5 or less means 30. |
idle_session_timeout | u64 | no | 30 | Seconds a session may stay idle in the pool before a check closes it. A value of 5 or less means 30. |
min_idle_session | integer | no | 0 | How many idle sessions a check keeps however long they have been idle, the newest first. 0 lets every idle session time out. |
A missing server fails with outbound proxy: missing server, and a missing port with outbound proxy: missing port. An AnyTLS outbound has a TCP address to probe, so it can be a member of a balancer.
Sessions and streams
Section titled “Sessions and streams”One TLS connection is one session. The client authenticates once, then sends its settings: its protocol version, the client value and the md5 of its padding scheme. Every proxied connection is a stream on the session. The client opens it with a SYN under a new stream id, sends the destination as the stream’s first data, and then the payload. A FIN from either side ends the stream. The session lives on after its streams end.
sequenceDiagram participant C as Client participant S as AnyTLS inbound participant T as Target C->>S: TLS handshake C->>S: Password hash and padding, settings, SYN and destination of stream 1 S-->>C: Version 2, and the server's scheme if the md5 differs S->>T: Connect through the outbound the router picks S-->>C: SYNACK for stream 1 once connected C->>S: Payload of stream 1 S->>T: Payload T-->>S: Reply S-->>C: Reply in stream 1 C->>S: FIN of stream 1, later SYN of stream 2 on the same session
The client’s session pool
Section titled “The client’s session pool”Like every reference client, the outbound gives each session to one flow at a time:
- A new flow takes the newest idle session from the pool, or dials a new TLS connection when none is idle. Two flows at once therefore use two sessions.
- When the flow ends, the outbound sends its FIN and puts the session back in the pool.
- Every
idle_session_check_interval, sessions idle longer thanidle_session_timeoutare closed, except the newestmin_idle_session. - A stream has no half-close. When the local side has finished sending, the outbound sends nothing and keeps reading until the server ends the stream.
- When a version 2 server sends no frame at all within 3 seconds of the SYN of a session’s second or later stream, the outbound takes the session to be stuck and drops it, and the flow fails with
anytls: the server did not answer the stream's SYN. - A session that fails, or that the server closes, is not reused. A flow that cannot open on an idle session is retried once on a new one.
What the server does
Section titled “What the server does”- A session holds at most 256 streams at once. The server declines a further stream with a FIN and keeps the session.
- A version 2 client gets a SYNACK once a stream’s outbound has connected. If the outbound cannot connect, it gets a SYNACK with the text
outbound connection failedand a FIN; nothing about the destination’s network reaches the client. A version 1 client gets neither, and sees only the FIN. - Each stream is routed on its own destination, with the session’s user.
- A malformed destination ends only that stream, with a FIN.
- A frame the protocol forbids ends the whole session: the server sends an Alert that names the fault and closes. The forbidden frames are an unknown command, data on a command that carries none, a command only servers send, a SYN before the client’s settings, a SYN whose id is not above the last one, and settings longer than 4096 bytes. A frame for a stream that has already ended is ignored.
- When the client’s connection ends, every stream of the session ends with it.
A wrong password ends the connection as soon as the 32-byte hash is in, without a reply.
UDP rides inside a stream, in sing’s UDP-over-TCP format. A stream whose destination is one of two reserved names carries packets instead of bytes:
| Destination | Version | What follows |
|---|---|---|
sp.v2.udp-over-tcp.arpa |
2 | A request (a connect flag and a SOCKS address), then packets. With the flag set, every packet goes to the request’s address and names none; without it, every packet names its own peer. |
sp.udp-over-tcp.arpa |
1 | Packets, each naming its own peer. |
The names are compared without regard to case, and the port is ignored. A packet that names its peer starts with an address of its own type bytes (0 IPv4, 1 IPv6, 2 domain, then the port), followed by a 2-byte length and the payload.
The inbound serves all three forms; there is no switch to turn UDP off. Every packet is routed on its own destination, so one stream can reach several peers through different outbounds, and each reply goes back named with the address it came from. To keep an AnyTLS inbound TCP-only, route its UDP to a blackhole:
[[route.rule]]outbound = "block"inbound_tag = ["anytls-in"]network = "udp"The outbound opens one version 2 stream per UDP flow, with the flag unset, so every datagram carries its own address and every reply names its sender.
| Limit | Value | What happens past it |
|---|---|---|
| UDP payload the server relays, either way | 8192 bytes | A larger packet from the client is skipped; the stream goes on. |
| A packet split across frames, held by the server until whole | 64 KiB per session, all streams together | A packet that would go past it is skipped. |
| Datagram the outbound sends | 16116 bytes | The send fails with anytls: datagram too large for UDP-over-TCP. |
Against an etemenanki-app server, keep datagrams at or under 8192 bytes.
Sniffing
Section titled “Sniffing”When the inbound’s sniffing is on (the default) and a stream’s destination is an IP address, the server looks for a TLS server name or an HTTP Host in the payload that arrived in the same read as the end of the destination. It does not hold the stream to wait for more. A client that sends its first payload in a later write may therefore be routed on its IP address alone. A domain destination and UDP streams are never sniffed.
Compatibility with sing-box
Section titled “Compatibility with sing-box”These shapes are tested end to end against a real sing-box 1.14.3 binary in the project’s tests:
| Shape | What is tested |
|---|---|
etemenanki-app anytls outbound to a sing-box anytls inbound |
TCP echoes from 43 bytes to 300 KiB, two flows at once, and UDP, under the default padding scheme and under a custom scheme the server pushes |
sing-box anytls outbound to an etemenanki-app anytls inbound |
The same |
Moving a sing-box config over:
| sing-box | etemenanki-app |
|---|---|
inbound users[].name, password |
[inbound.settings] users[].email, password |
inbound padding_scheme, a list of lines |
[inbound.settings] padding_scheme, the same lines |
inbound or outbound tls |
[inbound.stream] or [outbound.stream] with network = "tls", and its tls table |
outbound server, server_port |
server, port on the [[outbound]] table |
outbound password |
[outbound.settings] password |
outbound client_metadata |
[outbound.settings] client |
outbound idle_session_check_interval, idle_session_timeout (durations such as "30s") |
The same keys, in whole seconds: 30 |
outbound min_idle_session |
[outbound.settings] min_idle_session |
Subscribe files
Section titled “Subscribe files”A subscribe file can offer an AnyTLS node. AnyTLS is TLS over TCP and nothing else, so a node has no stream table: its TLS keys sit in the node itself.
[[proxy_server]]name = "tw-anytls"server = "proxy.example.com"port = 443protocol = "anytls"password = "replace-with-a-long-random-password"server_name = "cdn.example.com"| Key | Required | Meaning |
|---|---|---|
password |
Yes | The user’s password. |
server_name |
No | The SNI, and the name the certificate is checked against; the node’s server when absent. |
allow_insecure |
No | Skip the certificate check; false when absent. |
A stream key, or a session pool key such as idle_session_timeout, fails the file as an unknown field: the pool belongs to the client, and an outbound built from a node uses the defaults. A client announces the subscribe format revision it reads as etemenanki-subscribe/<version>; AnyTLS nodes need revision 0.3 or later. See Subscribe files.
katana nodes
Section titled “katana nodes”katana 4.9.0 and later serve AnyTLS nodes for newV2board (Xboard) and helium panels. On newV2board, set node_type = "AnyTLS" (any case) in [node.api]. On helium, set no node_type: the panel’s node config says anytls. The node is always TLS over TCP, with the certificate in [node.controller.cert], on the panel’s port (server_port on newV2board, port on helium). The panel’s padding_scheme, a list of lines, is pushed to clients; an absent list, or an empty one from newV2board, means the reference default. Each user’s password is the user’s UUID, and a wrong password counts toward the IP ban described in Device limits and IP bans. A new scheme from the panel reaches the sessions that start after it, and a node config katana cannot serve is refused. See AnyTLS nodes.
katana bills an AnyTLS user for the session’s bytes inside TLS: the authentication, the frames and the padding included, TLS records excluded. Every stream of a session counts toward the session’s user.
Limits
Section titled “Limits”| Limit | Value | What happens past it |
|---|---|---|
| Time to send the first byte after the TLS handshake | 10 seconds | The connection closes. |
| Time to finish the authentication and its padding, counted from the first byte | 10 seconds | The connection closes. |
| Idle time on a session, all streams together | 300 seconds | The connection closes. |
| Streams open at once on one session | 256 | The new stream is declined with a FIN; the session stays. |
| Client settings | 4096 bytes | The session ends with an Alert. |
| Padding scheme | 2048 bytes; record sizes 1 to 1048576; packet 0 at most 65535 | The configuration is refused. |
| UDP payloads and reassembly | See UDP | — |
| Outbound: answer to a later stream’s SYN from a version 2 server | 3 seconds | The session is given up and the flow fails. |
| Outbound: idle check interval and idle timeout | 30 seconds by default; 5 or less means 30 | A session idle past the timeout is closed at the next check. |
Per-inbound connection limits apply to AnyTLS like every stream inbound, counted per TLS connection; see Limits.
Common errors
Section titled “Common errors”A configuration error makes --test log configuration invalid: and a start log failed to start:, followed by the message below; both exit with status 1. The inbound in these messages is tagged anytls-in and the outbound proxy. An error inside a user entry or a setting prints a second line after the message, such as in `users` or in `idle_session_timeout`. The padding scheme errors are in The padding scheme.
| Message | Cause | Fix |
|---|---|---|
inbound anytls-in: anytls runs over tls only; its shape must be tls |
No [inbound.stream], or a network other than tls |
Set network = "tls" with a certificate and key |
outbound proxy@v0: anytls runs over tls only; its shape must be tls |
The same on an outbound | Set network = "tls" |
inbound anytls-in: reality is for vless only, or outbound proxy@v0: reality is for vless only |
security = "reality" |
Use network = "tls" |
inbound anytls-in: protocol anytls over a unix socket does not support stream network "tls" |
A listen path |
Listen on an address and port |
outbound proxy: security = "tls" is not valid with network = "tcp"; use network = "tls" for TLS over plain TCP (security = "tls" layers TLS under network = "ws" or "grpc") |
Xray’s spelling of TLS over TCP | Write network = "tls" |
inbound anytls-in: invalid settings: missing field `password` |
A user entry has no password |
Give every entry a password |
inbound anytls-in: invalid settings: unknown field `name`, expected `password` or `email` |
sing-box’s name in a user entry |
Write email |
inbound anytls-in: users[0] has no email; a user of this inbound is named by its email |
A user without email |
Give every user a unique email |
inbound anytls-in: users a@example.com and b@example.com present the same credential |
Two users share a password | Give each user its own password |
outbound proxy: invalid settings: missing field `password` |
The outbound has no [outbound.settings] or no password in it |
Add password |
outbound proxy@v0: anytls password must not be empty |
password = "" |
Set the password |
outbound proxy: invalid settings: invalid type: string "30s", expected u64 |
A sing-box duration | Write whole seconds: 30 |
outbound proxy: invalid settings: unknown field `padding_scheme`, expected one of `password`, `client`, `idle_session_check_interval`, `idle_session_timeout`, `min_idle_session` |
An inbound key on the outbound | The client takes the scheme from the server |
outbound proxy: tls.allow_insecure and tls.ca_file cannot both be set |
Both verification overrides | Keep one |
At run time, a refused connection is logged on the server at debug level, as one line per connection:
anytls connection from Some(198.51.100.7) ended: proxy core: anytls: invalid userOther reasons after ended: are proxy core: client did not complete its request in time (the authentication was not finished within 10 seconds of its first byte) and inbound handshake timed out after 10s (nothing was sent after the TLS handshake). A session ended over a forbidden frame logs anytls: closing session: <fault>, and a declined destination anytls: stream <id> connect failed: <error>, both at debug.
On the client, a flow that fails is logged by the inbound that carried it, also at debug, for example socks connection from Some(127.0.0.1) ended: <reason>:
| Reason | Meaning |
|---|---|
anytls: the server closed the session |
The server ended the connection: most often a wrong password. |
anytls: the server could not connect: outbound connection failed |
The server could not reach the destination. The session survives and is reused. |
anytls: the server did not answer the stream's SYN |
The 3-second check on a pooled session fired. |
anytls: alert from server: <text> |
The server refused a frame and ended the session. |