edge

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package edge provides utilities for connecting to OpenShell gateways through edge proxies such as Cloudflare Access. It includes convenience constructors for common edge auth patterns and a WebSocket tunnel proxy for gRPC transport through HTTP/1.1-only proxies.

Cloudflare Access

CloudflareAccess wraps any AuthProvider with the headers required by Cloudflare Access (cf-access-jwt-assertion and CF_Authorization cookie). The edge token is typically a service token or application token obtained from Cloudflare:

base := v1.StaticToken("my-gateway-token")
auth, err := edge.CloudflareAccess(base, os.Getenv("CF_ACCESS_TOKEN"))
if err != nil {
    log.Fatal(err)
}
client, err := v1.NewClient(v1.Config{
    Address: "gateway.example.com:443",
    Auth:    auth,
})
if err != nil {
    log.Fatal(err)
}
defer client.Close()

CloudflareAccess composes with any auth provider, including RefreshableToken for automatic token refresh:

tokenSource := oauth2Config.TokenSource(ctx, initialToken)
refreshAuth, err := v1.RefreshableToken(tokenSource)
if err != nil {
    log.Fatal(err)
}
auth, err := edge.CloudflareAccess(refreshAuth, cfToken)
if err != nil {
    log.Fatal(err)
}

WebSocket Tunnel

TunnelProxy bridges gRPC connections over a WebSocket tunnel for edge proxies that reject standard HTTP/2 POST requests. The tunnel carries its own edge token for proxy authentication, independent of the application-level auth provider.

Create a tunnel proxy pointed at the gateway, then dial the proxy's local address from the gRPC client:

tunnel, err := edge.NewTunnelProxy(
    "wss://gateway.example.com/ws",
    os.Getenv("CF_ACCESS_TOKEN"),
)
if err != nil {
    log.Fatal(err)
}
defer tunnel.Close()

auth := v1.StaticToken("my-gateway-token")
client, err := v1.NewClient(v1.Config{
    Address: tunnel.Addr(),
    Auth:    auth,
    TLS:     &v1.TLSConfig{Insecure: true}, // local tunnel
})
if err != nil {
    log.Fatal(err)
}
defer client.Close()

Use functional options to configure TLS, logging, and close timeout:

tunnel, err := edge.NewTunnelProxy(
    "wss://gateway.example.com/ws",
    cfToken,
    edge.WithTunnelTLS(&tls.Config{RootCAs: customCertPool}),
    edge.WithTunnelLogger(myLogger),
    edge.WithCloseTimeout(10*time.Second),
)

Close drains in-flight connections gracefully. If draining exceeds the configured timeout (default 5 seconds), remaining connections are force-closed:

err := tunnel.Close() // safe to call multiple times

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloudflareAccess

func CloudflareAccess(baseAuth v1.AuthProvider, edgeToken string) (v1.AuthProvider, error)

CloudflareAccess returns an AuthProvider that adds Cloudflare Access headers to every RPC. It sets:

  • cf-access-jwt-assertion: the edge JWT token
  • cookie: CF_Authorization=<token>

The edgeToken authenticates with the Cloudflare Access edge proxy. Returns an error if baseAuth is nil or edgeToken is empty.

Types

type TunnelOption

type TunnelOption func(*tunnelConfig)

TunnelOption configures TunnelProxy behavior.

func WithCloseTimeout

func WithCloseTimeout(d time.Duration) TunnelOption

WithCloseTimeout sets the maximum time Close waits for in-flight connections to drain before force-closing. Default is 5 seconds.

func WithTunnelLogger

func WithTunnelLogger(l types.Logger) TunnelOption

WithTunnelLogger sets the structured logger for tunnel events.

func WithTunnelTLS

func WithTunnelTLS(cfg *tls.Config) TunnelOption

WithTunnelTLS sets TLS configuration for the WebSocket connection (wss://).

type TunnelProxy

type TunnelProxy struct {
	// contains filtered or unexported fields
}

TunnelProxy bridges gRPC connections over a WebSocket tunnel. The gRPC client dials TunnelProxy.Addr() instead of the remote gateway. Each accepted connection spawns a goroutine that dials the gateway over WebSocket and copies data bidirectionally.

func NewTunnelProxy

func NewTunnelProxy(gatewayURL, edgeToken string, opts ...TunnelOption) (*TunnelProxy, error)

NewTunnelProxy creates a tunnel proxy that forwards TCP connections through a WebSocket connection to gatewayURL. The edgeToken authenticates with the edge proxy via Cloudflare Access headers on the WebSocket handshake.

Returns error if gatewayURL is empty or invalid, or if edgeToken is empty.

func (*TunnelProxy) Addr

func (tp *TunnelProxy) Addr() string

Addr returns the local address the gRPC client should dial.

func (*TunnelProxy) Close

func (tp *TunnelProxy) Close() error

Close drains in-flight connections (up to the configured timeout, default 5s) then force-closes any remaining connections. All goroutines are cleaned up. Safe to call multiple times; the second and subsequent calls return immediately.

Jump to

Keyboard shortcuts

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