diff options
| author | gabrix73 <gabriel1@frozenstar.info> | 2026-04-10 16:10:48 +0200 |
|---|---|---|
| committer | gabrix73 <gabriel1@frozenstar.info> | 2026-04-10 16:10:48 +0200 |
| commit | 88375da30ea667ec25c2000874557fcc4785a558 (patch) | |
| tree | 71364a43230977481c6d3280246aa8bb48562bdc /transport/transport.go | |
| download | khimera-88375da30ea667ec25c2000874557fcc4785a558.tar.gz khimera-88375da30ea667ec25c2000874557fcc4785a558.tar.xz khimera-88375da30ea667ec25c2000874557fcc4785a558.zip | |
Initial commit: Veilith v0.9 - P2P Encrypted Messenger
Military-grade secure messenger with:
- End-to-End Encryption (Noise Protocol XX)
- Forward Secrecy (automatic session rotation)
- Anti-Traffic Analysis (padding + cover traffic)
- Secure Deletion (DOD 5220.22-M)
- Tor Integration (embedded with bridge support)
- Portable Mode (no installation required)
Generated with Claude Code
Diffstat (limited to 'transport/transport.go')
| -rw-r--r-- | transport/transport.go | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/transport/transport.go b/transport/transport.go new file mode 100644 index 0000000..68fadea --- /dev/null +++ b/transport/transport.go @@ -0,0 +1,163 @@ +// Package transport provides an abstraction layer for different network transports +package transport + +import ( + "fmt" + "net" + "sync" +) + +// TransportType represents the type of network transport +type TransportType int + +const ( + TransportTor TransportType = iota + TransportMesh + TransportDirect +) + +// String returns string representation of transport type +func (t TransportType) String() string { + switch t { + case TransportTor: + return "Tor" + case TransportMesh: + return "Mesh" + case TransportDirect: + return "Direct" + default: + return "Unknown" + } +} + +// Transport defines the interface for network transports +type Transport interface { + // Connect establishes a connection to a remote peer + Connect(address string) (net.Conn, error) + + // Listen starts listening for incoming connections + Listen(address string) (net.Listener, error) + + // Type returns the transport type + Type() TransportType + + // Close closes the transport + Close() error +} + +// TransportManager manages multiple transports +type TransportManager struct { + transports map[TransportType]Transport + mu sync.RWMutex + closed bool +} + +// NewTransportManager creates a new transport manager +func NewTransportManager() *TransportManager { + return &TransportManager{ + transports: make(map[TransportType]Transport), + } +} + +// RegisterTransport registers a new transport +func (tm *TransportManager) RegisterTransport(transport Transport) error { + tm.mu.Lock() + defer tm.mu.Unlock() + + if tm.closed { + return fmt.Errorf("transport manager is closed") + } + + transportType := transport.Type() + if _, exists := tm.transports[transportType]; exists { + return fmt.Errorf("transport %s already registered", transportType) + } + + tm.transports[transportType] = transport + return nil +} + +// GetTransport retrieves a transport by type +func (tm *TransportManager) GetTransport(transportType TransportType) (Transport, error) { + tm.mu.RLock() + defer tm.mu.RUnlock() + + if tm.closed { + return nil, fmt.Errorf("transport manager is closed") + } + + transport, exists := tm.transports[transportType] + if !exists { + return nil, fmt.Errorf("transport %s not registered", transportType) + } + + return transport, nil +} + +// Connect connects using the specified transport +func (tm *TransportManager) Connect(transportType TransportType, address string) (net.Conn, error) { + transport, err := tm.GetTransport(transportType) + if err != nil { + return nil, err + } + + return transport.Connect(address) +} + +// Listen starts listening on the specified transport +func (tm *TransportManager) Listen(transportType TransportType, address string) (net.Listener, error) { + transport, err := tm.GetTransport(transportType) + if err != nil { + return nil, err + } + + return transport.Listen(address) +} + +// ListTransports returns all registered transport types +func (tm *TransportManager) ListTransports() []TransportType { + tm.mu.RLock() + defer tm.mu.RUnlock() + + types := make([]TransportType, 0, len(tm.transports)) + for transportType := range tm.transports { + types = append(types, transportType) + } + return types +} + +// Close closes all transports +func (tm *TransportManager) Close() error { + tm.mu.Lock() + defer tm.mu.Unlock() + + if tm.closed { + return nil + } + + var lastErr error + for transportType, transport := range tm.transports { + if err := transport.Close(); err != nil { + lastErr = fmt.Errorf("failed to close %s transport: %w", transportType, err) + } + } + + tm.transports = make(map[TransportType]Transport) + tm.closed = true + + return lastErr +} + +// Count returns the number of registered transports +func (tm *TransportManager) Count() int { + tm.mu.RLock() + defer tm.mu.RUnlock() + return len(tm.transports) +} + +// String returns a string representation +func (tm *TransportManager) String() string { + tm.mu.RLock() + defer tm.mu.RUnlock() + return fmt.Sprintf("TransportManager(transports=%d, closed=%v)", len(tm.transports), tm.closed) +} |
