mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
- Add reverse proxy at /livekit/* that forwards to LiveKit server - Server sends relative URL "/livekit" in voice_token; client resolves to wss://server:port/livekit using known server host - Fix API secret minimum length (32 chars required by LiveKit) - Pass TLS config to LiveKit process manager for TURN certs
204 lines
4.7 KiB
Go
204 lines
4.7 KiB
Go
// Package ws provides the LiveKit companion process manager.
|
|
//
|
|
// LiveKitProcess manages the lifecycle of a livekit-server binary running
|
|
// alongside chatserver. It auto-generates a minimal livekit.yaml config,
|
|
// starts the process, monitors health, and restarts on crash.
|
|
package ws
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/owncord/server/config"
|
|
)
|
|
|
|
// LiveKitProcess manages the companion livekit-server binary.
|
|
type LiveKitProcess struct {
|
|
cfg *config.VoiceConfig
|
|
tlsCfg *config.TLSConfig
|
|
dataDir string
|
|
|
|
mu sync.Mutex
|
|
cmd *exec.Cmd
|
|
cancel context.CancelFunc
|
|
stopped bool
|
|
}
|
|
|
|
// NewLiveKitProcess creates a new process manager. It does not start the
|
|
// process — call Start() to launch the LiveKit server.
|
|
// The tlsCfg is used to configure TLS on the LiveKit server using the same
|
|
// certs as OwnCord (avoids mixed-content blocks in WebView2).
|
|
func NewLiveKitProcess(cfg *config.VoiceConfig, tlsCfg *config.TLSConfig, dataDir string) *LiveKitProcess {
|
|
return &LiveKitProcess{
|
|
cfg: cfg,
|
|
tlsCfg: tlsCfg,
|
|
dataDir: dataDir,
|
|
}
|
|
}
|
|
|
|
// generateConfig writes a minimal livekit.yaml for the companion process.
|
|
func (p *LiveKitProcess) generateConfig() (string, error) {
|
|
cfgPath := filepath.Join(p.dataDir, "livekit.yaml")
|
|
|
|
// Build TLS section if OwnCord has TLS configured with cert files.
|
|
tlsSection := ""
|
|
if p.tlsCfg != nil && p.tlsCfg.CertFile != "" && p.tlsCfg.KeyFile != "" {
|
|
// Resolve cert/key paths relative to the data directory's parent
|
|
// (same working directory as chatserver).
|
|
certFile := p.tlsCfg.CertFile
|
|
keyFile := p.tlsCfg.KeyFile
|
|
tlsSection = fmt.Sprintf(`
|
|
turn:
|
|
enabled: true
|
|
tls_port: 5349
|
|
udp_port: 3478
|
|
cert_file: %s
|
|
key_file: %s
|
|
`, certFile, keyFile)
|
|
}
|
|
|
|
content := fmt.Sprintf(`# Auto-generated by OwnCord — do not edit manually.
|
|
port: 7880
|
|
rtc:
|
|
port_range_start: 50000
|
|
port_range_end: 60000
|
|
use_external_ip: true
|
|
keys:
|
|
%s: %s%s
|
|
logging:
|
|
level: info
|
|
`, p.cfg.LiveKitAPIKey, p.cfg.LiveKitAPISecret, tlsSection)
|
|
|
|
if err := os.MkdirAll(p.dataDir, 0o755); err != nil {
|
|
return "", fmt.Errorf("creating data dir: %w", err)
|
|
}
|
|
if err := os.WriteFile(cfgPath, []byte(content), 0o644); err != nil {
|
|
return "", fmt.Errorf("writing livekit config: %w", err)
|
|
}
|
|
|
|
return cfgPath, nil
|
|
}
|
|
|
|
// Start launches the livekit-server binary. If LiveKitBinaryPath is empty,
|
|
// this is a no-op (assumes LiveKit is managed externally).
|
|
func (p *LiveKitProcess) Start() error {
|
|
if p.cfg.LiveKitBinaryPath == "" {
|
|
slog.Info("livekit: no binary path configured, assuming externally managed")
|
|
return nil
|
|
}
|
|
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
|
|
if p.cmd != nil {
|
|
return fmt.Errorf("livekit process already running")
|
|
}
|
|
|
|
cfgPath, err := p.generateConfig()
|
|
if err != nil {
|
|
return fmt.Errorf("generating livekit config: %w", err)
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
p.cancel = cancel
|
|
|
|
go p.runLoop(ctx, cfgPath)
|
|
|
|
return nil
|
|
}
|
|
|
|
// runLoop starts and restarts the process until stopped or context cancelled.
|
|
func (p *LiveKitProcess) runLoop(ctx context.Context, cfgPath string) {
|
|
const restartDelay = 3 * time.Second
|
|
|
|
for {
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
|
|
cmd := exec.CommandContext(ctx, p.cfg.LiveKitBinaryPath, "--config", cfgPath)
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
|
|
p.mu.Lock()
|
|
if p.stopped {
|
|
p.mu.Unlock()
|
|
return
|
|
}
|
|
p.cmd = cmd
|
|
p.mu.Unlock()
|
|
|
|
slog.Info("livekit: starting process",
|
|
"binary", p.cfg.LiveKitBinaryPath,
|
|
"config", cfgPath)
|
|
|
|
err := cmd.Run()
|
|
|
|
p.mu.Lock()
|
|
p.cmd = nil
|
|
stopped := p.stopped
|
|
p.mu.Unlock()
|
|
|
|
if stopped || ctx.Err() != nil {
|
|
slog.Info("livekit: process stopped")
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
slog.Error("livekit: process exited unexpectedly",
|
|
"error", err,
|
|
"restart_delay", restartDelay)
|
|
}
|
|
|
|
select {
|
|
case <-time.After(restartDelay):
|
|
slog.Info("livekit: restarting process")
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// IsRunning returns true if the companion process is currently running.
|
|
func (p *LiveKitProcess) IsRunning() bool {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
return p.cmd != nil && p.cmd.Process != nil
|
|
}
|
|
|
|
// Stop gracefully stops the companion process.
|
|
func (p *LiveKitProcess) Stop() {
|
|
p.mu.Lock()
|
|
p.stopped = true
|
|
cancel := p.cancel
|
|
cmd := p.cmd
|
|
p.mu.Unlock()
|
|
|
|
if cancel != nil {
|
|
cancel()
|
|
}
|
|
|
|
// Wait briefly for the process to exit after context cancellation
|
|
if cmd != nil && cmd.Process != nil {
|
|
done := make(chan struct{})
|
|
go func() {
|
|
_ = cmd.Wait()
|
|
close(done)
|
|
}()
|
|
|
|
select {
|
|
case <-done:
|
|
slog.Info("livekit: process exited cleanly")
|
|
case <-time.After(5 * time.Second):
|
|
slog.Warn("livekit: process did not exit in time, killing")
|
|
_ = cmd.Process.Kill()
|
|
}
|
|
}
|
|
}
|