Commit Graph
13 Commits
Author SHA1 Message Date
eric e319670a83 fix(plugins): offer only the password in the C and Zig SOCKS5 clients
The C and Zig SOCKS5 plugins offered "no authentication" alongside
username/password when credentials were configured, and carried on if
the server picked it. The server, or anyone in the path of an
unprotected connection, could skip the check the deployer asked for.

With credentials set, the greeting now offers username/password alone,
and a server that picks anything else is a fatal handshake error before
the request goes out. Each suite adds a server that skips
authentication; both fail on the old code.
2026-12-04 09:55:40 +08:00
eric 146f50975b fix(log): keep connection destinations out of routine logs
At info level the dispatcher logged each session's source, destination,
sniffed host and process name, and the Go Trojan plugin logged the
destination of every engine it created. Anyone who could read the logs
could see where users connected, however the proxy link was protected.

Info now records the protocol, inbound, outbound and connect time. The full
line, and the Trojan plugin's, are logged at debug level, which has to be
asked for.
2026-12-04 09:55:40 +08:00
eric 1cf7f109b3 fix(plugin): drop a datagram the engine refuses, not the session
On an unreliable transport, a decode_packet failure ended the receive
side of the session, which leaf then removed. Every reference plugin
reports a reply it rejects -- failed authentication, an address it cannot
parse -- with PLUGIN_FAILURE, and a reply that fails authentication needs
no key. So one forged packet sent to the client's socket discarded every
reply that arrived before the client next sent something.

A frame the engine refuses with PLUGIN_FAILURE on an unreliable
transport is now dropped like any other damaged packet. Any other status
-- an argument the engine cannot use, an unsupported call, an output it
still cannot fit -- says the engine itself cannot go on, and still ends
the session. So does any failure on a reliable transport, since the
stream cannot be resynchronised, and an engine that breaks the ABI's
contract. The ABI documents the exception.

The e2e case now keeps one association, sends a good reply straight behind
each bad one, and adds a forged reply. It fails on the old host.
2026-12-04 09:55:40 +08:00
eric 00eb09a0cb feat(plugins): add the AnyTLS and ShadowTLS v3 reference plugins
Two protocols leaf does not implement, each a thin Go adapter over the
library existing implementations use: anytls/sing-anytls for AnyTLS, and
sing-shadowtls with metacubex/utls for ShadowTLS, at sing-box v1.14.4's pins.

AnyTLS keeps TLS inside the plugin, since its padding scheme is defined over
TLS records, and shares one client per outbound so a server's own scheme
carries to the next connection. ShadowTLS is a transport under a chain,
usually Shadowsocks.

The ShadowTLS adapter signs the session ID itself rather than through uTLS's
hook: with a browser fingerprint that hook sends Go's own ClientHello, and
breaks on a HelloRetryRequest.

Each has unit tests against the library's server, READMEs, and example
configs. Both binaries are GPL-3.0, from their libraries.
2026-12-03 19:31:53 +08:00
eric 5ac82cd54f fix(plugins): three Go SDK defects, and test helpers for plugin authors
host_ctx is opaque, and leaf puts an instance number in it. The SDK held it
as a Go pointer, so the first stack copy that found a value like 1 stopped
the process with "invalid pointer found on stack". It is now an integer on
the Go side and a pointer only in C.

ConnEngine refused application input behind a busy write loop without
reporting BLOCKED. The host read that as an engine waiting for its output to
be taken, buffered decoded bytes to make room, and failed saturated
two-way transfers as stalled, about one run in ten. It now reports BLOCKED
once the handshake is done and the input buffer is full.

On the application's half close ConnEngine closed the wrapped conn whole,
dropping the reply still on its way. It now calls CloseWrite when the conn
has one and keeps reading.

enginetest.Driver drives an engine the way the host does, for a plugin's own
tests; SharedLogger routes a per-outbound library logger into leaf's log.
2026-12-03 19:31:53 +08:00
eric dc75722f6f feat(plugins): add the SOCKS5 plugin in C and in Zig
The ABI's claim is that a plugin needs a C compiler and nothing else. These
two are the evidence: the same SOCKS5 stream engine written twice, once in
plain C11 against leaf_plugin_abi.h and once in Zig importing that same
header, with no SDK, no runtime and no dependency beyond libc between them.

