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
- Variables
- func DecodeControl(b []byte) (any, error)
- func EncodeControl(m any) ([]byte, error)
- func NormalizeSubdomain(s string) string
- func RandomSubdomain() string
- func ValidateSubdomain(s string) error
- type Bye
- type ErrorMsg
- type Frame
- type Hello
- type Ping
- type Pong
- type Req
- type Res
- type Welcome
Constants ¶
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.
const ( MsgHello = "hello" MsgWelcome = "welcome" MsgReq = "req" MsgRes = "res" MsgPing = "ping" MsgPong = "pong" MsgBye = "bye" MsgError = "error" )
Control message types.
const ( ErrCodeBadMessage = "bad_message" ErrCodeProtoVersion = "proto_version" 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.
const HeaderSize = 9
HeaderSize is the size of the binary frame header.
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.
const ProtoVersion = 1
ProtoVersion is the current protocol version.
const Subprotocol = "etunnel-v1"
Subprotocol is the WebSocket subprotocol negotiated during upgrade.
Variables ¶
var ErrBadFrame = errors.New("etunnel: malformed frame")
ErrBadFrame indicates a malformed binary frame.
var ErrBadSubdomain = errors.New("etunnel: invalid subdomain")
ErrBadSubdomain is returned by ValidateSubdomain.
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 ¶
DecodeControl decodes one JSON text message into a typed struct.
func EncodeControl ¶
EncodeControl marshals a control message to JSON bytes.
func NormalizeSubdomain ¶
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 ¶
ValidateSubdomain checks that a subdomain is a single DNS label usable as a tunnel hostname: 1-63 chars (#197, aligned with the portal's claim-side bounds and DNS label limits - premium 1-2 char names are purchasable, so the wire-level check must not reject what the account already owns), [a-z0-9-], no leading/trailing hyphen, not a reserved word.
Types ¶
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 ¶
Frame is one decoded binary frame.
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 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.