Skip to content

clients ​

What is handed out to the host's clients: a new WireGuard peer (keys, a tunnel address, the peer entry) and its configuration in client formats; links and a subscription for the users of the vless, hysteria2, tuic inbounds. It works on top of the loaded configuration and does not live with the instance: it is called by the Admin API (/api/v1/clients…) and ours client (CLI).

Crateours-mod-clients, modules/config/clients
Binary featureclients (in default, enables wireguard)
Crate featuresnone

requires ​

wireguard: the endpoint's options (WireGuardEndpointOptions, already merged with config_file), parsing and writing the wg/awg file (conf).

provides ​

Nothing: the module is functions over Config that the binary takes (app/ours/src/clients.rs).

Dependencies ​

ours-mod-wireguard, ours-wireguard (keys: keys::generate — like wg genkey, keys::random — the PSK), ours-net-info (wan — the address for auto), ours-native-config (runtime — the view of inbounds; fragment/replace — editing YAML), x25519-dalek (the REALITY public key from the private one), base64, qrcodegen (QR, without dependencies).

Design ​

  • src/lib.rs — list, add, remove, export; the answer is Reply { value, change, warnings }, Change::File (a new config_file text) or Change::Source (new YAML). persist writes through a temporary file in the same directory and rename, keeping the permissions. check_writable: the file is opened for appending and a probe file is created in the directory; otherwise adding and removing clients is the error "not writable … disabled".
  • Where a peer is stored. With config_file — a [Peer] section is appended to the end of the file (conf::append_peer, the rest of the text byte for byte), and removal takes out only its section. Without it — in the YAML, by the same mechanism as an edit in the dashboard: the endpoint's peers are appended as a block list (fragment + replace from native-config, comments outside peers stay intact); the Admin API applies the new YAML and saves it to the file if the running configuration was saved (otherwise a warning: unsaved dashboard edits are not written without asking), and ours client writes it to the -c file. There is no separate state file: a peer is an ordinary part of the configuration.
  • The client's private key exists only in the add answer; the file and the YAML have the public key and the PSK. export of an existing peer writes a placeholder instead of the key.
  • src/wg.rs — the address: in every subnet of the endpoint (one per family, /31, /32, /127, /128 are skipped) the smallest host address that is not the network or broadcast address, not the endpoint's address and not inside a peer's allowed_ips no wider than the subnet (a peer with 0.0.0.0/0 does not take an address); the search is limited to 2^20 addresses. public_endpoint: host:port, host (the port is listen_port), auto/empty — the WAN address, public before private, IPv4 before IPv6; a private or CGNAT one — a warning. Formats: wg (wg-quick), awg (awg-quick, with the endpoint's awg parameters and Ciphers), qr (an SVG of the .conf), sing-box (an endpoints element, 1.11+), xray (an outbounds element). wg, sing-box, xray are unavailable if the endpoint has AmneziaWG or ciphers lacks chacha20-poly1305; the reason is in unavailable.
  • src/links.rs — by the inbound's kind from ours_native_config::runtime: vless://uuid@host:port?… (security reality with pbk from the private key, sid, fp=chrome; tls with sni, alpn; type tcp/ws/httpupgrade/grpc/http/xhttp with path, host, serviceName, mode; flow), hysteria2://password@host:port/?sni&obfs&obfs-password, tuic://uuid:password@host:port?congestion_control&udp_relay_mode&sni&alpn; the name is the fragment #user. The host: from the request, otherwise tls.server_name (except REALITY), otherwise the WAN address. The subscription is the links line by line in base64 (as v2rayN serves and reads it). VLESS Encryption is not handed out as a link (the client's string cannot be derived from the server's decryption).
  • src/qr.rs — an SVG as one path, a 4-module border, error correction level L (long AWG configurations with I1–I5 do not fit otherwise).

Tests ​

src/tests.rs: golden files of the formats (tests/golden, OURS_BLESS=1 overwrites them), the awg file is read back by conf::parse; adding to YAML (the next address, comments intact, the key not stored, removal, an endpoint without peers); adding to config_file and removal (the text appended, no temporary files, a read-only file is an error); links — a golden file and parsing the subscription with the subscriptions module (the server fields match); exporting an existing peer without the key; the list. src/wg.rs: address allocation, public_endpoint.

GPL-3.0-or-later