api 测试

This commit is contained in:
shikong 2023-11-02 23:35:05 +08:00
parent eba89dc442
commit 0112e66551
5 changed files with 556 additions and 3 deletions

View File

@ -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

View File

@ -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"
}
}
}
}
}

View File

@ -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"

View File

@ -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)))
})
}

View File

@ -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)
}