Documentation ¶
Index ¶
- Constants
- Variables
- func ClientConnect(ctx context.Context, endpointURL, authHeader string, tlsConfig *tls.Config) (*smux.Session, error)
- func ExpectOK(r io.Reader) ([]byte, error)
- func HandleTCPTunnel(ctx context.Context, stream *smux.Stream, remoteAddr []byte) error
- func HandleUDPTunnel(ctx context.Context, stream *smux.Stream, payload []byte) error
- func NewEdgeMock(t Tester) (*Client, *DeviceClient, *EdgeMock)
- func Pipe(src io.ReadWriteCloser, dst io.ReadWriteCloser) (sent int64, received int64, err error)
- func UpgradeHandler(w http.ResponseWriter, r *http.Request) (*smux.Session, error)
- func WithIOWaitTimeout(ctx context.Context, timeout time.Duration) context.Context
- func WriteError(w io.Writer, err error) error
- func WriteMessage(w io.Writer, messageType MessageType, payload []byte) error
- func WriteOK(w io.Writer, payload []byte) error
- type Client
- func (cli *Client) Close() error
- func (cli *Client) GetSession() *smux.Session
- func (cli *Client) OpenStream(ctx context.Context, msgType MessageType, payload []byte) (*smux.Stream, error)
- func (cli *Client) OpenStreamPayload(ctx context.Context, msgType MessageType, payload []byte) (*smux.Stream, []byte, error)
- func (cli *Client) OpenTCPTunnel(ctx context.Context, localHostPort, remoteHostPort string) (*net.TCPListener, error)
- func (cli *Client) OpenUDPTunnel(ctx context.Context, localHostPort, remoteHostPort string) (*UDPTunnel, error)
- type DeviceClient
- func (cli *DeviceClient) Close() error
- func (cli *DeviceClient) Err() error
- func (cli *DeviceClient) IsRunning() bool
- func (cli *DeviceClient) Ready()
- func (cli *DeviceClient) Start(ctx context.Context)
- func (cli *DeviceClient) WithHandler(messageType MessageType, handler Handler) *DeviceClient
- func (cli *DeviceClient) WithSmuxConfig(smuxConfig *smux.Config) *DeviceClient
- type EdgeMock
- type Handler
- type MessageType
- type PTYCommand
- type PTYCommandType
- type Tester
- type UDPStream
- type UDPTunnel
Constants ¶
const ( // KB kilobyte KB = 1024 // MB megabyte MB = 1024 * KB )
const Protocol = "qbee-v1"
Protocol is the protocol to which the localListener is upgraded.
Variables ¶
var DefaultSmuxConfig = &smux.Config{ Version: 2, KeepAliveInterval: 45 * time.Second, KeepAliveTimeout: 120 * time.Second, MaxFrameSize: 32 * KB, MaxReceiveBuffer: 4 * MB, MaxStreamBuffer: 128 * KB, }
DefaultSmuxConfig is the default smux configuration.
Functions ¶
func ClientConnect ¶
func ClientConnect(ctx context.Context, endpointURL, authHeader string, tlsConfig *tls.Config) (*smux.Session, error)
ClientConnect initiates smux session with the provided edge endpoint.
func ExpectOK ¶ added in v1.24.5
ExpectOK reads a message from the given reader and returns the payload if it is an OK message. Otherwise, an error is returned.
func HandleTCPTunnel ¶
HandleTCPTunnel handles a TCP tunnel request as a device.
func HandleUDPTunnel ¶
HandleUDPTunnel handles a UDP tunnel stream as a device.
func NewEdgeMock ¶
func NewEdgeMock(t Tester) (*Client, *DeviceClient, *EdgeMock)
NewEdgeMock creates a new test server and returns it together with a client and a device client connected to it.
func Pipe ¶
func Pipe(src io.ReadWriteCloser, dst io.ReadWriteCloser) (sent int64, received int64, err error)
Pipe copies data from src to dst and vice versa and returns first non-nil error.
func UpgradeHandler ¶
UpgradeHandler upgrades the server request localListener to the remote access protocol. For protocol errors, the handler will write an error response and return an error.
func WithIOWaitTimeout ¶
WithIOWaitTimeout returns a copy of the context with the specified timeout for I/O operations.
func WriteError ¶ added in v1.24.5
WriteError writes an error message to the given writer.
func WriteMessage ¶
func WriteMessage(w io.Writer, messageType MessageType, payload []byte) error
WriteMessage writes a message to the given writer.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the client for the remote access service.
func NewClient ¶
func NewClient(ctx context.Context, endpointURL, authToken string, tlsConfig *tls.Config) (*Client, error)
NewClient creates a new remote access client instance connected to the given host.
func (*Client) GetSession ¶ added in v1.24.27
GetSession returns the underlying smux session.
func (*Client) OpenStream ¶ added in v1.24.5
func (cli *Client) OpenStream(ctx context.Context, msgType MessageType, payload []byte) (*smux.Stream, error)
OpenStream opens a new stream and sends the given message type and payload.
func (*Client) OpenStreamPayload ¶ added in v1.24.27
func (cli *Client) OpenStreamPayload(ctx context.Context, msgType MessageType, payload []byte) (*smux.Stream, []byte, error)
OpenStreamPayload opens a new stream and sends the given message type and payload.
func (*Client) OpenTCPTunnel ¶
func (cli *Client) OpenTCPTunnel(ctx context.Context, localHostPort, remoteHostPort string) (*net.TCPListener, error)
OpenTCPTunnel listens on the given local port and forwards all connections to the given remote host and port. The returned listener can be used to close the tunnel.
func (*Client) OpenUDPTunnel ¶
func (cli *Client) OpenUDPTunnel(ctx context.Context, localHostPort, remoteHostPort string) (*UDPTunnel, error)
OpenUDPTunnel listens on the given local port and forwards all packets to the given remote host and port. Remember to close the tunnel when you're done with it using the Close method.
type DeviceClient ¶
type DeviceClient struct {
// contains filtered or unexported fields
}
DeviceClient is the remote access client for devices.
func NewDeviceClient ¶
func NewDeviceClient(endpoint string, tlsConfig *tls.Config) (*DeviceClient, error)
NewDeviceClient creates a new DeviceClient. endpoint is the address of the remote access device registration endpoint (e.g. "https://edge.example.com/device").
func (*DeviceClient) Close ¶
func (cli *DeviceClient) Close() error
Close kills all streams and disconnects from the edge service.
func (*DeviceClient) Err ¶
func (cli *DeviceClient) Err() error
Err returns the error that caused the device client to stop.
func (*DeviceClient) IsRunning ¶
func (cli *DeviceClient) IsRunning() bool
IsRunning returns true if the device client is currently running.
func (*DeviceClient) Ready ¶
func (cli *DeviceClient) Ready()
Ready returns when the device client is ready.
func (*DeviceClient) Start ¶
func (cli *DeviceClient) Start(ctx context.Context)
Start starts the device client and processing loop. If connection to the edge service fails, the device client will retry to connect.
func (*DeviceClient) WithHandler ¶
func (cli *DeviceClient) WithHandler(messageType MessageType, handler Handler) *DeviceClient
WithHandler sets the handler for the given message type.
func (*DeviceClient) WithSmuxConfig ¶
func (cli *DeviceClient) WithSmuxConfig(smuxConfig *smux.Config) *DeviceClient
WithSmuxConfig sets the smux config for the device client.
type EdgeMock ¶
type EdgeMock struct {
// contains filtered or unexported fields
}
EdgeMock provides a minimalistic edge server implementation to make testing of clients a bit easier.
func (*EdgeMock) DeviceClient ¶
func (edge *EdgeMock) DeviceClient() *DeviceClient
DeviceClient returns a new device client connected to the test server.
type MessageType ¶
type MessageType uint8
MessageType defines the type of message sent over the wire.
const ( // MessageTypeOK indicates that requested operation was successful. // The payload is empty. MessageTypeOK MessageType = 0x01 // MessageTypeError indicates that requested operation failed. // The payload contains the error message. MessageTypeError MessageType = 0x02 // MessageTypeGoAway indicates that client should reconnect to another server. MessageTypeGoAway MessageType = 0x03 // MessageTypeTCPTunnel indicates that the message is a TCP tunnel request. // The payload contains the remote host and port. MessageTypeTCPTunnel MessageType = 0x10 // MessageTypeUDPTunnel indicates that the message is a UDP tunnel request. // The payload contains the suggested listener port, remote host and port. MessageTypeUDPTunnel MessageType = 0x11 // MessageTypePTY indicates that the message is a PTY request. // The payload contains initial JSON-encoded PTYCommand with PTYCommandTypeResize to set the initial window size. MessageTypePTY MessageType = 0x12 // MessageTypePTYCommand indicates that the message is a PTY command request. // The payload contains JSON-encoded PTYCommand. MessageTypePTYCommand MessageType = 0x13 // MessageTypeReload triggers a configuration reload. MessageTypeReload MessageType = 0x14 )
func ReadMessage ¶
func ReadMessage(r io.Reader) (messageType MessageType, payload []byte, err error)
ReadMessage reads a message from the given reader.
type PTYCommand ¶
type PTYCommand struct { // SessionID is the session ID (initiated with MessageTypePTY) to which the command applies. SessionID string `json:"sid"` // Type is the type of the command to be executed on the PTY. Type PTYCommandType `json:"type,omitempty"` // Cols and Rows are the new window size. // Those fields are only used when Type is PTYCommandTypeResize. Cols uint16 `json:"cols,omitempty"` Rows uint16 `json:"rows,omitempty"` // Command is the command to be executed on the PTY. Command string `json:"command,omitempty"` // CommandArgs are the arguments to be passed to the command. CommandArgs []string `json:"command_args,omitempty"` }
PTYCommand carries a command to be executed on the PTY stream.
type PTYCommandType ¶
type PTYCommandType uint8
PTYCommandType is the type of PTY command.
const ( // PTYCommandTypeResize indicates that the command is a resize request. PTYCommandTypeResize PTYCommandType = iota )
type UDPStream ¶
type UDPStream struct {
// contains filtered or unexported fields
}
UDPStream represents a stream of UDP datagrams between a local client and a remote server.
type UDPTunnel ¶
type UDPTunnel struct {
// contains filtered or unexported fields
}
UDPTunnel is a client implementation of a tunnel for UDP datagrams exchange. It listens on a local port and for each new client it receives a packet from, it creates a new smux stream to isolate and simplify the communication. When remote responds from a different port, it creates a new listener for that remote source port locally, so we can send the response to the client from a separate port as well.
func (*UDPTunnel) PrimaryAddr ¶
PrimaryAddr returns the primary address of the local listener.