diff --git a/docs/docs.go b/docs/docs.go index 8124a44..b6cdb3c 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -130,6 +130,13 @@ const docTemplate = `{ "schema": { "$ref": "#/definitions/iptables.Rule" } + }, + { + "type": "boolean", + "description": "是否插入第一条(高优先级),否则为追加", + "name": "isInsert", + "in": "query", + "required": true } ], "responses": { diff --git a/docs/swagger.json b/docs/swagger.json index 3b4e6a9..05ca8db 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -123,6 +123,13 @@ "schema": { "$ref": "#/definitions/iptables.Rule" } + }, + { + "type": "boolean", + "description": "是否插入第一条(高优先级),否则为追加", + "name": "isInsert", + "in": "query", + "required": true } ], "responses": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 2977f71..88047a3 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -211,6 +211,11 @@ paths: required: true schema: $ref: '#/definitions/iptables.Rule' + - description: 是否插入第一条(高优先级),否则为追加 + in: query + name: isInsert + required: true + type: boolean produces: - application/json responses: diff --git a/internel/controller/controller.go b/internel/controller/controller.go index 18177b5..41db311 100644 --- a/internel/controller/controller.go +++ b/internel/controller/controller.go @@ -61,14 +61,16 @@ func getRuleInfo(api fiber.Router) { // @Accept json // @Produce json // @Param vo body iptables.Rule true "规则" +// @Param isInsert query bool true "是否插入第一条(高优先级),否则为追加" // @Success 200 {object} response.Response{data=string} // @Failure default {object} errorx.CodeErrorResponse // @Router /rule/add [post] func addRule(api fiber.Router) { api.Post("/rule/add", func(ctx *fiber.Ctx) error { rule := &iptables.Rule{} + isInsert := ctx.QueryBool("isInsert", false) _ = ctx.BodyParser(rule) - if err := errorx.ParseError(iptables.AddRule(*rule)); err != nil { + if err := errorx.ParseError(iptables.AddRule(*rule, isInsert)); err != nil { return ctx.JSON(err) } else { return ctx.JSON(response.NewResponse("")) diff --git a/pkg/utils/iptables/handler.go b/pkg/utils/iptables/handler.go index 6316bdc..0defd3f 100644 --- a/pkg/utils/iptables/handler.go +++ b/pkg/utils/iptables/handler.go @@ -24,11 +24,16 @@ func appendArgsWithError[T string | Chain | PolicyTarget | Action](args []string return args, nil } -func AddRule(rule Rule) error { +func AddRule(rule Rule, isInsert bool) error { var err error args := make([]string, 0) - if args, err = appendArgsWithError(args, "-A", rule.Chain, errorx.NewDefaultError("规则链 Chain 不能为空")); err != nil { + actionArg := "-A" + if isInsert { + actionArg = "-I" + } + + if args, err = appendArgsWithError(args, actionArg, rule.Chain, errorx.NewDefaultError("规则链 Chain 不能为空")); err != nil { return err }