Skip to content

ours-wire ​

libs/wire — sans-IO codecs of what proxy protocols put on the wire. They read from and write to byte slices; the I/O and the contract's types (Destination, streams) stay with the callers.

Design ​

Feature addr — binary address layouts: a type byte, the address (a domain after its one-byte length) and a big-endian port. Layout holds the type bytes and where the port goes:

LayoutIPv4DomainIPv6No addressPort
SOCKS0x010x030x04—last
VMESS123—first
TUIC0x010x000x020xff, without a portlast
  • Address<'a> with Host::{Ip, Domain(&str)} — borrowed from the bytes it was read from.
  • Variant::read — the address at the start of a slice and how many bytes it took; Ok(None) while the slice ends early, an unknown type is an error from its type byte. Variant::read_optional also reads the layout's "no address" as None.
  • Variant::write, Variant::written_len (and *_optional); a domain over 255 bytes is refused and nothing is written.
  • Variant::size — the whole length of the address at the start of a slice, known from its type byte and a domain's length byte, for readers that take a stream piece by piece.
  • Error — UnknownType, EmptyDomain, DomainNotUtf8, DomainTooLong; callers keep their own messages.

Every preset reads an IPv4-mapped IPv6 address as IPv4, and a domain that spells an address (brackets around an IPv6 one included) as that address, as sing's M.ParseSocksaddrHostPort reads it; a domain spelling a mapped address stays IPv6. Where the protocols answer differently, a Variant keeps each:

PresetLayoutUsersIPv4-mapped IPv6 writtenEmpty domainNon-UTF-8 domain
SINGSOCKSUoT, AnyTLS, sing-mux packets, Trojanas IPv6errorerror once the port is there
SOCKS5SOCKSSOCKS5 requests, replies, UDPas IPv6errorerror at once
SHADOWSOCKSSOCKSShadowsocks headers and UDPas IPv4a domainerror once the port is there
SING_MUXSOCKSsing-mux stream requestsas IPv6a domainerror once the port is there
VMESSVMESSVLESS requests and packets, mux.cool, XUDPas IPv4a domainerror
TUICTUICTUIC commands and packetsas IPv6a domainerror once the port is there

