miaopanel
1package httpapi
2
3import (
4 "net/http"
5 "strconv"
6 "time"
7
8 "github.com/miaopanel/miaopanel/internal/system"
9)
10
11// handleSystemOverview is the single call the dashboard needs for its summary
12// strip: site health, certificate alerts and today's WAF activity.
13func (s *Server) handleSystemOverview(w http.ResponseWriter, r *http.Request) {
14 ctx := r.Context()
15 sites, err := s.db.ListSites(ctx)
16 if err != nil {
17 writeErr(w, err)
18 return
19 }
20 names := make(map[int64]string, len(sites))
21 protected, apps := 0, 0
22 for _, st := range sites {
23 names[st.ID] = st.Name
24 if st.Options.WAF {
25 protected++
26 }
27 if st.Kind == "go" || st.Kind == "node" || st.Kind == "proxy" {
28 apps++
29 }
30 }
31
32 type certAlert struct {
33 SiteID int64 `json:"site_id"`
34 Site string `json:"site"`
35 Domains string `json:"domains"`
36 NotAfter int64 `json:"not_after"`
37 DaysLeft int `json:"days_left"`
38 }
39 alerts := []certAlert{}
40 now := time.Now()
41 if certs, err := s.db.ListSSLCerts(ctx); err == nil {
42 for _, c := range certs {
43 days := int(time.Unix(c.NotAfter, 0).Sub(now).Hours() / 24)
44 if days > 30 {
45 continue
46 }
47 name := names[c.SiteID]
48 if name == "" {
49 name = "已删除的站点"
50 }
51 alerts = append(alerts, certAlert{
52 SiteID: c.SiteID, Site: name, Domains: c.Domains,
53 NotAfter: c.NotAfter, DaysLeft: days,
54 })
55 }
56 }
57
58 dayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
59 blocked := int64(0)
60 if totals, err := s.db.WAFTotals(ctx, dayStart.Unix(), now.Unix()+1); err == nil {
61 blocked = totals.Blocked
62 }
63 pending := 0
64 if s.apps != nil {
65 for _, t := range s.apps.Tasks(20) {
66 if t.Status == "running" {
67 pending++
68 }
69 }
70 }
71
72 writeOK(w, map[string]any{
73 "sites": len(sites),
74 "app_sites": apps,
75 "waf_sites": protected,
76 "nginx": s.nginx.Available(),
77 "preview": !s.nginx.Available(),
78 "waf_blocked": blocked,
79 "cert_expiring": alerts,
80 "running_tasks": pending,
81 "panel_version": s.version,
82 })
83}
84
85func (s *Server) handleSystemInfo(w http.ResponseWriter, r *http.Request) {
86 snap := system.Collect("/")
87 writeOK(w, snap)
88}
89
90func (s *Server) handleDistro(w http.ResponseWriter, r *http.Request) {
91 writeOK(w, system.Detect())
92}
93
94// handleSystemMeta exposes panel capabilities the UI needs to hide
95// unsupported features (for example local terminals on non Linux hosts).
96func (s *Server) handleSystemMeta(w http.ResponseWriter, r *http.Request) {
97 writeOK(w, map[string]any{
98 "version": s.version,
99 "files_root": s.cfg.WebRoot,
100 "terminal": s.distro.PanelOS == "linux",
101 "entry": s.cfg.Entry,
102 "nginx": s.nginx.Available(),
103 "secure_cookie": s.cfg.SecureCookie,
104 "https": s.cfg.HTTPSEnabled(),
105 })
106}
107
108func (s *Server) handleRuntimes(w http.ResponseWriter, r *http.Request) {
109 writeOK(w, system.DetectRuntimes())
110}
111
112func (s *Server) handleAudit(w http.ResponseWriter, r *http.Request) {
113 limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
114 rows, err := s.db.RecentAudit(r.Context(), limit)
115 if err != nil {
116 writeErr(w, err)
117 return
118 }
119 if rows == nil {
120 rows = []map[string]any{}
121 }
122 writeOK(w, rows)
123}
124
125func (s *Server) handleReboot(w http.ResponseWriter, r *http.Request) {
126 var req struct {
127 Confirm string `json:"confirm"`
128 }
129 if err := decodeJSON(w, r, &req, 4<<10); err != nil {
130 writeErr(w, err)
131 return
132 }
133 // The extra confirmation phrase prevents a stray JSON client from
134 // power-cycling the host without an explicit human decision.
135 if req.Confirm != "REBOOT" {
136 writeJSON(w, http.StatusBadRequest, Envelope{Code: 400, Msg: "确认信息不正确"})
137 return
138 }
139 started := time.Now()
140 mode, err := system.Reboot(r.Context())
141 if err != nil {
142 writeJSON(w, http.StatusBadRequest, Envelope{Code: 400, Msg: err.Error()})
143 return
144 }
145 s.audit(r, "system", "reboot", "host", "ok", started)
146 writeOK(w, map[string]any{"scheduled": true, "mode": mode})
147}
148
149func (s *Server) handleNginxStatus(w http.ResponseWriter, r *http.Request) {
150 out, err := s.nginx.Validate(r.Context())
151 writeOK(w, map[string]any{
152 "available": s.nginx.Available(),
153 "ok": err == nil,
154 "output": out,
155 "bin": s.cfg.Nginx.Bin,
156 "vhost_dir": s.cfg.Nginx.VhostDir,
157 })
158}