2026-03-25 12:22:10 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync/atomic"
|
|
|
|
|
"syscall"
|
2026-03-26 12:08:13 +01:00
|
|
|
"time"
|
2026-03-25 12:22:10 +01:00
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
|
|
"github.com/netsynth/netsynth/aggregate"
|
|
|
|
|
"github.com/netsynth/netsynth/capture"
|
|
|
|
|
"github.com/netsynth/netsynth/classify"
|
2026-03-26 13:15:35 +01:00
|
|
|
"github.com/netsynth/netsynth/encode"
|
2026-03-25 12:22:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
ifaceName string
|
|
|
|
|
listIfaces bool
|
|
|
|
|
verbose bool
|
2026-03-26 12:08:13 +01:00
|
|
|
outputPath string
|
2026-03-25 12:22:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
rootCmd := &cobra.Command{
|
|
|
|
|
Use: "netsynth",
|
|
|
|
|
Short: "Sonify live network traffic into ambient audio",
|
2026-03-26 13:15:35 +01:00
|
|
|
Long: "NetSynth captures network traffic, classifies it by protocol, and synthesizes an ambient MP3 soundscape.",
|
2026-03-25 12:22:10 +01:00
|
|
|
RunE: run,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rootCmd.Flags().StringVarP(&ifaceName, "interface", "i", "", "Network interface to capture on (required unless --list-interfaces)")
|
|
|
|
|
rootCmd.Flags().BoolVar(&listIfaces, "list-interfaces", false, "List available network interfaces and exit")
|
|
|
|
|
rootCmd.Flags().BoolVar(&verbose, "verbose", false, "Print per-window protocol activity to stderr")
|
2026-03-26 12:08:13 +01:00
|
|
|
rootCmd.Flags().StringVarP(&outputPath, "output", "o", "", "Output MP3 file path (default: netsynth-<timestamp>.mp3)")
|
2026-03-25 12:22:10 +01:00
|
|
|
|
|
|
|
|
if err := rootCmd.Execute(); err != nil {
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func run(cmd *cobra.Command, args []string) error {
|
|
|
|
|
// --list-interfaces mode (CAPT-02)
|
|
|
|
|
if listIfaces {
|
|
|
|
|
return runListInterfaces()
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 12:08:13 +01:00
|
|
|
// Resolve default output path (D-15 / OUT-01)
|
|
|
|
|
if outputPath == "" {
|
|
|
|
|
outputPath = fmt.Sprintf("netsynth-%s.mp3", time.Now().Format("20060102-150405"))
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 12:22:10 +01:00
|
|
|
// Require -i flag
|
|
|
|
|
if ifaceName == "" {
|
|
|
|
|
return fmt.Errorf("interface required: use -i <interface> or --list-interfaces to see available interfaces")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set up signal handling (Ctrl+C)
|
|
|
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
defer stop()
|
|
|
|
|
|
|
|
|
|
// Stage 1: Capture (CAPT-01)
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Starting capture on %s... (press Ctrl+C to stop)\n", ifaceName)
|
2026-03-26 14:33:29 +01:00
|
|
|
packets, droppedPtr, err := capture.StartCapture(ctx, ifaceName, "")
|
2026-03-25 12:22:10 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return err // CAPT-04: permission error already has platform-specific message
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stage 2: Classify (CLAS-01)
|
|
|
|
|
classifier := classify.NewClassifier(classify.DefaultRules)
|
|
|
|
|
classified := make(chan classify.ClassifiedPacket, 1024)
|
|
|
|
|
go func() {
|
|
|
|
|
defer close(classified)
|
|
|
|
|
for pkt := range packets {
|
|
|
|
|
classified <- classifier.Classify(pkt)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
// Stage 3: Aggregate with optional verbose callback (CLAS-04)
|
|
|
|
|
var onSnapshot func(classify.WindowSnapshot)
|
|
|
|
|
if verbose {
|
|
|
|
|
onSnapshot = func(snap classify.WindowSnapshot) {
|
|
|
|
|
aggregate.PrintWindowLine(os.Stderr, snap) // CLAS-04: --verbose output
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
snapshots := aggregate.Aggregate(ctx.Done(), classified, aggregate.DefaultWindowMs, onSnapshot)
|
|
|
|
|
|
2026-03-26 12:08:13 +01:00
|
|
|
// Accumulate snapshots for synthesis (Phase 3 will pass to encode.RunSynthesis)
|
2026-03-25 12:22:10 +01:00
|
|
|
totals := make(map[classify.TrafficClass]int64)
|
2026-03-26 12:08:13 +01:00
|
|
|
var collectedSnapshots []classify.WindowSnapshot
|
2026-03-25 12:22:10 +01:00
|
|
|
for snap := range snapshots {
|
2026-03-26 12:08:13 +01:00
|
|
|
collectedSnapshots = append(collectedSnapshots, snap)
|
2026-03-25 12:22:10 +01:00
|
|
|
aggregate.AccumulateTotals(totals, snap)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 13:15:35 +01:00
|
|
|
// D-08: Print protocol summary BEFORE encoding — user sees stats immediately
|
2026-03-25 12:22:10 +01:00
|
|
|
dropped := atomic.LoadInt64(droppedPtr)
|
|
|
|
|
if dropped > 0 {
|
|
|
|
|
fmt.Fprintf(os.Stderr, "\nWarning: %d packets dropped (channel buffer full)\n", dropped)
|
|
|
|
|
}
|
|
|
|
|
aggregate.PrintSummary(os.Stderr, totals)
|
|
|
|
|
|
2026-03-26 13:15:35 +01:00
|
|
|
// D-07: Encoding status line
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Encoding %d windows to %s...\n", len(collectedSnapshots), outputPath)
|
|
|
|
|
encodeStart := time.Now()
|
|
|
|
|
if err := encode.RunSynthesis(collectedSnapshots, outputPath); err != nil {
|
|
|
|
|
return fmt.Errorf("synthesis failed: %w", err)
|
|
|
|
|
}
|
|
|
|
|
encodeElapsed := time.Since(encodeStart)
|
|
|
|
|
|
|
|
|
|
// D-09: Saved confirmation with path, audio duration, file size, encoding time
|
|
|
|
|
audioDuration := float64(len(collectedSnapshots)) * float64(aggregate.DefaultWindowMs) / 1000.0
|
|
|
|
|
info, statErr := os.Stat(outputPath)
|
|
|
|
|
if statErr != nil {
|
|
|
|
|
return fmt.Errorf("stat output file: %w", statErr)
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintf(os.Stderr, "Saved %s (%.1fs, %d KB, encoded in %.1fs)\n",
|
|
|
|
|
outputPath, audioDuration, info.Size()/1024, encodeElapsed.Seconds())
|
2026-03-25 12:22:10 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func runListInterfaces() error {
|
|
|
|
|
ifaces, err := capture.ListInterfaces()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error listing interfaces: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if len(ifaces) == 0 {
|
|
|
|
|
fmt.Fprintln(os.Stderr, "No interfaces found. If interfaces are missing, run with sudo.")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintln(os.Stderr, "Available interfaces:")
|
|
|
|
|
for _, iface := range ifaces {
|
|
|
|
|
addrs, _ := iface.Addrs()
|
|
|
|
|
addrStrs := make([]string, len(addrs))
|
|
|
|
|
for i, a := range addrs {
|
|
|
|
|
addrStrs[i] = a.String()
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintf(os.Stderr, " %-15s flags=%s addrs=%s\n",
|
|
|
|
|
iface.Name, iface.Flags, strings.Join(addrStrs, ", "))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|