Initial commit — migrated from GitHub (account suspended)
This commit is contained in:
commit
09d0ee68c8
65 files changed
+23255
No files matched your search
@@ -0,0 +1,136 @@
|
||||
// Handler pembaruan sistem dan pembaruan panel (v0.4). Admin saja.
|
||||
//
|
||||
// Rute (TIDAK didaftarkan di sini — parent mendaftarkannya di routes()):
|
||||
//
|
||||
// GET /api/updates/system -> daftar paket yang bisa diperbarui (?security_only=1)
|
||||
// POST /api/updates/system/apply -> pasang paket {packages, confirm, allow_protected}
|
||||
// GET /api/updates/panel -> cek release/commit terbaru GitHub
|
||||
// POST /api/updates/panel/apply -> perbarui biner panel {confirm}
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/maspend/hostora/internal/updates"
|
||||
)
|
||||
|
||||
func (s *Server) requireAdmin(w http.ResponseWriter, r *http.Request) bool {
|
||||
if !s.isAdmin(r) {
|
||||
writeErr(w, http.StatusForbidden, "hanya admin")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// GET /api/updates/system — daftar pembaruan paket OS (admin saja).
|
||||
func (s *Server) handleSystemUpdates(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
writeErr(w, http.StatusMethodNotAllowed, "metode tidak diizinkan")
|
||||
return
|
||||
}
|
||||
if !s.requireAdmin(w, r) {
|
||||
return
|
||||
}
|
||||
securityOnly := r.URL.Query().Get("security_only") == "1"
|
||||
list, err := updates.ListUpdates(securityOnly)
|
||||
if err != nil {
|
||||
actor, ip := s.actorIP(r)
|
||||
s.log(actor, "updates.system.list", "", ip, "gagal", err.Error())
|
||||
writeErr(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if list == nil {
|
||||
list = []updates.PackageUpdate{}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"manager": updates.DetectManager(),
|
||||
"security_only": securityOnly,
|
||||
"updates": list,
|
||||
})
|
||||
}
|
||||
|
||||
// POST /api/updates/system/apply — pasang paket (admin saja).
|
||||
// confirm wajib true; paket kernel/panel ditolak kecuali
|
||||
// allow_protected=true.
|
||||
func (s *Server) handleSystemUpdatesApply(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeErr(w, http.StatusMethodNotAllowed, "metode tidak diizinkan")
|
||||
return
|
||||
}
|
||||
if !s.requireAdmin(w, r) {
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Packages []string `json:"packages"`
|
||||
Confirm bool `json:"confirm"`
|
||||
AllowProtected bool `json:"allow_protected"`
|
||||
}
|
||||
if err := readJSON(r, &in); err != nil {
|
||||
writeErr(w, http.StatusBadRequest, "JSON tidak valid")
|
||||
return
|
||||
}
|
||||
actor, ip := s.actorIP(r)
|
||||
out, err := updates.ApplyUpdates(in.Packages, in.Confirm, in.AllowProtected)
|
||||
if err != nil {
|
||||
s.log(actor, "updates.system.apply", strings.Join(in.Packages, ","), ip, "gagal", err.Error())
|
||||
writeErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
s.log(actor, "updates.system.apply", strings.Join(in.Packages, ","), ip, "ok", "paket dipasang")
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "output": out})
|
||||
}
|
||||
|
||||
// GET /api/updates/panel — cek versi terbaru (admin saja).
|
||||
func (s *Server) handlePanelUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
writeErr(w, http.StatusMethodNotAllowed, "metode tidak diizinkan")
|
||||
return
|
||||
}
|
||||
if !s.requireAdmin(w, r) {
|
||||
return
|
||||
}
|
||||
info, err := updates.CheckLatest(version)
|
||||
if err != nil {
|
||||
actor, ip := s.actorIP(r)
|
||||
s.log(actor, "updates.panel.check", "", ip, "gagal", err.Error())
|
||||
writeErr(w, http.StatusBadGateway, err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, info)
|
||||
}
|
||||
|
||||
// POST /api/updates/panel/apply — perbarui biner panel (admin saja).
|
||||
// confirm wajib true. Data dan akun tidak disentuh.
|
||||
func (s *Server) handlePanelUpdateApply(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
writeErr(w, http.StatusMethodNotAllowed, "metode tidak diizinkan")
|
||||
return
|
||||
}
|
||||
if !s.requireAdmin(w, r) {
|
||||
return
|
||||
}
|
||||
var in struct {
|
||||
Confirm bool `json:"confirm"`
|
||||
}
|
||||
if err := readJSON(r, &in); err != nil {
|
||||
writeErr(w, http.StatusBadRequest, "JSON tidak valid")
|
||||
return
|
||||
}
|
||||
actor, ip := s.actorIP(r)
|
||||
res, err := updates.UpdatePanel(updates.PanelUpdateOptions{
|
||||
InstalledVersion: version,
|
||||
BinaryPath: "/opt/hostora/bin/hostora",
|
||||
PanelPort: s.port,
|
||||
Confirm: in.Confirm,
|
||||
})
|
||||
if err != nil {
|
||||
s.log(actor, "updates.panel.apply", "", ip, "gagal", err.Error())
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]any{
|
||||
"ok": false, "restored": res.Restored, "detail": res.Detail, "error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
s.log(actor, "updates.panel.apply", "", ip, "ok", res.Detail)
|
||||
writeJSON(w, http.StatusOK, res)
|
||||
}
|
||||
Reference in new issue
Block a user