fix: 防火墙端口添加支持输入网段 (#738)

This commit is contained in:
ssongliu 2023-04-20 23:10:17 +08:00 committed by GitHub
parent 565fd1c605
commit 44a1d9d16c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 13 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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"

View File

@ -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<Host.RulePort>();
@ -107,7 +107,7 @@ const handleClose = () => {
const rules = reactive({
protocol: [Rules.requiredSelect],
port: [Rules.requiredInput],
address: [Rules.ip],
address: [Rules.requiredInput],
});
type FormInstance = InstanceType<typeof ElForm>;
@ -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('-')) {