Documentation
¶
Overview ¶
Package sync provides clipboard synchronization using libp2p
Package sync provides clipboard synchronization using libp2p ¶
Package sync provides clipboard synchronization using libp2p ¶
Package sync provides clipboard synchronization using libp2p ¶
Package sync provides clipboard synchronization using libp2p ¶
Package sync provides clipboard synchronization using libp2p
Index ¶
- Constants
- func ClipboardTypesToContentTypes(types []string) []string
- func ConvertToDiscoveryConfig(cfg *SyncConfig) *discovery.Config
- func ExpandPath(path string) string
- func GetConfigFromGlobal(cfg *config.Config) *types.SyncConfig
- func MapToLibp2pOptions(cfg *SyncConfig, nodeCfg NodeConfig, logger *zap.Logger) ([]libp2p.Option, error)
- func SaveIdentityToString(privKey crypto.PrivKey) (string, error)
- func ValidateSyncConfig(cfg *SyncConfig) error
- type DiscoveryConfig
- type FileInfo
- type InternalPeerInfo
- type Manager
- func (m *Manager) AddPeer(address string) error
- func (m *Manager) DisablePairing()
- func (m *Manager) DisconnectPeer(peerID string) error
- func (m *Manager) EnablePairing(handler types.PairingRequestCallback) (string, error)
- func (m *Manager) GetConfig() *types.SyncConfig
- func (m *Manager) GetConnectedPeers() []types.PeerInfo
- func (m *Manager) GetDiscoveredPeers() []types.PeerInfo
- func (m *Manager) GetPairedDevices() []types.PairedDevice
- func (m *Manager) IsConnected() bool
- func (m *Manager) IsPaired(peerID string) bool
- func (m *Manager) IsPairingEnabled() bool
- func (m *Manager) JoinGroup(group string) error
- func (m *Manager) LeaveGroup(group string) error
- func (m *Manager) ListGroups() ([]string, error)
- func (m *Manager) RemovePairedDevice(peerID string) error
- func (m *Manager) RemovePeer(peerID string) error
- func (m *Manager) RequestPairing(address string) (*types.PairingResponse, error)
- func (m *Manager) SendContent(content *types.ClipboardContent, group string) error
- func (m *Manager) SetContentHandler(handler types.ContentCallback)
- func (m *Manager) Start() error
- func (m *Manager) Stop() error
- type Node
- func (n *Node) AddPeer(pi InternalPeerInfo)
- func (n *Node) AddPeerByAddress(addrStr string) error
- func (n *Node) Close() error
- func (n *Node) DisconnectPeer(peerID string) error
- func (n *Node) GetConfig() *SyncConfig
- func (n *Node) GetConnectedPeers() []InternalPeerInfo
- func (n *Node) GetManualDiscoveryService() (*discovery.ManualDiscovery, error)
- func (n *Node) GetPeers() []InternalPeerInfo
- func (n *Node) Host() host.Host
- func (n *Node) ID() peer.ID
- func (n *Node) JoinTopic(group string) (*pubsub.Topic, *pubsub.Subscription, error)
- func (n *Node) LeaveTopic(group string) error
- func (n *Node) Pairing() *PairingManager
- func (n *Node) Protocols() *ProtocolManager
- func (n *Node) PublishToTopic(group string, data []byte) error
- func (n *Node) RemovePeer(id peer.ID)
- func (n *Node) RemovePeerByID(peerID string) error
- func (n *Node) Start() error
- func (n *Node) Stop() error
- type NodeConfig
- type NodeOption
- type PairedDevice
- type PairingDiscoveryService
- type PairingManager
- func (pm *PairingManager) DisablePairing()
- func (pm *PairingManager) EnablePairing(handler PairingRequestCallback) (string, error)
- func (pm *PairingManager) GetPairedDevices() []PairedDevice
- func (pm *PairingManager) Handle(stream network.Stream)
- func (pm *PairingManager) ID() protocol.ID
- func (pm *PairingManager) IsPaired(peerID string) bool
- func (pm *PairingManager) IsPairingEnabled() bool
- func (pm *PairingManager) RemovePairedDevice(peerID string) error
- func (pm *PairingManager) RequestPairing(address string) (*PairingResponse, error)
- func (pm *PairingManager) Start() error
- func (pm *PairingManager) Stop() error
- func (pm *PairingManager) UpdatePairedDevice(device PairedDevice) error
- type PairingRequest
- type PairingRequestCallback
- type PairingResponse
- type ProtocolConfig
- type ProtocolHandler
- type ProtocolManager
- func (pm *ProtocolManager) AddHandler(handler ProtocolHandler)
- func (pm *ProtocolManager) GetHandler(id protocol.ID) (ProtocolHandler, error)
- func (pm *ProtocolManager) OpenStream(peerID peer.ID, protocolID protocol.ID) (network.Stream, error)
- func (pm *ProtocolManager) Start() error
- func (pm *ProtocolManager) Stop() error
- type SyncConfig
- type SyncError
- type SyncMessage
- type TransferInfo
Constants ¶
const ( // PairingRequestTimeout is the timeout for pairing requests PairingRequestTimeout = 5 * time.Minute // AuthCodeLength is the length of the verification code in bytes AuthCodeLength = 4 )
const ( // Protocol versions ProtocolVersion = "1.0.0" // Base protocol path BaseProtocolPath = "/clipman/" // Full protocol paths PairingProtocolPath = BaseProtocolPath + ProtocolVersion + "/pairing" ClipboardProtocolPath = BaseProtocolPath + ProtocolVersion + "/clipboard" FileProtocolPath = BaseProtocolPath + ProtocolVersion + "/file" )
Protocol paths
Variables ¶
This section is empty.
Functions ¶
func ClipboardTypesToContentTypes ¶
ClipboardTypesToContentTypes converts string type names to ContentType values
func ConvertToDiscoveryConfig ¶
func ConvertToDiscoveryConfig(cfg *SyncConfig) *discovery.Config
GetDiscoveryConfig converts SyncConfig to discovery.Config
func ExpandPath ¶
ExpandPath expands paths like ~ to absolute paths
func GetConfigFromGlobal ¶
func GetConfigFromGlobal(cfg *config.Config) *types.SyncConfig
GetConfigFromGlobal extracts sync configuration from the global config
func MapToLibp2pOptions ¶
func MapToLibp2pOptions(cfg *SyncConfig, nodeCfg NodeConfig, logger *zap.Logger) ([]libp2p.Option, error)
MapToLibp2pOptions converts sync config to libp2p options
func SaveIdentityToString ¶
SaveIdentityToString converts a private key to a storable string format
func ValidateSyncConfig ¶
func ValidateSyncConfig(cfg *SyncConfig) error
ValidateSyncConfig validates the sync configuration
Types ¶
type DiscoveryConfig ¶
type DiscoveryConfig struct {
Method string
EnableMDNS bool
EnableDHT bool
BootstrapPeers []string
}
DiscoveryConfig contains discovery-specific configuration
func GetDiscoveryConfig ¶
func GetDiscoveryConfig(cfg *SyncConfig) DiscoveryConfig
GetDiscoveryConfig extracts discovery-specific configuration
type FileInfo ¶
type FileInfo struct {
Name string // Filename
Size int64 // File size in bytes
ModTime time.Time // Modification time
ContentType string // MIME type
Hash string // File hash for verification
}
FileInfo provides information about a file being transferred
type InternalPeerInfo ¶
type InternalPeerInfo struct {
ID string // Peer ID (string representation of peer.ID)
Name string // Human-readable name
Addrs []string // Multiaddrs for connecting to the peer
Groups []string // Groups the peer is a member of
LastSeen time.Time // When the peer was last seen
Capabilities map[string]string // Peer capabilities
Version string // Software version
DeviceType string // Type of device (desktop, mobile, etc.)
}
InternalPeerInfo contains detailed information about a discovered peer Used internally by the sync package
func FromExternalPeerInfo ¶
func FromExternalPeerInfo(p types.PeerInfo) InternalPeerInfo
FromExternalPeerInfo creates an internal peer info from the external type
func (InternalPeerInfo) ToExternalPeerInfo ¶
func (p InternalPeerInfo) ToExternalPeerInfo() types.PeerInfo
ToExternalPeerInfo converts an internal peer info to the external type
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager implements the types.SyncManager interface It orchestrates the libp2p node, protocols, and discovery
func (*Manager) DisablePairing ¶
func (m *Manager) DisablePairing()
DisablePairing disables pairing mode
func (*Manager) DisconnectPeer ¶
DisconnectPeer disconnects from a peer but keeps it in the peerstore
func (*Manager) EnablePairing ¶
func (m *Manager) EnablePairing(handler types.PairingRequestCallback) (string, error)
EnablePairing enables pairing mode
func (*Manager) GetConfig ¶
func (m *Manager) GetConfig() *types.SyncConfig
GetConfig returns the sync configuration for application use
func (*Manager) GetConnectedPeers ¶
GetConnectedPeers returns a list of currently connected peers
func (*Manager) GetDiscoveredPeers ¶
GetDiscoveredPeers returns the list of discovered peers
func (*Manager) GetPairedDevices ¶
func (m *Manager) GetPairedDevices() []types.PairedDevice
GetPairedDevices returns a list of paired devices
func (*Manager) IsConnected ¶
IsConnected checks if connected to the network
func (*Manager) IsPairingEnabled ¶
IsPairingEnabled checks if pairing mode is enabled
func (*Manager) LeaveGroup ¶
LeaveGroup leaves a group
func (*Manager) ListGroups ¶
ListGroups lists the joined groups
func (*Manager) RemovePairedDevice ¶
RemovePairedDevice removes a paired device
func (*Manager) RemovePeer ¶
RemovePeer removes a peer by its ID
func (*Manager) RequestPairing ¶
func (m *Manager) RequestPairing(address string) (*types.PairingResponse, error)
RequestPairing requests pairing with a device at the given address
func (*Manager) SendContent ¶
func (m *Manager) SendContent(content *types.ClipboardContent, group string) error
SendContent sends content to a group
func (*Manager) SetContentHandler ¶
func (m *Manager) SetContentHandler(handler types.ContentCallback)
SetContentHandler sets the handler for incoming content
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node manages the libp2p host and core networking functionality
func NewNode ¶
func NewNode(ctx context.Context, cfg *config.Config, logger *zap.Logger, opts ...NodeOption) (*Node, error)
NewNode creates a new libp2p node using the provided configuration
func (*Node) AddPeer ¶
func (n *Node) AddPeer(pi InternalPeerInfo)
AddPeer adds a peer to the internal peer store
func (*Node) AddPeerByAddress ¶
AddPeerByAddress adds a peer by its multiaddress string
func (*Node) DisconnectPeer ¶
DisconnectPeer disconnects from a peer but keeps it in the peerstore
func (*Node) GetConfig ¶
func (n *Node) GetConfig() *SyncConfig
GetConfig returns the node's configuration
func (*Node) GetConnectedPeers ¶
func (n *Node) GetConnectedPeers() []InternalPeerInfo
GetConnectedPeers returns a list of connected peers
func (*Node) GetManualDiscoveryService ¶
func (n *Node) GetManualDiscoveryService() (*discovery.ManualDiscovery, error)
GetManualDiscoveryService returns the manual discovery service
func (*Node) GetPeers ¶
func (n *Node) GetPeers() []InternalPeerInfo
GetPeers returns all peers in the internal store
func (*Node) LeaveTopic ¶
LeaveTopic leaves a pubsub topic for a group
func (*Node) Protocols ¶
func (n *Node) Protocols() *ProtocolManager
Protocols returns the protocol manager
func (*Node) PublishToTopic ¶
PublishToTopic publishes data to a topic
func (*Node) RemovePeer ¶
RemovePeer removes a peer from the internal store
func (*Node) RemovePeerByID ¶
RemovePeerByID removes a peer by its ID string
type NodeConfig ¶
type NodeConfig struct {
ListenAddresses []string
EnableNAT bool
EnableRelay bool
PeerIdentity string
}
NodeConfig contains configuration specific to the libp2p node
func GetNodeConfig ¶
func GetNodeConfig(cfg *SyncConfig) NodeConfig
GetNodeConfig extracts node-specific configuration
type NodeOption ¶
NodeOption is a functional option for configuring the Node
type PairedDevice ¶
type PairedDevice struct {
PeerID string `json:"peer_id"` // Peer ID of the paired device
DeviceName string `json:"device_name"` // Human-readable name
DeviceType string `json:"device_type"` // Type of device (desktop, mobile, etc.)
LastSeen time.Time `json:"last_seen"` // When this device was last seen
PairedAt time.Time `json:"paired_at"` // When the pairing was established
Addresses []string `json:"addresses"` // Known addresses for this device
Metadata map[string]string `json:"metadata"` // Additional device metadata
Capabilities []string `json:"capabilities"` // Device capabilities
}
PairedDevice represents a device that has been paired
type PairingDiscoveryService ¶
type PairingDiscoveryService struct {
// contains filtered or unexported fields
}
CreatePairingDiscoveryService creates a discovery service for paired devices
func NewPairingDiscoveryService ¶
func NewPairingDiscoveryService(manager *PairingManager, host host.Host) *PairingDiscoveryService
NewPairingDiscoveryService creates a new pairing discovery service
func (*PairingDiscoveryService) Name ¶
func (p *PairingDiscoveryService) Name() string
Name returns the name of the discovery service
func (*PairingDiscoveryService) Start ¶
func (p *PairingDiscoveryService) Start() error
Start starts the pairing discovery service
func (*PairingDiscoveryService) Stop ¶
func (p *PairingDiscoveryService) Stop() error
Stop stops the pairing discovery service
type PairingManager ¶
type PairingManager struct {
// contains filtered or unexported fields
}
PairingManager implements the pairing protocol
func NewPairingManager ¶
func NewPairingManager(ctx context.Context, host host.Host, pm *ProtocolManager, config *SyncConfig, logger *zap.Logger) *PairingManager
NewPairingManager creates a new pairing manager
func (*PairingManager) DisablePairing ¶
func (pm *PairingManager) DisablePairing()
DisablePairing disables pairing mode
func (*PairingManager) EnablePairing ¶
func (pm *PairingManager) EnablePairing(handler PairingRequestCallback) (string, error)
EnablePairing enables pairing mode for the device
func (*PairingManager) GetPairedDevices ¶
func (pm *PairingManager) GetPairedDevices() []PairedDevice
GetPairedDevices returns all paired devices
func (*PairingManager) Handle ¶
func (pm *PairingManager) Handle(stream network.Stream)
Handle processes incoming pairing streams
func (*PairingManager) IsPaired ¶
func (pm *PairingManager) IsPaired(peerID string) bool
IsPaired checks if a device is paired
func (*PairingManager) IsPairingEnabled ¶
func (pm *PairingManager) IsPairingEnabled() bool
IsPairingEnabled returns whether pairing is enabled
func (*PairingManager) RemovePairedDevice ¶
func (pm *PairingManager) RemovePairedDevice(peerID string) error
RemovePairedDevice removes a paired device
func (*PairingManager) RequestPairing ¶
func (pm *PairingManager) RequestPairing(address string) (*PairingResponse, error)
RequestPairing sends a pairing request to a device
func (*PairingManager) Start ¶
func (pm *PairingManager) Start() error
Start initializes the pairing manager
func (*PairingManager) Stop ¶
func (pm *PairingManager) Stop() error
Stop shuts down the pairing manager
func (*PairingManager) UpdatePairedDevice ¶
func (pm *PairingManager) UpdatePairedDevice(device PairedDevice) error
UpdatePairedDevice updates a paired device's information
type PairingRequest ¶
type PairingRequest struct {
DeviceName string `json:"device_name"`
DeviceType string `json:"device_type"`
Timestamp time.Time `json:"timestamp"`
RandomData string `json:"random_data"` // Used for verification code generation
PeerID string `json:"peer_id"` // Requester's peer ID
Nonce string `json:"nonce"` // Random nonce for this request
Metadata map[string]string `json:"metadata"` // Additional device metadata
}
PairingRequest contains information about a device requesting pairing
type PairingRequestCallback ¶
type PairingRequestCallback func(request PairingRequest, remotePeerId string) (bool, error)
PairingRequestCallback is the callback for handling pairing requests
type PairingResponse ¶
type PairingResponse struct {
Accepted bool `json:"accepted"` // Whether the pairing was accepted
ErrorMessage string `json:"error_message"` // Error message if rejected
PairingCode string `json:"pairing_code"` // Verification code
DeviceName string `json:"device_name"` // Responder's device name
DeviceType string `json:"device_type"` // Responder's device type
Timestamp time.Time `json:"timestamp"` // Time of response
RandomData string `json:"random_data"` // Used for verification code generation
PeerID string `json:"peer_id"` // Responder's peer ID
Metadata map[string]string `json:"metadata"` // Additional device metadata
ValidUntil time.Time `json:"valid_until"` // When this pairing expires (0 = never)
}
PairingResponse is the response to a pairing request
type ProtocolConfig ¶
ProtocolConfig contains protocol-specific configuration
func GetProtocolConfig ¶
func GetProtocolConfig(cfg *SyncConfig) ProtocolConfig
GetProtocolConfig extracts protocol-specific configuration
type ProtocolHandler ¶
type ProtocolHandler interface {
// ID returns the protocol ID
ID() protocol.ID
// Handle processes an incoming stream
Handle(stream network.Stream)
// Start initializes and starts the protocol handler
Start() error
// Stop stops the protocol handler
Stop() error
}
ProtocolHandler defines the interface for protocol-specific handlers
type ProtocolManager ¶
type ProtocolManager struct {
// contains filtered or unexported fields
}
ProtocolManager manages the various protocols used for peer communication
func NewProtocolManager ¶
func NewProtocolManager(ctx context.Context, host host.Host, config *SyncConfig, logger *zap.Logger) *ProtocolManager
NewProtocolManager creates a new protocol manager
func (*ProtocolManager) AddHandler ¶
func (pm *ProtocolManager) AddHandler(handler ProtocolHandler)
AddHandler adds a protocol handler
func (*ProtocolManager) GetHandler ¶
func (pm *ProtocolManager) GetHandler(id protocol.ID) (ProtocolHandler, error)
GetHandler returns the handler for a specific protocol
func (*ProtocolManager) OpenStream ¶
func (pm *ProtocolManager) OpenStream(peerID peer.ID, protocolID protocol.ID) (network.Stream, error)
OpenStream opens a stream to a peer using a specific protocol
func (*ProtocolManager) Start ¶
func (pm *ProtocolManager) Start() error
Start starts all protocol handlers
func (*ProtocolManager) Stop ¶
func (pm *ProtocolManager) Stop() error
Stop stops all protocol handlers
type SyncConfig ¶
type SyncConfig struct {
// Core Sync Settings
Enabled bool `json:"enable_sync"`
SyncOverInternet bool `json:"sync_over_internet"`
UseRelayNodes bool `json:"use_relay_nodes"`
ListenPort int `json:"listen_port"`
PeerIdentity string `json:"peer_identity"`
DiscoveryMethod string `json:"discovery_method"` // "mdns", "dht", or "manual"
DHTBootstrapPeers []string `json:"dht_bootstrap_peers"` // Bootstrap peers for DHT
BootstrapPeers []string `json:"bootstrap_peers"` // Bootstrap peers for the network
// Peer Persistence
PersistDiscoveredPeers bool `json:"persist_discovered_peers"` // Whether to save discovered peers to disk
DiscoveredPeersPath string `json:"discovered_peers_path"` // Path to store discovered peers
AutoReconnectToPeers bool `json:"auto_reconnect_to_peers"` // Whether to auto-reconnect to known peers
MaxStoredPeers int `json:"max_stored_peers"` // Maximum number of peers to store
// Clipboard Sync Options
ClipboardTypes []string `json:"clipboard_types"` // "text", "image", "files"
AutoCopyFromPeers bool `json:"auto_copy_from_peers"`
MaxClipboardSizeKB int `json:"max_clipboard_size_kb"`
ClipboardHistorySize int `json:"clipboard_history_size"`
ClipboardBlacklistApps []string `json:"clipboard_blacklist_apps"`
// File Transfer Options
EnableFileSharing bool `json:"enable_file_sharing"`
RequireFileConfirmation bool `json:"require_file_confirmation"`
DefaultDownloadFolder string `json:"default_download_folder"`
AutoAcceptFromPeers []string `json:"auto_accept_from_peers"`
MaxFileSizeMB int `json:"max_file_size_mb"`
// Privacy & Security
AllowOnlyKnownPeers bool `json:"allow_only_known_peers"`
TrustedPeers []string `json:"trusted_peers"`
RequireApprovalPin bool `json:"require_approval_pin"`
LogPeerActivity bool `json:"log_peer_activity"`
// Developer & Debug Options
DebugLogging bool `json:"debug_logging"`
ShowPeerDebugInfo bool `json:"show_peer_debug_info"`
DisableMultiplexing bool `json:"disable_multiplexing"`
ForceDirectConnectionOnly bool `json:"force_direct_connection_only"`
// DHT Discovery Options
DHTPersistentStorage bool `json:"dht_persistent_storage"` // Whether to store DHT data on disk
DHTStoragePath string `json:"dht_storage_path"` // Path to store DHT data
DHTServerMode bool `json:"dht_server_mode"` // Whether to run DHT in server mode
UseAsRelay bool `json:"use_as_relay"` // Whether to act as a relay for other peers
PersistDHTData bool `json:"persist_dht_data"` // Whether to persist DHT data
// Pairing Options
PairingEnabled bool `json:"pairing_enabled"`
DeviceName string `json:"device_name"`
DeviceType string `json:"device_type"`
PairingTimeout int `json:"pairing_timeout"`
}
SyncConfig holds all sync-related configuration
func LoadSyncConfig ¶
func LoadSyncConfig(cfg *config.Config) *SyncConfig
LoadSyncConfig extracts sync config from the global config
type SyncError ¶
type SyncError struct {
Code int // Error code
Message string // Error message
Err error // Original error
}
SyncError wraps errors from the sync package
func NewSyncError ¶
NewSyncError creates a new SyncError
type SyncMessage ¶
type SyncMessage struct {
Type string // Message type (content, control, etc.)
Source peer.ID // Source peer ID
Destination peer.ID // Destination peer ID (empty for broadcast)
Group string // Target group (if applicable)
Timestamp time.Time // Message timestamp
ID string // Unique message ID
Payload []byte // Message payload
Headers map[string]string // Message headers/metadata
}
SyncMessage represents a message exchanged between peers
type TransferInfo ¶
type TransferInfo struct {
ID string // Unique transfer ID
FileName string // Name of the file
FileSize int64 // Size of the file in bytes
Sender string // Sender peer ID
Recipient string // Recipient peer ID
Group string // Group context (if applicable)
StartTime time.Time // When the transfer started
IsIncoming bool // Whether this is an incoming or outgoing transfer
Status string // "pending", "in_progress", "completed", "failed", "cancelled"
}
TransferInfo provides information about a file transfer