diff --git a/README.MD b/README.MD index b3e15dd..04ff287 100644 --- a/README.MD +++ b/README.MD @@ -104,6 +104,9 @@ wcfhttp --cb http://your_host:your_port/callback ### Go 参考 [Go README.MD](clients/go/README.md) +### GoHttp +参考 [GoHttp README.MD](clients/gohttp/README.MD) + ### Java 参考 [Java README.MD](clients/java/README.MD) @@ -174,6 +177,7 @@ WeChatFerry │   └── demo.gif # 示例动图 ├── clients │   ├── go # Go 客户端 +│   ├── gohttp # HTTP 客户端 │   ├── http # HTTP 客户端 │   ├── java # Java 客户端 │   ├── pyauto # 群友封装的客户端 @@ -188,6 +192,9 @@ WeChatFerry ### go Go 客户端。 +### gohttp +HTTP 客户端,二进制发布无依赖 + ### http HTTP 客户端。 diff --git a/clients/gohttp/README.md b/clients/gohttp/README.md index 4c578b1..d2859da 100644 --- a/clients/gohttp/README.md +++ b/clients/gohttp/README.md @@ -4,13 +4,13 @@ ## 使用方法 -1、下载 [WeChatSetup-3.9.2.23](https://github.com/opentdp/wechat-rest/releases/download/v0.0.1/WeChatSetup-3.9.2.23.exe) 和 [Wechat-rest](https://github.com/opentdp/wechat-rest/releases) +1、下载并安装 [WeChatSetup-3.9.2.23](https://github.com/opentdp/wechat-rest/releases/download/v0.0.1/WeChatSetup-3.9.2.23.exe) 和 [Wechat-rest](https://github.com/opentdp/wechat-rest/releases) -2、在一台 Windows 系统电脑上安装刚刚下载的微信 +2、双击 `wrest.exe` 将自动启动微信和接口服务,扫码登录即可 -3、同一台电脑上,解压 `Wechat-rest` ,双击 `wrest.exe` 启动接口服务 +3、浏览器打开 `http://localhost:7600` 查看支持的接口 -4、浏览器打开 `http://localhost:7600` 查看支持的接口 +> 接口使用范例请参考 ## 配置说明 @@ -18,6 +18,7 @@ httpd: address: 127.0.0.1:7600 # api 监听地址 token: "" # 使用 token 验证请求 + swag: true # 启用 OpenApi 文档 logger: dir: logs # 日志目录 level: info # 日志级别 @@ -29,6 +30,8 @@ wcf: msgprint: true # 打印收到的消息 ``` +> 若设置了 `token`,请求时需携带 **header** 信息: `Authorization: Bearer $token` + ## 功能清单 - 检查登录状态 @@ -66,5 +69,5 @@ wcf: go get github.com/swaggo/swag/cmd/swag go install github.com/swaggo/swag/cmd/swag -swag init --parseDependency -g httpd/server.go -o public -ot json +swag init --parseDependency -g httpd/server.go -o public/swag -ot json ``` diff --git a/clients/gohttp/args/args.go b/clients/gohttp/args/args.go index cf2462a..0715c9d 100644 --- a/clients/gohttp/args/args.go +++ b/clients/gohttp/args/args.go @@ -29,8 +29,10 @@ var Logger = struct { var Httpd = struct { Address string Token string + Swag bool }{ Address: "127.0.0.1:7600", + Swag: true, } // Wcf 服务参数 diff --git a/clients/gohttp/config.yml b/clients/gohttp/config.yml index 8304935..bdf2724 100644 --- a/clients/gohttp/config.yml +++ b/clients/gohttp/config.yml @@ -1,6 +1,7 @@ httpd: address: 127.0.0.1:7600 token: "" + swag: true logger: dir: logs level: info diff --git a/clients/gohttp/httpd/midware/guard.go b/clients/gohttp/httpd/midware/guard.go index af82d28..67d082b 100644 --- a/clients/gohttp/httpd/midware/guard.go +++ b/clients/gohttp/httpd/midware/guard.go @@ -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() + } + +} diff --git a/clients/gohttp/httpd/server.go b/clients/gohttp/httpd/server.go index 7d0e9df..4874ae9 100644 --- a/clients/gohttp/httpd/server.go +++ b/clients/gohttp/httpd/server.go @@ -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) diff --git a/clients/gohttp/httpd/wcfrest/controller.go b/clients/gohttp/httpd/wcfrest/controller.go index d0372a1..84671db 100644 --- a/clients/gohttp/httpd/wcfrest/controller.go +++ b/clients/gohttp/httpd/wcfrest/controller.go @@ -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, diff --git a/clients/gohttp/httpd/wcfrest/forward.go b/clients/gohttp/httpd/wcfrest/forward.go new file mode 100644 index 0000000..7c20d44 --- /dev/null +++ b/clients/gohttp/httpd/wcfrest/forward.go @@ -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 + +} diff --git a/clients/gohttp/public/assets/style.css b/clients/gohttp/public/assets/style.css new file mode 100644 index 0000000..a867a0d --- /dev/null +++ b/clients/gohttp/public/assets/style.css @@ -0,0 +1,4 @@ +body { + margin: 0; + background: #f2f4f6; +} diff --git a/clients/gohttp/public/index.html b/clients/gohttp/public/index.html index a7ed7b5..d2c49fb 100644 --- a/clients/gohttp/public/index.html +++ b/clients/gohttp/public/index.html @@ -5,32 +5,15 @@ - - - - WeChat Rest API + + + WeChat Rest -
- - + \ No newline at end of file diff --git a/clients/gohttp/public/swag/index.html b/clients/gohttp/public/swag/index.html new file mode 100644 index 0000000..443938f --- /dev/null +++ b/clients/gohttp/public/swag/index.html @@ -0,0 +1,32 @@ + + + + + + + + + + + WeChat Rest Document + + + +
+ + + + + \ No newline at end of file diff --git a/clients/gohttp/public/swagger.json b/clients/gohttp/public/swag/swagger.json similarity index 99% rename from clients/gohttp/public/swagger.json rename to clients/gohttp/public/swag/swagger.json index a65c009..46e13b5 100644 --- a/clients/gohttp/public/swagger.json +++ b/clients/gohttp/public/swag/swagger.json @@ -1,7 +1,7 @@ { "swagger": "2.0", "info": { - "description": "基于 WeChatFerry RPC 实现的电脑版微信 REST-API,使用 Go 语言编写,无第三方运行时依赖。基于 HTTP 提供操作接口,轻松对接任意编程语言。", + "description": "基于 WeChatFerry RPC 实现的电脑版微信 REST API,使用 Go 语言编写,无第三方运行时依赖。基于 HTTP 提供操作接口,轻松对接任意编程语言。", "title": "Wechat Rest API", "contact": { "name": "WeChatRest", @@ -11,7 +11,7 @@ "name": "Apache 2.0", "url": "http://www.apache.org/licenses/LICENSE-2.0.html" }, - "version": "v0.4.2" + "version": "v0.4.6" }, "basePath": "/api", "paths": {