internal

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package belphegor provides functionality for managing clipboard data between nodes.

Index

Constants

View Source
const DataLength = 4

Variables

View Source
var (
	// Version is the current Version of binary
	Version = "freshest"
	// CommitHash is the current commit hash of binary
	CommitHash = "n/a"
	// BuildTime is the current build time of binary
	BuildTime = "n/a"
)
View Source
var (
	ErrAlreadyConnected = errors.New("already connected")
)
View Source
var (
	ErrVersionMismatch = errors.New("version mismatch")
)

Functions

func AcquireMessage

func AcquireMessage(data []byte) *gen.Message

AcquireMessage creates a new Message with the provided data.

func HandShake

func HandShake(self *gen.GreetMessage, other *gen.GreetMessage) error

func MessageIsDuplicate

func MessageIsDuplicate(self *gen.Message, from *gen.Message) bool

func ReleaseMessage

func ReleaseMessage(m *gen.Message)

ReleaseMessage returns the Message to the messagePool for reuse.

Types

type Channel

type Channel interface {
	// Write data to the provided byte slice.
	Write(b []byte)

	// Read returns a read-only channel for retrieving clipboard data.
	Read() <-chan []byte
}

Channel is an interface for managing clipboard data.

type ClipboardMonitor

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

ClipboardMonitor responsible for monitoring the local clipboard.

func NewClipboardMonitor

func NewClipboardMonitor(
	node *Node,
	cp clipboard.Manager,
	interval time.Duration,
	extUpdateChan Channel,
) *ClipboardMonitor

NewClipboardMonitor creates a new instance of ClipboardMonitor.

func (*ClipboardMonitor) Receive

func (cm *ClipboardMonitor) Receive()

Receive starts monitoring the clipboard and subsequently sending data to other nodes

type Handler

type Handler interface {
	Receive()
}

type Node

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

func NewNode

func NewNode(opts NodeOptions) *Node

NewNode creates a new instance of Node with the specified settings.

func (*Node) Broadcast

func (n *Node) Broadcast(msg *gen.Message, ignore UniqueID)

Broadcast sends a message to all connected nodes except those specified in the 'ignore' list. It first checks if the message is a duplicate of the last sent message by comparing their IDs and hashes. If the message is a duplicate, it is not sent. For each connection in the storage, it writes the message to the connection's writer. The method logs the sent messages and their hashes for debugging purposes. The 'msg' parameter is the message to be broadcast. The 'ignore' parameter is a variadic list of AddrPort to exclude from the broadcast.

func (*Node) ConnectTo

func (n *Node) ConnectTo(addr string) error

ConnectTo establishes a TCP connection to a remote clipboard at the specified address. It adds the connection to the node's storage and starts handling the connection using 'handleConnection'. The 'addr' parameter should be in the format "host:port" to specify the remote clipboard's address. If the connection is successfully established, it returns nil; otherwise, it returns an error.

func (*Node) EnableNodeDiscover

func (n *Node) EnableNodeDiscover()

EnableNodeDiscover enables node discovery. It creates a new peer discovery instance with the specified settings, including payload, discovery limits, time limits, delay, and whether to allow self-discovery. When a new node is discovered, it checks if the node already exists in the storage. If the node is not in the storage, it connects to the discovered node. If the connection attempt fails, an error is logged. If an error occurs while creating the peer discovery instance, the program exits with a fatal error message.

func (*Node) Start

func (n *Node) Start() error

Start starts the node by listening for incoming connections on the specified public port. It also starts a clipboard monitor to periodically scan and update the local clipboard. When a new connection is accepted, it invokes the 'handleConnection' method to handle the connection. The 'scanDelay' parameter determines the interval at which the clipboard is scanned and updated. The method returns an error if it fails to start listening.

type NodeOptions

type NodeOptions struct {
	Clipboard          clipboard.Manager
	Storage            *NodeStorage
	Port               int
	DiscoverDelay      time.Duration
	ClipboardChannel   Channel
	BitSize            int
	KeepAliveDelay     time.Duration
	ClipboardScanDelay time.Duration
}

NodeOptions represents options for Node If no options are specified, default values will be used

func (*NodeOptions) Prepare

func (o *NodeOptions) Prepare()

type NodeStorage

type NodeStorage = storage.SyncMap[UniqueID, *Peer]

type Peer

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

func AcquirePeer

func AcquirePeer(
	conn net.Conn,
	addr netip.AddrPort,
	id UniqueID,
	updates Channel,
	cipher *encrypter.Cipher,
) *Peer

func (*Peer) Addr

func (p *Peer) Addr() netip.AddrPort

func (*Peer) Close

func (p *Peer) Close() error

func (*Peer) Conn

func (p *Peer) Conn() net.Conn

func (*Peer) ID

func (p *Peer) ID() UniqueID

func (*Peer) Receive

func (p *Peer) Receive(cm clipboard.Manager)

func (*Peer) Release

func (p *Peer) Release()

func (*Peer) String

func (p *Peer) String() string

func (*Peer) Updates

func (p *Peer) Updates() Channel

type UniqueID

type UniqueID = string

type UpdateChannel

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

UpdateChannel is an implementation of the Channel interface.

func NewChannel

func NewChannel() *UpdateChannel

NewChannel creates a new instance of UpdateChannel and returns it as a Channel interface.

func (*UpdateChannel) Read

func (c *UpdateChannel) Read() <-chan []byte

Get implements the Get method of the Channel interface.

func (*UpdateChannel) Write

func (c *UpdateChannel) Write(b []byte)

Set implements the Set method of the Channel interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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