| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802 |
- // integrations.go — API de integraciones externas de routers (#1674):
- // pushers de routers externos (MikroTik RouterOS, Asuswrt) gestionados desde
- // la app con el contrato de agente externo. El slug de la integración es el
- // id del router external asociado, así el push del pusher (POST
- // /api/ingest/agent con Bearer token) se atribuye a la misma unidad.
- package httpapi
- import (
- "fmt"
- "net/http"
- "path/filepath"
- "sort"
- "strconv"
- "strings"
- "time"
- "github.com/gnacho/netpulse/server-go/internal/auth"
- "github.com/gnacho/netpulse/server-go/internal/rintegration"
- "github.com/gnacho/netpulse/server-go/internal/routerstore"
- )
- // integrationItem es la vista HTTP de una integración (nunca la contraseña).
- type integrationItem struct {
- Slug string `json:"slug"`
- Vendor string `json:"vendor"`
- Mode string `json:"mode"`
- Host string `json:"host"`
- Username string `json:"username"`
- Version string `json:"version"`
- Arch string `json:"arch"`
- IntervalS int `json:"intervalS"`
- Enabled bool `json:"enabled"`
- State string `json:"state"`
- LastError string `json:"lastError"`
- LastStartMs int64 `json:"lastStartMs"`
- LastPushMs int64 `json:"lastPushMs"`
- Fresh bool `json:"fresh"`
- }
- // vendorView es la vista HTTP del manifiesto de un vendor.
- type vendorView struct {
- ID string `json:"id"`
- Mode string `json:"mode"`
- Arches []string `json:"arches"`
- TagPrefix string `json:"tagPrefix"`
- Description string `json:"description"`
- }
- // registerIntegrationRoutes registra las rutas /api/integrations/*. Todas
- // exigen rol admin salvo el GET de vendors (solo sesión).
- func (s *server) registerIntegrationRoutes(mux *http.ServeMux) {
- mux.HandleFunc("GET /api/integrations/vendors", s.handleIntegrationsVendors)
- mux.Handle("GET /api/integrations", auth.RequireAdmin(http.HandlerFunc(s.handleIntegrationsList)))
- mux.Handle("POST /api/integrations", auth.RequireAdmin(http.HandlerFunc(s.handleIntegrationsCreate)))
- mux.Handle("POST /api/integrations/{slug}/start", auth.RequireAdmin(http.HandlerFunc(s.handleIntegrationStart)))
- mux.Handle("POST /api/integrations/{slug}/stop", auth.RequireAdmin(http.HandlerFunc(s.handleIntegrationStop)))
- mux.Handle("POST /api/integrations/{slug}/check", auth.RequireAdmin(http.HandlerFunc(s.handleIntegrationCheck)))
- mux.Handle("POST /api/integrations/{slug}/update", auth.RequireAdmin(http.HandlerFunc(s.handleIntegrationUpdate)))
- mux.Handle("PUT /api/integrations/{slug}", auth.RequireAdmin(http.HandlerFunc(s.handleIntegrationEdit)))
- mux.Handle("DELETE /api/integrations/{slug}", auth.RequireAdmin(http.HandlerFunc(s.handleIntegrationDelete)))
- }
- func (s *server) handleIntegrationsVendors(w http.ResponseWriter, _ *http.Request) {
- vs := rintegration.Vendors()
- out := make([]vendorView, 0, len(vs))
- for _, v := range vs {
- out = append(out, vendorView{
- ID: v.ID, Mode: v.Mode, Arches: v.Arches,
- TagPrefix: v.TagPrefix, Description: v.Description,
- })
- }
- writeJSON(w, http.StatusOK, map[string]any{"vendors": out})
- }
- func (s *server) integrationsStore() *rintegration.Store {
- return rintegration.NewStore(s.db.DB)
- }
- func (s *server) integrationFetcher() *rintegration.Fetcher {
- if s.integrations != nil {
- return s.integrations.Fetch()
- }
- return rintegration.NewFetcher()
- }
- func (s *server) getIntegrationRow(slug string) (rintegration.Row, bool) {
- if s.db == nil {
- return rintegration.Row{}, false
- }
- return s.integrationsStore().Get(slug)
- }
- // integrationLastPushMs lee agent.last_push_ts.<slug> (segundos unix) y lo
- // devuelve en milisegundos, 0 si nunca empujó.
- func (s *server) integrationLastPushMs(slug string) int64 {
- if s.db == nil {
- return 0
- }
- sec, err := strconv.ParseInt(kvGet(s.db.DB, "agent.last_push_ts."+slug), 10, 64)
- if err != nil {
- return 0
- }
- return sec * 1000
- }
- func (s *server) integrationItemOf(row rintegration.Row) integrationItem {
- lastPush := s.integrationLastPushMs(row.Slug)
- fresh := false
- if lastPush > 0 && row.IntervalS > 0 {
- fresh = time.Now().UnixMilli()-lastPush <= int64(row.IntervalS)*3*1000
- }
- return integrationItem{
- Slug: row.Slug, Vendor: row.Vendor, Mode: row.Mode, Host: row.Host,
- Username: row.Username, Version: row.Version, Arch: row.Arch,
- IntervalS: row.IntervalS, Enabled: row.Enabled, State: row.State,
- LastError: row.LastError, LastStartMs: row.LastStartMs,
- LastPushMs: lastPush, Fresh: fresh,
- }
- }
- func (s *server) integrationList() []integrationItem {
- if s.db == nil {
- return []integrationItem{}
- }
- rows, err := s.integrationsStore().List()
- if err != nil {
- return []integrationItem{}
- }
- out := make([]integrationItem, 0, len(rows))
- for _, row := range rows {
- out = append(out, s.integrationItemOf(row))
- }
- return out
- }
- func (s *server) broadcastIntegrations() {
- if s.hub != nil {
- s.hub.Broadcast("integrations", map[string]any{"integrations": s.integrationList()})
- }
- }
- func (s *server) handleIntegrationsList(w http.ResponseWriter, _ *http.Request) {
- writeJSON(w, http.StatusOK, map[string]any{"integrations": s.integrationList()})
- }
- type integrationCreateBody struct {
- Slug string `json:"slug"`
- Vendor string `json:"vendor"`
- Host string `json:"host"`
- Username string `json:"username"`
- Password string `json:"password"`
- IntervalS int `json:"intervalS"`
- Arch string `json:"arch"`
- }
- func (s *server) handleIntegrationsCreate(w http.ResponseWriter, r *http.Request) {
- if s.db == nil {
- writeError(w, http.StatusInternalServerError, "db_error")
- return
- }
- var body integrationCreateBody
- if st := readJSONBody(w, r, &body); st != 0 {
- writeBodyError(w, st, "invalid_body", `Se esperaba { "slug", "vendor", "host", "username", "password", "intervalS", "arch"? }`)
- return
- }
- if !agentSlugRe.MatchString(body.Slug) {
- writeError(w, http.StatusBadRequest, "invalid_slug", "slug must match ^[a-z0-9][a-z0-9-]{0,63}$")
- return
- }
- vendor, ok := rintegration.LookupVendor(body.Vendor)
- if !ok {
- writeError(w, http.StatusBadRequest, "invalid_vendor", "unknown vendor")
- return
- }
- if body.Host == "" || body.Username == "" || body.Password == "" {
- writeError(w, http.StatusBadRequest, "invalid_input", "host, username and password are required")
- return
- }
- if body.IntervalS < 10 || body.IntervalS > 86400 {
- writeError(w, http.StatusBadRequest, "invalid_interval", "intervalS must be between 10 and 86400")
- return
- }
- arch := body.Arch
- if arch == "" {
- arch = vendor.Arches[0]
- } else if !vendorSupportsArch(vendor, arch) {
- writeError(w, http.StatusBadRequest, "invalid_arch", "unsupported arch "+arch)
- return
- }
- store := s.integrationsStore()
- if _, exists := store.Get(body.Slug); exists {
- writeError(w, http.StatusConflict, "already_exists", "integration already exists")
- return
- }
- // Resolver y descargar el último release verificado. En modo local se
- // extrae el binario aquí (corre en este host); en modo ssh solo se
- // verifica: el router descarga el asset él mismo.
- fetch := s.integrationFetcher()
- res, err := fetch.ResolveLatest(vendor, arch)
- if err != nil {
- writeError(w, http.StatusBadGateway, "release_fetch_failed", err.Error())
- return
- }
- binPath := ""
- if vendor.Mode == "local" {
- if s.integrations == nil {
- writeError(w, http.StatusServiceUnavailable, "unavailable", "integrations supervisor not configured")
- return
- }
- installDir := filepath.Join(s.integrations.Dir(), body.Slug)
- binPath, err = fetch.Download(res, vendor.Binary, installDir)
- if err != nil {
- writeError(w, http.StatusBadGateway, "download_failed", err.Error())
- return
- }
- } else {
- if s.pool == nil {
- writeError(w, http.StatusServiceUnavailable, "ssh_unavailable", "el servidor no tiene pool SSH")
- return
- }
- if err := fetch.Verify(res); err != nil {
- writeError(w, http.StatusBadGateway, "download_failed", err.Error())
- return
- }
- }
- now := time.Now().UnixMilli()
- row := rintegration.Row{
- Slug: body.Slug, Vendor: vendor.ID, Mode: vendor.Mode, Host: body.Host,
- Username: body.Username, Version: res.Version, Asset: res.AssetName,
- Arch: arch, IntervalS: body.IntervalS, Enabled: true, State: "pending",
- CreatedAt: now, UpdatedAt: now,
- }
- if err := store.Upsert(row); err != nil {
- writeError(w, http.StatusInternalServerError, "db_error")
- return
- }
- // Router external cuyo id es el slug (push autenticado por token de
- // agente, agent.token.<slug>).
- router, err := routerstore.AddRouter(s.db.DB, routerstore.AddInput{
- Name: body.Slug, Host: body.Host, Type: "external", AgentOnly: true,
- })
- if err != nil || router.ID != body.Slug {
- _ = store.Delete(body.Slug)
- writeError(w, http.StatusInternalServerError, "router_error")
- return
- }
- s.syncRouters()
- rollback := func() {
- if vendor.Mode == "local" && s.integrations != nil {
- _ = s.integrations.Stop(body.Slug)
- _ = s.integrations.Cleanup(body.Slug)
- }
- _ = routerstore.RemoveRouter(s.db.DB, body.Slug)
- s.syncRouters()
- _ = store.Delete(body.Slug)
- }
- serverURL := s.agentBaseURL(r)
- token, err := s.rotateAgentTokenAtomic(body.Slug, func(tok string) error {
- env := s.pusherEnv(vendor, row, body.Password, serverURL, tok)
- if vendor.Mode == "local" {
- return s.integrations.Start(body.Slug, env, binPath)
- }
- return s.runSSHInstall(vendor, row, env, res.URL, res.SHA256)
- })
- if err != nil {
- rollback()
- writeError(w, http.StatusBadGateway, "start_failed", err.Error())
- return
- }
- _ = store.SetState(body.Slug, "running", "")
- _ = store.SetLastStart(body.Slug, time.Now().UnixMilli())
- s.broadcastIntegrations()
- item, _ := s.getIntegrationRow(body.Slug)
- writeJSON(w, http.StatusCreated, map[string]any{
- "integration": s.integrationItemOf(item),
- "token": token,
- })
- }
- func vendorSupportsArch(v rintegration.Vendor, arch string) bool {
- for _, a := range v.Arches {
- if a == arch {
- return true
- }
- }
- return false
- }
- // pusherEnv construye el entorno del pusher. En modo ssh el pusher corre en el
- // propio router, así que el host del dispositivo es la propia unidad
- // (127.0.0.1) en lugar del host SSH de la tabla.
- func (s *server) pusherEnv(v rintegration.Vendor, row rintegration.Row, password, serverURL, token string) map[string]string {
- prefix := strings.ToUpper(v.ID) + "_"
- host := row.Host
- if v.Mode == "ssh" {
- host = "127.0.0.1"
- }
- return map[string]string{
- prefix + "HOST": host,
- prefix + "USER": row.Username,
- prefix + "PASS": password,
- prefix + "INSECURE": "1",
- "NETPULSE_SERVER": serverURL,
- "NETPULSE_TOKEN": token,
- "NETPULSE_SLUG": row.Slug,
- "NETPULSE_INTERVAL": strconv.Itoa(row.IntervalS) + "s",
- }
- }
- func (s *server) runSSHInstall(v rintegration.Vendor, row rintegration.Row, env map[string]string, assetURL, sha256 string) error {
- _, err := s.pool.Run(row.Host, sshInstallScript(v, env, assetURL, sha256), 300*time.Second)
- return err
- }
- func (s *server) handleIntegrationStart(w http.ResponseWriter, r *http.Request) {
- slug := r.PathValue("slug")
- row, ok := s.getIntegrationRow(slug)
- if !ok {
- writeError(w, http.StatusNotFound, "not_found")
- return
- }
- vendor, ok := rintegration.LookupVendor(row.Vendor)
- if !ok {
- writeError(w, http.StatusInternalServerError, "invalid_vendor")
- return
- }
- store := s.integrationsStore()
- if vendor.Mode == "local" {
- if s.integrations == nil {
- writeError(w, http.StatusServiceUnavailable, "unavailable", "integrations supervisor not configured")
- return
- }
- if !s.integrations.Running(slug) {
- binPath := filepath.Join(s.integrations.Dir(), slug, vendor.Binary)
- if err := s.integrations.Start(slug, nil, binPath); err != nil && err != rintegration.ErrAlreadyRunning {
- writeError(w, http.StatusBadGateway, "start_failed", err.Error())
- return
- }
- }
- } else {
- if s.pool == nil {
- writeError(w, http.StatusServiceUnavailable, "ssh_unavailable", "el servidor no tiene pool SSH")
- return
- }
- if _, err := s.pool.Run(row.Host, sshServiceScript(vendor, "restart"), 60*time.Second); err != nil {
- writeError(w, http.StatusBadGateway, "ssh_failed", err.Error())
- return
- }
- }
- _ = store.SetState(slug, "running", "")
- _ = store.SetLastStart(slug, time.Now().UnixMilli())
- s.broadcastIntegrations()
- item, _ := s.getIntegrationRow(slug)
- writeJSON(w, http.StatusOK, map[string]any{"integration": s.integrationItemOf(item)})
- }
- func (s *server) handleIntegrationStop(w http.ResponseWriter, r *http.Request) {
- slug := r.PathValue("slug")
- row, ok := s.getIntegrationRow(slug)
- if !ok {
- writeError(w, http.StatusNotFound, "not_found")
- return
- }
- vendor, ok := rintegration.LookupVendor(row.Vendor)
- if !ok {
- writeError(w, http.StatusInternalServerError, "invalid_vendor")
- return
- }
- if vendor.Mode == "local" {
- if s.integrations != nil {
- _ = s.integrations.Stop(slug) // ErrNotRunning es no-op aquí
- }
- } else if s.pool != nil {
- _, _ = s.pool.Run(row.Host, sshServiceScript(vendor, "stop"), 60*time.Second)
- }
- _ = s.integrationsStore().SetState(slug, "stopped", "")
- s.broadcastIntegrations()
- item, _ := s.getIntegrationRow(slug)
- writeJSON(w, http.StatusOK, map[string]any{"integration": s.integrationItemOf(item)})
- }
- func (s *server) handleIntegrationCheck(w http.ResponseWriter, r *http.Request) {
- slug := r.PathValue("slug")
- row, ok := s.getIntegrationRow(slug)
- if !ok {
- writeError(w, http.StatusNotFound, "not_found")
- return
- }
- vendor, ok := rintegration.LookupVendor(row.Vendor)
- if !ok {
- writeError(w, http.StatusInternalServerError, "invalid_vendor")
- return
- }
- running := false
- if vendor.Mode == "local" {
- running = s.integrations != nil && s.integrations.Running(slug)
- } else if s.pool != nil {
- _, err := s.pool.Run(row.Host, sshServiceScript(vendor, "check"), 30*time.Second)
- running = err == nil
- }
- writeJSON(w, http.StatusOK, map[string]any{
- "integration": s.integrationItemOf(row),
- "running": running,
- })
- }
- func (s *server) handleIntegrationUpdate(w http.ResponseWriter, r *http.Request) {
- slug := r.PathValue("slug")
- row, ok := s.getIntegrationRow(slug)
- if !ok {
- writeError(w, http.StatusNotFound, "not_found")
- return
- }
- vendor, ok := rintegration.LookupVendor(row.Vendor)
- if !ok {
- writeError(w, http.StatusInternalServerError, "invalid_vendor")
- return
- }
- fetch := s.integrationFetcher()
- res, err := fetch.ResolveLatest(vendor, row.Arch)
- if err != nil {
- writeError(w, http.StatusBadGateway, "release_fetch_failed", err.Error())
- return
- }
- store := s.integrationsStore()
- if vendor.Mode == "local" {
- if s.integrations == nil {
- writeError(w, http.StatusServiceUnavailable, "unavailable", "integrations supervisor not configured")
- return
- }
- _ = s.integrations.Stop(slug)
- installDir := filepath.Join(s.integrations.Dir(), slug)
- if _, err := fetch.Download(res, vendor.Binary, installDir); err != nil {
- _ = store.SetState(slug, "error", err.Error())
- s.broadcastIntegrations()
- writeError(w, http.StatusBadGateway, "download_failed", err.Error())
- return
- }
- if err := s.integrations.Start(slug, nil, filepath.Join(installDir, vendor.Binary)); err != nil {
- _ = store.SetState(slug, "error", err.Error())
- s.broadcastIntegrations()
- writeError(w, http.StatusBadGateway, "start_failed", err.Error())
- return
- }
- } else {
- if s.pool == nil {
- writeError(w, http.StatusServiceUnavailable, "ssh_unavailable", "el servidor no tiene pool SSH")
- return
- }
- if err := fetch.Verify(res); err != nil {
- writeError(w, http.StatusBadGateway, "download_failed", err.Error())
- return
- }
- env := s.pusherEnv(vendor, row, "", s.agentBaseURL(r), "")
- if err := s.runSSHInstall(vendor, row, env, res.URL, res.SHA256); err != nil {
- writeError(w, http.StatusBadGateway, "ssh_failed", err.Error())
- return
- }
- }
- _ = store.SetVersion(slug, res.Version, res.AssetName, row.Arch)
- _ = store.SetState(slug, "running", "")
- _ = store.SetLastStart(slug, time.Now().UnixMilli())
- s.broadcastIntegrations()
- item, _ := s.getIntegrationRow(slug)
- writeJSON(w, http.StatusOK, map[string]any{"integration": s.integrationItemOf(item)})
- }
- // handleIntegrationEdit actualiza los ajustes mutables de una integración sin
- // recrearla: host, usuario, contraseña e intervalo. El fichero de entorno se
- // lee, se cambian solo las claves afectadas y se reescribe, de modo que el
- // token y la contraseña omitida se conservan (nunca viven en la base de datos).
- func (s *server) handleIntegrationEdit(w http.ResponseWriter, r *http.Request) {
- slug := r.PathValue("slug")
- row, ok := s.getIntegrationRow(slug)
- if !ok {
- writeError(w, http.StatusNotFound, "not_found")
- return
- }
- vendor, ok := rintegration.LookupVendor(row.Vendor)
- if !ok {
- writeError(w, http.StatusInternalServerError, "invalid_vendor")
- return
- }
- var body struct {
- Host *string `json:"host"`
- Username *string `json:"username"`
- Password *string `json:"password"`
- IntervalS *int `json:"intervalS"`
- }
- if st := readJSONBody(w, r, &body); st != 0 {
- writeBodyError(w, st, "invalid_body", "")
- return
- }
- changed := false
- if body.Host != nil {
- h := strings.TrimSpace(*body.Host)
- if h == "" {
- writeError(w, http.StatusBadRequest, "invalid_input", "host cannot be empty")
- return
- }
- row.Host = h
- changed = true
- }
- if body.Username != nil {
- u := strings.TrimSpace(*body.Username)
- if u == "" {
- writeError(w, http.StatusBadRequest, "invalid_input", "username cannot be empty")
- return
- }
- row.Username = u
- changed = true
- }
- if body.IntervalS != nil {
- if *body.IntervalS < 10 || *body.IntervalS > 86400 {
- writeError(w, http.StatusBadRequest, "invalid_interval")
- return
- }
- row.IntervalS = *body.IntervalS
- changed = true
- }
- password := ""
- if body.Password != nil {
- password = *body.Password
- changed = true
- }
- if !changed {
- writeError(w, http.StatusBadRequest, "invalid_input", "nothing to update")
- return
- }
- prefix := strings.ToUpper(vendor.ID) + "_"
- host := row.Host
- if vendor.Mode == "ssh" {
- host = "127.0.0.1"
- }
- store := s.integrationsStore()
- if vendor.Mode == "local" {
- if s.integrations == nil {
- writeError(w, http.StatusServiceUnavailable, "unavailable", "integrations supervisor not configured")
- return
- }
- env, err := s.integrations.Env(slug)
- if err != nil {
- _ = store.SetState(slug, "error", err.Error())
- s.broadcastIntegrations()
- writeError(w, http.StatusConflict, "env_unavailable", err.Error())
- return
- }
- env[prefix+"HOST"] = host
- env[prefix+"USER"] = row.Username
- if password != "" {
- env[prefix+"PASS"] = password
- }
- env["NETPULSE_INTERVAL"] = strconv.Itoa(row.IntervalS) + "s"
- if err := s.integrations.WriteEnv(slug, env); err != nil {
- _ = store.SetState(slug, "error", err.Error())
- s.broadcastIntegrations()
- writeError(w, http.StatusInternalServerError, "db_error", err.Error())
- return
- }
- _ = s.integrations.Stop(slug)
- binPath := filepath.Join(s.integrations.Dir(), slug, vendor.Binary)
- if err := s.integrations.Start(slug, nil, binPath); err != nil && err != rintegration.ErrAlreadyRunning {
- _ = store.SetState(slug, "error", err.Error())
- s.broadcastIntegrations()
- writeError(w, http.StatusBadGateway, "start_failed", err.Error())
- return
- }
- } else {
- if s.pool == nil {
- writeError(w, http.StatusServiceUnavailable, "ssh_unavailable", "el servidor no tiene pool SSH")
- return
- }
- raw, err := s.pool.Run(row.Host, sshEnvReadScript(vendor), 30*time.Second)
- if err != nil {
- writeError(w, http.StatusBadGateway, "ssh_failed", err.Error())
- return
- }
- env := rintegration.ParseEnv(raw)
- env[prefix+"HOST"] = host
- env[prefix+"USER"] = row.Username
- if password != "" {
- env[prefix+"PASS"] = password
- }
- env["NETPULSE_INTERVAL"] = strconv.Itoa(row.IntervalS) + "s"
- if _, err := s.pool.Run(row.Host, sshEnvWriteScript(vendor, env), 60*time.Second); err != nil {
- _ = store.SetState(slug, "error", err.Error())
- s.broadcastIntegrations()
- writeError(w, http.StatusBadGateway, "ssh_failed", err.Error())
- return
- }
- }
- if err := store.Upsert(row); err != nil {
- writeError(w, http.StatusInternalServerError, "db_error", err.Error())
- return
- }
- _ = store.SetState(slug, "running", "")
- _ = store.SetLastStart(slug, time.Now().UnixMilli())
- s.broadcastIntegrations()
- item, _ := s.getIntegrationRow(slug)
- writeJSON(w, http.StatusOK, map[string]any{"integration": s.integrationItemOf(item)})
- }
- func (s *server) handleIntegrationDelete(w http.ResponseWriter, r *http.Request) {
- slug := r.PathValue("slug")
- row, ok := s.getIntegrationRow(slug)
- if !ok {
- writeError(w, http.StatusNotFound, "not_found")
- return
- }
- vendor, _ := rintegration.LookupVendor(row.Vendor)
- if vendor.Mode == "local" {
- if s.integrations != nil {
- _ = s.integrations.Stop(slug)
- _ = s.integrations.Cleanup(slug)
- }
- } else if s.pool != nil {
- _, _ = s.pool.Run(row.Host, sshUninstallScript(vendor), 60*time.Second)
- }
- _ = s.integrationsStore().Delete(slug)
- _ = routerstore.RemoveRouter(s.db.DB, slug)
- s.syncRouters()
- if s.db != nil {
- _, _ = s.db.Exec("DELETE FROM kv WHERE key = ?", agentTokenKey(slug))
- }
- s.broadcastIntegrations()
- w.WriteHeader(http.StatusNoContent)
- }
- // ---------------------------------------------------------------------------
- // Scripts POSIX de instalación/gestión por SSH (modo ssh, p. ej. asuswrt)
- // ---------------------------------------------------------------------------
- func sshInstallScript(v rintegration.Vendor, env map[string]string, assetURL, sha256 string) string {
- bin := "/usr/sbin/" + v.Binary
- init := "/etc/init.d/" + v.Binary
- envFile := "/etc/" + v.Binary + ".env"
- keys := make([]string, 0, len(env))
- for k := range env {
- keys = append(keys, k)
- }
- sort.Strings(keys)
- var envBlock strings.Builder
- for _, k := range keys {
- envBlock.WriteString(k)
- envBlock.WriteString("=")
- envBlock.WriteString(rintegration.EscapeEnvValue(env[k]))
- envBlock.WriteString("\n")
- }
- return `#!/bin/sh
- set -e
- BIN="` + bin + `"
- INIT="` + init + `"
- ENV_FILE="` + envFile + `"
- SERVER="` + env["NETPULSE_SERVER"] + `"
- ASSET_URL="` + assetURL + `"
- SHA256="` + sha256 + `"
- # Preflight: el router debe alcanzar el server.
- if command -v curl >/dev/null 2>&1; then
- curl -fsSL -m 10 "$SERVER/api/health" >/dev/null 2>&1 || { echo "preflight: cannot reach $SERVER/api/health"; exit 10; }
- else
- wget -q -T 10 -O /dev/null "$SERVER/api/health" || { echo "preflight: cannot reach $SERVER/api/health"; exit 10; }
- fi
- # Parar el servicio previo (el proceso vivo mantiene el binario abierto).
- [ -x "$INIT" ] && "$INIT" stop >/dev/null 2>&1 || true
- # Descargar el asset desde la URL ya verificada en el server.
- if command -v curl >/dev/null 2>&1; then
- curl -fsSL -m 600 -o /tmp/` + v.Binary + `.tar.gz "$ASSET_URL"
- else
- wget -q -T 60 -O /tmp/` + v.Binary + `.tar.gz "$ASSET_URL"
- fi
- # Verificar sha256 antes de instalar.
- GOT=$(sha256sum /tmp/` + v.Binary + `.tar.gz | awk '{print $1}')
- [ "$GOT" = "$SHA256" ] || { echo "sha256 mismatch (want $SHA256, got $GOT)"; exit 21; }
- # Extraer y mover de forma atómica (.new -> rename).
- mkdir -p /tmp/` + v.Binary + `.extract
- tar -xzf /tmp/` + v.Binary + `.tar.gz -C /tmp/` + v.Binary + `.extract
- SRC=$(find /tmp/` + v.Binary + `.extract -type f -name "` + v.Binary + `" | head -n1)
- [ -n "$SRC" ] || { echo "binary not found in archive"; exit 22; }
- cp "$SRC" "$BIN.new"
- chmod 0755 "$BIN.new"
- mv -f "$BIN.new" "$BIN"
- rm -rf /tmp/` + v.Binary + `.extract /tmp/` + v.Binary + `.tar.gz
- # Env (chmod 600): solo se escribe en el alta; los updates preservan el
- # fichero existente (la contraseña y el token solo viven ahí).
- if [ ! -f "$ENV_FILE" ]; then
- cat > "$ENV_FILE" <<'ENVEOF'
- ` + envBlock.String() + `ENVEOF
- chmod 600 "$ENV_FILE"
- fi
- # Init procd.
- cat > "$INIT" <<'INITEOF'
- #!/bin/sh /etc/rc.common
- START=99
- STOP=10
- USE_PROCD=1
- BIN="/usr/sbin/` + v.Binary + `"
- ENV_FILE="/etc/` + v.Binary + `.env"
- start_service() {
- [ -x "$BIN" ] || return 1
- procd_open_instance ` + v.Binary + `
- procd_set_param command "$BIN"
- procd_set_param respawn 3600 5 5
- procd_set_param stdout 1
- procd_set_param stderr 1
- procd_set_param file "$ENV_FILE"
- procd_close_instance
- }
- INITEOF
- chmod 0755 "$INIT"
- "$INIT" enable
- "$INIT" restart
- `
- }
- // sshEnvReadScript prints the environment file of an integration running
- // inside the router, so the server can merge changes without ever storing the
- // token or the password.
- func sshEnvReadScript(v rintegration.Vendor) string {
- return "cat /etc/" + v.Binary + ".env"
- }
- // sshEnvWriteScript replaces the environment file inside the router and
- // restarts the service. Values come already escaped by EscapeEnvValue and the
- // heredoc is quoted, so the shell does not expand anything.
- func sshEnvWriteScript(v rintegration.Vendor, env map[string]string) string {
- init := "/etc/init.d/" + v.Binary
- envFile := "/etc/" + v.Binary + ".env"
- keys := make([]string, 0, len(env))
- for k := range env {
- keys = append(keys, k)
- }
- sort.Strings(keys)
- var envBlock strings.Builder
- for _, k := range keys {
- envBlock.WriteString(k)
- envBlock.WriteString("=")
- envBlock.WriteString(rintegration.EscapeEnvValue(env[k]))
- envBlock.WriteString("\n")
- }
- return `#!/bin/sh
- set -e
- INIT="` + init + `"
- ENV_FILE="` + envFile + `"
- cat > "$ENV_FILE.new" <<'ENVEOF'
- ` + envBlock.String() + `ENVEOF
- chmod 600 "$ENV_FILE.new"
- mv -f "$ENV_FILE.new" "$ENV_FILE"
- if [ -x "$INIT" ]; then
- "$INIT" stop >/dev/null 2>&1 || true
- "$INIT" start >/dev/null 2>&1 || true
- fi
- `
- }
- func sshServiceScript(v rintegration.Vendor, action string) string {
- init := "/etc/init.d/" + v.Binary
- switch action {
- case "start":
- return "test -x " + init + " && " + init + " enable && " + init + " start || true"
- case "stop":
- return "test -x " + init + " && " + init + " stop || true"
- case "restart":
- return "test -x " + init + " && " + init + " restart || true"
- case "check":
- return "pgrep -f " + v.Binary + " >/dev/null 2>&1"
- }
- return "true"
- }
- func sshUninstallScript(v rintegration.Vendor) string {
- init := "/etc/init.d/" + v.Binary
- bin := "/usr/sbin/" + v.Binary
- envFile := "/etc/" + v.Binary + ".env"
- return fmt.Sprintf(
- "test -x %s && %s stop || true; test -x %s && %s disable || true; rm -f %s %s %s",
- init, init, init, init, init, bin, envFile,
- )
- }
|