Documentation
¶
Overview ¶
Package etunnel is the Go SDK for etunnel tunnels: expose a local service on the public internet with one call.
t, err := etunnel.Connect(ctx, etunnel.Options{Server: "wss://t.example.com/tunnel", LocalAddr: ":8080"})
fmt.Println(t.URL()) // https://quick-fox-42.t.example.com
Login: the CLI loopback authorization flow (see server/auth_cli.go).
The SDK listens on a random 127.0.0.1 port, asks the server for an authorization URL, opens the user's browser there, waits for the browser to land back on the loopback port with a one-time code, and exchanges it for the account's API token and custom-domain list. The caller persists the returned Credentials and passes APIToken as Options.Token on future Connect calls.
Index ¶
- Constants
- Variables
- func NormalizeServerURL(s string) string
- type APIError
- type Credentials
- type Options
- type ReconnectingTunnel
- func (r *ReconnectingTunnel) Attempts() int
- func (r *ReconnectingTunnel) Close(reason string)
- func (r *ReconnectingTunnel) Done() <-chan struct{}
- func (r *ReconnectingTunnel) String() string
- func (r *ReconnectingTunnel) Subdomain() string
- func (r *ReconnectingTunnel) Tunnel() *Tunnel
- func (r *ReconnectingTunnel) URL() string
- type Tunnel
- func (t *Tunnel) Close(reason string)
- func (t *Tunnel) CloseReason() string
- func (t *Tunnel) Done() <-chan struct{}
- func (t *Tunnel) ExpiresAt() time.Time
- func (t *Tunnel) Hostname() string
- func (t *Tunnel) Plan() string
- func (t *Tunnel) ResumeToken() string
- func (t *Tunnel) Subdomain() string
- func (t *Tunnel) URL() string
- type TunnelPolicy
- type WebhookRule
Constants ¶
const ( DefaultReconnectMinBackoff = 500 * time.Millisecond DefaultReconnectMaxBackoff = 30 * time.Second )
Reconnect defaults.
const LoginTimeout = 10 * time.Minute
LoginTimeout bounds the whole flow: browser open, user login (if needed) and approval. The server's own code lives 5 minutes; 10 minutes here covers a login detour.
Variables ¶
var DefaultServer = "wss://t.etun.uk/tunnel"
DefaultServer is the connect target when Options.Server is empty; overridable at link time: -ldflags "-X etunnel/sdk.DefaultServer=wss://t.example.com/tunnel"
var ErrUnsupportedProto = errors.New("etunnel: server protocol too new")
ErrUnsupportedProto is returned when the server speaks a strictly newer wire protocol than this SDK. Retrying cannot fix it; the reconnecting supervisor treats it as permanent.
Functions ¶
func NormalizeServerURL ¶
NormalizeServerURL canonicalizes a server address into a ws(s) URL.
Types ¶
type Credentials ¶ added in v1.6.6
type Credentials struct {
// APIToken authenticates tunnel creation (Options.Token).
APIToken string `json:"api_token"`
// Subdomains are the account's custom domains (empty for free tier).
Subdomains []string `json:"subdomains"`
// Plan is the account tier ("free", ...).
Plan string `json:"plan"`
// ExpiresAt is 0: API tokens do not lapse on their own.
ExpiresAt int64 `json:"expires_at"`
}
Credentials is what Login returns and what applications persist.
func Login ¶ added in v1.6.6
func Login(ctx context.Context, server string, openBrowser func(string) error) (*Credentials, error)
Login runs the loopback authorization flow against server (e.g. "https://t.etun.uk"). openBrowser may be nil; headless environments get the URL printed to stderr instead.
type Options ¶
type Options struct {
// Server is the tunnel server endpoint, e.g. "wss://t.example.com/tunnel".
// A bare host gets "wss://" and "/tunnel" defaults.
Server string
// LocalAddr is the local service address to expose ("127.0.0.1:8080").
LocalAddr string
// Subdomain requests a static subdomain (a free key may hold one;
// paid keys hold more).
Subdomain string
// Token is an API key (free keys unlock one static subdomain).
Token string
// Client is a free-form client identification string.
Client string
// Policy declares per-tunnel edge protection enforced by the server
// before any visitor request reaches the local service. Nil = legacy.
Policy *TunnelPolicy
// OnLog, when set, receives lifecycle log lines.
OnLog func(format string, args ...any)
// ReconnectMinBackoff / ReconnectMaxBackoff bound the exponential backoff
// used by ConnectReconnecting (defaults 500ms / 30s).
ReconnectMinBackoff time.Duration
ReconnectMaxBackoff time.Duration
// OnReconnect, when set, fires after each successful reconnect with the
// attempt number and the new public URL.
OnReconnect func(attempt int, url string)
// contains filtered or unexported fields
}
Options configures a tunnel.
type ReconnectingTunnel ¶ added in v1.3.0
type ReconnectingTunnel struct {
// contains filtered or unexported fields
}
ReconnectingTunnel keeps a tunnel alive across disconnects. It is safe for concurrent use; every accessor proxies to the current underlying tunnel.
func ConnectReconnecting ¶ added in v1.3.0
func ConnectReconnecting(ctx context.Context, opts Options) (*ReconnectingTunnel, error)
ConnectReconnecting dials once (returning the first error, so bad credentials fail fast rather than looping forever) and then supervises the connection in the background.
func (*ReconnectingTunnel) Attempts ¶ added in v1.3.0
func (r *ReconnectingTunnel) Attempts() int
Attempts reports how many successful reconnects have happened.
func (*ReconnectingTunnel) Close ¶ added in v1.3.0
func (r *ReconnectingTunnel) Close(reason string)
Close stops the supervisor and the current tunnel. No reconnect follows.
func (*ReconnectingTunnel) Done ¶ added in v1.3.0
func (r *ReconnectingTunnel) Done() <-chan struct{}
Done is closed when the supervisor stops: the user called Close, the parent context ended, or reconnecting failed permanently.
func (*ReconnectingTunnel) String ¶ added in v1.3.0
func (r *ReconnectingTunnel) String() string
String renders the tunnel for logs.
func (*ReconnectingTunnel) Subdomain ¶ added in v1.3.0
func (r *ReconnectingTunnel) Subdomain() string
Subdomain returns the current subdomain label.
func (*ReconnectingTunnel) Tunnel ¶ added in v1.3.0
func (r *ReconnectingTunnel) Tunnel() *Tunnel
Tunnel returns the current underlying tunnel, or nil while a reconnect is in flight. Accessors like ResumeToken/CloseReason live on it.
func (*ReconnectingTunnel) URL ¶ added in v1.3.0
func (r *ReconnectingTunnel) URL() string
URL returns the live public URL (last known while reconnecting).
type Tunnel ¶
type Tunnel struct {
// contains filtered or unexported fields
}
Tunnel is one live reverse tunnel.
func Connect ¶
Connect establishes a reverse tunnel and returns it once the server has assigned a public hostname.
func (*Tunnel) CloseReason ¶
CloseReason returns why the tunnel ended ("" while running).
func (*Tunnel) Done ¶
func (t *Tunnel) Done() <-chan struct{}
Done is closed when the tunnel ends (server closed it, TTL expired, connection lost or Close was called).
func (*Tunnel) ExpiresAt ¶
ExpiresAt returns the tunnel expiry reported by the server (zero = none).
func (*Tunnel) ResumeToken ¶ added in v1.3.0
ResumeToken returns the server-signed token that lets a later connection reclaim this tunnel's subdomain (empty when the server does not issue one).
type TunnelPolicy ¶ added in v1.6.13
type TunnelPolicy struct {
// BasicAuth protects the tunnel with HTTP Basic ("user:pass").
BasicAuth string `json:"basicAuth,omitempty"`
// AllowIPs restricts visitors to these CIDRs / plain IPs.
AllowIPs []string `json:"allowIPs,omitempty"`
// Webhooks verifies signatures of the listed providers at the edge;
// verified requests carry X-Etunnel-Webhook to the local service.
Webhooks []WebhookRule `json:"webhooks,omitempty"`
}
TunnelPolicy is the SDK-side declaration of per-tunnel edge protection. It serializes into hello.policy and is enforced by the server: visitor requests are checked at the edge BEFORE any stream reaches the local service. Every layer is optional; zero fields = no policy is sent.
// demo behind a password
etunnel.Connect(etunnel.Options{..., Policy: &etunnel.TunnelPolicy{BasicAuth: "u:p"}})
// Stripe webhooks verified at the edge
Policy: &etunnel.TunnelPolicy{Webhooks: []etunnel.WebhookRule{{Provider: "stripe", Secret: "whsec_..."}}}
type WebhookRule ¶ added in v1.6.13
WebhookRule pins one provider's verification secret. Providers: stripe (Stripe-Signature, 5-min tolerance), github (X-Hub-Signature-256), gitlab (X-Gitlab-Token).