- Add streamd serve start|stop|restart|status|update with auto-written streamd.yaml - Panel modules: STREAMD_MODULES_DIR for describe, YAML channel fallback, textarea value fix - Per-stream WireGuard/SOCKS egress profiles + Settings UI - Drop drm-* from release builds; track streamd-bg.sh / streamd-update.sh wrappers - Docs: streamd-server-setup lifecycle and update flow
226 lines
6.1 KiB
Go
226 lines
6.1 KiB
Go
package db
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
EgressKindWireGuard = "wireguard"
|
|
EgressKindSocks = "socks"
|
|
|
|
EgressStopped = "stopped"
|
|
EgressStarting = "starting"
|
|
EgressUp = "up"
|
|
EgressDown = "down"
|
|
EgressError = "error"
|
|
)
|
|
|
|
// EgressProfile is a named origin-egress path (WireGuard netns or plain SOCKS).
|
|
// Conf blobs live on disk under data/egress/<name>/; never in this table.
|
|
type EgressProfile struct {
|
|
ID int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
Kind string `json:"kind"`
|
|
Enabled bool `json:"enabled"`
|
|
ListenPort int `json:"listen_port"`
|
|
SocksURL string `json:"socks_url,omitempty"`
|
|
Status string `json:"status"`
|
|
LastError string `json:"last_error,omitempty"`
|
|
EgressIP string `json:"egress_ip,omitempty"`
|
|
HasConf bool `json:"has_conf"`
|
|
CreatedAt string `json:"created_at"`
|
|
UpdatedAt string `json:"updated_at"`
|
|
}
|
|
|
|
func (s *Store) migrateEgress() error {
|
|
_, err := s.DB.Exec(`
|
|
CREATE TABLE IF NOT EXISTS egress_profiles (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT NOT NULL UNIQUE,
|
|
kind TEXT NOT NULL DEFAULT 'wireguard',
|
|
enabled INTEGER NOT NULL DEFAULT 0,
|
|
listen_port INTEGER NOT NULL DEFAULT 0,
|
|
socks_url TEXT NOT NULL DEFAULT '',
|
|
status TEXT NOT NULL DEFAULT 'stopped',
|
|
last_error TEXT NOT NULL DEFAULT '',
|
|
egress_ip TEXT NOT NULL DEFAULT '',
|
|
created_at TEXT NOT NULL,
|
|
updated_at TEXT NOT NULL
|
|
);
|
|
`)
|
|
return err
|
|
}
|
|
|
|
type CreateEgressProfile struct {
|
|
Name string `json:"name"`
|
|
Kind string `json:"kind"`
|
|
Enabled *bool `json:"enabled"`
|
|
SocksURL string `json:"socks_url"`
|
|
}
|
|
|
|
type UpdateEgressProfile struct {
|
|
Enabled *bool `json:"enabled"`
|
|
SocksURL *string `json:"socks_url"`
|
|
Kind *string `json:"kind"`
|
|
}
|
|
|
|
func normalizeEgressKind(k string) (string, error) {
|
|
switch strings.ToLower(strings.TrimSpace(k)) {
|
|
case "", EgressKindWireGuard:
|
|
return EgressKindWireGuard, nil
|
|
case EgressKindSocks:
|
|
return EgressKindSocks, nil
|
|
default:
|
|
return "", fmt.Errorf("kind must be %q or %q", EgressKindWireGuard, EgressKindSocks)
|
|
}
|
|
}
|
|
|
|
func (s *Store) ListEgressProfiles() ([]EgressProfile, error) {
|
|
rows, err := s.DB.Query(`
|
|
SELECT id, name, kind, enabled, listen_port, socks_url, status, last_error, egress_ip, created_at, updated_at
|
|
FROM egress_profiles ORDER BY name`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []EgressProfile
|
|
for rows.Next() {
|
|
p, err := scanEgress(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, p)
|
|
}
|
|
if out == nil {
|
|
out = []EgressProfile{}
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (s *Store) GetEgressProfile(id int64) (EgressProfile, error) {
|
|
row := s.DB.QueryRow(`
|
|
SELECT id, name, kind, enabled, listen_port, socks_url, status, last_error, egress_ip, created_at, updated_at
|
|
FROM egress_profiles WHERE id=?`, id)
|
|
return scanEgress(row)
|
|
}
|
|
|
|
func (s *Store) GetEgressProfileByName(name string) (EgressProfile, error) {
|
|
name = slug(name)
|
|
if name == "" {
|
|
return EgressProfile{}, fmt.Errorf("name required")
|
|
}
|
|
row := s.DB.QueryRow(`
|
|
SELECT id, name, kind, enabled, listen_port, socks_url, status, last_error, egress_ip, created_at, updated_at
|
|
FROM egress_profiles WHERE name=?`, name)
|
|
return scanEgress(row)
|
|
}
|
|
|
|
func scanEgress(row rowScanner) (EgressProfile, error) {
|
|
var p EgressProfile
|
|
var enabled int
|
|
err := row.Scan(
|
|
&p.ID, &p.Name, &p.Kind, &enabled, &p.ListenPort, &p.SocksURL,
|
|
&p.Status, &p.LastError, &p.EgressIP, &p.CreatedAt, &p.UpdatedAt,
|
|
)
|
|
if err != nil {
|
|
return p, err
|
|
}
|
|
p.Enabled = enabled != 0
|
|
if p.Status == "" {
|
|
p.Status = EgressStopped
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
func (s *Store) CreateEgressProfile(in CreateEgressProfile) (EgressProfile, error) {
|
|
name := slug(in.Name)
|
|
if name == "" {
|
|
return EgressProfile{}, fmt.Errorf("name required (slug of letters, digits, hyphens)")
|
|
}
|
|
kind, err := normalizeEgressKind(in.Kind)
|
|
if err != nil {
|
|
return EgressProfile{}, err
|
|
}
|
|
socks := strings.TrimSpace(in.SocksURL)
|
|
if kind == EgressKindSocks && socks == "" {
|
|
return EgressProfile{}, fmt.Errorf("socks_url required for kind=socks")
|
|
}
|
|
enabled := 0
|
|
if in.Enabled != nil && *in.Enabled {
|
|
enabled = 1
|
|
}
|
|
ts := now()
|
|
res, err := s.DB.Exec(`
|
|
INSERT INTO egress_profiles(name, kind, enabled, listen_port, socks_url, status, last_error, egress_ip, created_at, updated_at)
|
|
VALUES(?,?,?,0,?,?, '','',?,?)`,
|
|
name, kind, enabled, socks, EgressStopped, ts, ts)
|
|
if err != nil {
|
|
if strings.Contains(strings.ToLower(err.Error()), "unique") {
|
|
return EgressProfile{}, fmt.Errorf("egress profile %q already exists", name)
|
|
}
|
|
return EgressProfile{}, err
|
|
}
|
|
id, _ := res.LastInsertId()
|
|
return s.GetEgressProfile(id)
|
|
}
|
|
|
|
func (s *Store) UpdateEgressProfile(id int64, in UpdateEgressProfile) (EgressProfile, error) {
|
|
cur, err := s.GetEgressProfile(id)
|
|
if err != nil {
|
|
return EgressProfile{}, err
|
|
}
|
|
if in.Enabled != nil {
|
|
cur.Enabled = *in.Enabled
|
|
}
|
|
if in.SocksURL != nil {
|
|
cur.SocksURL = strings.TrimSpace(*in.SocksURL)
|
|
}
|
|
if in.Kind != nil {
|
|
kind, err := normalizeEgressKind(*in.Kind)
|
|
if err != nil {
|
|
return EgressProfile{}, err
|
|
}
|
|
cur.Kind = kind
|
|
}
|
|
if cur.Kind == EgressKindSocks && cur.SocksURL == "" {
|
|
return EgressProfile{}, fmt.Errorf("socks_url required for kind=socks")
|
|
}
|
|
enabled := 0
|
|
if cur.Enabled {
|
|
enabled = 1
|
|
}
|
|
ts := now()
|
|
_, err = s.DB.Exec(`
|
|
UPDATE egress_profiles SET kind=?, enabled=?, socks_url=?, updated_at=? WHERE id=?`,
|
|
cur.Kind, enabled, cur.SocksURL, ts, id)
|
|
if err != nil {
|
|
return EgressProfile{}, err
|
|
}
|
|
return s.GetEgressProfile(id)
|
|
}
|
|
|
|
func (s *Store) DeleteEgressProfile(id int64) error {
|
|
res, err := s.DB.Exec(`DELETE FROM egress_profiles WHERE id=?`, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n, _ := res.RowsAffected()
|
|
if n == 0 {
|
|
return sql.ErrNoRows
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetEgressRuntime updates live status fields without touching config.
|
|
func (s *Store) SetEgressRuntime(id int64, status, lastErr, egressIP string, listenPort int) error {
|
|
status = strings.TrimSpace(status)
|
|
if status == "" {
|
|
status = EgressStopped
|
|
}
|
|
_, err := s.DB.Exec(`
|
|
UPDATE egress_profiles SET status=?, last_error=?, egress_ip=?, listen_port=?, updated_at=? WHERE id=?`,
|
|
status, strings.TrimSpace(lastErr), strings.TrimSpace(egressIP), listenPort, now(), id)
|
|
return err
|
|
}
|