package httpapi import ( "net/http" "strconv" "time" "github.com/miaopanel/miaopanel/internal/system" ) // handleSystemOverview is the single call the dashboard needs for its summary // strip: site health, certificate alerts and today's WAF activity. func (s *Server) handleSystemOverview(w http.ResponseWriter, r *http.Request) { ctx := r.Context() sites, err := s.db.ListSites(ctx) if err != nil { writeErr(w, err) return } names := make(map[int64]string, len(sites)) protected, apps := 0, 0 for _, st := range sites { names[st.ID] = st.Name if st.Options.WAF { protected++ } if st.Kind == "go" || st.Kind == "node" || st.Kind == "proxy" { apps++ } } type certAlert struct { SiteID int64 `json:"site_id"` Site string `json:"site"` Domains string `json:"domains"` NotAfter int64 `json:"not_after"` DaysLeft int `json:"days_left"` } alerts := []certAlert{} now := time.Now() if certs, err := s.db.ListSSLCerts(ctx); err == nil { for _, c := range certs { days := int(time.Unix(c.NotAfter, 0).Sub(now).Hours() / 24) if days > 30 { continue } name := names[c.SiteID] if name == "" { name = "已删除的站点" } alerts = append(alerts, certAlert{ SiteID: c.SiteID, Site: name, Domains: c.Domains, NotAfter: c.NotAfter, DaysLeft: days, }) } } dayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) blocked := int64(0) if totals, err := s.db.WAFTotals(ctx, dayStart.Unix(), now.Unix()+1); err == nil { blocked = totals.Blocked } pending := 0 if s.apps != nil { for _, t := range s.apps.Tasks(20) { if t.Status == "running" { pending++ } } } writeOK(w, map[string]any{ "sites": len(sites), "app_sites": apps, "waf_sites": protected, "nginx": s.nginx.Available(), "preview": !s.nginx.Available(), "waf_blocked": blocked, "cert_expiring": alerts, "running_tasks": pending, "panel_version": s.version, }) } func (s *Server) handleSystemInfo(w http.ResponseWriter, r *http.Request) { snap := system.Collect("/") writeOK(w, snap) } func (s *Server) handleDistro(w http.ResponseWriter, r *http.Request) { writeOK(w, system.Detect()) } // handleSystemMeta exposes panel capabilities the UI needs to hide // unsupported features (for example local terminals on non Linux hosts). func (s *Server) handleSystemMeta(w http.ResponseWriter, r *http.Request) { writeOK(w, map[string]any{ "version": s.version, "files_root": s.cfg.WebRoot, "terminal": s.distro.PanelOS == "linux", "entry": s.cfg.Entry, "nginx": s.nginx.Available(), "secure_cookie": s.cfg.SecureCookie, "https": s.cfg.HTTPSEnabled(), }) } func (s *Server) handleRuntimes(w http.ResponseWriter, r *http.Request) { writeOK(w, system.DetectRuntimes()) } func (s *Server) handleAudit(w http.ResponseWriter, r *http.Request) { limit, _ := strconv.Atoi(r.URL.Query().Get("limit")) rows, err := s.db.RecentAudit(r.Context(), limit) if err != nil { writeErr(w, err) return } if rows == nil { rows = []map[string]any{} } writeOK(w, rows) } func (s *Server) handleReboot(w http.ResponseWriter, r *http.Request) { var req struct { Confirm string `json:"confirm"` } if err := decodeJSON(w, r, &req, 4<<10); err != nil { writeErr(w, err) return } // The extra confirmation phrase prevents a stray JSON client from // power-cycling the host without an explicit human decision. if req.Confirm != "REBOOT" { writeJSON(w, http.StatusBadRequest, Envelope{Code: 400, Msg: "确认信息不正确"}) return } started := time.Now() mode, err := system.Reboot(r.Context()) if err != nil { writeJSON(w, http.StatusBadRequest, Envelope{Code: 400, Msg: err.Error()}) return } s.audit(r, "system", "reboot", "host", "ok", started) writeOK(w, map[string]any{"scheduled": true, "mode": mode}) } func (s *Server) handleNginxStatus(w http.ResponseWriter, r *http.Request) { out, err := s.nginx.Validate(r.Context()) writeOK(w, map[string]any{ "available": s.nginx.Available(), "ok": err == nil, "output": out, "bin": s.cfg.Nginx.Bin, "vhost_dir": s.cfg.Nginx.VhostDir, }) }