2
0
mirror of https://github.com/acepanel/panel.git synced 2026-02-04 11:27:17 +08:00

fix: 重复的SSH设置

This commit is contained in:
2026-01-23 05:44:19 +08:00
parent 7f2c693735
commit 6811eb290f
6 changed files with 1 additions and 96 deletions

View File

@@ -3,8 +3,6 @@ package biz
import "context"
type SafeRepo interface {
GetSSH() (uint, bool, error)
UpdateSSH(ctx context.Context, port uint, status bool) error
GetPingStatus() (bool, error)
UpdatePingStatus(ctx context.Context, status bool) error
}

View File

@@ -6,13 +6,10 @@ import (
"log/slog"
"strings"
"github.com/spf13/cast"
"github.com/acepanel/panel/internal/biz"
"github.com/acepanel/panel/pkg/firewall"
"github.com/acepanel/panel/pkg/os"
"github.com/acepanel/panel/pkg/shell"
"github.com/acepanel/panel/pkg/systemctl"
)
type safeRepo struct {
@@ -33,45 +30,6 @@ func NewSafeRepo(log *slog.Logger) biz.SafeRepo {
}
}
func (r *safeRepo) GetSSH() (uint, bool, error) {
out, err := shell.Execf("cat /etc/ssh/sshd_config | grep 'Port ' | awk '{print $2}'")
if err != nil {
return 0, false, err
}
running, err := systemctl.Status(r.ssh)
if err != nil {
return 0, false, err
}
return cast.ToUint(out), running, nil
}
func (r *safeRepo) UpdateSSH(ctx context.Context, port uint, status bool) error {
oldPort, err := shell.Execf("cat /etc/ssh/sshd_config | grep 'Port ' | awk '{print $2}'")
if err != nil {
return err
}
_, _ = shell.Execf("sed -i 's/#Port %s/Port %d/g' /etc/ssh/sshd_config", oldPort, port)
_, _ = shell.Execf("sed -i 's/Port %s/Port %d/g' /etc/ssh/sshd_config", oldPort, port)
if !status {
if err = systemctl.Stop(r.ssh); err != nil {
return err
}
} else {
if err = systemctl.Restart(r.ssh); err != nil {
return err
}
}
// 记录日志
r.log.Info("ssh settings updated", slog.String("type", biz.OperationTypeSafe), slog.Uint64("operator_id", getOperatorID(ctx)), slog.Uint64("port", uint64(port)), slog.Bool("status", status))
return nil
}
func (r *safeRepo) GetPingStatus() (bool, error) {
out, err := shell.Execf(`firewall-cmd --list-rich-rules`)
if err != nil { // 可能防火墙已关闭等

View File

@@ -382,8 +382,6 @@ func (route *Http) Register(r *chi.Mux) {
})
r.Route("/safe", func(r chi.Router) {
r.Get("/ssh", route.safe.GetSSH)
r.Post("/ssh", route.safe.UpdateSSH)
r.Get("/ping", route.safe.GetPingStatus)
r.Post("/ping", route.safe.UpdatePingStatus)
})

View File

@@ -3,8 +3,6 @@ package service
import (
"net/http"
"github.com/libtnb/chix"
"github.com/acepanel/panel/internal/biz"
"github.com/acepanel/panel/internal/http/request"
)
@@ -19,33 +17,6 @@ func NewSafeService(safe biz.SafeRepo) *SafeService {
}
}
func (s *SafeService) GetSSH(w http.ResponseWriter, r *http.Request) {
port, status, err := s.safeRepo.GetSSH()
if err != nil {
Error(w, http.StatusInternalServerError, "%v", err)
return
}
Success(w, chix.M{
"port": port,
"status": status,
})
}
func (s *SafeService) UpdateSSH(w http.ResponseWriter, r *http.Request) {
req, err := Bind[request.SafeUpdateSSH](r)
if err != nil {
Error(w, http.StatusUnprocessableEntity, "%v", err)
return
}
if err = s.safeRepo.UpdateSSH(r.Context(), req.Port, req.Status); err != nil {
Error(w, http.StatusInternalServerError, "%v", err)
return
}
Success(w, nil)
}
func (s *SafeService) GetPingStatus(w http.ResponseWriter, r *http.Request) {
status, err := s.safeRepo.GetPingStatus()
if err != nil {