From 31780eb1154e64ace2d65b42e95700bfa7930bf9 Mon Sep 17 00:00:00 2001 From: samwaf Date: Mon, 8 Jun 2026 09:54:42 +0800 Subject: [PATCH] feat: add host nickname #IJR74L --- api/waf_host.go | 5 ++++ common/validfield/valid_filter_field.go | 2 +- model/hosts.go | 1 + model/request/waf_host_req.go | 2 ++ service/waf_service/waf_host.go | 38 ++++++++++++++++--------- wafdb/migrations_core.go | 23 +++++++++++++++ 6 files changed, 56 insertions(+), 15 deletions(-) diff --git a/api/waf_host.go b/api/waf_host.go index 1aa608f..97d5537 100644 --- a/api/waf_host.go +++ b/api/waf_host.go @@ -204,6 +204,11 @@ func (w *WafHostAPi) GetAllListApi(c *gin.Context) { // 构建括号内的内容 var bracketContent []string + // 如果有昵称,优先显示昵称 + if wafHosts[i].Nickname != "" { + bracketContent = append(bracketContent, wafHosts[i].Nickname) + } + // 如果是SSL,添加SSL标识 if wafHosts[i].Ssl == 1 { bracketContent = append(bracketContent, "SSL") diff --git a/common/validfield/valid_filter_field.go b/common/validfield/valid_filter_field.go index 503bcc5..53958fe 100644 --- a/common/validfield/valid_filter_field.go +++ b/common/validfield/valid_filter_field.go @@ -2,7 +2,7 @@ package validfield // IsValidHostFilterField 检测host字段是否合法 func IsValidHostFilterField(field string) bool { - var allowedFilterFields = []string{"host", "port", "remote_ip", "remote_port", "remarks"} + var allowedFilterFields = []string{"host", "port", "remote_ip", "remote_port", "remarks", "nickname"} for _, allowedField := range allowedFilterFields { if field == allowedField { diff --git a/model/hosts.go b/model/hosts.go index 91964ae..43e7ff3 100644 --- a/model/hosts.go +++ b/model/hosts.go @@ -19,6 +19,7 @@ type Hosts struct { Remote_ip string `gorm:"size:64" json:"remote_ip"` //远端指定IP Certfile string `gorm:"type:text" json:"certfile"` //证书文件 Keyfile string `gorm:"type:text" json:"keyfile"` //密钥文件 + Nickname string `gorm:"size:200" json:"nickname"` //网站昵称 REMARKS string `gorm:"size:500" json:"remarks"` //备注 GLOBAL_HOST int `json:"global_host"` //默认全局 1 全局 0非全局 DEFENSE_JSON string `gorm:"type:text" json:"defense_json"` //自身防御 json diff --git a/model/request/waf_host_req.go b/model/request/waf_host_req.go index a3ecbc8..1cc4b54 100644 --- a/model/request/waf_host_req.go +++ b/model/request/waf_host_req.go @@ -12,6 +12,7 @@ type WafHostAddReq struct { Remote_host string `json:"remote_host"` //远端域名 Remote_ip string `json:"remote_ip"` //远端指定IP Remote_port int `json:"remote_port"` //远端端口 + Nickname string `json:"nickname"` //网站昵称 REMARKS string `json:"remarks"` //备注 Certfile string `json:"certfile"` // 证书文件 Keyfile string `json:"keyfile"` // 密钥文件 @@ -55,6 +56,7 @@ type WafHostDetailReq struct { type WafHostEditReq struct { CODE string `json:"code"` + Nickname string `json:"nickname"` //网站昵称 Host string `json:"host"` //域名 Port int `json:"port"` //端口 Ssl int `json:"ssl"` //是否是ssl diff --git a/service/waf_service/waf_host.go b/service/waf_service/waf_host.go index da39637..b3e4279 100644 --- a/service/waf_service/waf_host.go +++ b/service/waf_service/waf_host.go @@ -71,6 +71,7 @@ func (receiver *WafHostService) AddApi(wafHostAddReq request.WafHostAddReq) (str Remote_ip: wafHostAddReq.Remote_ip, Certfile: wafHostAddReq.Certfile, Keyfile: wafHostAddReq.Keyfile, + Nickname: wafHostAddReq.Nickname, REMARKS: wafHostAddReq.REMARKS, GLOBAL_HOST: 0, DEFENSE_JSON: wafHostAddReq.DEFENSE_JSON, @@ -132,6 +133,7 @@ func (receiver *WafHostService) ModifyApi(wafHostEditReq request.WafHostEditReq) "Remote_host": wafHostEditReq.Remote_host, "Remote_ip": wafHostEditReq.Remote_ip, "Remote_port": wafHostEditReq.Remote_port, + "Nickname": wafHostEditReq.Nickname, "REMARKS": wafHostEditReq.REMARKS, "GLOBAL_HOST": 0, "Certfile": wafHostEditReq.Certfile, @@ -197,24 +199,32 @@ func (receiver *WafHostService) GetListApi(req request.WafHostSearchReq) ([]mode } whereField = whereField + " code=? " } - for _, by := range splitFilterBys { - - if len(by) > 0 { - if !validfield.IsValidHostFilterField(by) { - return nil, 0, errors.New("输入过滤字段不合法") - } - if len(whereField) > 0 { - whereField = whereField + " and " - } - whereField = whereField + " " + by + " like ? " - } - } //where字段赋值 if len(req.Code) > 0 { whereValues = append(whereValues, req.Code) } - for _, val := range splitFilterValues { - if len(val) > 0 { + for i, by := range splitFilterBys { + if len(by) == 0 { + continue + } + if !validfield.IsValidHostFilterField(by) { + return nil, 0, errors.New("输入过滤字段不合法") + } + val := "" + if i < len(splitFilterValues) { + val = splitFilterValues[i] + } + if len(val) == 0 { + continue + } + if len(whereField) > 0 { + whereField += " and " + } + if by == "host" { + whereField += " (host like ? OR nickname like ?) " + whereValues = append(whereValues, "%"+val+"%", "%"+val+"%") + } else { + whereField += " " + by + " like ? " whereValues = append(whereValues, "%"+val+"%") } } diff --git a/wafdb/migrations_core.go b/wafdb/migrations_core.go index d08c190..f92103b 100644 --- a/wafdb/migrations_core.go +++ b/wafdb/migrations_core.go @@ -934,6 +934,29 @@ func RunCoreDBMigrations(db *gorm.DB) error { return nil }, }, + // 迁移23: 为 hosts 表添加 nickname 字段(网站昵称) + { + ID: "202606080001_add_hosts_nickname", + Migrate: func(tx *gorm.DB) error { + zlog.Info("迁移 202606080001: 为 hosts 表添加 nickname 字段") + if tx.Migrator().HasColumn(&model.Hosts{}, "nickname") { + zlog.Info("nickname 字段已存在,跳过添加") + return nil + } + if err := tx.Migrator().AddColumn(&model.Hosts{}, "nickname"); err != nil { + return fmt.Errorf("添加 nickname 字段失败: %w", err) + } + zlog.Info("nickname 字段添加成功") + return nil + }, + Rollback: func(tx *gorm.DB) error { + zlog.Info("回滚 202606080001: 删除 hosts 表的 nickname 字段") + if tx.Migrator().HasColumn(&model.Hosts{}, "nickname") { + return tx.Migrator().DropColumn(&model.Hosts{}, "nickname") + } + return nil + }, + }, }) // 执行迁移