trusttunnel

package
v0.2.0-dev.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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
)
View Source
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
)
View Source
const MaxAuthorityBytes = 8 * 1024
View Source
const (

	// MaxProxyAuthorizationBytes bounds parsing and building Basic credentials.
	MaxProxyAuthorizationBytes = 8 * 1024
)

Variables

View Source
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")
)
View Source
var (
	ErrInvalidAuthority = errors.New("invalid CONNECT authority")
	ErrAuthorityTooLong = errors.New("CONNECT authority is too long")
)
View Source
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")
)
View Source
var (
	ErrInvalidUDPFrame     = errors.New("invalid _udp2 frame")
	ErrUDPFrameTooShort    = errors.New("_udp2 frame is too short")
	ErrUDPFrameTooLarge    = errors.New("_udp2 frame exceeds the protocol limit")
	ErrInvalidUDPDirection = errors.New("invalid _udp2 frame direction")
)

Functions

func AppendQUICVarint

func AppendQUICVarint(destination []byte, value uint64) ([]byte, error)

AppendQUICVarint appends value using the QUIC variable-length integer encoding. Values greater than 2^62-1 are rejected.

func BuildProxyAuthorization

func BuildProxyAuthorization(username, password string) (string, error)

BuildProxyAuthorization builds a complete Proxy-Authorization header value. A colon is not allowed in the username because it is the field separator.

func EncodeDeepLink(link DeepLink) (string, error)

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

func EncodeUDPClientFrame(frame UDPFrame) ([]byte, error)

EncodeUDPClientFrame encodes a client-to-server frame, including its one-byte-length-prefixed application name.

func EncodeUDPServerFrame

func EncodeUDPServerFrame(frame UDPFrame) ([]byte, error)

EncodeUDPServerFrame encodes a server-to-client frame without an app name.

func ParseProxyAuthorization

func ParseProxyAuthorization(value string) (username, password string, err error)

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.

func ReadQUICVarint

func ReadQUICVarint(data []byte) (value uint64, n int, err error)

ReadQUICVarint reads one QUIC variable-length integer from data.

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

func ParseAuthority(value string) (Authority, error)

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 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(value string) (DeepLink, error)

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
)

type UDPFrame

type UDPFrame struct {
	AppName string
	Source  netip.AddrPort
	Target  netip.AddrPort
	Payload []byte
}

UDPFrame is one TrustTunnel _udp2 datagram. AppName is present only in client-to-server frames.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL