websocket

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAuthError

func IsAuthError(err error) bool

IsAuthError checks if an error is an authentication error

Types

type AuthError

type AuthError struct {
	StatusCode int
	Message    string
}

AuthError represents an authentication/authorization error (401/403)

func (*AuthError) Error

func (e *AuthError) Error() string

type Client

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

func NewClient

func NewClient(ID, secret, userToken, orgId, endpoint string, pingInterval time.Duration, pingTimeout time.Duration, opts ...ClientOption) (*Client, error)

NewClient creates a new websocket client

func (*Client) Close

func (c *Client) Close() error

Close closes the WebSocket connection gracefully

func (*Client) Connect

func (c *Client) Connect() error

Connect establishes the WebSocket connection

func (*Client) Disconnect added in v1.4.0

func (c *Client) Disconnect() error

Disconnect cleanly closes the websocket connection and suspends message intervals, but allows reconnecting later.

func (*Client) GetConfig

func (c *Client) GetConfig() *Config

func (*Client) GetConfigVersion added in v1.4.0

func (c *Client) GetConfigVersion() int

GetConfigVersion returns the current config version

func (*Client) OnAuthError

func (c *Client) OnAuthError(callback func(statusCode int, message string))

func (*Client) OnConnect

func (c *Client) OnConnect(callback func() error)

func (*Client) OnTokenUpdate

func (c *Client) OnTokenUpdate(callback func(token string, exitNodes []ExitNode))

func (*Client) RegisterHandler

func (c *Client) RegisterHandler(messageType string, handler MessageHandler)

RegisterHandler registers a handler for a specific message type

func (*Client) SendMessage

func (c *Client) SendMessage(messageType string, data interface{}) error

SendMessage sends a message through the WebSocket connection

func (*Client) SendMessageInterval

func (c *Client) SendMessageInterval(messageType string, data interface{}, interval time.Duration, maxAttempts int) (stop func(), update func(newData interface{}))

func (*Client) StartPingMonitor added in v1.4.0

func (c *Client) StartPingMonitor()

StartPingMonitor starts the ping monitor goroutine. This should be called after the client is registered and connected. It is safe to call multiple times - only the first call will start the monitor.

type ClientOption

type ClientOption func(*Client)

func WithBaseURL

func WithBaseURL(url string) ClientOption

WithBaseURL sets the base URL for the client

func WithPingDataProvider added in v1.4.0

func WithPingDataProvider(fn func() map[string]any) ClientOption

WithPingDataProvider sets a callback to provide additional data for ping messages

func WithTLSConfig

func WithTLSConfig(config TLSConfig) ClientOption

WithTLSConfig sets the TLS configuration for the client

type Config

type Config struct {
	ID            string
	Secret        string
	Endpoint      string
	TlsClientCert string // legacy PKCS12 file path
	UserToken     string // optional user token for websocket authentication
	OrgID         string // optional organization ID for websocket authentication
}

this is not json anymore

type ExitNode

type ExitNode struct {
	Endpoint  string `json:"endpoint"`
	RelayPort uint16 `json:"relayPort"`
	PublicKey string `json:"publicKey"`
	SiteIds   []int  `json:"siteIds"`
}

type MessageHandler

type MessageHandler func(message WSMessage)

type TLSConfig

type TLSConfig struct {
	// New separate certificate support
	ClientCertFile string
	ClientKeyFile  string
	CAFiles        []string

	// Existing PKCS12 support (deprecated)
	PKCS12File string
}

TLSConfig holds TLS configuration options

type TokenResponse

type TokenResponse struct {
	Data struct {
		Token         string     `json:"token"`
		ExitNodes     []ExitNode `json:"exitNodes"`
		ServerVersion string     `json:"serverVersion"`
	} `json:"data"`
	Success bool   `json:"success"`
	Message string `json:"message"`
}

type WSMessage

type WSMessage struct {
	Type          string      `json:"type"`
	Data          interface{} `json:"data"`
	ConfigVersion int         `json:"configVersion,omitempty"`
}

Jump to

Keyboard shortcuts

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