streamd: serve start/stop/update, streamd.yaml, egress, module panel fixes
- 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
This commit is contained in:
parent
da8e1123ec
commit
8fc7e4c0b8
57 changed files with 4031 additions and 257 deletions
|
|
@ -13,6 +13,7 @@ import (
|
|||
"streamd/apps/streamd/internal/auth"
|
||||
"streamd/apps/streamd/internal/db"
|
||||
"streamd/apps/streamd/internal/deps"
|
||||
"streamd/apps/streamd/internal/egress"
|
||||
"streamd/apps/streamd/internal/httpx"
|
||||
"streamd/apps/streamd/internal/modstore"
|
||||
)
|
||||
|
|
@ -61,6 +62,7 @@ type Handler struct {
|
|||
Modules *modstore.Store
|
||||
// ModulesNotify is set when the hub supports module change pushes.
|
||||
ModulesNotify ModulesNotifier
|
||||
Egress *egress.Manager
|
||||
Log *slog.Logger
|
||||
}
|
||||
|
||||
|
|
@ -70,6 +72,7 @@ func (h *Handler) Mount(mux *http.ServeMux) {
|
|||
mux.HandleFunc("/api/streams/", h.streamAction)
|
||||
mux.HandleFunc("/api/overview", h.overview)
|
||||
h.MountControl(mux)
|
||||
h.MountEgress(mux)
|
||||
mux.HandleFunc("/api/apps", h.apps)
|
||||
mux.HandleFunc("/api/modules", h.modules)
|
||||
mux.HandleFunc("/api/modules/", h.modules)
|
||||
|
|
@ -539,6 +542,7 @@ func changeNote(before, after db.Stream, in db.PatchStream) string {
|
|||
add("key", in.Key != nil && before.Key != after.Key)
|
||||
add("headers", in.HeadersJSON != nil && before.HeadersJSON != after.HeadersJSON)
|
||||
add("proxy", in.Proxy != nil && before.Proxy != after.Proxy)
|
||||
add("egress", in.Egress != nil && before.Egress != after.Egress)
|
||||
add("variants", before.VariantsJSON != after.VariantsJSON)
|
||||
add("outputs", before.OutputsJSON != after.OutputsJSON)
|
||||
add("settings", before.SettingsJSON != after.SettingsJSON)
|
||||
|
|
@ -570,6 +574,9 @@ func credentialsChanged(before, after db.Stream, in db.PatchStream) bool {
|
|||
if in.Proxy != nil && strings.TrimSpace(before.Proxy) != strings.TrimSpace(after.Proxy) {
|
||||
return true
|
||||
}
|
||||
if in.Egress != nil && strings.TrimSpace(before.Egress) != strings.TrimSpace(after.Egress) {
|
||||
return true
|
||||
}
|
||||
if in.VariantsJSON != nil || len(in.Variants) > 0 {
|
||||
if strings.TrimSpace(before.VariantsJSON) != strings.TrimSpace(after.VariantsJSON) {
|
||||
return true
|
||||
|
|
|
|||
|
|
@ -2,8 +2,12 @@ package api
|
|||
|
||||
import (
|
||||
"net/http"
|
||||
"sort"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"streamd/apps/streamd/internal/auth"
|
||||
"streamd/apps/streamd/internal/modstore"
|
||||
"streamd/modhost"
|
||||
"streamd/mpd"
|
||||
"streamd/repo"
|
||||
|
|
@ -44,13 +48,17 @@ func (h *Handler) apps(w http.ResponseWriter, r *http.Request) {
|
|||
for _, m := range mods {
|
||||
bin, err := modhost.EnsureBuilt(m)
|
||||
if err != nil {
|
||||
out = append(out, appInfo{Name: m.Name, Channels: []channelInfo{}, Rewriter: "none", Error: err.Error()})
|
||||
info := appInfo{Name: m.Name, Channels: []channelInfo{}, Rewriter: "none", Error: err.Error()}
|
||||
h.fillChannelsFromStore(&info, m.Name)
|
||||
out = append(out, info)
|
||||
h.logger().Warn("module describe skipped", "module", m.Name, "err", err)
|
||||
continue
|
||||
}
|
||||
d, err := modhost.RunDescribe(bin)
|
||||
if err != nil {
|
||||
out = append(out, appInfo{Name: m.Name, Channels: []channelInfo{}, Rewriter: "none", Error: err.Error()})
|
||||
info := appInfo{Name: m.Name, Channels: []channelInfo{}, Rewriter: "none", Error: err.Error()}
|
||||
h.fillChannelsFromStore(&info, m.Name)
|
||||
out = append(out, info)
|
||||
h.logger().Warn("module describe skipped", "module", m.Name, "err", err)
|
||||
continue
|
||||
}
|
||||
|
|
@ -61,10 +69,14 @@ func (h *Handler) apps(w http.ResponseWriter, r *http.Request) {
|
|||
for _, ch := range d.Channels {
|
||||
info.Channels = append(info.Channels, channelInfo{ID: ch.ID, Label: ch.Label})
|
||||
}
|
||||
info.HasConfig = len(info.Channels) > 0
|
||||
if d.Rewriter != "" {
|
||||
info.Rewriter = d.Rewriter
|
||||
}
|
||||
// Older module binaries may not see the panel store; fill from module.yaml.
|
||||
if len(info.Channels) == 0 {
|
||||
h.fillChannelsFromStore(&info, m.Name)
|
||||
}
|
||||
info.HasConfig = len(info.Channels) > 0
|
||||
out = append(out, info)
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
|
|
@ -72,3 +84,53 @@ func (h *Handler) apps(w http.ResponseWriter, r *http.Request) {
|
|||
"rewriters": mpd.Names(),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) fillChannelsFromStore(info *appInfo, name string) {
|
||||
if h == nil || h.Modules == nil || info == nil {
|
||||
return
|
||||
}
|
||||
chans := channelsFromStoreYAML(h.Modules, name)
|
||||
if len(chans) == 0 {
|
||||
return
|
||||
}
|
||||
info.Channels = chans
|
||||
info.HasConfig = true
|
||||
if info.Error != "" {
|
||||
// Soft: yaml channels recovered even though describe failed.
|
||||
info.Error = ""
|
||||
}
|
||||
}
|
||||
|
||||
// channelsFromStoreYAML reads channels from the panel module.yaml when describe
|
||||
// returns none (common with uploaded binaries that resolve values via cwd).
|
||||
func channelsFromStoreYAML(store *modstore.Store, name string) []channelInfo {
|
||||
if store == nil {
|
||||
return nil
|
||||
}
|
||||
raw, err := store.ReadYAML(name)
|
||||
if err != nil || len(raw) == 0 {
|
||||
return nil
|
||||
}
|
||||
var doc struct {
|
||||
Channels map[string]struct {
|
||||
Label string `yaml:"label"`
|
||||
} `yaml:"channels"`
|
||||
}
|
||||
if err := yaml.Unmarshal(raw, &doc); err != nil || len(doc.Channels) == 0 {
|
||||
return nil
|
||||
}
|
||||
ids := make([]string, 0, len(doc.Channels))
|
||||
for id := range doc.Channels {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
out := make([]channelInfo, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
label := doc.Channels[id].Label
|
||||
if label == "" {
|
||||
label = id
|
||||
}
|
||||
out = append(out, channelInfo{ID: id, Label: label})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
|
|
|||
45
apps/streamd/internal/api/apps_test.go
Normal file
45
apps/streamd/internal/api/apps_test.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"streamd/apps/streamd/internal/modstore"
|
||||
)
|
||||
|
||||
func TestChannelsFromStoreYAML(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
store := modstore.New(root)
|
||||
modDir := filepath.Join(root, "rte")
|
||||
if err := os.MkdirAll(modDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
yaml := []byte(`package: air.RTE
|
||||
channels:
|
||||
rteone:
|
||||
label: RTE One
|
||||
rtetwo:
|
||||
label: RTE 2
|
||||
`)
|
||||
if err := os.WriteFile(filepath.Join(modDir, "module.yaml"), yaml, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
chans := channelsFromStoreYAML(store, "rte")
|
||||
if len(chans) != 2 {
|
||||
t.Fatalf("got %d channels: %+v", len(chans), chans)
|
||||
}
|
||||
if chans[0].ID != "rteone" || chans[0].Label != "RTE One" {
|
||||
t.Fatalf("first channel: %+v", chans[0])
|
||||
}
|
||||
if chans[1].ID != "rtetwo" {
|
||||
t.Fatalf("second channel: %+v", chans[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestChannelsFromStoreYAMLMissing(t *testing.T) {
|
||||
store := modstore.New(t.TempDir())
|
||||
if chans := channelsFromStoreYAML(store, "nope"); chans != nil {
|
||||
t.Fatalf("expected nil, got %+v", chans)
|
||||
}
|
||||
}
|
||||
|
|
@ -162,6 +162,7 @@ func (h *Handler) duplicate(w http.ResponseWriter, r *http.Request, id int64) {
|
|||
Key: src.Key,
|
||||
HeadersJSON: src.HeadersJSON,
|
||||
Proxy: src.Proxy,
|
||||
Egress: src.Egress,
|
||||
VideoSelect: src.VideoSelect,
|
||||
AudioSelect: src.AudioSelect,
|
||||
Rewriter: src.Rewriter,
|
||||
|
|
@ -271,9 +272,19 @@ func (h *Handler) doProbe(ctx context.Context, st db.Stream, target string) prob
|
|||
start := time.Now()
|
||||
|
||||
client := &http.Client{Timeout: 20 * time.Second}
|
||||
// Fetch through the stream's own proxy, so a geo-restricted origin is
|
||||
// probed the same way the downloader would reach it.
|
||||
if proxy := strings.TrimSpace(st.Proxy); proxy != "" {
|
||||
// Fetch through the stream's effective proxy (explicit or named egress),
|
||||
// so a geo-restricted origin is probed the same way the downloader would.
|
||||
proxy := strings.TrimSpace(st.Proxy)
|
||||
if h.Egress != nil {
|
||||
if p, err := h.Egress.ResolveProxy(st); err == nil {
|
||||
proxy = p
|
||||
} else if strings.TrimSpace(st.Egress) != "" && proxy == "" {
|
||||
res.Error = "egress: " + err.Error()
|
||||
res.Summary = "egress profile could not be brought up"
|
||||
return res
|
||||
}
|
||||
}
|
||||
if proxy != "" {
|
||||
if c, err := httpClientWithProxy(proxy); err == nil {
|
||||
client = c
|
||||
} else {
|
||||
|
|
|
|||
254
apps/streamd/internal/api/egress.go
Normal file
254
apps/streamd/internal/api/egress.go
Normal file
|
|
@ -0,0 +1,254 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"streamd/apps/streamd/internal/auth"
|
||||
"streamd/apps/streamd/internal/db"
|
||||
)
|
||||
|
||||
// MountEgress registers /api/egress routes.
|
||||
func (h *Handler) MountEgress(mux *http.ServeMux) {
|
||||
mux.HandleFunc("/api/egress", h.egressRoot)
|
||||
mux.HandleFunc("/api/egress/", h.egressAction)
|
||||
}
|
||||
|
||||
func (h *Handler) egressRoot(w http.ResponseWriter, r *http.Request) {
|
||||
if h.Egress == nil {
|
||||
writeErr(w, http.StatusServiceUnavailable, "egress manager not configured")
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
if _, ok := auth.Guard(w, r, auth.RoleViewer); !ok {
|
||||
return
|
||||
}
|
||||
list, err := h.Store.ListEgressProfiles()
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
for i := range list {
|
||||
h.Egress.Enrich(&list[i])
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"profiles": list})
|
||||
case http.MethodPost:
|
||||
p, ok := auth.Guard(w, r, auth.RoleAdmin)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var in db.CreateEgressProfile
|
||||
if err := readJSON(r, &in); err != nil {
|
||||
writeErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
prof, err := h.Store.CreateEgressProfile(in)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
h.Egress.Enrich(&prof)
|
||||
h.audit(r, p, http.StatusOK, "create egress "+prof.Name)
|
||||
writeJSON(w, http.StatusOK, prof)
|
||||
default:
|
||||
writeErr(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) egressAction(w http.ResponseWriter, r *http.Request) {
|
||||
if h.Egress == nil {
|
||||
writeErr(w, http.StatusServiceUnavailable, "egress manager not configured")
|
||||
return
|
||||
}
|
||||
path := strings.Trim(strings.TrimPrefix(r.URL.Path, "/api/egress/"), "/")
|
||||
if path == "" {
|
||||
h.egressRoot(w, r)
|
||||
return
|
||||
}
|
||||
parts := strings.Split(path, "/")
|
||||
name := db.Slug(parts[0])
|
||||
if name == "" {
|
||||
writeErr(w, http.StatusBadRequest, "invalid name")
|
||||
return
|
||||
}
|
||||
action := ""
|
||||
if len(parts) > 1 {
|
||||
action = strings.ToLower(parts[1])
|
||||
}
|
||||
|
||||
switch {
|
||||
case action == "" && r.Method == http.MethodGet:
|
||||
if _, ok := auth.Guard(w, r, auth.RoleViewer); !ok {
|
||||
return
|
||||
}
|
||||
prof, err := h.Egress.Status(name)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
writeErr(w, http.StatusNotFound, "not found")
|
||||
return
|
||||
}
|
||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, prof)
|
||||
|
||||
case action == "" && r.Method == http.MethodPut:
|
||||
p, ok := auth.Guard(w, r, auth.RoleAdmin)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
h.putEgress(w, r, p, name)
|
||||
|
||||
case action == "" && r.Method == http.MethodDelete:
|
||||
p, ok := auth.Guard(w, r, auth.RoleAdmin)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
prof, err := h.Store.GetEgressProfileByName(name)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusNotFound, "not found")
|
||||
return
|
||||
}
|
||||
_ = h.Egress.Down(name)
|
||||
if err := h.Store.DeleteEgressProfile(prof.ID); err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
_ = h.Egress.RemoveFiles(name)
|
||||
h.audit(r, p, http.StatusOK, "delete egress "+name)
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": true})
|
||||
|
||||
case action == "up" && r.Method == http.MethodPost:
|
||||
p, ok := auth.Guard(w, r, auth.RoleAdmin)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := h.Egress.EnsureUp(name); err != nil {
|
||||
writeErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
prof, _ := h.Egress.Status(name)
|
||||
h.audit(r, p, http.StatusOK, "egress up "+name)
|
||||
writeJSON(w, http.StatusOK, prof)
|
||||
|
||||
case action == "down" && r.Method == http.MethodPost:
|
||||
p, ok := auth.Guard(w, r, auth.RoleAdmin)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := h.Egress.Down(name); err != nil {
|
||||
writeErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
prof, _ := h.Egress.Status(name)
|
||||
h.audit(r, p, http.StatusOK, "egress down "+name)
|
||||
writeJSON(w, http.StatusOK, prof)
|
||||
|
||||
case action == "test" && r.Method == http.MethodPost:
|
||||
p, ok := auth.Guard(w, r, auth.RoleAdmin)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
out, err := h.Egress.TestDial(name)
|
||||
if err != nil {
|
||||
if out == nil {
|
||||
writeErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
h.audit(r, p, http.StatusOK, "egress test "+name+" fail")
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
return
|
||||
}
|
||||
h.audit(r, p, http.StatusOK, "egress test "+name)
|
||||
writeJSON(w, http.StatusOK, out)
|
||||
|
||||
case action == "conf" && r.Method == http.MethodPut:
|
||||
p, ok := auth.Guard(w, r, auth.RoleAdmin)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, 1<<20))
|
||||
_ = r.Body.Close()
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
contents := string(body)
|
||||
ct := r.Header.Get("Content-Type")
|
||||
if strings.Contains(ct, "json") || strings.HasPrefix(strings.TrimSpace(contents), "{") {
|
||||
var in struct {
|
||||
Conf string `json:"conf"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &in); err != nil || strings.TrimSpace(in.Conf) == "" {
|
||||
writeErr(w, http.StatusBadRequest, "conf required")
|
||||
return
|
||||
}
|
||||
contents = in.Conf
|
||||
}
|
||||
if strings.TrimSpace(contents) == "" {
|
||||
writeErr(w, http.StatusBadRequest, "empty conf")
|
||||
return
|
||||
}
|
||||
if _, err := h.Store.GetEgressProfileByName(name); err != nil {
|
||||
writeErr(w, http.StatusNotFound, "not found")
|
||||
return
|
||||
}
|
||||
if err := h.Egress.WriteConf(name, contents); err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
prof, _ := h.Egress.Status(name)
|
||||
h.audit(r, p, http.StatusOK, "egress conf "+name)
|
||||
writeJSON(w, http.StatusOK, prof)
|
||||
|
||||
default:
|
||||
writeErr(w, http.StatusMethodNotAllowed, "method not allowed")
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) putEgress(w http.ResponseWriter, r *http.Request, p *auth.Principal, name string) {
|
||||
prof, err := h.Store.GetEgressProfileByName(name)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusNotFound, "not found")
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Enabled *bool `json:"enabled"`
|
||||
SocksURL *string `json:"socks_url"`
|
||||
Kind *string `json:"kind"`
|
||||
Conf *string `json:"conf"`
|
||||
}
|
||||
if err := readJSON(r, &in); err != nil {
|
||||
writeErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
upd, err := h.Store.UpdateEgressProfile(prof.ID, db.UpdateEgressProfile{
|
||||
Enabled: in.Enabled, SocksURL: in.SocksURL, Kind: in.Kind,
|
||||
})
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if in.Conf != nil && strings.TrimSpace(*in.Conf) != "" {
|
||||
if err := h.Egress.WriteConf(name, *in.Conf); err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
h.Egress.Enrich(&upd)
|
||||
if in.Enabled != nil && *in.Enabled {
|
||||
_ = h.Egress.EnsureUp(name)
|
||||
upd, _ = h.Egress.Status(name)
|
||||
}
|
||||
if in.Enabled != nil && !*in.Enabled {
|
||||
_ = h.Egress.Down(name)
|
||||
upd, _ = h.Egress.Status(name)
|
||||
}
|
||||
h.audit(r, p, http.StatusOK, "update egress "+name)
|
||||
writeJSON(w, http.StatusOK, upd)
|
||||
}
|
||||
|
|
@ -222,6 +222,14 @@ func (h *Handler) uploadModule(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
defer file.Close()
|
||||
if name == "" && hdr != nil {
|
||||
// rte.zip → rte (helps flat release zips before binary inference).
|
||||
base := strings.TrimSuffix(filepath.Base(hdr.Filename), filepath.Ext(hdr.Filename))
|
||||
base = strings.ToLower(strings.TrimSpace(base))
|
||||
if modstore.NameRe.MatchString(base) {
|
||||
name = base
|
||||
}
|
||||
}
|
||||
if hdr.Size > modstore.MaxPackageBytes {
|
||||
writeErr(w, http.StatusBadRequest, "zip too large")
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue