mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-12 02:06:37 +08:00
feat(waf): complete composable rule orchestration
Add the React Flow rule editor, ordered graph APIs and runtime DAG execution.\n\nPublish rules only on OpenResty reload and reconcile checksum-driven IP group snapshots in bounded shared memory.
This commit is contained in:
@@ -12,14 +12,6 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
||||
func handleLogicError(c *gin.Context, err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
return apiutil.AbortNotFoundIfMissing(c, err, "记录不存在")
|
||||
}
|
||||
|
||||
func routeIDParam(c *gin.Context) (uint, bool) {
|
||||
raw := c.Param("route_id")
|
||||
if raw == "" {
|
||||
@@ -34,167 +26,6 @@ func routeIDParam(c *gin.Context) (uint, bool) {
|
||||
return uint(id64), true
|
||||
}
|
||||
|
||||
// ListRuleGroupsHandler 列出全部 WAF 规则组。
|
||||
// @Summary 列出 WAF 规则组
|
||||
// @Description 返回全部 WAF 规则组,需要管理员权限
|
||||
// @Tags openflare-waf
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Success 200 {object} response.Any{data=[]waf.RuleGroupView} "规则组列表"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或不存在"
|
||||
// @Failure 500 {object} response.Any "内部错误"
|
||||
// @Router /api/v1/d/waf/rule-groups [get]
|
||||
func ListRuleGroupsHandler(c *gin.Context) {
|
||||
groups, err := ListRuleGroups(c.Request.Context())
|
||||
if handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(groups))
|
||||
}
|
||||
|
||||
// GetRuleGroupHandler 获取 WAF 规则组详情。
|
||||
// @Summary 获取 WAF 规则组详情
|
||||
// @Description 按 ID 返回 WAF 规则组详情,需要管理员权限
|
||||
// @Tags openflare-waf
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Param id path int true "规则组 ID"
|
||||
// @Success 200 {object} response.Any{data=waf.RuleGroupView} "规则组详情"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或不存在"
|
||||
// @Failure 404 {object} response.Any "记录不存在"
|
||||
// @Failure 500 {object} response.Any "内部错误"
|
||||
// @Router /api/v1/d/waf/rule-groups/{id} [get]
|
||||
func GetRuleGroupHandler(c *gin.Context) {
|
||||
id, ok := apiutil.IDParam(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
group, err := GetRuleGroup(c.Request.Context(), id)
|
||||
if handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(group))
|
||||
}
|
||||
|
||||
// CreateRuleGroupHandler 创建 WAF 规则组。
|
||||
// @Summary 创建 WAF 规则组
|
||||
// @Description 创建新的 WAF 规则组,需要管理员权限
|
||||
// @Tags openflare-waf
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Param request body waf.RuleGroupInput true "规则组参数"
|
||||
// @Success 200 {object} response.Any{data=waf.RuleGroupView} "创建成功的规则组"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或不存在"
|
||||
// @Failure 500 {object} response.Any "内部错误"
|
||||
// @Router /api/v1/d/waf/rule-groups [post]
|
||||
func CreateRuleGroupHandler(c *gin.Context) {
|
||||
var input RuleGroupInput
|
||||
if !apiutil.BindJSON(c, &input) {
|
||||
return
|
||||
}
|
||||
group, err := CreateRuleGroup(c.Request.Context(), input)
|
||||
if handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(group))
|
||||
}
|
||||
|
||||
// UpdateRuleGroupHandler 更新 WAF 规则组。
|
||||
// @Summary 更新 WAF 规则组
|
||||
// @Description 按 ID 更新 WAF 规则组,需要管理员权限
|
||||
// @Tags openflare-waf
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Param id path int true "规则组 ID"
|
||||
// @Param request body waf.RuleGroupInput true "规则组参数"
|
||||
// @Success 200 {object} response.Any{data=waf.RuleGroupView} "更新后的规则组"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或不存在"
|
||||
// @Failure 404 {object} response.Any "记录不存在"
|
||||
// @Failure 500 {object} response.Any "内部错误"
|
||||
// @Router /api/v1/d/waf/rule-groups/{id}/update [post]
|
||||
func UpdateRuleGroupHandler(c *gin.Context) {
|
||||
id, ok := apiutil.IDParam(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var input RuleGroupInput
|
||||
if !apiutil.BindJSON(c, &input) {
|
||||
return
|
||||
}
|
||||
group, err := UpdateRuleGroup(c.Request.Context(), id, input)
|
||||
if handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(group))
|
||||
}
|
||||
|
||||
// DeleteRuleGroupHandler 删除 WAF 规则组。
|
||||
// @Summary 删除 WAF 规则组
|
||||
// @Description 按 ID 删除 WAF 规则组,需要管理员权限
|
||||
// @Tags openflare-waf
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Param id path int true "规则组 ID"
|
||||
// @Success 200 {object} response.Any "删除成功"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或不存在"
|
||||
// @Failure 404 {object} response.Any "记录不存在"
|
||||
// @Failure 500 {object} response.Any "内部错误"
|
||||
// @Router /api/v1/d/waf/rule-groups/{id}/delete [post]
|
||||
func DeleteRuleGroupHandler(c *gin.Context) {
|
||||
id, ok := apiutil.IDParam(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := DeleteRuleGroup(c.Request.Context(), id); handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OKNil())
|
||||
}
|
||||
|
||||
// ReplaceRuleGroupSitesHandler 替换规则组绑定的站点。
|
||||
// @Summary 替换规则组站点绑定
|
||||
// @Description 替换 WAF 规则组关联的代理站点列表,需要管理员权限
|
||||
// @Tags openflare-waf
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security SessionCookie
|
||||
// @Param id path int true "规则组 ID"
|
||||
// @Param request body waf.IDsRequest true "站点 ID 列表"
|
||||
// @Success 200 {object} response.Any{data=waf.RuleGroupView} "更新后的规则组"
|
||||
// @Failure 400 {object} response.Any "参数错误"
|
||||
// @Failure 401 {object} response.Any "未登录"
|
||||
// @Failure 404 {object} response.Any "无权限或不存在"
|
||||
// @Failure 404 {object} response.Any "记录不存在"
|
||||
// @Failure 500 {object} response.Any "内部错误"
|
||||
// @Router /api/v1/d/waf/rule-groups/{id}/sites [post]
|
||||
func ReplaceRuleGroupSitesHandler(c *gin.Context) {
|
||||
id, ok := apiutil.IDParam(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var request IDsRequest
|
||||
if !apiutil.BindJSON(c, &request) {
|
||||
return
|
||||
}
|
||||
group, err := ReplaceRuleGroupSites(c.Request.Context(), id, request.IDs)
|
||||
if handleLogicError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(group))
|
||||
}
|
||||
|
||||
// GetSiteRuleGroupsHandler 获取站点的 WAF 规则组绑定。
|
||||
// @Summary 获取站点 WAF 规则组
|
||||
// @Description 返回代理站点关联的 WAF 规则组绑定,需要管理员权限
|
||||
@@ -215,7 +46,7 @@ func GetSiteRuleGroupsHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
view, err := GetSiteRuleGroups(c.Request.Context(), routeID)
|
||||
if handleLogicError(c, err) {
|
||||
if handleRuleError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(view))
|
||||
@@ -247,7 +78,7 @@ func ReplaceSiteRuleGroupsHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
view, err := ReplaceSiteRuleGroups(c.Request.Context(), routeID, request.IDs)
|
||||
if handleLogicError(c, err) {
|
||||
if handleRuleError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(view))
|
||||
@@ -267,7 +98,7 @@ func ReplaceSiteRuleGroupsHandler(c *gin.Context) {
|
||||
// @Router /api/v1/d/waf/ip-groups [get]
|
||||
func ListIPGroupsHandler(c *gin.Context) {
|
||||
groups, err := ListIPGroups(c.Request.Context())
|
||||
if handleLogicError(c, err) {
|
||||
if handleRuleError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(groups))
|
||||
@@ -293,7 +124,7 @@ func GetIPGroupHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
group, err := GetIPGroup(c.Request.Context(), id)
|
||||
if handleLogicError(c, err) {
|
||||
if handleRuleError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(group))
|
||||
@@ -319,7 +150,7 @@ func CreateIPGroupHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
group, err := CreateIPGroup(c.Request.Context(), input)
|
||||
if handleLogicError(c, err) {
|
||||
if handleRuleError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(group))
|
||||
@@ -351,7 +182,7 @@ func UpdateIPGroupHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
group, err := UpdateIPGroup(c.Request.Context(), id, input)
|
||||
if handleLogicError(c, err) {
|
||||
if handleRuleError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(group))
|
||||
@@ -376,7 +207,7 @@ func DeleteIPGroupHandler(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := DeleteIPGroup(c.Request.Context(), id); handleLogicError(c, err) {
|
||||
if err := DeleteIPGroup(c.Request.Context(), id); handleRuleError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OKNil())
|
||||
@@ -402,7 +233,7 @@ func SyncIPGroupHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
result, err := SyncIPGroup(c.Request.Context(), id)
|
||||
if handleLogicError(c, err) {
|
||||
if handleRuleError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(result))
|
||||
@@ -428,8 +259,8 @@ func TestIPGroupAutoConfigHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
result, err := TestIPGroupAutoConfig(c.Request.Context(), input)
|
||||
if handleLogicError(c, err) {
|
||||
if handleRuleError(c, err) {
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, response.OK(result))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user