diff --git a/backend/app/api/v1/firewall.go b/backend/app/api/v1/firewall.go index cef07f766..26afe9c8f 100644 --- a/backend/app/api/v1/firewall.go +++ b/backend/app/api/v1/firewall.go @@ -38,6 +38,10 @@ func (b *BaseApi) SearchFirewallRule(c *gin.Context) { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } + if err := global.VALID.Struct(req); err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } total, list, err := firewallService.SearchWithPage(req) if err != nil { diff --git a/backend/app/dto/firewall.go b/backend/app/dto/firewall.go index dc553be4b..c1f5e6d7d 100644 --- a/backend/app/dto/firewall.go +++ b/backend/app/dto/firewall.go @@ -9,8 +9,10 @@ type FirewallBaseInfo struct { type RuleSearch struct { PageInfo - Info string `json:"info"` - Type string `json:"type" validate:"required"` + Info string `json:"info"` + Status string `json:"status"` + Strategy string `json:"strategy"` + Type string `json:"type" validate:"required"` } type FirewallOperation struct { @@ -28,6 +30,7 @@ type PortRuleOperate struct { } type UpdateFirewallDescription struct { + Type string `json:"type"` Address string `json:"address"` Port string `json:"port"` Protocol string `json:"protocol"` diff --git a/backend/app/model/firewall.go b/backend/app/model/firewall.go index e3918cb32..ed89be900 100644 --- a/backend/app/model/firewall.go +++ b/backend/app/model/firewall.go @@ -3,6 +3,7 @@ package model type Firewall struct { BaseModel + Type string `gorm:"type:varchar(64);not null" json:"type"` Port string `gorm:"type:varchar(64);not null" json:"port"` Protocol string `gorm:"type:varchar(64);not null" json:"protocol"` Address string `gorm:"type:varchar(64);not null" json:"address"` diff --git a/backend/app/repo/host.go b/backend/app/repo/host.go index 4979a05db..0ca114a1c 100644 --- a/backend/app/repo/host.go +++ b/backend/app/repo/host.go @@ -24,7 +24,7 @@ type IHostRepo interface { ListFirewallRecord() ([]model.Firewall, error) SaveFirewallRecord(firewall *model.Firewall) error DeleteFirewallRecordByID(id uint) error - DeleteFirewallRecord(port, protocol, address, strategy string) error + DeleteFirewallRecord(fType, port, protocol, address, strategy string) error } func NewIHostRepo() IHostRepo { @@ -136,9 +136,16 @@ func (h *HostRepo) SaveFirewallRecord(firewall *model.Firewall) error { return global.DB.Save(firewall).Error } var data model.Firewall - _ = global.DB.Where("port = ? AND protocol = ? AND address = ? AND strategy = ?", firewall.Port, firewall.Protocol, firewall.Address, firewall.Strategy).First(&data) - if data.ID != 0 { - firewall.ID = data.ID + if firewall.Type == "port" { + _ = global.DB.Where("type = ? AND port = ? AND protocol = ? AND address = ? AND strategy = ?", "port", firewall.Port, firewall.Protocol, firewall.Address, firewall.Strategy).First(&data) + if data.ID != 0 { + firewall.ID = data.ID + } + } else { + _ = global.DB.Where("type = ? AND address = ? AND strategy = ?", "address", firewall.Address, firewall.Strategy).First(&data) + if data.ID != 0 { + firewall.ID = data.ID + } } return global.DB.Save(firewall).Error } @@ -147,6 +154,6 @@ func (h *HostRepo) DeleteFirewallRecordByID(id uint) error { return global.DB.Where("id = ?", id).Delete(&model.Firewall{}).Error } -func (h *HostRepo) DeleteFirewallRecord(port, protocol, address, strategy string) error { - return global.DB.Where("port = ? AND protocol = ? AND address = ? AND strategy = ?", port, protocol, address, strategy).Delete(&model.Firewall{}).Error +func (h *HostRepo) DeleteFirewallRecord(fType, port, protocol, address, strategy string) error { + return global.DB.Where("type = ? AND port = ? AND protocol = ? AND address = ? AND strategy = ?", fType, port, protocol, address, strategy).Delete(&model.Firewall{}).Error } diff --git a/backend/app/service/firewall.go b/backend/app/service/firewall.go index e00e832fa..6ff0748ae 100644 --- a/backend/app/service/firewall.go +++ b/backend/app/service/firewall.go @@ -104,20 +104,57 @@ func (u *FirewallService) SearchWithPage(req dto.RuleSearch) (int64, interface{} datas = addrs } } - total, start, end := len(datas), (req.Page-1)*req.PageSize, req.Page*req.PageSize + + var datasFilterStatus []fireClient.FireInfo + if len(req.Status) != 0 { + for _, data := range datas { + portItem, _ := strconv.Atoi(data.Port) + if req.Status == "free" && !common.ScanPort(portItem) { + datasFilterStatus = append(datasFilterStatus, data) + } + if req.Status == "used" && common.ScanPort(portItem) { + datasFilterStatus = append(datasFilterStatus, data) + } + } + } else { + datasFilterStatus = datas + } + var datasFilterStrategy []fireClient.FireInfo + if len(req.Strategy) != 0 { + for _, data := range datasFilterStatus { + if req.Strategy == data.Strategy { + datasFilterStrategy = append(datasFilterStrategy, data) + } + } + } else { + datasFilterStrategy = datasFilterStatus + } + + total, start, end := len(datasFilterStrategy), (req.Page-1)*req.PageSize, req.Page*req.PageSize if start > total { backDatas = make([]fireClient.FireInfo, 0) } else { if end >= total { end = total } - backDatas = datas[start:end] + backDatas = datasFilterStrategy[start:end] } datasFromDB, _ := hostRepo.ListFirewallRecord() for i := 0; i < len(backDatas); i++ { for _, des := range datasFromDB { - if backDatas[i].Port == des.Port && backDatas[i].Protocol == des.Protocol && backDatas[i].Strategy == des.Strategy && backDatas[i].Address == des.Address { + if req.Type != des.Type { + continue + } + if backDatas[i].Port == des.Port && + req.Type == "port" && + backDatas[i].Protocol == des.Protocol && + backDatas[i].Strategy == des.Strategy && + backDatas[i].Address == des.Address { + backDatas[i].Description = des.Description + break + } + if req.Type == "address" && backDatas[i].Strategy == des.Strategy && backDatas[i].Address == des.Address { backDatas[i].Description = des.Description break } @@ -488,10 +525,11 @@ func (u *FirewallService) addPortsBeforeStart(client firewall.FirewallClient) er func (u *FirewallService) addPortRecord(req dto.PortRuleOperate) error { if req.Operation == "remove" { - return hostRepo.DeleteFirewallRecord(req.Port, req.Protocol, req.Address, req.Strategy) + return hostRepo.DeleteFirewallRecord("port", req.Port, req.Protocol, req.Address, req.Strategy) } return hostRepo.SaveFirewallRecord(&model.Firewall{ + Type: "port", Port: req.Port, Protocol: req.Protocol, Address: req.Address, @@ -502,9 +540,10 @@ func (u *FirewallService) addPortRecord(req dto.PortRuleOperate) error { func (u *FirewallService) addAddressRecord(req dto.AddrRuleOperate) error { if req.Operation == "remove" { - return hostRepo.DeleteFirewallRecord("", "", req.Address, req.Strategy) + return hostRepo.DeleteFirewallRecord("address", "", "", req.Address, req.Strategy) } return hostRepo.SaveFirewallRecord(&model.Firewall{ + Type: "address", Address: req.Address, Strategy: req.Strategy, Description: req.Description, diff --git a/backend/init/migration/migrations/init.go b/backend/init/migration/migrations/init.go index 3618f5b3f..dc1fd1476 100644 --- a/backend/init/migration/migrations/init.go +++ b/backend/init/migration/migrations/init.go @@ -572,14 +572,12 @@ var UpdateCronjobWithDb = &gormigrate.Migration{ } var AddTableFirewall = &gormigrate.Migration{ - ID: "20230825-add-table-firewall", + ID: "20230828-add-table-firewall", Migrate: func(tx *gorm.DB) error { if err := tx.AutoMigrate(&model.Firewall{}, model.SnapshotStatus{}, &model.Cronjob{}); err != nil { return err } - if err := tx.Exec("alter table remote_dbs rename to databases;").Error; err != nil { - return err - } + _ = tx.Exec("alter table remote_dbs rename to databases;").Error return nil }, } diff --git a/frontend/src/api/interface/host.ts b/frontend/src/api/interface/host.ts index 298d2b500..a6bd723a8 100644 --- a/frontend/src/api/interface/host.ts +++ b/frontend/src/api/interface/host.ts @@ -66,6 +66,8 @@ export namespace Host { pingStatus: string; } export interface RuleSearch extends ReqPage { + status: string; + strategy: string; info: string; type: string; } diff --git a/frontend/src/views/database/mysql/index.vue b/frontend/src/views/database/mysql/index.vue index 209afcedc..323a70bb8 100644 --- a/frontend/src/views/database/mysql/index.vue +++ b/frontend/src/views/database/mysql/index.vue @@ -140,9 +140,7 @@ - - - + { }; const onChange = async (info: any) => { - if (!info.edit) { - await updateMysqlDescription({ id: info.id, description: info.description }); - MsgSuccess(i18n.global.t('commons.msg.operationSuccess')); - } + await updateMysqlDescription({ id: info.id, description: info.description }); + MsgSuccess(i18n.global.t('commons.msg.operationSuccess')); }; const goDashboard = async () => { diff --git a/frontend/src/views/host/firewall/ip/index.vue b/frontend/src/views/host/firewall/ip/index.vue index df081592c..1fd5f151f 100644 --- a/frontend/src/views/host/firewall/ip/index.vue +++ b/frontend/src/views/host/firewall/ip/index.vue @@ -45,6 +45,16 @@ + + + + {{ $t('firewall.strategy') }} + + + + + + - - - + ([]); const searchName = ref(); +const searchStrategy = ref(''); const fireName = ref(); const maskShow = ref(true); @@ -165,6 +170,8 @@ const search = async () => { } let params = { type: activeTag.value, + status: '', + strategy: searchStrategy.value, info: searchName.value, page: paginationConfig.currentPage, pageSize: paginationConfig.pageSize, @@ -200,10 +207,9 @@ const toDoc = () => { }; const onChange = async (info: any) => { - if (!info.edit) { - await updateFirewallDescription(info); - MsgSuccess(i18n.global.t('commons.msg.operationSuccess')); - } + info.type = 'address'; + await updateFirewallDescription(info); + MsgSuccess(i18n.global.t('commons.msg.operationSuccess')); }; const onChangeStatus = async (row: Host.RuleInfo, status: string) => { diff --git a/frontend/src/views/host/firewall/port/index.vue b/frontend/src/views/host/firewall/port/index.vue index e4f86f1e4..675d5e7da 100644 --- a/frontend/src/views/host/firewall/port/index.vue +++ b/frontend/src/views/host/firewall/port/index.vue @@ -35,6 +35,22 @@ + + + + {{ $t('commons.table.status') }} + + + + + + {{ $t('firewall.strategy') }} + + + + + + @@ -110,13 +126,7 @@ prop="description" > - - - + ([]); const searchName = ref(); +const searchStatus = ref(''); +const searchStrategy = ref(''); const maskShow = ref(true); const fireStatus = ref('running'); @@ -197,6 +209,8 @@ const search = async () => { } let params = { type: activeTag.value, + status: searchStatus.value, + strategy: searchStrategy.value, info: searchName.value, page: paginationConfig.currentPage, pageSize: paginationConfig.pageSize, @@ -279,10 +293,9 @@ const onChangeStatus = async (row: Host.RuleInfo, status: string) => { }; const onChange = async (info: any) => { - if (!info.edit) { - await updateFirewallDescription(info); - MsgSuccess(i18n.global.t('commons.msg.operationSuccess')); - } + info.type = 'port'; + await updateFirewallDescription(info); + MsgSuccess(i18n.global.t('commons.msg.operationSuccess')); }; const onDelete = async (row: Host.RuleInfo | null) => { diff --git a/frontend/src/views/setting/snapshot/index.vue b/frontend/src/views/setting/snapshot/index.vue index cb272ea41..655131785 100644 --- a/frontend/src/views/setting/snapshot/index.vue +++ b/frontend/src/views/setting/snapshot/index.vue @@ -74,9 +74,7 @@ - - - + { }; const onChange = async (info: any) => { - if (!info.edit) { - await updateSnapshotDescription({ id: info.id, description: info.description }); - MsgSuccess(i18n.global.t('commons.msg.operationSuccess')); - } + await updateSnapshotDescription({ id: info.id, description: info.description }); + MsgSuccess(i18n.global.t('commons.msg.operationSuccess')); }; const submitAddSnapshot = (formEl: FormInstance | undefined) => {