addr::text — addresses written as text, host:port, one way for every user (Hysteria, Hysteria 2, HTTP CONNECT, the HTTP client's URL, SOCKS4a, the WireGuard client endpoint, a domain in a binary layout). split(text) takes the host out of its brackets ([v6], [v6]:port), otherwise the port follows the last colon, and a host with a colon of its own must be an IPv6 address (2001:db8::1 is refused, as net.SplitHostPort refuses it; ::ffff:192.0.2.7:80 is that address at port 80); Error is an unclosed bracket, text after it, or too many colons. port(text) takes decimal digits only, as strconv.ParseUint(text, 10, 16): no sign, space or empty port. host(text) reads a host as sing's M.ParseSocksaddrHostPort: brackets come off an address and stay on a domain ([example.com]), an IPv4-mapped IPv6 address stays IPv6.

Host displays as the bare host, Address as host:port with an IPv6 address in brackets.

Feature uuid — uuid::parse reads a UUID as gofrs uuid.FromString v5.5.1 does (canonical, 32 digits, either in braces or after a lower-case urn:uuid:), uuid::format writes the canonical form. VLESS falls back to a name-based UUID itself.

Feature varint — varint::leb128 (put, get, the byte-at-a-time Reader; more than 64 bits is Overflow, as in Go's binary.ReadUvarint) for sing-mux lengths, and varint::quic (put, get, len from the first byte; RFC 9000 §16) for HTTP/3 and Hysteria 2.

Feature frag — UDP packets cut into pieces that fit one QUIC datagram and put back together, as sing-quic's fragUDPMessage and udpDefragger. frag::pieces(data, header, limit) cuts a packet whose data does not fit beside its header (none when it fits or no data fits; the count is a byte and wraps past 255 pieces, as in sing-quic). frag::Defragger<K, M> puts fragments together by a key the caller picks and keeps a meta (the address) of type M; the time is an argument of feed. A fragment past the count or already in is dropped, a new count starts the packet over, a packet that ends up empty is dropped, and every fragment drops packets that got no new fragment for Variant::age. Variant::meta picks whose address the packet takes: HYSTERIA — the fragment that completed it, HYSTERIA2 and TUIC — the first one. Hysteria and TUIC key a connection's packets by session and packet, Hysteria 2 has a reassembler per session.

Multiplexer frames. The sessions (tasks, queues, windows, what a command means) stay with their modules; here are the bytes, with the protocol's constants:

FeatureModuleHeaderWhat reads
smuxsmux8 bytes: version 1, command, length, stream id, little endiandecode refuses another version; the command is the session's to judge
yamuxyamux12 bytes: version 0, type, flags, stream id, length, big endiandecode refuses another version; type and flags are the session's
anytlsanytls7 bytes: command, stream id, length, big endiandecode takes every header; put appends a header and its data
cool (with addr)coolmetadata length, session, status, option, then network and address (VMESS), then the data lengthFrame::parse — the frame at the start of a slice, Ok(None) until it is all there; the address is read at once, what follows it in the metadata (Xray's global ID) is passed over; put_header appends the header
sing-mux (with addr, varint)sing_muxsession Request (version, protocol, padding); StreamRequest (flags, address in SING_MUX); response status and LEB128 error lengthRequest::decode refuses a short slice; StreamRequest::decode; ResponseReader takes the response a byte at a time so nothing past it is read

A length past 16 bits keeps its low bits in smux and AnyTLS headers, as the Go implementations do; the sessions never write one. ResponseReader takes any status but error as success, as sing-mux's ReadStreamResponse does.

Who uses it ​

ours-api (addr): Destination::address and From<Address>, Destination::{read, read_optional, write, write_optional, written_len, written_len_optional} in a given Variant, the async read_from and read_optional_from that take an address off a stream and nothing after it, and Destination::from_host for a host in text. Protocols call these with their preset and only word the errors; addr::Error converts to io::Error (InvalidInput for a domain too long to write, InvalidData otherwise). tests/layers.py keeps functions of a destination codec's names (read_address, write_destination, host_port, parse_address, ...) out of modules/ and the shared crates, with a reasoned list of exceptions (net/url in subscriptions and in the Hysteria 2 options).

Through it: ours-proto (SOCKS and HTTP CONNECT), ours-uot, ours-http (the host it dials), ours-mod-socks, ours-mod-shadowsocks, ours-mod-trojan, ours-mod-anytls, ours-mod-mux (also sing-mux, and smux, yamux, cool under its features of those names), ours-mod-vless and ours-mod-tuic (also uuid, frag), ours-mod-hysteria2 (text, varint, frag), ours-mod-hysteria (text, frag). Directly: ours-quinn-ext, ours-mod-transport-xhttp (varint), ours-http-client (text: a URL's host and port), ours-mod-clients (text: the WireGuard client endpoint).

Specifics ​

No dependencies; tests/layers.py checks that nothing under it, tests included, takes tokio.

Tests ​

tests/addr.rs, tests/text.rs, tests/uuid.rs, tests/varint.rs, tests/frag.rs, tests/frames.rs read the vectors in tests/vectors/*.txt through tests/vectors/harness.rs: each file is what one implementation answered for the same inputs, cut at every byte, before they shared this crate. WIRE_BLESS=1 rewrites a file from the current answers. Fragment vectors feed fragments at given times and cut packets of given sizes. Text vectors (text-*.txt) are what each text reader answered for the same hosts and host:port texts; the modules keep their own test of them too, since their errors are worded there. Fuzzing — fuzz/ (the targets addr, text, frag, uuid, varint, smux, yamux, anytls, cool, sing_mux).

GPL-3.0-or-later