Documentation
¶
Overview ¶
Package wire is the small JSON protocol between Echo and the connectors inside a site (the WordPress mu-plugin, the Laravel package, the Node package): a connector reports each email and each outbound HTTP request. For HTTP requests Echo answers whether to let it through or what to answer instead. Keep it stable: connectors in other languages implement it.
POST /v1/capture Capture → Decision
GET /v1/health {"wire": Version, ...}
schema.json is the same protocol as a JSON Schema, generated from these types (go generate), for connectors in other languages and for the conformance stand-in. Tags beyond json feed it: enum lists a field's values, since names the wire version that added a field.
Index ¶
Constants ¶
const ( Before = "before" // about to send: Echo decides After = "after" // sent (observe mode, or an allowed host): the response )
Phases of an HTTP capture.
const ( Allow = "allow" // send it for real Fake = "fake" // don't send; answer with Response Block = "block" // don't send; fail the request )
Decisions for an HTTP capture.
const MinVersion = 1
MinVersion is the oldest wire version Echo still accepts.
const RunEnv = "RHYTHM_ECHO_RUN"
RunEnv carries the same "<run>/<action>" value as RunHeader to work done outside a request. Echo sets it on the commands it runs (WP-CLI, artisan, scripts). The connectors and sendmail read it.
const RunHeader = "X-Echo-Run"
RunHeader carries "<run>/<action>" from the thing that triggered a request (Soundcheck's browser, `rhythm echo run`) to the connector, which copies it into every capture, so messages are tied to the action that caused them. Connectors also accept it as a cookie of the same name.
const SiteEnv = "RHYTHM_ECHO_SITE"
SiteEnv is the site's own address (a URL or a host), set by whoever configures the connector. Connectors use it, never a request header, to tell calls to the site itself from calls to providers.
const TokenHeader = "X-Echo-Token" //nolint:gosec // a header name, not a credential
TokenHeader authenticates connectors when Echo listens beyond loopback.
const Version = 2
Version is the newest wire version, the one connectors send. Echo accepts captures from MinVersion to Version and says which it speaks in /v1/health. A connector that finds an older Echo there sends that version instead, without the fields the older one lacks.
Variables ¶
var Schema []byte
Schema is schema.json: the protocol as a JSON Schema (draft 2020-12).
Functions ¶
func SignRun ¶ added in v1.1.0
SignRun returns "<run>/<action>.<sig>", where sig is the first 16 bytes of HMAC-SHA256(token, "<run>/<action>") in unpadded base64url. A connector that knows the token accepts a run value from a request only when it verifies, so a visitor cannot add messages to a baseline or drop them from one. With no token there is nothing to sign with and the value is returned unsigned.
func SplitRun ¶ added in v1.1.0
SplitRun splits a run value into "<run>/<action>" and its signature, "" for an unsigned value. An action name with a dot in it ("order.paid") is not taken for a signature.
func VerifyRun ¶ added in v1.1.0
VerifyRun splits a run value into its run and action and reports whether it carries a valid signature for token. An unsigned "<run>/<action>" still returns its run and action, with ok false, so callers that accept unsigned values (no token configured) can use them. A value with a signature that does not verify returns nothing.
Types ¶
type Attachment ¶ added in v1.1.0
type Attachment struct {
Name string `json:"name,omitempty"` // the file name the recipient sees
Type string `json:"type,omitempty"` // MIME type, application/pdf
Size int64 `json:"size,omitempty"` // decoded size in bytes
SHA256 string `json:"sha256,omitempty"` // hex digest of the decoded content
}
Attachment describes one attached file. Connectors send what they know about it, never its content.
type Capture ¶
type Capture struct {
Version int `json:"v"` // the wire version this capture follows
Kind string `json:"kind" enum:"email,http"` // email or http
Phase string `json:"phase,omitempty" enum:"before,after"` // http: before or after; email: after (with id) reports a real send's outcome
ID string `json:"id,omitempty"` // after: the ID Echo returned for before
Run string `json:"run,omitempty"` // from X-Echo-Run, "<run>/<action>"
Source Source `json:"source"`
Email *Email `json:"email,omitempty"` // set when kind is email
HTTP *HTTP `json:"http,omitempty"` // set when kind is http
Error string `json:"error,omitempty"` // the send failed (wp_mail_failed, a transport error)
}
Capture is one email or HTTP request a connector saw.
type Decision ¶
type Decision struct {
ID string `json:"id"` // the stored message, for the after phase; empty for a request Echo let through unrecorded
Mode string `json:"mode" enum:"seal,observe"` // the mode Echo is in
Action string `json:"action,omitempty" enum:"allow,fake,block"` // http: allow, fake or block; email: fake (don't send) or allow
Response *Response `json:"response,omitempty"` // for fake
}
Decision is Echo's answer to a capture.
type Email ¶
type Email struct {
From string `json:"from,omitempty"`
To []string `json:"to,omitempty"`
CC []string `json:"cc,omitempty"`
BCC []string `json:"bcc,omitempty"`
ReplyTo string `json:"reply_to,omitempty"`
Subject string `json:"subject,omitempty"`
Text string `json:"text,omitempty"`
HTML string `json:"html,omitempty"`
Headers map[string]string `json:"headers,omitempty"` // the message's other headers, by name
Raw string `json:"raw,omitempty"` // full MIME, when the connector has it
// Attachments lists the files attached to the message, for connectors
// that send no Raw MIME. With Raw set, Echo reads them from the MIME.
Attachments []Attachment `json:"attachments,omitempty" since:"2"`
}
Email is a message handed to the site's mailer.
type HTTP ¶
type HTTP struct {
Method string `json:"method"`
URL string `json:"url"` // absolute; Echo decides on its host
Headers map[string]string `json:"headers,omitempty"` // credential headers masked, a Stripe key's mode prefix kept so Echo can tell live from test
Body string `json:"body,omitempty"` // as sent; Echo masks it before storing
Status int `json:"status,omitempty"` // after
Response string `json:"response,omitempty"` // after: the response body
}
HTTP is an outbound request and, after sending, its response.
type Response ¶
type Response struct {
Status int `json:"status"` // the HTTP status to answer with
Headers map[string]string `json:"headers,omitempty"` // the provider's own, such as Content-Type
Body string `json:"body,omitempty"` // shaped like the provider's real answer, so the site's SDK reads it
}
Response is the answer a connector gives the site instead of sending.
type Source ¶
type Source struct {
Via string `json:"via"` // wordpress, laravel, node, smtp, proxy
Stack string `json:"stack,omitempty"` // wordpress, laravel, node, php
Plugin string `json:"plugin,omitempty"` // best guess at the sender: a plugin slug, a package
Hook string `json:"hook,omitempty"` // wp_mail, pre_http_request, fetch, nodemailer…
Host string `json:"host,omitempty"` // the site's own host (always allowed)
Site string `json:"site,omitempty" since:"2"` // the site's ID when one Echo serves many sites
}
Source describes the connector and what it hooked.