# Privacy, security and censorship
What a leaf configuration protects, what it leaves visible, and which choices
decide the difference. Option details are in
[configuration.md](configuration.md); this page is about what they mean for the
people whose traffic goes through leaf.
None of this makes any configuration safe from a determined censor. Which
protocols get noticed or blocked depends on the network, on what else runs
there, and on what the censor is looking for this month. Treat every
recommendation here as a starting point to test on the networks your users are
actually on.
## What the proxy hides, and what it does not
With an encrypted protocol and a verified certificate, the network between the
user and the proxy cannot read the destinations, the traffic, or the proxy
credential. It can still see:
- the proxy's address and port,
- when connections start and end, and how much goes each way,
- whatever the transport shows before encryption starts: for TLS, the server
name (unless ECH is used) and the handshake's own fingerprint; what runs
inside it, such as the WebSocket upgrade, is not visible,
- any DNS query leaf or the application sends outside the proxy.
Anyone who can read leaf's logs, its API, or its statistics can see more; see
[Logs and records](#logs-and-records).
## Keeping traffic behind the proxy
Most leaks are not in the protocol but around it: a lookup sent before the
proxy is used, or a request sent directly when the proxy is down.
### Names
- **Give the proxy as an IP address**, with the name on its certificate in
`tls.serverName`. A proxy given by name is looked up over the local network,
and with the default resolver that query is plain DNS naming the proxy before
any traffic reaches it. If it has to be a name, pin it in `dns.hosts`.
- **Let the proxy resolve destinations.** When the application hands leaf a
name -- SOCKS5 with a domain, HTTP `CONNECT`, FakeDNS under TUN -- leaf passes
the name to the proxy and never looks it up itself. An application that
resolves the name first has already sent the query, over the user's network,
wherever its own resolver is.
- **Leave `router.domainResolve` off.** With it on, every name no rule matches
is looked up before the outbound is chosen.
- **Route leaf's own queries through the proxy.** A `dns.servers` entry without
`direct:` is sent through the router; with a `FINAL` rule to the proxy it
goes through the proxy. Prefer DoH with the resolver's address given:
`doh:resolver.example@192.0.2.53`. Without the address, leaf asks the
operating system for the resolver's name first.
- **Avoid `system`** where lookups must not leave the proxy: it hands every
name to the operating system.
- The default resolver is `1.1.1.1` over plain DNS. That is fine through the
proxy and visible anywhere else. Set `dns.servers` explicitly.
### Where unmatched traffic goes
Traffic no rule matches goes to the first outbound, or to the target of the
`FINAL` rule. Make that the proxy, and write the exceptions that may go direct
as explicit rules.
### When the proxy is down
- A `failover` or `tryall` with a `direct` actor sends requests directly when
the proxy does not answer -- or, for `tryall`, all the time. Leave `direct`
out of them where a direct request would expose the user: the request then
fails.
- A `failover`'s `lastResort` (`last-resort` in `.conf`) is the same choice
made separately: set to `direct`, it sends everything directly whenever the
health check finds the proxy down.
- leaf does not retry a failed connection directly on its own. The application
might. Check what it does when the proxy is unreachable.
- `failover` health checks contact `www.google.com` and `1.1.1.1` through every
actor on a timer, `direct` included, unless `healthCheckTcpDestination` and
`healthCheckUdpDestination` name somewhere else.
### TUN
- `auto` takes over the IPv4 default route only. Unless `ENABLE_IPV6=true`,
IPv6 traffic leaves through the original interface. On a network with IPv6,
enable it, refuse IPv6 with `TUN_REJECT_IPV6`, or remove the host's IPv6
route.
- Traffic the operating system routes elsewhere never reaches leaf: other
routes, VPNs, software bound to a specific interface.
- With a local SOCKS or HTTP inbound instead, leaf sees only what each
application chooses to send it.
The only reliable check is from outside: capture traffic on the user's side of
the network, with IPv4 and IPv6, while using the application normally and
while the proxy is stopped, and look for anything that is not the proxy.
## Certificates
- `insecure: true`, in the `tls` outbound or the TLS plugins, accepts any
certificate. Anyone in the path can then pose as the proxy, read the Trojan
password and every destination, and change the traffic. It exists for test
fixtures. Do not ship it.
- `serverName` is what the certificate is checked against. Set it; left out,
it is the next hop's address, which is an IP address in the recommended
setup.
- The built-in `tls` outbound and `tls-cabi-rs` trust a bundled copy of the
public web's roots, `tls-cabi-go` the operating system's. `certificate`
replaces either with exactly the CAs given, which is the way to use a
private CA -- and means nothing else is trusted.
- The DNS-over-HTTPS client always checks against the public roots.
## Choosing a protocol
Encryption hides content, not the fact that a protocol is in use. A censor
can block a proxy's address once it knows it, without looking at the traffic
at all, and can look for traffic that does not resemble what it expects on
its network. The questions are what the protocol looks like on the wire, and
how that compares with what else the network carries.
- **Shadowsocks** makes every byte look random from the first packet. On a
network that sees mostly TLS and HTTP that stands out, and censors have acted
on it: the Great Firewall has been documented probing suspected Shadowsocks
servers [1] and blocking fully encrypted traffic that is not recognisably TLS
or HTTP [2]. Do not make it the default for networks with that kind of
filtering. `prefix` changes the first bytes, but identically on every
connection, which is a pattern of its own.
- **Trojan over WebSocket over TLS**, with a verified certificate, starts as an
ordinary TLS connection and hides the destination and credential inside it,
which is the approach of running the real protocol rather than imitating one
[3]. It is the better default where ordinary TLS is allowed. It is not
invisible: TLS implementations have fingerprints of their own, and leaf's
(rustls, or Go's for the Go plugins) differ from browsers' [4]; and the TLS
handshake a browser makes *inside* the tunnel can be recognised from the
sizes and timing of the encrypted packets around it [5]. Users in China
reported Trojan being blocked at scale in 2022 [6].
- **SOCKS5** has no encryption. The destination and any credentials are in the
clear. Use it only to reach something on the same machine or inside another
transport.
- **VLESS** has no encryption of its own. Use it inside TLS or REALITY.
- **obfs** imitates HTTP or TLS headers without being either.
- **AnyTLS** and **ShadowTLS** are available as plugins. They were added after
the audit and have not been reviewed in the same way.
The proxy's address is the other half. If many users share one address,
blocking it cuts all of them off; how quickly a censor finds more addresses
depends on how they are handed out, which is the operator's decision rather
than leaf's [7].
Long-lived connections stand out on networks where most are short, and
periodic ones -- health checks, `amux` keeping connections open -- form a
pattern. Whether that matters depends on the network; it is worth measuring
against ordinary traffic from the same vantage point.
## Logs and records
- At `info` leaf logs only protocols, tags and timings. At `debug` and `trace`
it logs source addresses, destinations, sniffed names and process names. Use
those levels to diagnose, limit who can read the output, and delete it.
- `MAX_RECENT_CONNECTIONS` and `HOST_STATS` keep destinations in memory for the
API and the FFI. Leave them off unless the application shows them only to
the user they belong to.
- The API has no authentication and can shut leaf down. Keep `API_LISTEN` on
loopback.
- On macOS `LOG_CONSOLE_OUT` sends console logging to the system log, where
other software can read it.
## Local listeners
- Inbounds listen on `127.0.0.1` unless told otherwise. On any other address
anyone who can reach the port can use the proxy.
- The `http` inbound has no authentication.
- A `socks` inbound's username and password are sent in the clear, and its UDP
relay tells clients apart by IP address only: anyone sharing an
authenticated client's address can use its association.
## Plugins
A plugin runs inside leaf with leaf's privileges and sees every byte it
carries. Ship only plugins built from source you trust, pin each with `sha256`,
and keep the library, its directory and the configuration writable only by
whoever deploys them. `--verify-plugin` loads the library to read it, which
runs its code, so use it on builds you were going to deploy anyway. See the
[README](../README.md#plugins).
Go plugins each carry their own Go runtime, and leaf keeps them loaded for the
life of the process. An application that ships two Go plugins should test
connections that close during the handshake or with a read or write pending.
The end-to-end suite does this for the Go TLS and Go Trojan plugins in one
chain (`plugin-cleanup/all-go/*`), checking that every engine instance is
destroyed and that leaf still shuts down; `make e2e` runs it.
## A starting point
A client that sends everything, DNS included, through Trojan over WebSocket
over TLS, connecting to the proxy by address and verifying it by name:
```json
{
"log": { "level": "warn", "output": "console" },
"dns": { "servers": ["doh:resolver.example@192.0.2.53"] },
"inbounds": [{
"tag": "socks-in", "protocol": "socks",
"address": "127.0.0.1", "port": 1080, "settings": {}
}],
"outbounds": [
{
"tag": "proxy", "protocol": "chain",
"settings": { "actors": ["tls", "ws", "trojan"] }
},
{
"tag": "tls", "protocol": "tls",
"settings": { "serverName": "proxy.example", "alpn": ["http/1.1"] }
},
{
"tag": "ws", "protocol": "ws",
"settings": { "path": "/proxy", "headers": { "Host": "proxy.example" } }
},
{
"tag": "trojan", "protocol": "trojan",
"settings": { "address": "192.0.2.10", "port": 443, "password": "REPLACE_ME" }
}
],
"router": {
"domainResolve": false,
"rules": [{ "type": "FINAL", "target": "proxy" }]
}
}
```
- The proxy is reached at `192.0.2.10`, so nothing is looked up to find it, and
its certificate is checked against `proxy.example`.
- leaf's own lookups go to the DoH resolver through the proxy, and the
resolver's address is given, so nothing is asked of the operating system.
- Everything goes to the proxy. If it is down, requests fail rather than go
direct.
- `warn` keeps connection details out of the log.
Change the addresses, names, path and password; the shape is the point. The
TLS plugins can stand in for the `tls` outbound, as in
[the examples](../examples/README.md).
## References
1. Alice et al., *How China Detects and Blocks Shadowsocks*, IMC 2020.
2. Wu et al., *How the Great Firewall of China Detects and Blocks Fully
Encrypted Traffic*, USENIX Security 2023.
3. Houmansadr et al., *The Parrot Is Dead: Observing Unobservable Network
Communications*, IEEE S&P 2013.
4. Frolov and Wustrow, *The use of TLS in Censorship Circumvention*, NDSS 2019.
5. Xue et al., *Fingerprinting Obfuscated Proxy Traffic with Encapsulated TLS
Handshakes*, USENIX Security 2024.
6. *Large scale blocking of TLS-based censorship circumvention tools in
China*, GFW Report, 2022.
7. Bocovich et al., *Snowflake, a censorship circumvention system using
temporary WebRTC proxies*, USENIX Security 2024.