Documentation
¶
Index ¶
- Variables
- type Agent
- type AgentOption
- func WithAgentConnectCAs(pool *x509.CertPool) AgentOption
- func WithAgentConnectURL(addr string) AgentOption
- func WithAgentDescription(desc string) AgentOption
- func WithAgentMetadata(meta string) AgentOption
- func WithAuthtoken(token string) AgentOption
- func WithAutoConnect(auto bool) AgentOption
- func WithClientInfo(clientType, version string, comments ...string) AgentOption
- func WithDialer(dialer Dialer) AgentOption
- func WithEventHandler(handler EventHandler) AgentOption
- func WithHeartbeatInterval(interval time.Duration) AgentOption
- func WithHeartbeatTolerance(tolerance time.Duration) AgentOption
- func WithLogger(logger *slog.Logger) AgentOption
- func WithMultiLeg(enable bool) AgentOption
- func WithProxyURL(urlSpec string) AgentOption
- func WithRPCHandler(handler RPCHandler) AgentOption
- func WithTLSConfig(tlsCustomizer func(*tls.Config)) AgentOption
- type AgentSession
- type Dialer
- type Endpoint
- type EndpointForwarder
- type EndpointListener
- type EndpointOption
- func WithAgentTLSTermination(config *tls.Config) EndpointOption
- func WithBindings(bindings ...string) EndpointOption
- func WithDescription(desc string) EndpointOption
- func WithMetadata(meta string) EndpointOption
- func WithPoolingEnabled(pool bool) EndpointOption
- func WithTrafficPolicy(policy string) EndpointOption
- func WithURL(urlSpec string) EndpointOption
- type Error
- type Event
- type EventAgentConnectSucceeded
- type EventAgentDisconnected
- type EventAgentHeartbeatReceived
- type EventHandler
- type EventType
- type ProxyProtoVersion
- type RPCHandler
- type Upstream
- type UpstreamOption
Constants ¶
This section is empty.
Variables ¶
var DefaultAgent, _ = NewAgent(
WithAuthtoken(os.Getenv("NGROK_AUTHTOKEN")),
)
A default Agent instance to use when you don't need a custom one.
Functions ¶
This section is empty.
Types ¶
type Agent ¶
type Agent interface { // Connect begins a new Session by connecting and authenticating to the ngrok cloud service. Connect(context.Context) error // Disconnect terminates the current Session which disconnects it from the ngrok cloud service. Disconnect() error // Session returns an object describing the connection of the Agent to the ngrok cloud service. Session() (AgentSession, error) // Endpoints returns the list of endpoints created by this Agent from calls to either Listen or Forward. Endpoints() []Endpoint // Listen creates an Endpoint which returns received connections to the caller via an EndpointListener. Listen(context.Context, ...EndpointOption) (EndpointListener, error) // Forward creates an Endpoint which forwards received connections to a target upstream URL. Forward(context.Context, *Upstream, ...EndpointOption) (EndpointForwarder, error) }
Agent is the main interface for interacting with the ngrok service.
func NewAgent ¶
func NewAgent(agentOpts ...AgentOption) (Agent, error)
NewAgent creates a new Agent object.
type AgentOption ¶
type AgentOption func(*agentOpts)
AgentOption is a functional option used to configure NewAgent.
func WithAgentConnectCAs ¶
func WithAgentConnectCAs(pool *x509.CertPool) AgentOption
WithAgentConnectCAs defines the CAs used to validate the TLS certificate returned by the ngrok service when establishing a session.
func WithAgentConnectURL ¶
func WithAgentConnectURL(addr string) AgentOption
WithAgentConnectURL defines the URL the agent connects to in order to establish a connection to the ngrok cloud service.
func WithAgentDescription ¶
func WithAgentDescription(desc string) AgentOption
WithAgentDescription sets a human-readable description for the agent session.
func WithAgentMetadata ¶
func WithAgentMetadata(meta string) AgentOption
WithAgentMetadata sets opaque, machine-readable metadata for the agent session.
See https://ngrok.com/docs/api/resources/tunnel-sessions/#response-1
func WithAuthtoken ¶
func WithAuthtoken(token string) AgentOption
WithAuthtoken specifies the authtoken to use for authenticating to the ngrok cloud service during Connect.
func WithAutoConnect ¶
func WithAutoConnect(auto bool) AgentOption
WithAutoConnect controls whether the Agent will automatically call Connect(). When enabled, if an endpoint is created via Listen() or Connect() and the Agent does not have an active session, it will automatically Connect().
func WithClientInfo ¶
func WithClientInfo(clientType, version string, comments ...string) AgentOption
WithClientInfo provides client information to the ngrok cloud service.
func WithDialer ¶
func WithDialer(dialer Dialer) AgentOption
WithDialer customizes how the Agent establishes connections to the ngrok cloud service.
func WithEventHandler ¶
func WithEventHandler(handler EventHandler) AgentOption
WithEventHandler registers a callback to receive events from the Agent. If called multiple times, each handler will receive callbacks. See EventHandler for details on correctly authoring handlers.
func WithHeartbeatInterval ¶
func WithHeartbeatInterval(interval time.Duration) AgentOption
WithHeartbeatInterval sets how often the agent will send heartbeat messages to the ngrok service.
func WithHeartbeatTolerance ¶
func WithHeartbeatTolerance(tolerance time.Duration) AgentOption
WithHeartbeatTolerance sets how long to wait for a heartbeat response before assuming the connection is dead.
func WithLogger ¶
func WithLogger(logger *slog.Logger) AgentOption
WithLogger sets the logger to use for the agent. Accepts a standard log/slog.Logger from the Go standard library.
func WithMultiLeg ¶
func WithMultiLeg(enable bool) AgentOption
WithMultiLeg enables connecting to the ngrok service on secondary legs. This option is EXPERIMENTAL and may be removed without a breaking version change.
func WithProxyURL ¶
func WithProxyURL(urlSpec string) AgentOption
WithProxyURL sets the proxy URL to use when connecting to the ngrok service. The URL will be parsed and processed during Connect.
If used with WithDialer, the custom dialer will be used to establish the connection to the proxy, which will then connect to the ngrok service.
func WithRPCHandler ¶
func WithRPCHandler(handler RPCHandler) AgentOption
WithRPCHandler registers a handler for RPC commands from the ngrok service. This handler will be called when the agent receives RPC requests like StopAgent, RestartAgent, or UpdateAgent.
func WithTLSConfig ¶
func WithTLSConfig(tlsCustomizer func(*tls.Config)) AgentOption
WithTLSConfig customizes the TLS configuration for connections to the ngrok service.
type AgentSession ¶
type AgentSession interface { // ID returns the server-assigned ID of the agent session // TODO(alan): implement when the server begins setting this value // ID() string // Warnings is a list of warnings returned by the ngrok cloud service after the Agent has connected Warnings() []error // Agent returns the agent that started this session Agent() Agent // StartedAt returns the time that the AgentSession was connected StartedAt() time.Time }
AgentSession represents an active connection from an Agent to the ngrok cloud service.
type Dialer ¶
type Dialer interface { Dial(network, address string) (net.Conn, error) DialContext(ctx context.Context, network, address string) (net.Conn, error) }
Dialer is an interface that is satisfied by net.Dialer or you can specify your own implementation.
type Endpoint ¶
type Endpoint interface { // Agent returns the Agent that created this Endpoint. Agent() Agent // PoolingEnabled returns whether the endpoint supports pooling set by WithPoolingEnabled. PoolingEnabled() bool // Bindings returns the endpoint's bindings set by WithBindings Bindings() []string // Close() is equivalent to for CloseWithContext(context.Background()) Close() error // CloseWithContext closes the endpoint with the provided context. CloseWithContext(context.Context) error // Description returns the endpoint's human-readable description set by WithDescription. Description() string // Done returns a channel that is closed when the endpoint stops. Done() <-chan struct{} // ID returns the unique endpoint identifier assigned by the ngrok cloud service. ID() string // Metadata returns the endpoint's opaque user-defined metadata set by WithMetadata. Metadata() string // Protocol is sugar for URL().Scheme Protocol() string // AgentTLSTermination returns the TLS config that the agent uses to terminate TLS connections. AgentTLSTermination() *tls.Config // TrafficPolicy returns the traffic policy for the endpoint. TrafficPolicy() string // URL returns the Endpoint's URL URL() *url.URL }
Endpoint is the interface implemented by both EndpointListener and EndpointForwarder.
type EndpointForwarder ¶
type EndpointForwarder interface { Endpoint // UpstreamProtocol returns the protocol used to communicate with the upstream server. // This differs from UpstreamURL().Scheme if http2 is used. UpstreamProtocol() string // UpstreamURL returns the URL that the endpoint forwards its traffic to. UpstreamURL() url.URL // UpstreamTLSClientConfig returns the TLS client configuration used for upstream connections. UpstreamTLSClientConfig() *tls.Config // ProxyProtocol returns the PROXY protocol version used for the endpoint. // Returns a ProxyProtoVersion or empty string if not enabled. ProxyProtocol() ProxyProtoVersion }
EndpointForwarder is an Endpoint that forwards traffic to an upstream service.
func Forward ¶
func Forward(ctx context.Context, upstream *Upstream, opts ...EndpointOption) (EndpointForwarder, error)
Forward is sugar for DefaultAgent.Forward().
type EndpointListener ¶
type EndpointListener interface { Endpoint // Accept returns the next connection received the Endpoint. Accept() (net.Conn, error) // Addr() returns where the Endpoint is listening. Addr() net.Addr }
EndpointListener is an endpoint that you may treat as a net.Listener.
func Listen ¶
func Listen(ctx context.Context, opts ...EndpointOption) (EndpointListener, error)
Listen is equivalent to DefaultAgent.Listen().
type EndpointOption ¶
type EndpointOption func(*endpointOpts)
EndpointOption is a functional option used to configure endpoints.
func WithAgentTLSTermination ¶
func WithAgentTLSTermination(config *tls.Config) EndpointOption
WithAgentTLSTermination sets a TLS configuration that the agent will use to terminate connections received on the Endpoint.
func WithBindings ¶
func WithBindings(bindings ...string) EndpointOption
WithBindings sets the endpoint's bindings.
func WithDescription ¶
func WithDescription(desc string) EndpointOption
WithDescription sets a human-readable description for the endpoint.
func WithMetadata ¶
func WithMetadata(meta string) EndpointOption
WithMetadata sets opaque, machine-readable metadata for the endpoint.
func WithPoolingEnabled ¶
func WithPoolingEnabled(pool bool) EndpointOption
WithPoolingEnabled controls whether the endpoint supports connection pooling.
See https://ngrok.com/docs/universal-gateway/endpoint-pooling/
func WithTrafficPolicy ¶
func WithTrafficPolicy(policy string) EndpointOption
WithTrafficPolicy defines the Endpoint's Traffic Policy.
type Error ¶
Error is a custom error type that includes a unique ngrok error code. All errors that are returned by the ngrok cloud service are of this type.
type EventAgentConnectSucceeded ¶
type EventAgentConnectSucceeded struct { Agent Agent Session AgentSession // contains filtered or unexported fields }
EventAgentConnectSucceeded is emitted when an agent successfully establishes a connection
type EventAgentDisconnected ¶
type EventAgentDisconnected struct { Agent Agent Session AgentSession Error error // contains filtered or unexported fields }
EventAgentDisconnected is emitted when an agent disconnects
type EventAgentHeartbeatReceived ¶
type EventAgentHeartbeatReceived struct { Agent Agent Session AgentSession Latency time.Duration // contains filtered or unexported fields }
EventAgentHeartbeatReceived is emitted when a heartbeat is successful
type EventHandler ¶
type EventHandler func(Event)
EventHandler is the function type for event callbacks. EventHandlers must not block. If you would like to run operations on an event that will block or fail, instead write your handler to either non-blockingly push the Event onto a channel or spin up a goroutine.
type ProxyProtoVersion ¶
type ProxyProtoVersion string
ProxyProtoVersion represents PROXY protocol version
const ( ProxyProtoV1 ProxyProtoVersion = "v1" ProxyProtoV2 ProxyProtoVersion = "v2" )
type RPCHandler ¶
RPCHandler is a function that processes RPC requests from the ngrok service. It receives the context, agent session, and request, and returns an optional response payload and error.
type Upstream ¶
type Upstream struct {
// contains filtered or unexported fields
}
Upstream represents configuration for forwarding to an upstream service.
func WithUpstream ¶
func WithUpstream(addr string, opts ...UpstreamOption) *Upstream
WithUpstream creates an Upstream configuration with a required address. The address can be in various formats such as: - "80" (a port number for local services) - "example.com:8080" (a host:port combination) - "http://example.com" (a full URL)
type UpstreamOption ¶
type UpstreamOption func(*Upstream)
UpstreamOption configures an Upstream instance.
func WithUpstreamDialer ¶
func WithUpstreamDialer(dialer Dialer) UpstreamOption
WithUpstreamDialer sets a custom dialer to use when connecting to the upstream server. This allows for custom network configurations or connection methods when reaching the upstream.
func WithUpstreamProtocol ¶
func WithUpstreamProtocol(proto string) UpstreamOption
WithUpstreamProtocol sets the protocol to use when forwarding to the upstream. This is typically used to specify "http2" when communicating with an upstream HTTP/2 server.
func WithUpstreamProxyProto ¶
func WithUpstreamProxyProto(proxyProto ProxyProtoVersion) UpstreamOption
WithUpstreamProxyProto sets the PROXY protocol version to use when connecting to the upstream server. Valid values are ProxyProtoV1 or ProxyProtoV2.
See https://ngrok.com/docs/agent/config/v3/#upstreamproxy_protocol
func WithUpstreamTLSClientConfig ¶
func WithUpstreamTLSClientConfig(config *tls.Config) UpstreamOption
WithUpstreamTLSClientConfig sets the TLS client configuration to use when connecting to the upstream server over TLS.