Both are state machines over the greeting, the method reply, the request and
the reply, so neither ever blocks or holds a thread. Each carries its own
tests -- socks5_test.c runs the engine against truncated and malformed
replies, and the Zig build runs its tests through `zig build test`.
2026-12-03 19:31:53 +08:00
eric 32945334a9 feat(plugins): add the Go SDK, and the TLS and Trojan plugins on it
Writing a plugin in Go means writing the vtables and const-pointer shims cgo
cannot express, handle allocation, panic recovery, the two-pass
get_last_error protocol and address encoding -- once per plugin, by hand.
leaf-abi-go holds all of it once, so a plugin is a package that registers
engines and implements them over byte slices: tls-cabi-go is 143 lines,
trojan-cabi-go 321.

ConnEngine is the other half. crypto/tls insists on a blocking net.Conn,
which the ABI forbids an engine call from being, so it runs the
implementation on goroutines over an in-memory pipe and reports BLOCKED with
a wake while those are what it waits on. A codec needing none of that
implements StreamEngine directly, as trojan does.

The SDK revokes its host handle once Destroy returns, so a goroutine
outliving its engine drops a log line rather than calling into memory the
host has reclaimed, and it masks BLOCKED off when the host offered no wake
callback. Register runs from Go's init and calls out to C; it cannot go the
other way, since entering Go binds a thread to the Go runtime for good and
the thread that loads a plugin is the host's.

The descriptor declares that these plugins embed a language runtime, so the
host never unmaps them.
2026-12-03 19:31:53 +08:00
eric 60c2b4d014 feat(plugins): add the shadowsocks and TLS plugins in Rust
Two reference plugins on the C ABI, and the first users of the contract.

shadowsocks-cabi-rs is the sample the in-process API used to carry, rewritten
as a stream engine: AEAD ciphers, the SOCKS address codec, and the request
header written when the engine is created rather than on the first push, so a
connection that sends nothing still reaches the far end. The wire header stays
out of the address buffer it is derived from.

tls-cabi-rs wraps rustls as a stream engine, and depends on nothing from leaf,
so the cdylib carries no second copy of the proxy engine.

Both catch a panic at the boundary and report it as an error. A panic that
crosses the ABI is undefined behaviour, and a plugin that faults its host is
worse than one that fails.
2026-12-03 19:31:53 +08:00
eric a3df017b4d feat(plugin)!: replace the in-process plugin API with a C ABI host
The old API handed dynamic libraries Rust trait objects, tying every plugin to
leaf's exact compiler and crate versions and ruling out other languages. It is
removed, along with its shadowsocks sample, which returns on the new ABI in a
later commit.

The host loads plugins over leaf-plugin-abi instead. It validates the
descriptor, dials the endpoint, and drives the engines as sans-io codecs over
its own transport, exposing them as ordinary outbound handlers. Datagram
engines are either unreliable, with a UDP socket of their own, or reliable,
framed over the stream transport. Plugin failures carry the plugin's last error
string, and plugin logs join the session's tracing span.

Everything crossing the boundary is treated as untrusted: counts are checked
against the buffers they describe, reported strings are bounded and escaped,
engines get an opaque id rather than a pointer for host_ctx, and an engine that
takes no input is backpressure rather than failure. A poll does a bounded
amount of work, and a datagram the receiver cannot take is dropped instead of
the session.

An outbound names its path, host and port, and may pin the sha256 of the
library it loads -- the only check on this path that still holds against
someone who can write the file.

`leaf --verify-plugin <path>` runs the same checks without a config file and
prints what the plugin declares: its ABI version, its engines, whether the
outbound must carry host and port, and the digest to pin it to. A build can
then be vetted before it is in the path of any traffic.
2026-12-03 19:31:53 +08:00
eric e748167d79 Enhance logging 2026-01-01 21:24:55 +08:00
eric 53944ad1f8 Replace trust-dns-proto with hickory-proto 2026-01-01 17:39:32 +08:00
eric 9d201ff6e0 refactor: additional abstraction over handler input and output 2021-07-06 22:48:53 +08:00
eric d35e649e62 Experimental plugin system 2021-07-04 10:11:32 +08:00