Commit Graph
17 Commits
Author SHA1 Message Date
eric bedc609506 test(e2e): run the reference plugins against upstream servers
A new interop lane puts a real leaf with each plugin in front of the servers
people deploy, built from source at pinned revisions: anytls-go, sing-box
stable and its testing branch, mihomo, and the original shadow-tls. Every
pair runs the stream behaviours, plus the server's padding scheme checked on
the wire, fingerprints, and the connections that must fail: wrong password,
hijacked to the real site, TLS 1.2 site in strict mode.

AnyTLS skips half-close-tail, which its protocol cannot carry. ShadowTLS
marks it a known defect: the host shuts the socket's write side while an
engine is still handshaking.

`make interop` runs the lane, `make e2e` leaves it out, and CI runs it in a
job of its own with the built servers cached. The harness no longer hands
out a port twice in one process, and builds upstream Rust servers with their
own toolchain, outside the workspace.
2026-12-03 19:31:53 +08:00
eric 9ef8d0d343 test(e2e): add the end-to-end suite for the plugin host
`leaf-e2e` starts real leaf instances and drives them from the outside over
SOCKS5, so a case covers the path a deployment takes -- config parsing, plugin
loading, routing, the dispatcher, the C ABI data plane -- and nothing reaches
into leaf to build a handler by hand. Cases are values rather than `#[test]`
functions, so the suite is a matrix of implementation, protocol, transport and
behaviour instead of a wall of copies, and each runs in its own process, free
to crash, hang, or set load-time environment.

The lanes:

- `differential` runs a behaviour natively and again with a plugin substituted
  for one stage, and requires the two to agree. The server is stock leaf, so
  these are interoperability tests. `protocols`, `halfclose` and `teardown`
  extend that across every stack, relay and selector, the end of a request on
  each transport, and shutdown.
- `abi`, `loader` and `coexist` check what the host accepts, refuses, and can
  load at once, through a real `dlopen`.
- `hostile` and `datagram` drive conformance plugins -- one in Rust, one in Go
  -- that misbehave on request, exercising the host's bounds checks, clamps,
  buffer growth and `decode_packet` handling.
- `stress` and the meters measure both sides back to back and assert the ratio,
  since absolute numbers from a shared runner say little; throughput, latency,
  setup cost and cpu cost land in `meters.json` beside the case logs. What the
  process is holding is asserted directly instead, against the case's own
  allocator, because what matters there is that it stopped growing.
- `blackbox` runs the shipped binary: on a config file, and on a plugin
  library through `--verify-plugin`, whose report has to agree with what the
  loader itself would do.

Plugins are built by the harness, and a case whose toolchain is missing is
reported as ignored with the reason; `--strict`, which CI passes, makes that a
failure, so the suite cannot go green by running nothing. The suite runs on
Windows too, where a pause is never shorter than 16ms and cases that dribble
bytes bound their pauses instead.
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 97ea9fabd2 feat(plugin-abi): declare the C ABI that a host and its plugins share
Plugins are dynamic libraries, and the boundary between them and leaf has
to be something both sides can name without agreeing on a compiler. This
crate is that boundary and nothing else: dependency-free, no_std, every
repr(C) type and constant in one place, with include/leaf_plugin_abi.h as
the single C declaration for plugins that are not written in Rust.

A plugin exports a descriptor naming its ABI version, its struct sizes,
optional stream and datagram engines, and whether it embeds a language
runtime. abi_major must match exactly; abi_minor is informational and
never gates loading. Each struct freezes a REQUIRED_SIZE prefix for its
major version, and peers validate against that rather than their own
sizeof, so an old plugin runs on a newer host and the reverse.

The contract is written next to the declarations, because the parts that
matter are the ones a signature cannot state: what each object's lifetime
is, that instances move between threads and calls never overlap, that no
call may block -- the readiness callback is what makes that workable --
and that decode_packet reports how much input it consumed, which is the
only way a codec can be driven over a transport with no message
boundaries.

A parity test compiles a C probe and compares sizeof, offsetof and every
constant against the Rust definitions, so the two declarations cannot
drift unnoticed.
2026-12-03 19:31:53 +08:00
eric 7757552bdc refactor: rename leaf-bin -> leaf-cli 2024-04-18 16:39:58 +08:00
eric 26f7543174 chore(deps): use feature resolver v2 2023-08-25 16:15:48 +08:00
eric 86f80c153a ci: fix Windowns build 2022-11-11 00:49:24 +08:00
eric 5a6c4cec96 chore: strip was stabilized 2022-04-19 02:23:41 +08:00
eric d35e649e62 Experimental plugin system 2021-07-04 10:11:32 +08:00
eric 07eac7e465 chore: cleanup 2021-05-07 13:28:28 +08:00
eric 927f404ced outbound/tun: gateway mode 2021-04-19 02:11:17 +08:00
eric ea3454f0b2 ffi: allows building as static library for all targets 2021-04-05 18:21:19 +08:00
eric f17c2ca3d9 Upgrade tokio to v1.x 2021-03-16 13:24:41 +08:00
eric 2da69970c9 ci: pass strip flag via env variable 2021-03-06 10:13:57 +08:00
eric 8db8a7e78c fix tun2socks-bin 2020-10-19 08:27:59 +08:00
eric 897fc87ce8 initial commit 2020-10-07 22:14:06 +08:00