socket

package
v0.0.0-...-31b24f9 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidHeader   = errors.New("invalid packet header")
	ErrPayloadTooLarge = errors.New("payload too large")
	ErrInvalidAction   = errors.New("invalid action")

	ErrConnectionClosed              = errors.New("connection closed")
	ErrConnectionNotEstablished      = errors.New("connection not established")
	ErrConnectionAlreadyReconnecting = errors.New("connection already reconnecting")
	ErrConnectionTLSUpgradeFailed    = errors.New("tls upgrade failed")
	ErrExhaustedReconnectAttempts    = errors.New("exhausted reconnect attempts")
)
View Source
var DefaultConnHandlers = map[Action]HandlerFunc{
	ActionPing: func(c *Conn, header Header, r io.Reader) {
		if err := c.sendPong(); err != nil {
			c.GenLogMsg().Error().Msgf("failed to send pong: %v", err).Send()
		}
	},
	ActionPong: func(c *Conn, header Header, r io.Reader) {
		select {
		case c.pongCh <- struct{}{}:
		default:
		}
	},
}
View Source
var ErrAddressRequired = errors.New("address is required")
View Source
var ErrTLSMissingConfig = errors.New("tls config is required")

Functions

func WrapTLS

func WrapTLS(conn net.Conn, cfg *tls.Config) (net.Conn, error)

Wraps a net.Conn in a TLS connection

Types

type Action

type Action uint8

The header byte denoting the type of the message

const (
	ActionInvalid Action = iota
	ActionAck            // Generic ACK
	ActionError          // Error message

	// Control & lifecycle
	ActionPing    // Keepalive & healthcheck
	ActionPong    // Response to ping
	ActionHello   // Initial handshake (agent info)
	ActionGoodbye // Disconnect notification

	// Config management
	ActionRequestConfig // Agent asking for config
	ActionPushConfig    // Daemon pushing config

	// File transfers
	ActionSendFile      // Agent uploads file (e.g., flag, log)
	ActionRequestLogs   // Daemon requests logs from agent
	ActionSendFileChunk // (Optional) Chunked file part

	// Status and logs
	ActionPushStatus    // Agent pushes status update
	ActionRequestStatus // Server requests current status
)

type Conn

type Conn struct {
	Config *ConnConfig

	ReadDone chan struct{} // closes when reading is done
	// contains filtered or unexported fields
}

func DailWithRetry

func DailWithRetry(cfg *ConnConfig) (*Conn, error)

func NewConn

func NewConn(cfg *ConnConfig) *Conn

func NewConnWithRaw

func NewConnWithRaw(raw net.Conn, cfg *ConnConfig) *Conn

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) Connect

func (c *Conn) Connect() error

func (*Conn) GenLogMsg

func (c *Conn) GenLogMsg() *log.LogMessage

func (*Conn) IsOpen

func (c *Conn) IsOpen() bool

func (*Conn) Listen

func (c *Conn) Listen()

func (*Conn) Read

func (c *Conn) Read(b []byte) (int, error)

func (*Conn) Reconnect

func (c *Conn) Reconnect() error

func (*Conn) ReconnectOrClose

func (c *Conn) ReconnectOrClose() error

func (*Conn) Register

func (c *Conn) Register(action Action, fn HandlerFunc)

func (*Conn) RegisterLogger

func (c *Conn) RegisterLogger(l *log.Logger)

Otherwise uses the default logger

func (*Conn) SafeWrite

func (c *Conn) SafeWrite(b []byte) error

func (*Conn) String

func (c *Conn) String() string

func (*Conn) Write

func (c *Conn) Write(b []byte) (int, error)

type ConnConfig

type ConnConfig struct {
	Address string // The address to connect to
	Name    string // The name of the connection. This only really holds significance in logs.

	UseTLS    bool
	TLSConfig *tls.Config

	AutoReconnect           bool
	MaxReconnectionAttempts int
	ReconnectionDelay       time.Duration // The amount of time to wait between reconnection attempts

	HeartbeatInterval time.Duration // The interval at which to send pings. Set to 0 to disable.

	MessageSendTimeout time.Duration // The maximum amount of time to wait for a message to be sent
	MessageRecvTimeout time.Duration // The maximum amount of time to wait for a message to be received

	MaxHeaderSize  uint
	MaxMessageSize uint

	Handlers map[Action]HandlerFunc // The handlers to use for each action
}

func DefaultConnConfig

func DefaultConnConfig(address, name string, tlsCfg *tls.Config) *ConnConfig

func (*ConnConfig) Validate

func (c *ConnConfig) Validate() error

type ConnState

type ConnState uint8
const (
	ConnStateIdle ConnState = iota
	ConnStateUnknown
	ConnStateOpen
	ConnStateClosed
	ConnStateReconnecting
)

func (ConnState) String

func (s ConnState) String() string

type HandlerFunc

type HandlerFunc func(c *Conn, header Header, r io.Reader)
type Header struct {
	Action Action
	Len    uint64 // Payload size
}

The packet header

func UnmarshalHeader

func UnmarshalHeader(buf []byte) (Header, error)

func (*Header) MarshalBytes

func (h *Header) MarshalBytes() ([]byte, error)

func (*Header) UnmarshalBytes

func (h *Header) UnmarshalBytes(buf []byte) error

Jump to

Keyboard shortcuts

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