From a6e12d88a3f32c54a50e3584fc9ec875e3365be3 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Thu, 7 Dec 2023 14:40:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B7=A5=E5=85=B7=E7=AE=B1=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E9=98=B2=E6=AD=A2=E5=91=BD=E4=BB=A4=E6=B3=A8=E5=85=A5?= =?UTF-8?q?=20(#3215)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/dto/fail2ban.go | 3 +- backend/app/service/device.go | 15 +++ backend/app/service/fail2ban.go | 5 + backend/utils/toolbox/fail2ban.go | 12 ++- frontend/src/lang/modules/en.ts | 2 + frontend/src/lang/modules/tw.ts | 2 + frontend/src/lang/modules/zh.ts | 2 + .../src/views/toolbox/device/swap/index.vue | 3 +- frontend/src/views/toolbox/fail2ban/index.vue | 15 +++ .../views/toolbox/fail2ban/log-path/index.vue | 102 ++++++++++++++++++ 10 files changed, 157 insertions(+), 4 deletions(-) create mode 100644 frontend/src/views/toolbox/fail2ban/log-path/index.vue diff --git a/backend/app/dto/fail2ban.go b/backend/app/dto/fail2ban.go index b96898d5c..3f6881ce1 100644 --- a/backend/app/dto/fail2ban.go +++ b/backend/app/dto/fail2ban.go @@ -10,6 +10,7 @@ type Fail2BanBaseInfo struct { BanTime string `json:"banTime"` FindTime string `json:"findTime"` BanAction string `json:"banAction"` + LogPath string `json:"logPath"` } type Fail2BanSearch struct { @@ -17,7 +18,7 @@ type Fail2BanSearch struct { } type Fail2BanUpdate struct { - Key string `json:"key" validate:"required,oneof=port bantime findtime maxretry banaction"` + Key string `json:"key" validate:"required,oneof=port bantime findtime maxretry banaction logpath"` Value string `json:"value"` } diff --git a/backend/app/service/device.go b/backend/app/service/device.go index 3630a5381..9f1442478 100644 --- a/backend/app/service/device.go +++ b/backend/app/service/device.go @@ -109,6 +109,9 @@ func (u *DeviceService) CheckDNS(key, value string) (bool, error) { func (u *DeviceService) Update(key, value string) error { switch key { case "TimeZone": + if cmd.CheckIllegal(value) { + return buserr.New(constant.ErrCmdIllegal) + } if err := ntp.UpdateSystemTimeZone(value); err != nil { return err } @@ -123,11 +126,17 @@ func (u *DeviceService) Update(key, value string) error { return err } case "Hostname": + if cmd.CheckIllegal(value) { + return buserr.New(constant.ErrCmdIllegal) + } std, err := cmd.Execf("%s hostnamectl set-hostname %s", cmd.SudoHandleCmd(), value) if err != nil { return errors.New(std) } case "Ntp", "LocalTime": + if cmd.CheckIllegal(value) { + return buserr.New(constant.ErrCmdIllegal) + } ntpValue := value if key == "LocalTime" { ntpItem, err := settingRepo.Get(settingRepo.WithByKey("NtpSite")) @@ -193,6 +202,9 @@ func (u *DeviceService) UpdateHosts(req []dto.HostHelper) error { } func (u *DeviceService) UpdatePasswd(req dto.ChangePasswd) error { + if cmd.CheckIllegal(req.User, req.Passwd) { + return buserr.New(constant.ErrCmdIllegal) + } std, err := cmd.Execf("%s echo '%s:%s' | %s chpasswd", cmd.SudoHandleCmd(), req.User, req.Passwd, cmd.SudoHandleCmd()) if err != nil { if strings.Contains(err.Error(), "does not exist") { @@ -204,6 +216,9 @@ func (u *DeviceService) UpdatePasswd(req dto.ChangePasswd) error { } func (u *DeviceService) UpdateSwap(req dto.SwapHelper) error { + if cmd.CheckIllegal(req.Path) { + return buserr.New(constant.ErrCmdIllegal) + } if !req.IsNew { std, err := cmd.Execf("%s swapoff %s", cmd.SudoHandleCmd(), req.Path) if err != nil { diff --git a/backend/app/service/fail2ban.go b/backend/app/service/fail2ban.go index 7741e5a1d..7b151cb36 100644 --- a/backend/app/service/fail2ban.go +++ b/backend/app/service/fail2ban.go @@ -208,4 +208,9 @@ func loadFailValue(line string, baseInfo *dto.Fail2BanBaseInfo) { itemValue = strings.ReplaceAll(itemValue, "=", "") baseInfo.BanAction = strings.TrimSpace(itemValue) } + if strings.HasPrefix(line, "logpath") { + itemValue := strings.ReplaceAll(line, "logpath", "") + itemValue = strings.ReplaceAll(itemValue, "=", "") + baseInfo.LogPath = strings.TrimSpace(itemValue) + } } diff --git a/backend/utils/toolbox/fail2ban.go b/backend/utils/toolbox/fail2ban.go index 7e90dfc45..9ef42b2a4 100644 --- a/backend/utils/toolbox/fail2ban.go +++ b/backend/utils/toolbox/fail2ban.go @@ -94,7 +94,7 @@ func (f *Fail2ban) ReBanIPs(ips []string) error { func (f *Fail2ban) ListBanned() ([]string, error) { var lists []string - stdout, err := cmd.Exec("fail2ban-client get sshd banned") + stdout, err := cmd.Exec("fail2ban-client get sshd banip") if err != nil { return lists, err } @@ -147,7 +147,7 @@ maxretry = 5 findtime = 300 bantime = 600 action = %(action_mwl)s -logpath = /var/log/secure` +logpath = $logpath` banaction := "" if active, _ := systemctl.IsActive("firewalld"); active { @@ -158,6 +158,14 @@ logpath = /var/log/secure` banaction = "iptables-allports" } initFile = strings.ReplaceAll(initFile, "$banaction", banaction) + + logPath := "" + if _, err := os.Stat("/var/log/secure"); err == nil { + logPath = "/var/log/secure" + } else { + logPath = "/var/log/auth.log" + } + initFile = strings.ReplaceAll(initFile, "$logpath", logPath) if err := os.WriteFile(defaultPath, []byte(initFile), 0640); err != nil { return err } diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index 604dc7861..917e19424 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -965,6 +965,8 @@ const message = { allPorts: ' (All Ports)', ignoreIP: 'IP Whitelist', bannedIP: 'IP Blacklist', + logPath: 'Log Path', + logPathHelper: 'Default is /var/log/secure or /var/log/auth.log', }, }, logs: { diff --git a/frontend/src/lang/modules/tw.ts b/frontend/src/lang/modules/tw.ts index a90bb246a..deb56ba2d 100644 --- a/frontend/src/lang/modules/tw.ts +++ b/frontend/src/lang/modules/tw.ts @@ -916,6 +916,8 @@ const message = { allPorts: ' (所有端口)', ignoreIP: 'IP 白名單', bannedIP: 'IP 黑名單', + logPath: '日誌路徑', + logPathHelper: '預設為 /var/log/secure 或者 /var/log/auth.log', }, }, logs: { diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index a6376ebea..a1a8ef40b 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -917,6 +917,8 @@ const message = { allPorts: ' (所有端口)', ignoreIP: 'IP 白名单', bannedIP: 'IP 黑名单', + logPath: '日志路径', + logPathHelper: '默认 /var/log/secure 或者 /var/log/auth.log', }, }, logs: { diff --git a/frontend/src/views/toolbox/device/swap/index.vue b/frontend/src/views/toolbox/device/swap/index.vue index b68fc419b..f6d4e251a 100644 --- a/frontend/src/views/toolbox/device/swap/index.vue +++ b/frontend/src/views/toolbox/device/swap/index.vue @@ -61,7 +61,7 @@ - + @@ -202,6 +202,7 @@ const onSave = async (row) => { }; const loadItemSize = (row: any) => { + console.log(row.size, row.sizeUnit); switch (row.sizeUnit) { case 'KB': return row.size; diff --git a/frontend/src/views/toolbox/fail2ban/index.vue b/frontend/src/views/toolbox/fail2ban/index.vue index bcb83925b..fa45d2a87 100644 --- a/frontend/src/views/toolbox/fail2ban/index.vue +++ b/frontend/src/views/toolbox/fail2ban/index.vue @@ -102,6 +102,15 @@ + + + + + {{ $t('commons.button.set') }} + + + + @@ -154,6 +163,7 @@ + @@ -168,6 +178,7 @@ import MaxRetry from '@/views/toolbox/fail2ban/max-retry/index.vue'; import BanTime from '@/views/toolbox/fail2ban/ban-time/index.vue'; import FindTime from '@/views/toolbox/fail2ban/find-time/index.vue'; import BanAction from '@/views/toolbox/fail2ban/ban-action/index.vue'; +import LogPath from '@/views/toolbox/fail2ban/log-path/index.vue'; import IPs from '@/views/toolbox/fail2ban/ips/index.vue'; import i18n from '@/lang'; import { MsgSuccess } from '@/utils/message'; @@ -185,6 +196,7 @@ const banTimeRef = ref(); const findTimeRef = ref(); const banActionRef = ref(); const listRef = ref(); +const logPathRef = ref(); const autoStart = ref('enable'); @@ -242,6 +254,9 @@ const onChangeFindTime = () => { const onChangeBanAction = () => { banActionRef.value.acceptParams({ banAction: form.banAction }); }; +const onChangeLogPath = () => { + logPathRef.value.acceptParams({ logPath: form.logPath }); +}; const onOperate = async (operation: string) => { let msg = operation === 'enable' || operation === 'disable' ? 'ssh.' : 'commons.button.'; diff --git a/frontend/src/views/toolbox/fail2ban/log-path/index.vue b/frontend/src/views/toolbox/fail2ban/log-path/index.vue new file mode 100644 index 000000000..8aafe87c1 --- /dev/null +++ b/frontend/src/views/toolbox/fail2ban/log-path/index.vue @@ -0,0 +1,102 @@ + + + + + + + + + + + + + + + + {{ $t('toolbox.fail2ban.logPathHelper') }} + + + + + + + {{ $t('commons.button.cancel') }} + + {{ $t('commons.button.confirm') }} + + + + + + +