From 733889407eb0722fa37441d68fb38dda272c47f4 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Wed, 20 Dec 2023 13:46:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E5=9F=9F=E6=8D=A2=E8=A1=8C=E7=AC=A6=E6=A0=A1=E9=AA=8C=20(#3399?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/firewall.go | 6 ++++++ backend/utils/firewall/client.go | 1 + backend/utils/firewall/client/firewalld.go | 8 ++++++++ backend/utils/firewall/client/ufw.go | 8 ++++++++ backend/utils/toolbox/fail2ban.go | 2 +- frontend/src/views/container/image/pull/index.vue | 2 +- frontend/src/views/container/image/push/index.vue | 2 +- frontend/src/views/container/image/tag/index.vue | 2 +- .../src/views/container/setting/mirror/index.vue | 3 +++ .../src/views/container/setting/registry/index.vue | 3 +++ frontend/src/views/setting/safe/allowips/index.vue | 13 +++++++++++-- frontend/src/views/toolbox/fail2ban/ips/index.vue | 12 +++++++++++- 12 files changed, 55 insertions(+), 7 deletions(-) diff --git a/backend/app/service/firewall.go b/backend/app/service/firewall.go index 85b180807..84aa829f9 100644 --- a/backend/app/service/firewall.go +++ b/backend/app/service/firewall.go @@ -196,6 +196,12 @@ func (u *FirewallService) OperateFirewall(operation string) error { } _, _ = cmd.Exec("systemctl restart docker") return nil + case "restart": + if err := client.Restart(); err != nil { + return err + } + _, _ = cmd.Exec("systemctl restart docker") + return nil case "disablePing": return u.updatePingStatus("0") case "enablePing": diff --git a/backend/utils/firewall/client.go b/backend/utils/firewall/client.go index 18ac3825a..f2512bcd6 100644 --- a/backend/utils/firewall/client.go +++ b/backend/utils/firewall/client.go @@ -12,6 +12,7 @@ type FirewallClient interface { Name() string // ufw firewalld Start() error Stop() error + Restart() error Reload() error Status() (string, error) // running not running Version() (string, error) diff --git a/backend/utils/firewall/client/firewalld.go b/backend/utils/firewall/client/firewalld.go index 3ceb23812..569e286d0 100644 --- a/backend/utils/firewall/client/firewalld.go +++ b/backend/utils/firewall/client/firewalld.go @@ -52,6 +52,14 @@ func (f *Firewall) Stop() error { return nil } +func (f *Firewall) Restart() error { + stdout, err := cmd.Exec("systemctl restart firewalld") + if err != nil { + return fmt.Errorf("restart the firewall failed, err: %s", stdout) + } + return nil +} + func (f *Firewall) Reload() error { stdout, err := cmd.Exec("firewall-cmd --reload") if err != nil { diff --git a/backend/utils/firewall/client/ufw.go b/backend/utils/firewall/client/ufw.go index 3a00fedaf..ac006e114 100644 --- a/backend/utils/firewall/client/ufw.go +++ b/backend/utils/firewall/client/ufw.go @@ -64,6 +64,14 @@ func (f *Ufw) Stop() error { return nil } +func (f *Ufw) Restart() error { + stdout, err := cmd.Execf("%s systemctl restart ufw.service", cmd.SudoHandleCmd()) + if err != nil { + return fmt.Errorf("restart the firewall failed, err: %s", stdout) + } + return nil +} + func (f *Ufw) Reload() error { return nil } diff --git a/backend/utils/toolbox/fail2ban.go b/backend/utils/toolbox/fail2ban.go index 2c9cce169..74ecdccbd 100644 --- a/backend/utils/toolbox/fail2ban.go +++ b/backend/utils/toolbox/fail2ban.go @@ -97,7 +97,7 @@ func (f *Fail2ban) ListBanned() ([]string, error) { if err != nil { return lists, err } - itemList := strings.Split(stdout, " ") + itemList := strings.Split(strings.ReplaceAll(stdout, "\n", ""), " ") for _, item := range itemList { if len(item) != 0 { lists = append(lists, item) diff --git a/frontend/src/views/container/image/pull/index.vue b/frontend/src/views/container/image/pull/index.vue index b49a40352..7d03697d1 100644 --- a/frontend/src/views/container/image/pull/index.vue +++ b/frontend/src/views/container/image/pull/index.vue @@ -21,7 +21,7 @@ :rules="Rules.requiredSelect" prop="repoID" > - + diff --git a/frontend/src/views/container/image/push/index.vue b/frontend/src/views/container/image/push/index.vue index 658cbca20..29fa23529 100644 --- a/frontend/src/views/container/image/push/index.vue +++ b/frontend/src/views/container/image/push/index.vue @@ -18,7 +18,7 @@ - + - + diff --git a/frontend/src/views/container/setting/mirror/index.vue b/frontend/src/views/container/setting/mirror/index.vue index ffb9f1409..1370ec022 100644 --- a/frontend/src/views/container/setting/mirror/index.vue +++ b/frontend/src/views/container/setting/mirror/index.vue @@ -70,6 +70,9 @@ function checkMirrors(rule: any, value: any, callback: any) { const reg = /^https?:\/\/[a-zA-Z0-9.-]+$/; let mirrors = form.mirrors.split('\n'); for (const item of mirrors) { + if (item === '') { + continue; + } if (!reg.test(item)) { return callback(new Error(i18n.global.t('commons.rule.mirror'))); } diff --git a/frontend/src/views/container/setting/registry/index.vue b/frontend/src/views/container/setting/registry/index.vue index cc9af7777..aab561535 100644 --- a/frontend/src/views/container/setting/registry/index.vue +++ b/frontend/src/views/container/setting/registry/index.vue @@ -70,6 +70,9 @@ function checkRegistries(rule: any, value: any, callback: any) { const reg = /^[a-zA-Z0-9]{1}[a-z:A-Z0-9_/.-]{0,150}$/; let regis = form.registries.split('\n'); for (const item of regis) { + if (item === '') { + continue; + } if (!reg.test(item)) { return callback(new Error(i18n.global.t('commons.rule.imageName'))); } diff --git a/frontend/src/views/setting/safe/allowips/index.vue b/frontend/src/views/setting/safe/allowips/index.vue index bee3ec064..de81f58d1 100644 --- a/frontend/src/views/setting/safe/allowips/index.vue +++ b/frontend/src/views/setting/safe/allowips/index.vue @@ -58,6 +58,9 @@ function checkAddress(rule: any, value: any, callback: any) { if (form.allowIPs !== '') { let addrs = form.allowIPs.split('\n'); for (const item of addrs) { + if (item === '') { + continue; + } if (item === '0.0.0.0') { return callback(new Error(i18n.global.t('firewall.addressFormatError'))); } @@ -102,8 +105,14 @@ const onSave = async (formEl: FormInstance | undefined) => { type: 'info', }).then(async () => { loading.value = true; - - await updateSetting({ key: 'AllowIPs', value: form.allowIPs.replaceAll('\n', ',') }) + let ipItems = form.allowIPs.split('\n'); + let ips = []; + for (const item of ipItems) { + if (item !== '') { + ips.push(item); + } + } + await updateSetting({ key: 'AllowIPs', value: ips.join(',') }) .then(() => { loading.value = false; MsgSuccess(i18n.global.t('commons.msg.operationSuccess')); diff --git a/frontend/src/views/toolbox/fail2ban/ips/index.vue b/frontend/src/views/toolbox/fail2ban/ips/index.vue index 59c20d541..b30495ff8 100644 --- a/frontend/src/views/toolbox/fail2ban/ips/index.vue +++ b/frontend/src/views/toolbox/fail2ban/ips/index.vue @@ -49,6 +49,9 @@ function checkIPs(rule: any, value: any, callback: any) { if (form.ips !== '') { let addr = form.ips.split('\n'); for (const item of addr) { + if (item === '') { + continue; + } if (item.indexOf('/') !== -1) { if (checkCidr(item)) { return callback(new Error(i18n.global.t('firewall.addressFormatError'))); @@ -95,9 +98,16 @@ const onSave = async (formEl: FormInstance | undefined) => { type: 'info', }, ).then(async () => { + let ipItems = form.ips.split('\n'); + let ips = []; + for (const item of ipItems) { + if (item !== '') { + ips.push(item); + } + } let param = { operate: form.operate, - ips: form.ips.split('\n'), + ips: ips, }; loading.value = true; await operatorFail2banSSHD(param)