mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 16:10:59 +08:00
fix: 重复的SSH设置
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 { // 可能防火墙已关闭等
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { http } from '@/utils'
|
||||
|
||||
export default {
|
||||
ssh: (): any => http.Get('/safe/ssh'),
|
||||
updateSsh: (status: boolean, port: number): any => http.Post('/safe/ssh', { status, port }),
|
||||
pingStatus: (): any => http.Get('/safe/ping'),
|
||||
updatePingStatus: (status: boolean): any => http.Post('/safe/ping', { status })
|
||||
}
|
||||
|
||||
@@ -6,18 +6,12 @@ import { useGettext } from 'vue3-gettext'
|
||||
const { $gettext } = useGettext()
|
||||
const model = ref({
|
||||
firewallStatus: false,
|
||||
sshStatus: false,
|
||||
pingStatus: false,
|
||||
sshPort: 22
|
||||
pingStatus: false
|
||||
})
|
||||
|
||||
useRequest(firewall.status).onSuccess(({ data }) => {
|
||||
model.value.firewallStatus = data
|
||||
})
|
||||
useRequest(safe.ssh).onSuccess(({ data }) => {
|
||||
model.value.sshStatus = data.status
|
||||
model.value.sshPort = data.port
|
||||
})
|
||||
useRequest(safe.pingStatus).onSuccess(({ data }) => {
|
||||
model.value.pingStatus = data
|
||||
})
|
||||
@@ -28,12 +22,6 @@ const handleFirewallStatus = () => {
|
||||
})
|
||||
}
|
||||
|
||||
const handleSsh = () => {
|
||||
useRequest(safe.updateSsh(model.value.sshStatus, model.value.sshPort)).onSuccess(() => {
|
||||
window.$message.success($gettext('Settings saved successfully'))
|
||||
})
|
||||
}
|
||||
|
||||
const handlePingStatus = () => {
|
||||
useRequest(safe.updatePingStatus(model.value.pingStatus)).onSuccess(() => {
|
||||
window.$message.success($gettext('Settings saved successfully'))
|
||||
@@ -46,15 +34,9 @@ const handlePingStatus = () => {
|
||||
<n-form-item path="firewall" :label="$gettext('System Firewall')">
|
||||
<n-switch v-model:value="model.firewallStatus" @update:value="handleFirewallStatus" />
|
||||
</n-form-item>
|
||||
<n-form-item path="ssh" :label="$gettext('SSH Switch')">
|
||||
<n-switch v-model:value="model.sshStatus" @update:value="handleSsh" />
|
||||
</n-form-item>
|
||||
<n-form-item path="ping" :label="$gettext('Allow Ping')">
|
||||
<n-switch v-model:value="model.pingStatus" @update:value="handlePingStatus" />
|
||||
</n-form-item>
|
||||
<n-form-item path="sshPort" :label="$gettext('SSH Port')">
|
||||
<n-input-number v-model:value="model.sshPort" @blur="handleSsh" />
|
||||
</n-form-item>
|
||||
</n-form>
|
||||
</template>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user