diff --git a/backend/app/service/firewall.go b/backend/app/service/firewall.go index 556c088f5..d5cb6be12 100644 --- a/backend/app/service/firewall.go +++ b/backend/app/service/firewall.go @@ -369,7 +369,7 @@ func (u *FirewallService) pingStatus() string { return constant.StatusNone } commond := "cat /etc/sysctl.conf | grep net/ipv4/icmp_echo_ignore_all= " - if cmd.HasSudo() { + if cmd.HasNoPasswordSudo() { commond = "sudo cat /etc/sysctl.conf | grep net/ipv4/icmp_echo_ignore_all= " } stdout, _ := cmd.Exec(commond) @@ -409,7 +409,7 @@ func (u *FirewallService) updatePingStatus(enabel string) error { } commond := "sysctl -p" - if cmd.HasSudo() { + if cmd.HasNoPasswordSudo() { commond = "sudo sysctl -p" } stdout, err := cmd.Exec(commond) diff --git a/backend/utils/cmd/cmd.go b/backend/utils/cmd/cmd.go index 7c28556b2..9425d4851 100644 --- a/backend/utils/cmd/cmd.go +++ b/backend/utils/cmd/cmd.go @@ -100,11 +100,3 @@ func HasNoPasswordSudo() bool { err2 := cmd2.Run() return err2 == nil } - -func HasSudo() bool { - cmd := exec.Command("sudo", "-nv") - if err := cmd.Run(); err != nil { - return false - } - return true -} diff --git a/backend/utils/firewall/client/ufw.go b/backend/utils/firewall/client/ufw.go index dd431300b..4bbb592e3 100644 --- a/backend/utils/firewall/client/ufw.go +++ b/backend/utils/firewall/client/ufw.go @@ -13,7 +13,7 @@ type Ufw struct { func NewUfw() (*Ufw, error) { var ufw Ufw - if cmd.HasSudo() { + if cmd.HasNoPasswordSudo() { ufw.CmdStr = "sudo ufw" } else { ufw.CmdStr = "ufw" diff --git a/frontend/src/views/host/firewall/port/operate/index.vue b/frontend/src/views/host/firewall/port/operate/index.vue index bf5344f7f..93e1b1679 100644 --- a/frontend/src/views/host/firewall/port/operate/index.vue +++ b/frontend/src/views/host/firewall/port/operate/index.vue @@ -70,7 +70,7 @@ import DrawerHeader from '@/components/drawer-header/index.vue'; import { MsgError, MsgSuccess } from '@/utils/message'; import { Host } from '@/api/interface/host'; import { operatePortRule, updatePortRule } from '@/api/modules/host'; -import { checkPort, deepCopy } from '@/utils/util'; +import { checkIp, checkPort, deepCopy } from '@/utils/util'; const loading = ref(); const oldRule = ref(); @@ -107,7 +107,7 @@ const handleClose = () => { const rules = reactive({ protocol: [Rules.requiredSelect], port: [Rules.requiredInput], - address: [Rules.ip], + address: [Rules.requiredInput], }); type FormInstance = InstanceType; @@ -121,6 +121,18 @@ const onSubmit = async (formEl: FormInstance | undefined) => { if (!dialogData.value.rowData) return; if (dialogData.value.rowData.source === 'anyWhere') { dialogData.value.rowData.address = ''; + } else { + if (dialogData.value.rowData.address.indexOf('/') !== -1) { + if (checkIp(dialogData.value.rowData.address.split('/')[0])) { + MsgError(i18n.global.t('firewall.addressFormatError')); + return; + } + } else { + if (checkIp(dialogData.value.rowData.address)) { + MsgError(i18n.global.t('firewall.addressFormatError')); + return; + } + } } let ports = []; if (dialogData.value.rowData.port.indexOf('-') !== -1 && !dialogData.value.rowData.port.startsWith('-')) {