Files
Strix/internal/api/handlers/discover.go
T
eduard256 453769a376 Add multi-authentication support and comprehensive stream discovery
Major improvements to camera stream discovery system:

**Multi-Authentication Support:**
- Implement smart authentication fallback chain for HTTP/MJPEG/JPEG streams
- Add combined authentication method (Basic Auth header + query params) - fixes ZOSI cameras
- Support for: No Auth, Basic Auth, Query Params, Combined, Digest
- Auto-detect authentication method from URL and try appropriate chain

**Protocol & Detection Enhancements:**
- Add HLS (.m3u8) stream detection
- Add MPEG-DASH (.mpd) stream detection
- Add WebSocket stream detection
- Improve JPEG detection by URL extension when Content-Type is incorrect
- Add AuthMethod field to DiscoveredStream model

**Bug Fixes:**
- Fix port 0 bug: use default ports (HTTP=80, HTTPS=443, RTSP=554) when entry.Port==0
- Ensure URLs are built with correct ports from database or defaults

**Debug & Logging:**
- Add comprehensive DEBUG logging to builder.go (URL generation)
- Add comprehensive DEBUG logging to tester.go (stream testing & auth)
- Add comprehensive DEBUG logging to scanner.go (URL collection & deduplication)
- Log auth method detection, chain determination, and test results

**Results:**
- Tested with ZOSI ZG23213M camera: 4 streams found (was 0)
- Combined auth method successfully detects streams requiring both header + params
- Better coverage for cameras with non-standard authentication requirements

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-29 02:15:21 +03:00

130 lines
3.4 KiB
Go

package handlers
import (
"encoding/json"
"net/http"
"github.com/go-playground/validator/v10"
"github.com/strix-project/strix/internal/camera/discovery"
"github.com/strix-project/strix/internal/models"
"github.com/strix-project/strix/pkg/sse"
)
// DiscoverHandler handles stream discovery requests
type DiscoverHandler struct {
scanner *discovery.Scanner
sseServer *sse.Server
validator *validator.Validate
logger interface{ Debug(string, ...any); Error(string, error, ...any); Info(string, ...any) }
}
// NewDiscoverHandler creates a new discover handler
func NewDiscoverHandler(
scanner *discovery.Scanner,
sseServer *sse.Server,
logger interface{ Debug(string, ...any); Error(string, error, ...any); Info(string, ...any) },
) *DiscoverHandler {
return &DiscoverHandler{
scanner: scanner,
sseServer: sseServer,
validator: validator.New(),
logger: logger,
}
}
// ServeHTTP handles discovery requests
func (h *DiscoverHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Parse request body
var req models.StreamDiscoveryRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
h.logger.Error("failed to decode discovery request", err)
h.sendErrorResponse(w, "Invalid request body", http.StatusBadRequest)
return
}
// Set defaults
if req.ModelLimit <= 0 {
req.ModelLimit = 6
}
if req.Timeout <= 0 {
req.Timeout = 240 // 4 minutes
}
if req.MaxStreams <= 0 {
req.MaxStreams = 10
}
// Validate request
if err := h.validator.Struct(req); err != nil {
h.logger.Error("discovery request validation failed", err)
h.sendErrorResponse(w, "Validation failed: "+err.Error(), http.StatusBadRequest)
return
}
h.logger.Info("stream discovery requested",
"target", req.Target,
"model", req.Model,
"timeout", req.Timeout,
"max_streams", req.MaxStreams,
"remote_addr", r.RemoteAddr,
)
// Check if SSE is supported
flusher, ok := w.(http.Flusher)
if !ok {
h.logger.Info("SSE not supported by client", "remote_addr", r.RemoteAddr)
h.sendErrorResponse(w, "SSE not supported", http.StatusInternalServerError)
return
}
// Set SSE headers
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("X-Accel-Buffering", "no") // Disable Nginx buffering
// Flush headers
flusher.Flush()
// Create SSE stream writer
streamWriter, err := h.sseServer.NewStreamWriter(w, r)
if err != nil {
h.logger.Error("failed to create SSE stream", err)
return
}
defer streamWriter.Close()
// Perform discovery
result, err := h.scanner.Scan(r.Context(), req, streamWriter)
if err != nil {
h.logger.Error("discovery failed", err)
streamWriter.SendError(err)
return
}
h.logger.Info("discovery completed",
"target", req.Target,
"tested", result.TotalTested,
"found", result.TotalFound,
"duration", result.Duration,
)
}
// sendErrorResponse sends an error response for non-SSE requests
func (h *DiscoverHandler) sendErrorResponse(w http.ResponseWriter, message string, statusCode int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
response := map[string]interface{}{
"error": true,
"message": message,
"code": statusCode,
}
json.NewEncoder(w).Encode(response)
}