From d12286939f4b745441e35299d2d983bcc8af6b3d Mon Sep 17 00:00:00 2001 From: Shikong <919411476@qq.com> Date: Fri, 11 Nov 2022 22:43:21 +0800 Subject: [PATCH] =?UTF-8?q?wol=20=E6=B5=8B=E8=AF=95=20WafRateLimitTest=20?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=20X-Forwarded-For?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/test/test.go | 39 +++++++++++++++++++++++++++++++- docs/docs.go | 50 +++++++++++++++++++++++++++++++++++++++++ docs/swagger.json | 50 +++++++++++++++++++++++++++++++++++++++++ docs/swagger.yaml | 30 +++++++++++++++++++++++++ services/services.go | 2 ++ 5 files changed, 170 insertions(+), 1 deletion(-) diff --git a/controller/test/test.go b/controller/test/test.go index 62edb0a..8b4eabf 100644 --- a/controller/test/test.go +++ b/controller/test/test.go @@ -1,6 +1,7 @@ package test import ( + "fmt" "github.com/gofiber/fiber/v2" "gofiber.study.skcks.cn/common/errorx" "gofiber.study.skcks.cn/common/logger" @@ -10,6 +11,8 @@ import ( "gofiber.study.skcks.cn/model/dto" "gofiber.study.skcks.cn/model/vo" "gofiber.study.skcks.cn/services/waf" + "gofiber.study.skcks.cn/services/wol" + "strconv" "time" ) @@ -194,7 +197,13 @@ func (c *Controller) VerifyCaptchaTest() { // @Router /test/waf/access [get] func (c *Controller) WafRateLimitTest() { c.Router.Add(fiber.MethodGet, "/waf/access", func(ctx *fiber.Ctx) error { - access := waf.Service.Access(ctx.IP()) + logger.Log.Infof("ip: %v", ctx.GetReqHeaders()) + ip := ctx.GetReqHeaders()["X-Forwarded-For"] + if len(ip) == 0 { + ip = ctx.IP() + } + + access := waf.Service.Access(ip) if !access { return ctx.JSON(errorx.NewErrorWithCode(fiber.StatusTooManyRequests, "访问频率超过限制")) } @@ -202,3 +211,31 @@ func (c *Controller) WafRateLimitTest() { return ctx.JSON(response.NewResponse("正常访问")) }) } + +// WolTest wol 测试 +// +// @Summary wol 测试 +// @Description wol 测试 +// @Tags Test +// @Accept json +// @Produce json +// @Param mac query string true "mac" +// @Success 200 {object} response.Response{data=string} +// @Failure default {object} errorx.CodeErrorResponse +// @Router /test/wol [get] +func (c *Controller) WolTest() { + c.Router.Add(fiber.MethodGet, "/wol", func(ctx *fiber.Ctx) error { + mac := ctx.Query("mac") + port, err := strconv.Atoi(ctx.Query("port", "9")) + if err := errorx.ParseError(err); err != nil { + return ctx.JSON(err) + } + + err = wol.Services.WakeUp(mac, port) + if err := errorx.ParseError(err); err != nil { + return ctx.JSON(err) + } else { + return ctx.JSON(response.NewResponse(fmt.Sprintf("%s 唤醒成功", mac))) + } + }) +} diff --git a/docs/docs.go b/docs/docs.go index ac72ec7..47e2cf6 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -758,6 +758,56 @@ const docTemplate = `{ } } }, + "/test/wol": { + "get": { + "description": "wol 测试", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Test" + ], + "summary": "wol 测试", + "parameters": [ + { + "type": "string", + "description": "mac", + "name": "mac", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/response.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "string" + } + } + } + ] + } + }, + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + } + }, "/user/account": { "get": { "security": [ diff --git a/docs/swagger.json b/docs/swagger.json index a7d00a9..7a8546a 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -750,6 +750,56 @@ } } }, + "/test/wol": { + "get": { + "description": "wol 测试", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Test" + ], + "summary": "wol 测试", + "parameters": [ + { + "type": "string", + "description": "mac", + "name": "mac", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/response.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "string" + } + } + } + ] + } + }, + "default": { + "description": "", + "schema": { + "$ref": "#/definitions/errorx.CodeErrorResponse" + } + } + } + } + }, "/user/account": { "get": { "security": [ diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 9fbed69..5be0134 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -584,6 +584,36 @@ paths: summary: waf 访问频率限制 测试 tags: - Test + /test/wol: + get: + consumes: + - application/json + description: wol 测试 + parameters: + - description: mac + in: query + name: mac + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/response.Response' + - properties: + data: + type: string + type: object + default: + description: "" + schema: + $ref: '#/definitions/errorx.CodeErrorResponse' + summary: wol 测试 + tags: + - Test /user/account: get: consumes: diff --git a/services/services.go b/services/services.go index cad8563..c46dc3f 100644 --- a/services/services.go +++ b/services/services.go @@ -4,10 +4,12 @@ import ( "gofiber.study.skcks.cn/services/auth" "gofiber.study.skcks.cn/services/user" "gofiber.study.skcks.cn/services/waf" + "gofiber.study.skcks.cn/services/wol" ) func Init() { auth.InitService() user.InitService() waf.InitService() + wol.InitService() }