message

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2022 License: BSD-2-Clause Imports: 2 Imported by: 4

README

legs-message

Library for define legs protocol messages for legs-server and legs-client

Basic Usage

install
go get github.com/iij/legs-message

and

import "github.com/iij/legs-message"
create message
msg := message.NewConsoleStartMessage("session-id", "shell cmd")
Encode to msgpack
b, err := message.Marshal(msg)
Decode from msgpack
msg := &message.ConsoleMessage{}
err = message.Unmarshal(b, msg)

TODOs

  • CI (testing, formatting)

Documentation

Index

Constants

View Source
const (
	// TransferActionPost is the action for post json or text to external service.
	TransferActionPost = "post"

	// TransferActionGet is the action for download file from external service.
	TransferActionGet = "get"
)
View Source
const (
	// TypeInvalid is type id for invalid type
	TypeInvalid int8 = 0000

	// TypeNone is type id for BaseMessage
	TypeNone int8 = 0001

	// TypeCommand is type id for CommandMessage
	TypeCommand int8 = 0010

	// TypeRoutingConfigure is type id for RoutingConfigureMessage
	TypeRoutingConfigure int8 = 0020

	// TypeClientConfigure is type id for ClientConfigureMessage
	TypeClientConfigure int8 = 0021

	// TypeConsole is type id for Console
	TypeConsole int8 = 0040

	// TypeProxy is type id for Proxy
	TypeProxy int8 = 0050
)

Variables

This section is empty.

Functions

func GetMessageType

func GetMessageType(msg []byte) (id int8, err error)

GetMessageType gets type for message by extended header in msgpack

func Marshal

func Marshal(msg interface{}) (packBytes []byte, err error)

Marshal returns msgpack encoding of msg

func Unmarshal

func Unmarshal(b []byte, msg interface{}) error

Unmarshal returns decoding msgpack with type of msg param.

Types

type BaseMessage

type BaseMessage struct {
	MessageType string `json:"messageType"`
	Model       string `json:"model"`
}

BaseMessage defines basic message.

func NewMessage

func NewMessage() BaseMessage

NewMessage creates instance of plane message

func (*BaseMessage) GetMessageType

func (m *BaseMessage) GetMessageType() string

GetMessageType returns message type property.

func (*BaseMessage) GetModel

func (m *BaseMessage) GetModel() string

GetModel returns model property.

type ClientConfig

type ClientConfig struct {
	PingInterval int    `json:"ping_interval"`
	DeviceID     string `json:"device_id"`
}

ClientConfig defines data model for client basic setting.

type ClientConfigure

type ClientConfigure struct {
	BaseMessage
	Data ClientConfig `json:"data"`
}

ClientConfigure defines message for configuring client basic config.

type Command

type Command struct {
	BaseMessage
	Data CommandData `json:"data"`
}

Command defines message for command execution

func (*Command) SetResult

func (c *Command) SetResult(result string)

SetResult set a result of execution command to command message.

func (*Command) SetStatus

func (c *Command) SetStatus(status CommandStatus)

SetStatus set a status attribute to command message.

type CommandData

type CommandData struct {
	ID       string        `json:"id"`
	DeviceID string        `json:"device_id"`
	Status   CommandStatus `json:"status"`
	Result   string        `json:"result"`
	Command  string        `json:"command"`
}

CommandData defines data for command message

type CommandMessage

type CommandMessage interface {
	Message
	SetStatus(status CommandStatus)
	SetResult(result string)
}

CommandMessage defines interface for command message methods

func NewCommand

func NewCommand(commandID, deviceID string, status CommandStatus, result, command string) CommandMessage

NewCommand creates command execution message

type CommandStatus

type CommandStatus string

CommandStatus defines type of command message status

const (
	// CommandExecutingStatus specifies the status of executing command in client.
	CommandExecutingStatus CommandStatus = "executing"

	// CommandErrorStatus specifies the status of command execution was failed.
	CommandErrorStatus CommandStatus = "error"

	// CommandExecutedStatus specifies the status of command execution was success.
	CommandExecutedStatus CommandStatus = "executed"
)

type Console

type Console struct {
	BaseMessage
	State     ConsoleState `json:"state"`
	SessionID string       `json:"session_id"`
	Data      []byte       `json:"data"`
}

Console defines message for create console session between server and client.

type ConsoleState

type ConsoleState string

ConsoleState defines type of console message state

const (
	// ConsoleStartState is state of message of start console session to legs-client.
	ConsoleStartState ConsoleState = "start"

	// ConsoleInputState is state of message of input to legs-client console.
	ConsoleInputState ConsoleState = "input"

	// ConsoleCloseState is state of message of request for close console session.
	ConsoleCloseState ConsoleState = "close"

	// ConsoleOutputState is state of message of output from legs-client to server.
	ConsoleOutputState ConsoleState = "output"
)

type Message

type Message interface {
	GetMessageType() string
	GetModel() string
}

Message defines interface for every message structs

func NewClientConfigure

func NewClientConfigure(config ClientConfig) Message

NewClientConfigure creates client configure message.

func NewConsoleClose

func NewConsoleClose(sessionID string) Message

NewConsoleClose creates console message at close session.

func NewConsoleInput

func NewConsoleInput(sessionID string, in []byte) Message

NewConsoleInput creates console message for input to client device.

func NewConsoleOutput

func NewConsoleOutput(sessionID string, in []byte) Message

NewConsoleOutput creates console message for output from client device.

func NewConsoleStart

func NewConsoleStart(sessionID, shell string) Message

NewConsoleStart creates console message at start session

func NewProxyRequest

func NewProxyRequest(serverID, id, sessionID, url string, req []byte) Message

NewProxyRequest creates instance of Proxy with request bytes

func NewProxyResponse

func NewProxyResponse(requestData ProxyData, res []byte) Message

NewProxyResponse creates instance of Proxy with response bytes

func NewTransferRequest

func NewTransferRequest(sessionID string, req TransferRequestData) Message

NewTransferRequest creates a transfer request message from string url.

func NewTransferResponse

func NewTransferResponse(data transferData, res []TransferResponseData) Message

NewTransferResponse creates a transfer response message from request data.

type Proxy

type Proxy struct {
	BaseMessage
	Data ProxyData `json:"data"`
}

Proxy defines message for url forwarding protocol.

func (*Proxy) SetResponse

func (p *Proxy) SetResponse(response []byte)

SetResponse set a response to proxy data

type ProxyData

type ProxyData struct {
	ID        string `json:"id"`
	URL       string `json:"url"`
	Request   []byte `json:"request"`
	Response  []byte `json:"response"`
	ServerID  string `json:"server_id"`
	SessionID string `json:"session_id"`
}

ProxyData defines data for proxy message

type Transfer

type Transfer struct {
	BaseMessage
	Data transferData `json:"data"`
}

Transfer defines message for data transfer request using raw url.

type TransferRequestData

type TransferRequestData struct {
	Action string `json:"action"`
	Target string `json:"target"`
	Value  string `json:"value"`
}

TransferRequestData defines request for http request transfer. Action: `TransferActionPost` or `TransferActionGet` Target: Transfer request destination. The Target can be a valid url (beginning with `http`) and routing name stored on the server. Value: The Request body for transfer http request.

type TransferResponseData

type TransferResponseData struct {
	URL        string `json:"url"`
	Body       []byte `json:"body"`
	StatusCode int    `json:"status_code"`
}

TransferResponseData defines response when transferred http request. URL: transfer destination Body: http response body StatusCode: http response code

Jump to

Keyboard shortcuts

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