protocol

package
v1.7.5 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 8 Imported by: 1 module, 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 protocol defines the etunnel wire protocol shared between the server and SDK clients.

Transport: one WebSocket connection per tunnel.

  • Text frames carry JSON control messages (hello/welcome/req/res/ping/pong/bye/error).
  • Binary frames carry multiplexed stream data with a 9-byte header.

Binary frame layout (big endian):

byte     0        frame kind (0x01 DATA, 0x02 FIN, 0x03 RST)
uint32   1..4     stream ID
uint32   5..8     payload length
bytes    9..      payload

Index

Constants

View Source
const (
	KindData byte = 0x01 // payload is a chunk of the stream body
	KindFin  byte = 0x02 // sender finished writing on this stream
	KindRst  byte = 0x03 // abort the stream
)

Frame kinds carried in binary WebSocket messages.

View Source
const (
	MsgHello   = "hello"
	MsgWelcome = "welcome"
	MsgReq     = "req"
	MsgRes     = "res"
	MsgPing    = "ping"
	MsgPong    = "pong"
	MsgBye     = "bye"
	MsgError   = "error"
)

Control message types.

View Source
const (
	ErrCodeBadMessage     = "bad_message"
	ErrCodeProtoVersion   = "proto_version"
	ErrCodeUnauthorized   = "unauthorized"
	ErrCodeSubdomainTaken = "subdomain_taken"
	ErrCodeSubdomainPlan  = "subdomain_plan"
	ErrCodeSubdomainBad   = "subdomain_invalid"
	ErrCodeLimitReached   = "limit_reached"
	ErrCodeTTlExpired     = "ttl_expired"
	ErrCodePingTimeout    = "ping_timeout"
	ErrCodeServerShutdown = "server_shutdown"
	ErrCodeTooManyTunnels = "too_many_tunnels"
)

Error codes carried by error messages.

View Source
const HeaderSize = 9

HeaderSize is the size of the binary frame header.

View Source
const MaxPayload = 32 * 1024

MaxPayload is the maximum payload size of one DATA frame. Both peers must chunk larger writes. It also bounds the per-frame memory cost of the per-stream frame queues.

View Source
const ProtoVersion = 1

ProtoVersion is the current protocol version.

View Source
const Subprotocol = "etunnel-v1"

Subprotocol is the WebSocket subprotocol negotiated during upgrade.

Variables

View Source
var ErrBadFrame = errors.New("etunnel: malformed frame")

ErrBadFrame indicates a malformed binary frame.

View Source
var ErrBadSubdomain = errors.New("etunnel: invalid subdomain")

ErrBadSubdomain is returned by ValidateSubdomain.

View Source
var ErrUnknownControlType = errors.New("etunnel: unknown control message type")

ErrUnknownControlType reports a well-formed control envelope whose `type` is not part of this protocol version. Per PROTOCOL.md both ends IGNORE unknown control types (forward compatibility: newer clients may probe extensions); callers distinguish it from malformed known messages via errors.Is.

Functions

func DecodeControl

func DecodeControl(b []byte) (any, error)

DecodeControl decodes one JSON text message into a typed struct.

func EncodeControl

func EncodeControl(m any) ([]byte, error)

EncodeControl marshals a control message to JSON bytes.

func NormalizeSubdomain

func NormalizeSubdomain(s string) string

NormalizeSubdomain trims and lowercases a requested subdomain.

func RandomSubdomain

func RandomSubdomain() string

RandomSubdomain generates a human-readable random subdomain such as "quick-fox-k3f9qz". The word pair is memorable; the 6-char suffix from a 30-symbol no-lookalike alphabet carries the entropy. Total keyspace 48*48*30^6 ≈ 2^40.6: the old word-word-NN format (2^17.8, ~230k names) could be exhaustively scanned in under an hour, which defeated the purpose for anonymous users who do not want their URL guessed.

func ValidateSubdomain

func ValidateSubdomain(s string) error

ValidateSubdomain checks that a subdomain is a single DNS label usable as a tunnel hostname: 3-40 chars, [a-z0-9-], no leading/trailing hyphen, not a reserved word.

Types

type Bye

type Bye struct {
	Type   string `json:"type"`
	Reason string `json:"reason,omitempty"`
}

Bye is sent by the side closing the connection, best effort.

type ErrorMsg

type ErrorMsg struct {
	Type    string `json:"type"`
	Code    string `json:"code,omitempty"`
	Message string `json:"message,omitempty"`
}

ErrorMsg reports a protocol or policy error.

type Frame

type Frame struct {
	Kind     byte
	StreamID uint32
	Payload  []byte
}

Frame is one decoded binary frame.

func ReadFrame

func ReadFrame(r io.Reader) (Frame, error)

ReadFrame reads a single frame from a WebSocket binary message reader.

func (Frame) Encode

func (f Frame) Encode() []byte

Encode serializes the frame into a fresh byte slice suitable for sending as one binary WebSocket message.

type Hello

type Hello struct {
	Type      string `json:"type"`
	Proto     int    `json:"proto"`
	Subdomain string `json:"subdomain,omitempty"` // requested custom subdomain (plan gated)
	Token     string `json:"token,omitempty"`     // paid plan API key
	Client    string `json:"client,omitempty"`    // free-form client identification
	// Resume is a server-signed token from a previous Welcome. Reconnecting
	// clients replay it to reclaim the same subdomain; it is self-validating
	// (HMAC + expiry) so it survives a server restart.
	Resume string `json:"resume,omitempty"`
	// Policy declares per-tunnel edge protection (basic auth, IP allowlist,
	// webhook verification). Omitted = legacy behavior (no protection).
	// Opaque to the protocol package: the server parses and validates it;
	// SDKs pass it through as a raw JSON object.
	Policy json.RawMessage `json:"policy,omitempty"`
}

Hello is the first message, client -> server.

type Ping

type Ping struct {
	Type string `json:"type"`
	TS   int64  `json:"ts"`
}

Ping / Pong are application-level heartbeats (independent of WS ping).

type Pong

type Pong struct {
	Type string `json:"type"`
	TS   int64  `json:"ts"`
}

type Req

type Req struct {
	Type     string              `json:"type"`
	StreamID uint32              `json:"streamId"`
	Method   string              `json:"method"`
	Path     string              `json:"path"` // path + query
	Headers  map[string][]string `json:"headers,omitempty"`
}

Req announces a new server-initiated stream carrying one HTTP request.

type Res

type Res struct {
	Type     string              `json:"type"`
	StreamID uint32              `json:"streamId"`
	Status   int                 `json:"status"`
	Headers  map[string][]string `json:"headers,omitempty"`
}

Res is the client's response header block for a stream.

type Welcome

type Welcome struct {
	Type      string `json:"type"`
	Proto     int    `json:"proto"`
	Hostname  string `json:"hostname"`            // e.g. quick-fox-42.t.etun.uk
	Subdomain string `json:"subdomain"`           // e.g. quick-fox-42
	URL       string `json:"url"`                 // full public URL including scheme
	ExpiresAt int64  `json:"expiresAt,omitempty"` // unix seconds, 0 = no expiry
	Plan      string `json:"plan,omitempty"`      // tier name
	// ResumeToken lets the client reclaim this subdomain after a disconnect
	// (including a server restart). Opaque; replay it in Hello.Resume.
	ResumeToken string `json:"resumeToken,omitempty"`
}

Welcome is the server reply to a successful Hello.

Jump to

Keyboard shortcuts

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