Impl swag and auth switch

This commit is contained in:
若海
2023-12-29 21:28:18 +08:00
parent 21f63ee545
commit 524faae9cf
12 changed files with 148 additions and 54 deletions
+12 -2
View File
@@ -8,7 +8,7 @@ import (
"wechat-rest/args"
)
func AuthGuard(c *gin.Context) {
func ApiGuard(c *gin.Context) {
token := ""
@@ -19,9 +19,19 @@ func AuthGuard(c *gin.Context) {
}
if token != args.Httpd.Token {
c.Set("Error", gin.H{"Code": 401, "Message": "未授权的操作"})
c.Set("Error", gin.H{"Code": 401, "Message": "操作未授权"})
c.Set("ExitCode", 401)
c.Abort()
}
}
func SwagGuard(c *gin.Context) {
if !args.Httpd.Swag && strings.HasPrefix(c.Request.URL.Path, "/swag") {
c.Set("Error", gin.H{"Code": 403, "Message": "功能已禁用"})
c.Set("ExitCode", 403)
c.Abort()
}
}
+9 -4
View File
@@ -9,8 +9,8 @@ import (
)
// @title Wechat Rest API
// @version v0.4.2
// @description 基于 WeChatFerry RPC 实现的电脑版微信 REST-API,使用 Go 语言编写,无第三方运行时依赖。基于 HTTP 提供操作接口,轻松对接任意编程语言。
// @version v0.4.6
// @description 基于 WeChatFerry RPC 实现的电脑版微信 REST API,使用 Go 语言编写,无第三方运行时依赖。基于 HTTP 提供操作接口,轻松对接任意编程语言。
// @contact.name WeChatRest
// @contact.url https://github.com/opentdp/wechat-rest
// @license.name Apache 2.0
@@ -20,13 +20,18 @@ import (
func Server() {
httpd.Engine(args.Debug)
httpd.Use(midware.OutputHandle)
// Api 守卫
api := httpd.Group("/api")
api.Use(midware.OutputHandle, midware.AuthGuard)
api.Use(midware.ApiGuard)
// 注册 WCF
// Wcf 路由
wcfrest.Route(api)
// Swagger 守卫
httpd.Use(midware.SwagGuard)
// 前端文件路由
httpd.StaticEmbed("/", "public", args.Efs)
+8 -18
View File
@@ -6,7 +6,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/opentdp/go-helper/logman"
"github.com/opentdp/go-helper/request"
"github.com/opentdp/go-helper/strutil"
"github.com/opentdp/wechat-rest/wcferry"
@@ -14,13 +13,12 @@ import (
)
var wc *wcferry.Client
var forwardUrls = map[string]bool{}
func initService() {
host, port, err := net.SplitHostPort(args.Wcf.Address)
if err != nil {
logman.Fatal("failed to start wcf", "error", err)
logman.Fatal("invalid address", "error", err)
}
wc = &wcferry.Client{
@@ -578,20 +576,7 @@ func enableForwardMsg(c *gin.Context) {
return
}
if _, ok := forwardUrls[req.Url]; ok {
c.Set("Error", "url already exists")
return
}
err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) {
logman.Info("forward msg", "url", req.Url, "Id", msg.Id)
request.JsonPost(req.Url, msg, request.H{})
})
if err == nil {
forwardUrls[req.Url] = true
}
err := enableForwardToUrl(req.Url)
c.Set("Payload", RespPayload{
Success: err == nil,
Error: err,
@@ -606,8 +591,13 @@ func enableForwardMsg(c *gin.Context) {
// @Router /disable_forward_msg [post]
func disableForwardMsg(c *gin.Context) {
err := wc.DisableReceiver()
var req ForwardMsgRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.Set("Error", err)
return
}
err := disableForwardToUrl(req.Url)
c.Set("Payload", RespPayload{
Success: err == nil,
Error: err,
+57
View File
@@ -0,0 +1,57 @@
package wcfrest
import (
"errors"
"github.com/opentdp/go-helper/logman"
"github.com/opentdp/go-helper/request"
"github.com/opentdp/wechat-rest/wcferry"
)
var forwardToUrlStat = false
var forwardToUrlList = map[string]bool{}
func enableForwardToUrl(url string) error {
if !forwardToUrlStat {
err := wc.EnrollReceiver(true, func(msg *wcferry.WxMsg) {
for url := range forwardToUrlList {
logman.Info("forward msg", "url", url, "Id", msg.Id)
request.JsonPost(url, msg, request.H{})
}
})
if err != nil {
return err
}
}
if _, ok := forwardToUrlList[url]; ok {
return errors.New("url already exists")
}
forwardToUrlStat = true
forwardToUrlList[url] = true
return nil
}
func disableForwardToUrl(url string) error {
if _, ok := forwardToUrlList[url]; !ok {
return errors.New("url not exists")
}
delete(forwardToUrlList, url)
if len(forwardToUrlList) == 0 {
if err := wc.DisableReceiver(false); err != nil {
return err
}
forwardToUrlStat = false
}
return nil
}