websocket

package
v0.0.0-...-d4fad9c Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WsError                         int64 = -1
	WsNotification                  int64 = 1
	WsAgentStatusRequest            int64 = 2
	WsAgentStatusResponse           int64 = 3
	WsAgentDisconnectedNotification int64 = 4
	WsAgentConnectedNotification    int64 = 5
	WsRuleDetectionAlert            int64 = 6
)

Message Types

Variables

This section is empty.

Functions

func Upgrade

func Upgrade(rw http.ResponseWriter, r *http.Request) (*websocket.Conn, error)

* This function will upgrade a HTTP connection to a websocket connection that will be used to send and receive messages * TO DO ... better logging of the error messages

Types

type AgentClient

type AgentClient struct {
	Id     string
	Status string
	Conn   *websocket.Conn
	Pool   *Pool
}

func (*AgentClient) Read

func (c *AgentClient) Read()

* This function will wait for a message to be sent by the client and based on the message type different functions from the pool will be called

type AgentMessage

type AgentMessage struct {
	C    *AgentClient
	Type int    `json:"type"`
	Body string `json:"body"`
}

type AgentStatusRequest

type AgentStatusRequest struct {
	AgentId string `json:"agentId"`
}

func (*AgentStatusRequest) FromJSON

func (asr *AgentStatusRequest) FromJSON(r io.Reader) error

type AgentStatusResponse

type AgentStatusResponse struct {
	AgentId string `json:"agentId"`
	Status  string `json:"status"`
}

func (*AgentStatusResponse) FromJSON

func (asr *AgentStatusResponse) FromJSON(r io.Reader) error

type DashboardClient

type DashboardClient struct {
	Id     int64
	Status string
	Conn   *websocket.Conn
	Pool   *Pool
}

* This structure will define a client that connected to the chat service. * Each client will have a unique id, a websocket connection that will be used to send and receive messages * The client structure will also have a pointer to the pool structure which will be used for conccurency

func (*DashboardClient) Read

func (c *DashboardClient) Read()

* This function will wait for a message to be sent by the client and based on the message type different functions from the pool will be called

type DashboardMessage

type DashboardMessage struct {
	C    *DashboardClient
	Type int    `json:"type"`
	Body string `json:"body"`
}

* This structure will define a message that can be sent/received on the websocket * The type variable will be used to determine if the websocket message is text or binary as it will have different values based on that * The body will be the payload that the other users it is delivered to should receive

type Notification

type Notification struct {
	AgentId string `json:"agentId"`
	Message string `json:"message"`
}

Notification Websocket message

func (*Notification) FromJSON

func (not *Notification) FromJSON(r io.Reader) error

type Pool

type Pool struct {
	RegisterDashboard   chan *DashboardClient     //Channel which will handle new dashboard client connections
	UnregisterDashboard chan *DashboardClient     //Channel which will handle a dashboard client disconnecting
	DashboardClients    map[*DashboardClient]bool //A map of dashboard client connections and associated state of the connection (true for online)
	DashboardBroadcast  chan DashboardMessage     //Channel which will be used to handle a message from the dashboard client
	RegisterAgent       chan *AgentClient         //Channel which will handle new agent connections
	UnregisterAgent     chan *AgentClient         //Channgel which will handle agent client disconnecting
	AgentClients        map[*AgentClient]bool     //A map of dashboard client connections and associated state of the connection (true for online)
	AgentBroadcast      chan AgentMessage         //Channel which will be used to handle a message from the agent
	// contains filtered or unexported fields
}

* This structure will handle concurrent connections using channels * Each channel will have a particular functionality

func NewPool

func NewPool(l logging.ILogger, dbConn database.IConnection, conf config.Configuration) *Pool

* This function will create a new pool that can then be used when starting the chat service

func (*Pool) AgentMessageReceived

func (pool *Pool) AgentMessageReceived(message AgentMessage)

* This function will handle when a message is recevied from a client * There should be more types of messages that can be received from the client

func (*Pool) AgentRegistered

func (pool *Pool) AgentRegistered(c *AgentClient)

func (*Pool) AgentUnregistered

func (pool *Pool) AgentUnregistered(c *AgentClient)

func (*Pool) DashboardClientRegistered

func (pool *Pool) DashboardClientRegistered(c *DashboardClient)

* This function will handle the register of a client (the client connected to the websocket) * It should be registered in the pool

func (*Pool) DashboardClientUnregistered

func (pool *Pool) DashboardClientUnregistered(c *DashboardClient)

func (*Pool) DashboardMessageReceived

func (pool *Pool) DashboardMessageReceived(message DashboardMessage)

* This function will handle when a message is recevied from a client * There should be more types of messages that can be received from the client

func (*Pool) HandleAgentStatusRequest

func (pool *Pool) HandleAgentStatusRequest(msg WebSocketMessage) (AgentStatusResponse, error)

func (*Pool) HandleNotification

func (pool *Pool) HandleNotification(msg WebSocketMessage) error

func (*Pool) HandleRuleDetectionAlert

func (pool *Pool) HandleRuleDetectionAlert(msg WebSocketMessage) error

func (*Pool) Start

func (pool *Pool) Start()

* This function will start the pool which will handle client connections, client disconnections and broadcast messages

type RuleDetectionAlert

type RuleDetectionAlert struct {
	AgentId         string `json:"agentId"`
	RuleId          string `json:"ruleId"`
	RuleName        string `json:"ruleName"`
	RuleDescription string `json:"ruleDescription"`
	Classification  string `json:"classification"`
	Severity        string `json:"severity"`
	Timestamp       int64  `json:"timestamp"`
}

func (*RuleDetectionAlert) FromJSON

func (rda *RuleDetectionAlert) FromJSON(r io.Reader) error

type WebSocketMessage

type WebSocketMessage struct {
	Type int64       `json:"type"` //The type of the message
	Data interface{} `json:"data"` //The data of the message as interface (can be any struct)
}

WebSocket message format

func (*WebSocketMessage) FromJSON

func (wsm *WebSocketMessage) FromJSON(r io.Reader) error

func (*WebSocketMessage) ToJSON

func (wsm *WebSocketMessage) ToJSON(w io.Writer) error

Jump to

Keyboard shortcuts

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