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) }