Impl swag and auth switch
This commit is contained in:
parent
21f63ee545
commit
524faae9cf
@ -104,6 +104,9 @@ wcfhttp --cb http://your_host:your_port/callback
|
|||||||
### Go
|
### Go
|
||||||
参考 [Go README.MD](clients/go/README.md)
|
参考 [Go README.MD](clients/go/README.md)
|
||||||
|
|
||||||
|
### GoHttp
|
||||||
|
参考 [GoHttp README.MD](clients/gohttp/README.MD)
|
||||||
|
|
||||||
### Java
|
### Java
|
||||||
参考 [Java README.MD](clients/java/README.MD)
|
参考 [Java README.MD](clients/java/README.MD)
|
||||||
|
|
||||||
@ -174,6 +177,7 @@ WeChatFerry
|
|||||||
│ └── demo.gif # 示例动图
|
│ └── demo.gif # 示例动图
|
||||||
├── clients
|
├── clients
|
||||||
│ ├── go # Go 客户端
|
│ ├── go # Go 客户端
|
||||||
|
│ ├── gohttp # HTTP 客户端
|
||||||
│ ├── http # HTTP 客户端
|
│ ├── http # HTTP 客户端
|
||||||
│ ├── java # Java 客户端
|
│ ├── java # Java 客户端
|
||||||
│ ├── pyauto # 群友封装的客户端
|
│ ├── pyauto # 群友封装的客户端
|
||||||
@ -188,6 +192,9 @@ WeChatFerry
|
|||||||
### go
|
### go
|
||||||
Go 客户端。
|
Go 客户端。
|
||||||
|
|
||||||
|
### gohttp
|
||||||
|
HTTP 客户端,二进制发布无依赖
|
||||||
|
|
||||||
### http
|
### http
|
||||||
HTTP 客户端。
|
HTTP 客户端。
|
||||||
|
|
||||||
|
@ -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` 查看支持的接口
|
> 接口使用范例请参考 <https://github.com/opentdp/wechat-robot>
|
||||||
|
|
||||||
## 配置说明
|
## 配置说明
|
||||||
|
|
||||||
@ -18,6 +18,7 @@
|
|||||||
httpd:
|
httpd:
|
||||||
address: 127.0.0.1:7600 # api 监听地址
|
address: 127.0.0.1:7600 # api 监听地址
|
||||||
token: "" # 使用 token 验证请求
|
token: "" # 使用 token 验证请求
|
||||||
|
swag: true # 启用 OpenApi 文档
|
||||||
logger:
|
logger:
|
||||||
dir: logs # 日志目录
|
dir: logs # 日志目录
|
||||||
level: info # 日志级别
|
level: info # 日志级别
|
||||||
@ -29,6 +30,8 @@ wcf:
|
|||||||
msgprint: true # 打印收到的消息
|
msgprint: true # 打印收到的消息
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> 若设置了 `token`,请求时需携带 **header** 信息: `Authorization: Bearer $token`
|
||||||
|
|
||||||
## 功能清单
|
## 功能清单
|
||||||
|
|
||||||
- 检查登录状态
|
- 检查登录状态
|
||||||
@ -66,5 +69,5 @@ wcf:
|
|||||||
go get github.com/swaggo/swag/cmd/swag
|
go get github.com/swaggo/swag/cmd/swag
|
||||||
go install 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
|
||||||
```
|
```
|
||||||
|
@ -29,8 +29,10 @@ var Logger = struct {
|
|||||||
var Httpd = struct {
|
var Httpd = struct {
|
||||||
Address string
|
Address string
|
||||||
Token string
|
Token string
|
||||||
|
Swag bool
|
||||||
}{
|
}{
|
||||||
Address: "127.0.0.1:7600",
|
Address: "127.0.0.1:7600",
|
||||||
|
Swag: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wcf 服务参数
|
// Wcf 服务参数
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
httpd:
|
httpd:
|
||||||
address: 127.0.0.1:7600
|
address: 127.0.0.1:7600
|
||||||
token: ""
|
token: ""
|
||||||
|
swag: true
|
||||||
logger:
|
logger:
|
||||||
dir: logs
|
dir: logs
|
||||||
level: info
|
level: info
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
"wechat-rest/args"
|
"wechat-rest/args"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AuthGuard(c *gin.Context) {
|
func ApiGuard(c *gin.Context) {
|
||||||
|
|
||||||
token := ""
|
token := ""
|
||||||
|
|
||||||
@ -19,9 +19,19 @@ func AuthGuard(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if token != args.Httpd.Token {
|
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.Set("ExitCode", 401)
|
||||||
c.Abort()
|
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,8 +9,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// @title Wechat Rest API
|
// @title Wechat Rest API
|
||||||
// @version v0.4.2
|
// @version v0.4.6
|
||||||
// @description 基于 WeChatFerry RPC 实现的电脑版微信 REST-API,使用 Go 语言编写,无第三方运行时依赖。基于 HTTP 提供操作接口,轻松对接任意编程语言。
|
// @description 基于 WeChatFerry RPC 实现的电脑版微信 REST API,使用 Go 语言编写,无第三方运行时依赖。基于 HTTP 提供操作接口,轻松对接任意编程语言。
|
||||||
// @contact.name WeChatRest
|
// @contact.name WeChatRest
|
||||||
// @contact.url https://github.com/opentdp/wechat-rest
|
// @contact.url https://github.com/opentdp/wechat-rest
|
||||||
// @license.name Apache 2.0
|
// @license.name Apache 2.0
|
||||||
@ -20,13 +20,18 @@ import (
|
|||||||
func Server() {
|
func Server() {
|
||||||
|
|
||||||
httpd.Engine(args.Debug)
|
httpd.Engine(args.Debug)
|
||||||
|
httpd.Use(midware.OutputHandle)
|
||||||
|
|
||||||
|
// Api 守卫
|
||||||
api := httpd.Group("/api")
|
api := httpd.Group("/api")
|
||||||
api.Use(midware.OutputHandle, midware.AuthGuard)
|
api.Use(midware.ApiGuard)
|
||||||
|
|
||||||
// 注册 WCF
|
// Wcf 路由
|
||||||
wcfrest.Route(api)
|
wcfrest.Route(api)
|
||||||
|
|
||||||
|
// Swagger 守卫
|
||||||
|
httpd.Use(midware.SwagGuard)
|
||||||
|
|
||||||
// 前端文件路由
|
// 前端文件路由
|
||||||
httpd.StaticEmbed("/", "public", args.Efs)
|
httpd.StaticEmbed("/", "public", args.Efs)
|
||||||
|
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/opentdp/go-helper/logman"
|
"github.com/opentdp/go-helper/logman"
|
||||||
"github.com/opentdp/go-helper/request"
|
|
||||||
"github.com/opentdp/go-helper/strutil"
|
"github.com/opentdp/go-helper/strutil"
|
||||||
"github.com/opentdp/wechat-rest/wcferry"
|
"github.com/opentdp/wechat-rest/wcferry"
|
||||||
|
|
||||||
@ -14,13 +13,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var wc *wcferry.Client
|
var wc *wcferry.Client
|
||||||
var forwardUrls = map[string]bool{}
|
|
||||||
|
|
||||||
func initService() {
|
func initService() {
|
||||||
|
|
||||||
host, port, err := net.SplitHostPort(args.Wcf.Address)
|
host, port, err := net.SplitHostPort(args.Wcf.Address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logman.Fatal("failed to start wcf", "error", err)
|
logman.Fatal("invalid address", "error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
wc = &wcferry.Client{
|
wc = &wcferry.Client{
|
||||||
@ -578,20 +576,7 @@ func enableForwardMsg(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := forwardUrls[req.Url]; ok {
|
err := enableForwardToUrl(req.Url)
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Set("Payload", RespPayload{
|
c.Set("Payload", RespPayload{
|
||||||
Success: err == nil,
|
Success: err == nil,
|
||||||
Error: err,
|
Error: err,
|
||||||
@ -606,8 +591,13 @@ func enableForwardMsg(c *gin.Context) {
|
|||||||
// @Router /disable_forward_msg [post]
|
// @Router /disable_forward_msg [post]
|
||||||
func disableForwardMsg(c *gin.Context) {
|
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{
|
c.Set("Payload", RespPayload{
|
||||||
Success: err == nil,
|
Success: err == nil,
|
||||||
Error: err,
|
Error: err,
|
||||||
|
57
clients/gohttp/httpd/wcfrest/forward.go
Normal file
57
clients/gohttp/httpd/wcfrest/forward.go
Normal 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
|
||||||
|
|
||||||
|
}
|
4
clients/gohttp/public/assets/style.css
Normal file
4
clients/gohttp/public/assets/style.css
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
body {
|
||||||
|
margin: 0;
|
||||||
|
background: #f2f4f6;
|
||||||
|
}
|
@ -5,32 +5,15 @@
|
|||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<meta http-equiv="Content-Security-Policy" />
|
<meta http-equiv="Content-Security-Policy" />
|
||||||
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
|
||||||
<link rel="shortcut icon" type="image/svg" href="assets/image/icon.svg" />
|
<link rel="shortcut icon" type="image/svg" href="/assets/image/icon.svg" />
|
||||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui.min.css" />
|
<link rel="stylesheet" href="/assets/style.css" />
|
||||||
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui-bundle.min.js"></script>
|
<title>WeChat Rest</title>
|
||||||
<title>WeChat Rest API</title>
|
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div id="swagger-ui"></div>
|
<ul>
|
||||||
<style type="text/css">
|
<li><a href="/swag">Api Document</a></li>
|
||||||
body {
|
</ul>
|
||||||
margin: 0
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
<script type="text/javascript">
|
|
||||||
SwaggerUIBundle({
|
|
||||||
url: 'swagger.json',
|
|
||||||
dom_id: '#swagger-ui',
|
|
||||||
layout: 'BaseLayout',
|
|
||||||
deepLinking: true,
|
|
||||||
showExtensions: true,
|
|
||||||
showCommonExtensions: true,
|
|
||||||
presets: [
|
|
||||||
SwaggerUIBundle.presets.apis
|
|
||||||
]
|
|
||||||
})
|
|
||||||
</script>
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
32
clients/gohttp/public/swag/index.html
Normal file
32
clients/gohttp/public/swag/index.html
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="zh-Hans-CN">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<meta http-equiv="Content-Security-Policy" />
|
||||||
|
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
|
||||||
|
<link rel="shortcut icon" type="image/svg" href="/assets/image/icon.svg" />
|
||||||
|
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui.min.css" />
|
||||||
|
<link rel="stylesheet" href="/assets/style.css" />
|
||||||
|
<title>WeChat Rest Document</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div id="swagger-ui"></div>
|
||||||
|
<script src="//cdn.jsdelivr.net/npm/swagger-ui-dist/swagger-ui-bundle.min.js"></script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
SwaggerUIBundle({
|
||||||
|
url: './swagger.json',
|
||||||
|
dom_id: '#swagger-ui',
|
||||||
|
layout: 'BaseLayout',
|
||||||
|
deepLinking: true,
|
||||||
|
showExtensions: true,
|
||||||
|
showCommonExtensions: true,
|
||||||
|
presets: [
|
||||||
|
SwaggerUIBundle.presets.apis
|
||||||
|
]
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"swagger": "2.0",
|
"swagger": "2.0",
|
||||||
"info": {
|
"info": {
|
||||||
"description": "基于 WeChatFerry RPC 实现的电脑版微信 REST-API,使用 Go 语言编写,无第三方运行时依赖。基于 HTTP 提供操作接口,轻松对接任意编程语言。",
|
"description": "基于 WeChatFerry RPC 实现的电脑版微信 REST API,使用 Go 语言编写,无第三方运行时依赖。基于 HTTP 提供操作接口,轻松对接任意编程语言。",
|
||||||
"title": "Wechat Rest API",
|
"title": "Wechat Rest API",
|
||||||
"contact": {
|
"contact": {
|
||||||
"name": "WeChatRest",
|
"name": "WeChatRest",
|
||||||
@ -11,7 +11,7 @@
|
|||||||
"name": "Apache 2.0",
|
"name": "Apache 2.0",
|
||||||
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
|
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
|
||||||
},
|
},
|
||||||
"version": "v0.4.2"
|
"version": "v0.4.6"
|
||||||
},
|
},
|
||||||
"basePath": "/api",
|
"basePath": "/api",
|
||||||
"paths": {
|
"paths": {
|
Loading…
Reference in New Issue
Block a user