Documentation
¶
Overview ¶
Package trusttunnel implements the TrustTunnel wire format from the public specification of AdGuard's Apache-2.0 TrustTunnel project. This is an independent implementation and contains no upstream code; it imports only the standard library.
It covers the format only, not transport: the _udp2 datagram frames (UDPDecoder, EncodeUDPClientFrame, EncodeUDPServerFrame), the Proxy-Authorization Basic credentials, the CONNECT :authority including the _check, _udp2 and _icmp pseudo-hosts, and tt:// deep links.
The format is defined by what the official client and server do, so the readers here accept what they accept and the tests pin each rule with bytes written out by hand. Round-trip tests alone are not evidence of compatibility: a codec that is wrong in the same way on both sides passes them.
Index ¶
- Constants
- Variables
- func AppendQUICVarint(destination []byte, value uint64) ([]byte, error)
- func BuildProxyAuthorization(username, password string) (string, error)
- func EncodeDeepLink(link DeepLink) (string, error)
- func EncodeUDPClientFrame(frame UDPFrame) ([]byte, error)
- func EncodeUDPServerFrame(frame UDPFrame) ([]byte, error)
- func ParseProxyAuthorization(value string) (username, password string, err error)
- func ReadQUICVarint(data []byte) (value uint64, n int, err error)
- type Authority
- type AuthorityKind
- type DeepLink
- type UDPDecoder
- type UDPDirection
- type UDPFrame
Constants ¶
const ( DeepLinkPrefix = "tt://?" // MaxDeepLinkBytes bounds the links this package writes (QR-friendly). MaxDeepLinkBytes = 8 * 1024 // MaxDeepLinkDecodeBytes bounds the links it reads. DEEP_LINK.md asks // parsers for "at least 8 KiB"; a certificate chain can need more. MaxDeepLinkDecodeBytes = 64 * 1024 )
const ( MaxUDPAppNameBytes = 255 // MaxUDPClientPayload is the payload limit of a client frame with an empty // app name; a name of L bytes lowers it to MaxUDPClientPayload - 2*L (the // official server drops a frame whose Length exceeds 65471 - L). MaxUDPClientPayload = 65434 // MaxUDPServerPayload is the largest payload the encoder produces for the // client (65536 - 20 - 8, the official server's UDP read buffer). MaxUDPServerPayload = 65508 )
const MaxAuthorityBytes = 8 * 1024
const (
// MaxProxyAuthorizationBytes bounds parsing and building Basic credentials.
MaxProxyAuthorizationBytes = 8 * 1024
)
Variables ¶
var ( // ErrInvalidProxyAuthorization indicates malformed or empty Basic credentials. ErrInvalidProxyAuthorization = errors.New("invalid Proxy-Authorization") // ErrProxyAuthorizationTooLong indicates the value exceeds its package limit. ErrProxyAuthorizationTooLong = errors.New("Proxy-Authorization is too long") )
var ( ErrInvalidAuthority = errors.New("invalid CONNECT authority") ErrAuthorityTooLong = errors.New("CONNECT authority is too long") )
var ( ErrInvalidDeepLink = errors.New("invalid TrustTunnel deep link") ErrDeepLinkTooLong = errors.New("TrustTunnel deep link is too long") ErrUnsupportedVersion = errors.New("unsupported TrustTunnel deep link version") ErrInvalidQUICVarint = errors.New("invalid QUIC variable-length integer") )
Functions ¶
func AppendQUICVarint ¶
AppendQUICVarint appends value using the QUIC variable-length integer encoding. Values greater than 2^62-1 are rejected.
func BuildProxyAuthorization ¶
BuildProxyAuthorization builds a complete Proxy-Authorization header value. A colon is not allowed in the username because it is the field separator.
func EncodeDeepLink ¶
EncodeDeepLink encodes a tt:// link: version 1, or 2 for one with a subscription_url (see the comment at the version tag). Tags are written in the order of the official encoder. has_ipv6 and upstream_protocol are always explicit (a Flutter client reads a missing has_ipv6 as false), except in a subscription-only link, which carries only version and subscription_url. Fields at their default (false, empty) are omitted.
func EncodeUDPClientFrame ¶
EncodeUDPClientFrame encodes a client-to-server frame, including its one-byte-length-prefixed application name.
func EncodeUDPServerFrame ¶
EncodeUDPServerFrame encodes a server-to-client frame without an app name.
func ParseProxyAuthorization ¶
ParseProxyAuthorization parses a Proxy-Authorization value with the exact, case-sensitive "Basic " prefix. Passwords may contain colons; the first colon separates the username from the password.
Types ¶
type Authority ¶
type Authority struct {
Kind AuthorityKind
Host string
Port uint16
}
Authority is a parsed CONNECT authority. Pseudo-hosts have port zero.
func ParseAuthority ¶
ParseAuthority classifies the three reserved pseudo-hosts and parses host:port or [IPv6]:port authorities. Reserved pseudo-hosts are accepted without a port; every ordinary host requires a decimal port from 1 to 65535.
type AuthorityKind ¶
type AuthorityKind uint8
AuthorityKind describes a CONNECT authority.
const ( AuthorityInvalid AuthorityKind = iota AuthorityHostPort AuthorityCheck AuthorityUDP2 AuthorityICMP )
type DeepLink ¶
type DeepLink struct {
Hostname string
Addresses []string
// HasIPv6 is true when the tag is absent (the format's default); the
// encoder always writes it explicitly.
HasIPv6 bool
Username string
Password string
Name string
// DNSUpstreams are the raw upstream strings ("1.1.1.1", "tls://host",
// "https://host/dns-query"). The decoder does not interpret them.
DNSUpstreams []string
CustomSNI string
SkipVerification bool
Certificate []byte // concatenated DER certificates, nil when absent
HTTP3 bool // upstream_protocol 2; false is HTTP/2 (the default)
AntiDPI bool
ClientRandomPrefix string // "hexprefix[/hexmask]"
ClientRandomAuthKey string // hex
SubscriptionURL string // https:// only
}
DeepLink is the decoded content of a tt:// link (DEEP_LINK.md, version 2). Static connection fields are required unless SubscriptionURL is set.
The decoder takes every tag of the format. The encoder additionally applies the Mistgate B6 policy: exactly one address (none when SubscriptionURL is set) and at most two IPv4 DNS upstreams.
func DecodeDeepLink ¶
DecodeDeepLink decodes a tt:// link the way the official decoder does: "tt://?" or the older "tt://" prefix, unpadded canonical base64url, unknown tags skipped, a repeated tag overrides the earlier one (addresses accumulate), a version above 2 fails. Required fields may be missing only when a subscription_url is present. Deviations from the official decoder, all stricter: addresses must be valid host:port, and a tag number above 255 is unknown instead of wrapping around.
type UDPDecoder ¶
type UDPDecoder struct {
// contains filtered or unexported fields
}
UDPDecoder incrementally reads length-prefixed _udp2 frames. Feed accepts arbitrary chunk boundaries, returns every complete valid frame, and reports the first dropped-frame error while continuing at the next frame boundary. A frame the official reader would drop is dropped here too, with the same number of bytes skipped, so both stay in step with the stream. Its retained buffer is bounded by the largest frame admitted by the protocol.
Each returned Payload is a copy that the caller owns; the decoder reuses its internal buffer. A UDPDecoder is not safe for concurrent use.
func NewUDPDecoder ¶
func NewUDPDecoder(direction UDPDirection) *UDPDecoder
NewUDPDecoder creates a streaming decoder for one frame direction.
func (*UDPDecoder) Feed ¶
func (decoder *UDPDecoder) Feed(input []byte) ([]UDPFrame, error)
Feed appends data to the stream and returns all complete frames. When a malformed frame is dropped, Feed returns its error together with any later frames decoded from the same input.
func (*UDPDecoder) Reset ¶
func (decoder *UDPDecoder) Reset()
Reset forgets any partial frame and pending discard state.
type UDPDirection ¶
type UDPDirection uint8
UDPDirection selects which side's frame layout a UDPDecoder accepts.
const ( UDPClientToServer UDPDirection = iota + 1 UDPServerToClient )