listen

package
v1.4.42-prerelease Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2024 License: Apache-2.0 Imports: 21 Imported by: 6

Documentation

Overview

Package listen can create a Border0 listener with configurable options. The border0 listener is a net.Listener that can be used to accept incoming connections. When the listener is passed to http.Serve, the server will accept HTTP requests sent by Border0 and forward them to an HTTP handler. The handler's response will be sent back to Border0. If no options are provided, some default values will be used.

Example:

listener := listen.New(
	listen.WithSocketName("sdk-socket-http"),              // http socket name the listener will be bound to, socket will be created if not exists
	listen.WithAuthToken(os.Getenv("BORDER0_AUTH_TOKEN")), // optional, if not provided, Border0 SDK will use BORDER0_AUTH_TOKEN env var
)
err := listener.Start(context.Background())
if err != nil {
	// handle error
}
defer listener.Close()

// create a handler
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
	// do something
})

// start a server using the listener
http.Serve(listener, handler)

See Option for more configurable options.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Listener

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

Listener is a net.Listener that connects to a Border0 tunnel server and forwards connections to the local machine.

func New

func New(options ...Option) *Listener

New creates a new Listener with the given options.

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

Accept waits for and returns the next connection to the listener.

func (*Listener) Addr

func (l *Listener) Addr() net.Addr

Addr returns the listener's network address.

func (*Listener) Close

func (l *Listener) Close() error

Close closes the listener.

func (*Listener) Start

func (l *Listener) Start(ctx context.Context) error

Start connects to Border0 tunnel server and starts listening for connections. It will block until the listener is ready to accept connections.

type Option

type Option func(*Listener)

Option is a function that configures a Listener.

func WithAPIClient

func WithAPIClient(api client.Requester) Option

WithAPIClient sets the API client to use for the Listener.

func WithAuthToken

func WithAuthToken(token string) Option

WithAuthToken sets the auth token to use with the API client.

func WithPolicies added in v1.3.0

func WithPolicies(names []string) Option

WithPolicies sets the policy names to use for the Listener's underlaying HTTP socket. Policies with the given names will be attached to the socket. If the policy names list is empty, then no policies will be attached. If there are any changes made to the policy names list between listener startups, they will get properly handled. The listener will check the socket's already attached policies, and compare them with the given policy names. Removed policies will be detached from the socket, and new policies will be checked and made sure they exist, and then they will be attached to the listener's socket.

func WithSocketName

func WithSocketName(name string) Option

WithSocketName sets the socket name to use for the HTTP socket that the Listener will create.

func WithTunnelServer

func WithTunnelServer(server string) Option

WithTunnelServer sets the tunnel server address for the Listener.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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