diff --git a/backend/app/api/v1/firewall.go b/backend/app/api/v1/firewall.go index b2429068e..04f8549f1 100644 --- a/backend/app/api/v1/firewall.go +++ b/backend/app/api/v1/firewall.go @@ -54,19 +54,6 @@ func (b *BaseApi) OperatePortRule(c *gin.Context) { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } - if req.Protocol == "tcp/udp" { - req.Protocol = "tcp" - if err := firewallService.OperatePortRule(req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) - return - } - req.Protocol = "udp" - if err := firewallService.OperatePortRule(req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) - return - } - helper.SuccessWithData(c, nil) - } if err := firewallService.OperatePortRule(req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return diff --git a/backend/app/dto/firewall.go b/backend/app/dto/firewall.go index 047b6c154..77960cb34 100644 --- a/backend/app/dto/firewall.go +++ b/backend/app/dto/firewall.go @@ -9,7 +9,7 @@ type PortRuleOperate struct { Operation string `json:"operation" validate:"required,oneof=add remove"` Address string `json:"address"` Port string `json:"port" validate:"required"` - Protocol string `json:"protocol" validate:"required,oneof=tcp udp tcp/upd"` + Protocol string `json:"protocol" validate:"required,oneof=tcp udp tcp/udp"` Strategy string `json:"strategy" validate:"required,oneof=accept drop"` } diff --git a/backend/app/service/firewall.go b/backend/app/service/firewall.go index a7d01176a..7220183f2 100644 --- a/backend/app/service/firewall.go +++ b/backend/app/service/firewall.go @@ -59,16 +59,17 @@ func (u *FirewallService) OperatePortRule(req dto.PortRuleOperate) error { if err != nil { return err } - - var fireInfo fireClient.FireInfo - if err := copier.Copy(&fireInfo, &req); err != nil { + if req.Protocol == "tcp/udp" { + req.Protocol = "tcp" + if err := u.createPort(client, req); err != nil { + return err + } + req.Protocol = "udp" + } + if err := u.createPort(client, req); err != nil { return err } - - if len(fireInfo.Address) != 0 || fireInfo.Strategy == "drop" { - return client.RichRules(fireInfo, req.Operation) - } - return client.Port(fireInfo, req.Operation) + return client.Reload() } func (u *FirewallService) OperateAddressRule(req dto.AddrRuleOperate) error { @@ -81,5 +82,20 @@ func (u *FirewallService) OperateAddressRule(req dto.AddrRuleOperate) error { if err := copier.Copy(&fireInfo, &req); err != nil { return err } - return client.RichRules(fireInfo, req.Operation) + if err := client.RichRules(fireInfo, req.Operation); err != nil { + return err + } + return client.Reload() +} + +func (u *FirewallService) createPort(client firewall.FirewallClient, req dto.PortRuleOperate) error { + var fireInfo fireClient.FireInfo + if err := copier.Copy(&fireInfo, &req); err != nil { + return err + } + + if len(fireInfo.Address) != 0 || fireInfo.Strategy == "drop" { + return client.RichRules(fireInfo, req.Operation) + } + return client.Port(fireInfo, req.Operation) } diff --git a/backend/utils/firewall/client.go b/backend/utils/firewall/client.go index 2dc599f11..433a6ec63 100644 --- a/backend/utils/firewall/client.go +++ b/backend/utils/firewall/client.go @@ -19,10 +19,10 @@ type FirewallClient interface { func NewFirewallClient() (FirewallClient, error) { // if _, err := os.Stat("/usr/sbin/firewalld"); err == nil { - return client.NewFirewalld() + // return client.NewFirewalld() // } // if _, err := os.Stat("/usr/sbin/ufw"); err == nil { - // return client.NewUfw() + return client.NewUfw() // } // return nil, errors.New("no such type") } diff --git a/backend/utils/firewall/client/firewalld.go b/backend/utils/firewall/client/firewalld.go index 34cca4cf4..2d34ef168 100644 --- a/backend/utils/firewall/client/firewalld.go +++ b/backend/utils/firewall/client/firewalld.go @@ -111,9 +111,6 @@ func (f *Firewall) Port(port FireInfo, operation string) error { if err != nil { return fmt.Errorf("%s port failed, err: %s", operation, stdout) } - if err := f.Reload(); err != nil { - return err - } return nil } @@ -134,9 +131,6 @@ func (f *Firewall) RichRules(rule FireInfo, operation string) error { if err != nil { return fmt.Errorf("%s rich rules failed, err: %s", operation, stdout) } - if err := f.Reload(); err != nil { - return err - } return nil } @@ -150,9 +144,6 @@ func (f *Firewall) PortForward(info Forward, operation string) error { if err != nil { return fmt.Errorf("%s port forward failed, err: %s", operation, stdout) } - if err := f.Reload(); err != nil { - return err - } return nil } diff --git a/backend/utils/firewall/client/info.go b/backend/utils/firewall/client/info.go index 9f9a0a9f6..d931084fb 100644 --- a/backend/utils/firewall/client/info.go +++ b/backend/utils/firewall/client/info.go @@ -4,7 +4,7 @@ type FireInfo struct { Family string `json:"family"` // ipv4 ipv6 Address string `json:"address"` // Anywhere Port string `json:"port"` - Protocol string `json:"protocol"` // tcp udp tcp/upd + Protocol string `json:"protocol"` // tcp udp tcp/udp Strategy string `json:"strategy"` // accept drop } diff --git a/backend/utils/firewall/client/ufw.go b/backend/utils/firewall/client/ufw.go index 0ff449ff0..33cc6edb2 100644 --- a/backend/utils/firewall/client/ufw.go +++ b/backend/utils/firewall/client/ufw.go @@ -61,7 +61,7 @@ func (f *Ufw) ListPort() ([]FireInfo, error) { if err != nil { return nil, err } - portInfos := strings.Split(strings.ReplaceAll(stdout, "\n", ""), " ") + portInfos := strings.Split(stdout, "\n") var datas []FireInfo isStart := false for _, line := range portInfos { @@ -73,7 +73,7 @@ func (f *Ufw) ListPort() ([]FireInfo, error) { continue } itemFire := f.loadInfo(line, "port") - if len(itemFire.Address) != 0 { + if len(itemFire.Port) != 0 { datas = append(datas, itemFire) } } @@ -85,7 +85,7 @@ func (f *Ufw) ListAddress() ([]FireInfo, error) { if err != nil { return nil, err } - portInfos := strings.Split(strings.ReplaceAll(stdout, "\n", ""), " ") + portInfos := strings.Split(stdout, "\n") var datas []FireInfo isStart := false for _, line := range portInfos { @@ -96,8 +96,11 @@ func (f *Ufw) ListAddress() ([]FireInfo, error) { if !isStart { continue } + if !strings.Contains(line, " IN") { + continue + } itemFire := f.loadInfo(line, "address") - if len(itemFire.Address) != 0 { + if len(itemFire.Port) == 0 { datas = append(datas, itemFire) } } @@ -166,7 +169,7 @@ func (f *Ufw) loadInfo(line string, fireType string) FireInfo { if len(fields) < 4 { return itemInfo } - if fields[0] == "Anywhere" && fireType == "port" { + if fields[0] == "Anywhere" && fireType != "port" { itemInfo.Strategy = "drop" if fields[2] == "ALLOW" { itemInfo.Strategy = "accept" diff --git a/frontend/src/views/host/firewall/ip/index.vue b/frontend/src/views/host/firewall/ip/index.vue index 5fc8bcd77..19c626154 100644 --- a/frontend/src/views/host/firewall/ip/index.vue +++ b/frontend/src/views/host/firewall/ip/index.vue @@ -5,23 +5,8 @@ + + diff --git a/frontend/src/views/host/firewall/port/create/index.vue b/frontend/src/views/host/firewall/port/create/index.vue deleted file mode 100644 index 632e6c9b5..000000000 --- a/frontend/src/views/host/firewall/port/create/index.vue +++ /dev/null @@ -1,114 +0,0 @@ - - - diff --git a/frontend/src/views/host/firewall/port/index.vue b/frontend/src/views/host/firewall/port/index.vue index 91695b00b..be8fdb69f 100644 --- a/frontend/src/views/host/firewall/port/index.vue +++ b/frontend/src/views/host/firewall/port/index.vue @@ -51,7 +51,7 @@