Files
leaf/Makefile
ericandClaude Opus 5.5 a77273f777 feat(plugins): run plugins compiled into the client, named by builtin
iOS will not load code from a file, and Android will not from anywhere an
app can write, so there a plugin is linked in at build time instead. It
is registered under a name with register_builtin_plugin, and a config
refers to it with `builtin`, in conf ([Plugin] builtin=) and JSON alike.

A registered builtin wins over path and url, which are then only the
fallback for a client without it: nothing is downloaded and no cache
directory is needed. A client with none of the three fails to start and
lists the builtins it has. A builtin's descriptor goes through the same
validation as a library's; only the file's checks -- path, permissions,
sha256 -- do not apply, since there is no file. Loaded plugins are now
keyed by source, so two outbounds on one builtin share it.

Only plugins written in other languages are linked in. Plugins exist for
protocols written in something other than Rust; one written in Rust
belongs in leaf as an outbound of its own. Linking a Rust plugin in would
also merge its dependencies' features with leaf's -- the TLS plugin's
rustls with ring on top of leaf's aws-lc-rs, for one, which leaves rustls
with no default provider and panics the DoH client. The in-tree Rust
plugins stay loadable libraries only.

For C, defining LEAF_PLUGIN_STATIC_NAME renames the descriptor function
to leaf_plugin_<name>_get_descriptor and drops its export, through the
canonical header, so the plugin's source does not change and any number
of C plugins can be linked into one binary.

leaf-ffi's plugin-socks5-c feature links the SOCKS5 C plugin into the
library (compiled by build.rs) and registers it as the builtin socks5-c
on the first call that reads a config. leaf_register_plugin registers one
the app links itself. Its unit tests load the linked-in plugin through
the host.

The builtin/ e2e cases register a fixture's descriptor function and show
that it carries TCP and UDP to a stock leaf server, wins over a path and
over a url (with no cache directory to fall back on), gives way to the
url on a client without it, fails clearly with nothing to fall back on,
and is held to the same descriptor validation as a library.

The READMEs describe builtins and how to link a plugin in, and
plugin-test and CI run the linked-in tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 01:03:00 +08:00

68 lines
2.7 KiB
Makefile

.PHONY: cli cli-dev test e2e soak plugin-test plugin-test-san proto-gen
CFG_COMMIT_HASH := $(shell git rev-parse HEAD | cut -c 1-7)
export CFG_COMMIT_HASH := $(CFG_COMMIT_HASH)
CFG_COMMIT_DATE := $(shell git log --format="%ci" -n 1)
export CFG_COMMIT_DATE := $(CFG_COMMIT_DATE)
cli:
cargo build -p leaf-cli --release
cli-dev:
cargo build -p leaf-cli
test:
cargo test -p leaf -- --nocapture
# End-to-end plugin tests. Builds the plugins it needs on its own; pass
# arguments through, e.g. `make e2e E2E_ARGS="--tag go"`.
#
# The soak lane is left out: it is minutes per case and answers a question
# about the long run, which is `make soak` below.
e2e:
cargo test -p leaf-e2e --test e2e -- --strict --no-tag soak $(E2E_ARGS)
# The long lane: soaks and every cost meter, including the two that are too
# slow for a pull request. Each meter leaves its numbers in
# `target/<profile>/e2e-artifacts/<case>/meters.json`.
soak:
cargo test -p leaf-e2e --test e2e -- --strict --tag soak --tag perf $(E2E_ARGS)
# Everything plugin-related that is not an end-to-end run: the host's own tests
# for the loader and the wrappers, and each plugin's unit tests, one language at
# a time. These cover what an end-to-end run cannot reach: a descriptor that
# lies about its size, a handshake arriving a byte at a time, every way a server
# can refuse, and the buffer bookkeeping under all of it.
CC ?= cc
ABI_INCLUDE := leaf-plugin-abi/include
plugin-test:
cargo test -p leaf --features plugin --lib
cargo test -p leaf --features plugin-fetch --lib
cargo test -p leaf-ffi --lib --features plugin-fetch,plugin-socks5-c
cargo test -p leaf-plugin-abi
cargo test -p tls-cabi-rs -p shadowsocks-cabi-rs
cd leaf-plugins/leaf-abi-go && go test -race ./...
cd leaf-plugins/tls-cabi-go && go test ./...
cd leaf-plugins/trojan-cabi-go && go test ./...
mkdir -p target
$(CC) -std=c11 -O1 -g -Wall -Wextra -Werror -fstack-protector-strong \
-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -I$(ABI_INCLUDE) \
-o target/socks5_cabi_c_test leaf-plugins/socks5-cabi-c/socks5_test.c
./target/socks5_cabi_c_test
cd leaf-plugins/socks5-cabi-zig && zig build test
# The C plugin's own tests under the sanitizers. Kept separate from
# `plugin-test` because not every toolchain ships the runtimes, and this is the
# lane that answers the question `-Wall` cannot: what the parser does with a
# server that lies about its lengths.
plugin-test-san:
mkdir -p target
$(CC) -std=c11 -O1 -g -Wall -Wextra -Werror -fsanitize=address,undefined \
-fno-omit-frame-pointer -I$(ABI_INCLUDE) \
-o target/socks5_cabi_c_test_san leaf-plugins/socks5-cabi-c/socks5_test.c
./target/socks5_cabi_c_test_san
proto-gen:
./scripts/regenerate_proto_files.sh