feat: add host nickname

#IJR74L
This commit is contained in:
samwaf
2026-06-08 09:54:42 +08:00
parent 61b320abd5
commit 31780eb115
6 changed files with 56 additions and 15 deletions
+5
View File
@@ -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")
+1 -1
View File
@@ -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 {
+1
View File
@@ -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
+2
View File
@@ -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
+24 -14
View File
@@ -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+"%")
}
}
+23
View File
@@ -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
},
},
})
// 执行迁移