diff --git a/docs/docs.go b/docs/docs.go index b7a4429..9d286af 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -22,7 +22,201 @@ const docTemplate = `{ }, "host": "{{.Host}}", "basePath": "{{.BasePath}}", - "paths": {} + "paths": { + "/info": { + "get": { + "description": "获取 iptables 规则 信息", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Info" + ], + "summary": "获取 iptables 规则 信息", + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/resp.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/iptables.Info" + } + } + } + ] + } + }, + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + } + } + }, + "definitions": { + "errorx.CodeErrorResponse": { + "type": "object", + "properties": { + "code": { + "description": "状态码", + "type": "integer", + "example": 200 + }, + "data": { + "description": "数据" + }, + "msg": { + "description": "信息", + "type": "string", + "example": "OK" + } + } + }, + "iptables.Info": { + "type": "object", + "properties": { + "chains": { + "type": "array", + "items": { + "type": "string" + } + }, + "policies": { + "type": "array", + "items": { + "$ref": "#/definitions/iptables.Policy" + } + }, + "rules": { + "type": "array", + "items": { + "$ref": "#/definitions/iptables.Rule" + } + } + } + }, + "iptables.Policy": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "target": { + "type": "string" + } + } + }, + "iptables.Rule": { + "type": "object", + "properties": { + "chain": { + "type": "string" + }, + "destination": { + "description": "-d [dest] example: 192.168.1.1, 192.168.1.0/24", + "type": "string" + }, + "dstPort": { + "description": "--dport example: 80", + "type": "string" + }, + "dstPorts": { + "description": "--dports example: 45000:46000", + "type": "string" + }, + "excludeDestination": { + "description": "! -s [source] example: 192.168.1.1, 192.168.1.0/24", + "type": "string" + }, + "excludeInputInterface": { + "description": "! -i [interface]", + "type": "string" + }, + "excludeOutputInterface": { + "description": "! -o [interface]", + "type": "string" + }, + "excludeProtocol": { + "description": "! -p [proto] example: all, tcp, udp, icmp", + "type": "string" + }, + "excludeSource": { + "description": "! -s [source] example: 192.168.1.1, 192.168.1.0/24", + "type": "string" + }, + "goto": { + "description": "-g [chain Chain]", + "type": "string" + }, + "inputInterface": { + "description": "-i [interface]", + "type": "string" + }, + "jump": { + "description": "-j [target Chain]", + "type": "string" + }, + "limit": { + "description": "--limit example: 3/min", + "type": "string" + }, + "match": { + "description": "-m [match] 用于匹配扩展模块 example: tcp udp icmp", + "type": "string" + }, + "outputInterface": { + "description": "-o [interface]", + "type": "string" + }, + "protocol": { + "description": "-p [proto] example: all, tcp, udp, icmp", + "type": "string" + }, + "source": { + "description": "-s [source] example: 192.168.1.1, 192.168.1.0/24", + "type": "string" + }, + "srcPort": { + "description": "--sport example: 22 80", + "type": "string" + }, + "srcPorts": { + "description": "--sports example: 20000:40000", + "type": "string" + } + } + }, + "resp.Response": { + "type": "object", + "properties": { + "code": { + "description": "状态码", + "type": "integer", + "example": 200 + }, + "data": { + "description": "数据" + }, + "msg": { + "description": "信息", + "type": "string", + "example": "OK" + } + } + } + } }` // SwaggerInfo holds exported Swagger Info so clients can modify it diff --git a/docs/swagger.json b/docs/swagger.json index cb95463..fa58a65 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -15,5 +15,199 @@ "version": "1.0" }, "basePath": "/", - "paths": {} + "paths": { + "/info": { + "get": { + "description": "获取 iptables 规则 信息", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Info" + ], + "summary": "获取 iptables 规则 信息", + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/resp.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/iptables.Info" + } + } + } + ] + } + }, + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + } + } + }, + "definitions": { + "errorx.CodeErrorResponse": { + "type": "object", + "properties": { + "code": { + "description": "状态码", + "type": "integer", + "example": 200 + }, + "data": { + "description": "数据" + }, + "msg": { + "description": "信息", + "type": "string", + "example": "OK" + } + } + }, + "iptables.Info": { + "type": "object", + "properties": { + "chains": { + "type": "array", + "items": { + "type": "string" + } + }, + "policies": { + "type": "array", + "items": { + "$ref": "#/definitions/iptables.Policy" + } + }, + "rules": { + "type": "array", + "items": { + "$ref": "#/definitions/iptables.Rule" + } + } + } + }, + "iptables.Policy": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "target": { + "type": "string" + } + } + }, + "iptables.Rule": { + "type": "object", + "properties": { + "chain": { + "type": "string" + }, + "destination": { + "description": "-d [dest] example: 192.168.1.1, 192.168.1.0/24", + "type": "string" + }, + "dstPort": { + "description": "--dport example: 80", + "type": "string" + }, + "dstPorts": { + "description": "--dports example: 45000:46000", + "type": "string" + }, + "excludeDestination": { + "description": "! -s [source] example: 192.168.1.1, 192.168.1.0/24", + "type": "string" + }, + "excludeInputInterface": { + "description": "! -i [interface]", + "type": "string" + }, + "excludeOutputInterface": { + "description": "! -o [interface]", + "type": "string" + }, + "excludeProtocol": { + "description": "! -p [proto] example: all, tcp, udp, icmp", + "type": "string" + }, + "excludeSource": { + "description": "! -s [source] example: 192.168.1.1, 192.168.1.0/24", + "type": "string" + }, + "goto": { + "description": "-g [chain Chain]", + "type": "string" + }, + "inputInterface": { + "description": "-i [interface]", + "type": "string" + }, + "jump": { + "description": "-j [target Chain]", + "type": "string" + }, + "limit": { + "description": "--limit example: 3/min", + "type": "string" + }, + "match": { + "description": "-m [match] 用于匹配扩展模块 example: tcp udp icmp", + "type": "string" + }, + "outputInterface": { + "description": "-o [interface]", + "type": "string" + }, + "protocol": { + "description": "-p [proto] example: all, tcp, udp, icmp", + "type": "string" + }, + "source": { + "description": "-s [source] example: 192.168.1.1, 192.168.1.0/24", + "type": "string" + }, + "srcPort": { + "description": "--sport example: 22 80", + "type": "string" + }, + "srcPorts": { + "description": "--sports example: 20000:40000", + "type": "string" + } + } + }, + "resp.Response": { + "type": "object", + "properties": { + "code": { + "description": "状态码", + "type": "integer", + "example": 200 + }, + "data": { + "description": "数据" + }, + "msg": { + "description": "信息", + "type": "string", + "example": "OK" + } + } + } + } } \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml index d18dbae..a7d6ca3 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,4 +1,112 @@ basePath: / +definitions: + errorx.CodeErrorResponse: + properties: + code: + description: 状态码 + example: 200 + type: integer + data: + description: 数据 + msg: + description: 信息 + example: OK + type: string + type: object + iptables.Info: + properties: + chains: + items: + type: string + type: array + policies: + items: + $ref: '#/definitions/iptables.Policy' + type: array + rules: + items: + $ref: '#/definitions/iptables.Rule' + type: array + type: object + iptables.Policy: + properties: + name: + type: string + target: + type: string + type: object + iptables.Rule: + properties: + chain: + type: string + destination: + description: '-d [dest] example: 192.168.1.1, 192.168.1.0/24' + type: string + dstPort: + description: '--dport example: 80' + type: string + dstPorts: + description: '--dports example: 45000:46000' + type: string + excludeDestination: + description: '! -s [source] example: 192.168.1.1, 192.168.1.0/24' + type: string + excludeInputInterface: + description: '! -i [interface]' + type: string + excludeOutputInterface: + description: '! -o [interface]' + type: string + excludeProtocol: + description: '! -p [proto] example: all, tcp, udp, icmp' + type: string + excludeSource: + description: '! -s [source] example: 192.168.1.1, 192.168.1.0/24' + type: string + goto: + description: -g [chain Chain] + type: string + inputInterface: + description: -i [interface] + type: string + jump: + description: -j [target Chain] + type: string + limit: + description: '--limit example: 3/min' + type: string + match: + description: '-m [match] 用于匹配扩展模块 example: tcp udp icmp' + type: string + outputInterface: + description: -o [interface] + type: string + protocol: + description: '-p [proto] example: all, tcp, udp, icmp' + type: string + source: + description: '-s [source] example: 192.168.1.1, 192.168.1.0/24' + type: string + srcPort: + description: '--sport example: 22 80' + type: string + srcPorts: + description: '--sports example: 20000:40000' + type: string + type: object + resp.Response: + properties: + code: + description: 状态码 + example: 200 + type: integer + data: + description: 数据 + msg: + description: 信息 + example: OK + type: string + type: object info: contact: email: 919411476@qq.com @@ -10,5 +118,29 @@ info: termsOfService: http://swagger.io/terms/ title: iptables-helper API version: "1.0" -paths: {} +paths: + /info: + get: + consumes: + - application/json + description: 获取 iptables 规则 信息 + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/resp.Response' + - properties: + data: + $ref: '#/definitions/iptables.Info' + type: object + default: + description: "" + schema: + $ref: '#/definitions/errorx.CodeErrorResponse' + summary: 获取 iptables 规则 信息 + tags: + - Info swagger: "2.0" diff --git a/internel/controller/controller.go b/internel/controller/controller.go new file mode 100644 index 0000000..b401d88 --- /dev/null +++ b/internel/controller/controller.go @@ -0,0 +1,30 @@ +package controller + +import ( + "github.com/gofiber/fiber/v2" + response "iptables-helper/pkg/resp" + "iptables-helper/pkg/utils/command" + "iptables-helper/pkg/utils/iptables" +) + +func SetupController(r fiber.Router) { + api := r.Group("/") + getRuleInfo(api) +} + +// getRuleInfo +// @Summary 获取 iptables 规则 信息 +// @Description 获取 iptables 规则 信息 +// @Tags Info +// @Accept json +// @Produce json +// @Success 200 {object} response.Response{data=iptables.Info} +// @Failure default {object} errorx.CodeErrorResponse +// @Router /info [get] +func getRuleInfo(api fiber.Router) { + api.Get("/info", func(ctx *fiber.Ctx) error { + cmder := command.Commander{} + result := cmder.ExecuteWithResult("sudo iptables -S") + return ctx.JSON(response.NewResponse(iptables.Parse(result))) + }) +} diff --git a/internel/route/route.go b/internel/route/route.go index 4a1be74..8e84b1a 100644 --- a/internel/route/route.go +++ b/internel/route/route.go @@ -2,6 +2,7 @@ package route import ( "github.com/gofiber/fiber/v2" + "iptables-helper/internel/controller" "iptables-helper/pkg/config" ) @@ -9,4 +10,6 @@ func SetupRoute(app *fiber.App, conf *config.Conf) { if conf.Server.EnableSwag { SwaggerHandler(app) } + + controller.SetupController(app) }