mirror of
https://github.com/samsonjs/vibetunnel.git
synced 2026-03-29 10:05:53 +00:00
* Add Linux implementation of VibeTunnel This commit introduces a complete Linux port of VibeTunnel, providing feature parity with the macOS version. The implementation includes: - Full Go-based server with identical REST API and WebSocket endpoints - Terminal session management using PTY (pseudo-terminal) handling - Asciinema recording format for session playback - Compatible CLI interface matching the macOS `vt` command - Support for all VibeTunnel features: password protection, network modes, ngrok integration - Comprehensive build system with Makefile supporting various installation methods - Systemd service integration for running as a system daemon The Linux version maintains 100% compatibility with the existing web UI and can be used as a drop-in replacement for the macOS app on Linux systems. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Add comprehensive ngrok integration to Linux VibeTunnel Implements full ngrok tunnel support for the Go/Linux version to match the macOS Swift implementation, enabling secure public access to local VibeTunnel instances. - **ngrok Service**: Complete lifecycle management with status tracking - **HTTP API**: RESTful endpoints matching macOS version - **CLI Support**: Command-line ngrok flags and integration - **Auto-forwarding**: Built-in HTTP request forwarding to local server - `POST /api/ngrok/start` - Start tunnel with auth token - `POST /api/ngrok/stop` - Stop active tunnel - `GET /api/ngrok/status` - Get current tunnel status - Uses `golang.ngrok.com/ngrok` SDK for native Go integration - Thread-safe service with mutex protection - Comprehensive error handling and logging - Real-time status updates (disconnected/connecting/connected/error) - Proper context cancellation for graceful shutdown ```bash vibetunnel --serve --ngrok --ngrok-token "your_token" vibetunnel --serve --port 4030 --ngrok --ngrok-token "your_token" ``` - Added golang.ngrok.com/ngrok v1.13.0 - Updated web packages (security fixes for puppeteer) Maintains full API compatibility with macOS VibeTunnel for seamless cross-platform operation and consistent web frontend integration. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * up * Fix SSE streaming performance with byte-based approach Addresses @badlogic's review feedback to prevent performance issues with line-based file reading in processNewContent(). ## Changes Made ### Performance Fix - **Byte-based seeking**: Replace line counting with file position tracking - **Efficient reads**: Only read new content since last position using file.Seek() - **Memory optimization**: Avoid reading entire file on each update - **Incomplete line handling**: Properly handle partial lines at file end ### Technical Details - Changed lastLineCount *int → seenBytes *int64 - Use file.Seek(seenBytes, 0) to jump to last read position - Read only new content with currentSize - seenBytes - Handle incomplete lines by adjusting seek position - Maintains same functionality with better performance ### Benefits - **Scalability**: No longer reads entire file for each update - **Performance**: O(new_content) instead of O(total_content) - **Memory**: Constant memory usage regardless of file size - **Reliability**: Handles concurrent writes and partial lines correctly This prevents the SSE streaming from exploding in our faces as @badlogic warned, especially for long-running sessions with large output files. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Optimize streaming performance to reduce 1+ second delays Implements multiple optimizations to address user-reported 1+ second delay between typing and stream rendering: ## PTY Reading Optimizations - **Reduced sleep times**: 100ms → 1ms for EOF checks - **Faster polling**: 10ms → 1ms for zero-byte reads - **FIFO optimization**: 1s → 100ms for stdin EOF polling ## UTF-8 Buffering Improvements - **Timeout-based flushing**: 5ms timer for incomplete UTF-8 sequences - **Real-time streaming**: Don't wait for complete sequences in interactive mode - **Smart buffering**: Balance correctness with responsiveness ## File I/O Optimizations - **Immediate sync**: Call file.Sync() after each write for instant fsnotify - **Reduced SSE timeout**: 1s → 100ms for session alive checks - **Better responsiveness**: Ensure file changes trigger immediately ## Technical Changes - Added StreamWriter.scheduleFlush() with 5ms timeout - Enhanced writeEvent() with conditional file syncing - Optimized PTY read/write loop timing - Improved SSE streaming frequency These changes target the main bottlenecks identified in the PTY → file → fsnotify → SSE → browser pipeline. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix critical stdin polling delay causing 1+ second input lag - Reduced FIFO EOF polling from 100ms to 1ms - Reduced EAGAIN polling from 1ms to 100µs - Added immediate continue after successful writes - This eliminates the major input delay bottleneck 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix critical performance issues causing resource leaks and CPU burns Performance optimizations based on code review feedback: 1. **Fix SSE goroutine leaks**: - Added client disconnect detection to SSE streams - Propagate write errors to detect when clients close connections - Prevents memory leaks from abandoned streaming goroutines 2. **Fix PTY busy-loop CPU burn**: - Increased sleep from 1ms to 10ms in idle scenarios - Reduces CPU wake-ups from 1000/s to 100/s (10x improvement) - Significantly reduces CPU usage when PTY is idle 3. **Multi-stream disconnect detection**: - Added error checking to multi-stream write operations - Prevents goroutine leaks in multi-session streaming These fixes address the "thing of the things" - performance\! 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Standardize session creation API response format to match Rust server Changes: - Updated Go server session creation response to include success/message/error fields - Now returns: {"success": true, "message": "Session created successfully", "error": null, "sessionId": "..."} - Maintains backward compatibility with existing sessionId field - Go server already supported both input formats (cmdline/command, cwd/workingDir) This achieves protocol compatibility between Go and Rust implementations. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix delete endpoint to return 200 OK with JSON response - Changed handleKillSession to return 200 OK instead of 204 No Content - Added JSON response with success/message fields for consistency - Fixes benchmark tool compatibility expecting 200 response 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Update Go server API to match Rust format exactly - Use 'command' array instead of 'cmdline' - Use 'workingDir' instead of 'cwd' - Remove compatibility shims for cleaner API - Better error messages matching Rust server 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Major performance optimizations for Go server - Remove 100ms artificial delay in session creation (-100ms per session) - Optimize PTY I/O handling with reduced polling intervals - Implement persistent stdin pipes to avoid repeated open/close - Batch file sync operations to reduce I/O overhead (5ms batching) - Remove blocking status updates from API handlers - Increase SSE session check interval from 100ms to 1s Target: Match Rust performance (60ms avg latency, 16+ ops/sec) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix O_NONBLOCK compilation issue * Add comprehensive TLS/HTTPS support with Caddy integration Features: - Optional TLS support via CLI flags (defaults to HTTP like Rust) - Self-signed certificate generation for localhost development - Let's Encrypt automatic certificate management for domains - Custom certificate support for production environments - HTTP to HTTPS redirect capability - Maintains 100% backward compatibility with Rust version Usage examples: - Default HTTP: ./vibetunnel --serve (same as Rust) - HTTPS with self-signed: ./vibetunnel --serve --tls - HTTPS with domain: ./vibetunnel --serve --tls --tls-domain example.com - HTTPS with custom certs: ./vibetunnel --serve --tls --tls-cert cert.pem --tls-key key.pem 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Fix terminal sizing issues and implement dynamic resize support Backend changes: - Add handleResizeSession API endpoint for dynamic terminal resizing - Implement Session.Resize() and PTY.Resize() methods with proper validation - Add session registry in Manager to track running sessions with PTY access - Fix stdin error handling to prevent session crashes on EAGAIN errors - Write resize events to asciinema stream for frontend synchronization - Update default terminal dimensions from 80x24 to 120x30 Frontend changes: - Add width/height parameters to SessionCreateData interface - Calculate appropriate terminal dimensions when creating sessions - Implement automatic resize API calls when terminal dimensions change - Add terminal-resize event dispatch for backend synchronization - Ensure resize events bubble properly for session management Fixes nvim being stuck at 80x24 by implementing proper terminal dimension management and dynamic resizing capabilities. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Add client-side resize caching and Hack Nerd Font support - Implement resize request caching to prevent redundant API calls - Add debouncing to terminal resize events (250ms delay) - Replace ResizeObserver with window.resize events only to eliminate pixel-level jitter - Add Hack Nerd Font Mono as primary terminal font with Fira Code fallback - Update session creation to use conservative 120x30 defaults - Fix terminal dimension calculation in normal mode 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * Add comprehensive XTerm color and rendering enhancements - Complete 256-color palette support with CSS variables (0-255) - Enhanced XTerm configuration with proper terminal options - True xterm-compatible 16-color theme - Text attribute support: bold, italic, underline, dim, strikethrough, inverse, invisible - Cursor blinking with CSS animation - Font rendering optimizations (disabled ligatures, antialiasing) - Terminal-specific CSS styling for better rendering - Mac option key as meta, alt-click cursor movement - Selection colors and inactive selection support 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> --------- Co-authored-by: Claude <noreply@anthropic.com>
326 lines
No EOL
6.7 KiB
Go
326 lines
No EOL
6.7 KiB
Go
package protocol
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
type AsciinemaHeader struct {
|
|
Version uint32 `json:"version"`
|
|
Width uint32 `json:"width"`
|
|
Height uint32 `json:"height"`
|
|
Timestamp int64 `json:"timestamp,omitempty"`
|
|
Command string `json:"command,omitempty"`
|
|
Title string `json:"title,omitempty"`
|
|
Env map[string]string `json:"env,omitempty"`
|
|
}
|
|
|
|
type EventType string
|
|
|
|
const (
|
|
EventOutput EventType = "o"
|
|
EventInput EventType = "i"
|
|
EventResize EventType = "r"
|
|
EventMarker EventType = "m"
|
|
)
|
|
|
|
type AsciinemaEvent struct {
|
|
Time float64 `json:"time"`
|
|
Type EventType `json:"type"`
|
|
Data string `json:"data"`
|
|
}
|
|
|
|
type StreamEvent struct {
|
|
Type string `json:"type"`
|
|
Header *AsciinemaHeader `json:"header,omitempty"`
|
|
Event *AsciinemaEvent `json:"event,omitempty"`
|
|
Message string `json:"message,omitempty"`
|
|
}
|
|
|
|
type StreamWriter struct {
|
|
writer io.Writer
|
|
header *AsciinemaHeader
|
|
startTime time.Time
|
|
mutex sync.Mutex
|
|
closed bool
|
|
buffer []byte
|
|
lastWrite time.Time
|
|
flushTimer *time.Timer
|
|
syncTimer *time.Timer
|
|
needsSync bool
|
|
}
|
|
|
|
func NewStreamWriter(writer io.Writer, header *AsciinemaHeader) *StreamWriter {
|
|
return &StreamWriter{
|
|
writer: writer,
|
|
header: header,
|
|
startTime: time.Now(),
|
|
buffer: make([]byte, 0, 4096),
|
|
lastWrite: time.Now(),
|
|
}
|
|
}
|
|
|
|
func (w *StreamWriter) WriteHeader() error {
|
|
w.mutex.Lock()
|
|
defer w.mutex.Unlock()
|
|
|
|
if w.closed {
|
|
return fmt.Errorf("stream writer closed")
|
|
}
|
|
|
|
if w.header.Timestamp == 0 {
|
|
w.header.Timestamp = w.startTime.Unix()
|
|
}
|
|
|
|
data, err := json.Marshal(w.header)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = fmt.Fprintf(w.writer, "%s\n", data)
|
|
return err
|
|
}
|
|
|
|
func (w *StreamWriter) WriteOutput(data []byte) error {
|
|
return w.writeEvent(EventOutput, data)
|
|
}
|
|
|
|
func (w *StreamWriter) WriteInput(data []byte) error {
|
|
return w.writeEvent(EventInput, data)
|
|
}
|
|
|
|
func (w *StreamWriter) WriteResize(width, height uint32) error {
|
|
data := fmt.Sprintf("%dx%d", width, height)
|
|
return w.writeEvent(EventResize, []byte(data))
|
|
}
|
|
|
|
func (w *StreamWriter) writeEvent(eventType EventType, data []byte) error {
|
|
w.mutex.Lock()
|
|
defer w.mutex.Unlock()
|
|
|
|
if w.closed {
|
|
return fmt.Errorf("stream writer closed")
|
|
}
|
|
|
|
w.buffer = append(w.buffer, data...)
|
|
w.lastWrite = time.Now()
|
|
|
|
completeData, remaining := extractCompleteUTF8(w.buffer)
|
|
w.buffer = remaining
|
|
|
|
if len(completeData) == 0 {
|
|
// If we have incomplete UTF-8 data, set up a timer to flush it after a short delay
|
|
if len(w.buffer) > 0 {
|
|
w.scheduleFlush()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
elapsed := time.Since(w.startTime).Seconds()
|
|
event := []interface{}{elapsed, string(eventType), string(completeData)}
|
|
|
|
eventData, err := json.Marshal(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = fmt.Fprintf(w.writer, "%s\n", eventData)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Schedule sync instead of immediate sync for better performance
|
|
w.scheduleBatchSync()
|
|
|
|
return nil
|
|
}
|
|
|
|
// scheduleFlush sets up a timer to flush incomplete UTF-8 data after a short delay
|
|
func (w *StreamWriter) scheduleFlush() {
|
|
// Cancel existing timer if any
|
|
if w.flushTimer != nil {
|
|
w.flushTimer.Stop()
|
|
}
|
|
|
|
// Set up new timer for 5ms flush delay
|
|
w.flushTimer = time.AfterFunc(5*time.Millisecond, func() {
|
|
w.mutex.Lock()
|
|
defer w.mutex.Unlock()
|
|
|
|
if w.closed || len(w.buffer) == 0 {
|
|
return
|
|
}
|
|
|
|
// Force flush incomplete UTF-8 data for real-time streaming
|
|
elapsed := time.Since(w.startTime).Seconds()
|
|
event := []interface{}{elapsed, string(EventOutput), string(w.buffer)}
|
|
|
|
eventData, err := json.Marshal(event)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
fmt.Fprintf(w.writer, "%s\n", eventData)
|
|
|
|
// Schedule sync instead of immediate sync for better performance
|
|
w.scheduleBatchSync()
|
|
|
|
// Clear buffer after flushing
|
|
w.buffer = w.buffer[:0]
|
|
})
|
|
}
|
|
|
|
// scheduleBatchSync batches sync operations to reduce I/O overhead
|
|
func (w *StreamWriter) scheduleBatchSync() {
|
|
w.needsSync = true
|
|
|
|
// Cancel existing sync timer if any
|
|
if w.syncTimer != nil {
|
|
w.syncTimer.Stop()
|
|
}
|
|
|
|
// Schedule sync after 5ms to batch multiple writes
|
|
w.syncTimer = time.AfterFunc(5*time.Millisecond, func() {
|
|
if w.needsSync {
|
|
if file, ok := w.writer.(*os.File); ok {
|
|
file.Sync()
|
|
}
|
|
w.needsSync = false
|
|
}
|
|
})
|
|
}
|
|
|
|
func (w *StreamWriter) Close() error {
|
|
w.mutex.Lock()
|
|
defer w.mutex.Unlock()
|
|
|
|
if w.closed {
|
|
return nil
|
|
}
|
|
|
|
// Cancel timers
|
|
if w.flushTimer != nil {
|
|
w.flushTimer.Stop()
|
|
}
|
|
if w.syncTimer != nil {
|
|
w.syncTimer.Stop()
|
|
}
|
|
|
|
if len(w.buffer) > 0 {
|
|
elapsed := time.Since(w.startTime).Seconds()
|
|
event := []interface{}{elapsed, string(EventOutput), string(w.buffer)}
|
|
eventData, _ := json.Marshal(event)
|
|
fmt.Fprintf(w.writer, "%s\n", eventData)
|
|
}
|
|
|
|
w.closed = true
|
|
if closer, ok := w.writer.(io.Closer); ok {
|
|
return closer.Close()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func extractCompleteUTF8(data []byte) (complete, remaining []byte) {
|
|
if len(data) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
lastValid := len(data)
|
|
for i := len(data) - 1; i >= 0 && i >= len(data)-4; i-- {
|
|
if data[i]&0x80 == 0 {
|
|
break
|
|
}
|
|
if data[i]&0xC0 == 0xC0 {
|
|
expectedLen := 1
|
|
if data[i]&0xE0 == 0xC0 {
|
|
expectedLen = 2
|
|
} else if data[i]&0xF0 == 0xE0 {
|
|
expectedLen = 3
|
|
} else if data[i]&0xF8 == 0xF0 {
|
|
expectedLen = 4
|
|
}
|
|
|
|
if i+expectedLen > len(data) {
|
|
lastValid = i
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
return data[:lastValid], data[lastValid:]
|
|
}
|
|
|
|
type StreamReader struct {
|
|
reader io.Reader
|
|
decoder *json.Decoder
|
|
header *AsciinemaHeader
|
|
headerRead bool
|
|
}
|
|
|
|
func NewStreamReader(reader io.Reader) *StreamReader {
|
|
return &StreamReader{
|
|
reader: reader,
|
|
decoder: json.NewDecoder(reader),
|
|
}
|
|
}
|
|
|
|
func (r *StreamReader) Next() (*StreamEvent, error) {
|
|
if !r.headerRead {
|
|
var header AsciinemaHeader
|
|
if err := r.decoder.Decode(&header); err != nil {
|
|
return nil, err
|
|
}
|
|
r.header = &header
|
|
r.headerRead = true
|
|
return &StreamEvent{
|
|
Type: "header",
|
|
Header: &header,
|
|
}, nil
|
|
}
|
|
|
|
var raw json.RawMessage
|
|
if err := r.decoder.Decode(&raw); err != nil {
|
|
if err == io.EOF {
|
|
return &StreamEvent{Type: "end"}, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
var array []interface{}
|
|
if err := json.Unmarshal(raw, &array); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if len(array) != 3 {
|
|
return nil, fmt.Errorf("invalid event format")
|
|
}
|
|
|
|
timestamp, ok := array[0].(float64)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid timestamp")
|
|
}
|
|
|
|
eventType, ok := array[1].(string)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid event type")
|
|
}
|
|
|
|
data, ok := array[2].(string)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid event data")
|
|
}
|
|
|
|
return &StreamEvent{
|
|
Type: "event",
|
|
Event: &AsciinemaEvent{
|
|
Time: timestamp,
|
|
Type: EventType(eventType),
|
|
Data: data,
|
|
},
|
|
}, nil
|
|
} |