1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-06-30 20:03:24 +00:00

Compare commits

...

114 Commits

Author SHA1 Message Date
bf77951f8d Merge branch 'dev' 2021-08-15 02:55:42 +08:00
971cb5d854 fix lint. 2021-08-15 02:53:35 +08:00
e13a5bdad0 update doc & fix typo. 2021-08-15 02:51:32 +08:00
bf4f7fb41e fix lint. 2021-08-15 02:40:39 +08:00
eaa8154a33 update doc. 2021-08-15 02:37:51 +08:00
6240e875ce add: mark_msg_as_read api. 2021-08-15 02:36:11 +08:00
3c9433d7b7 remove: mark msg as read. 2021-08-15 02:22:47 +08:00
4b4193c6e3 feat: load leveldb conf from environment variable. close #1002 2021-08-15 02:15:33 +08:00
a1e3c57488 fix typo. 2021-08-15 01:58:43 +08:00
b43bdc1da5 update slider.md 2021-08-15 01:58:20 +08:00
aa46ab0119 add: session delete log. 2021-08-15 01:54:03 +08:00
92ad7d5938 fix(scf): fix write response. 2021-08-14 14:33:36 +08:00
ae04d26f51 Merge pull request #1005 from sam01101/dev.fix_time
修改错误的 `Timestamp`
2021-08-13 19:09:21 +08:00
d3b22a7a46 [Fix] Using timestamp from server 2021-08-13 18:55:04 +08:00
87d16f0e15 .github : update bug template. 2021-08-12 10:33:37 +08:00
c355549c8d fix(server): ws reverse reconnect. 2021-08-11 22:40:21 +08:00
75e82eaf35 fix(event): friend poke self. 2021-08-10 15:10:42 +08:00
4c8b2e9f13 drop cache file .cqimg. 2021-08-09 21:31:06 +08:00
84488f9bf1 remove message.ShortVideoElement in LocalVideoElement 2021-08-09 21:15:18 +08:00
5b705273c2 docs: fix typo (#998) 2021-08-09 13:38:49 +08:00
4bcfe9b8f1 feat: member special title updated event. 2021-08-09 06:17:12 +08:00
1863c44d11 update go.sum 2021-08-09 06:11:15 +08:00
c6f19016e1 update dep. 2021-08-09 06:11:04 +08:00
d88c30a3c7 update dep. 2021-08-09 06:08:04 +08:00
fd10f0d0aa doc: format & typo. 2021-08-07 23:05:44 +08:00
a6a666fe31 fix(config): panic on parsing env.
Fixes: #984
2021-08-07 19:07:10 +08:00
c423f6d6bb feat(coolq): new field "shut_up_timestamp" in group members.
Fixes: #918
2021-08-07 16:48:00 +08:00
605e572b87 feat(log): remove logrus-easy-formatter. 2021-08-07 12:56:49 +08:00
e506622399 feat(codec): enhance ffmpeg output with debug. 2021-08-07 12:04:32 +08:00
3326b2aa2f feat(coolq): mimetypes switch to array. 2021-08-06 22:31:27 +08:00
a38b86b763 Merge pull request #981 from fumiama/master
Fix: Image mime scan add type webp
2021-08-05 00:08:29 +08:00
b44f9545a8 fix(coolq): upload resources when send forward message.
Fixes: #987
2021-08-04 23:57:41 +08:00
38fff9bac0 Fix: Image mime scan add type webp 2021-08-03 23:52:27 +08:00
38865584ec update MiraiGo. 2021-08-03 15:39:40 +08:00
6c52734324 ci(chore): Fix stylings 2021-08-01 20:29:21 +00:00
a2f5be2166 feat: media mime scan. 2021-08-02 04:28:19 +08:00
cf307f455e doc: scf. 2021-08-01 21:41:01 +08:00
58d96004a3 perf(coolq): small change. 2021-08-01 21:41:00 +08:00
c951caba9f Merge branch 'dev' of github.com:/Mrs4s/go-cqhttp into dev 2021-08-01 06:29:37 +08:00
f4e38cb416 fix: go mod. 2021-08-01 06:29:10 +08:00
6fd104a8b9 perf(coolq): optimize CQCodeEscapeText. 2021-08-01 00:16:21 +08:00
86202d9d90 style: clean up io/ioutil.
rf '
  ex {
    import "io/ioutil";
    import "os";
    ioutil.WriteFile -> os.WriteFile
    ioutil.ReadFile -> os.ReadFile
  }
'
2021-07-29 15:18:50 +08:00
a3906a5d33 style: go1.17 fmt ./... 2021-07-28 19:29:48 +08:00
0be4c79d2c style: remove init func in main. 2021-07-28 19:21:25 +08:00
d705dd0617 chore(dep): update MiraiGo. 2021-07-26 18:34:29 +08:00
e5a8a406a4 clean send msg arg. 2021-07-26 18:11:57 +08:00
89eaf91371 fix(server): always send response. 2021-07-25 13:28:25 +08:00
19b2a86c7c fix golint. 2021-07-25 13:04:15 +08:00
e56e3b5036 feat(server): support serverless functions. 2021-07-25 12:41:57 +08:00
cdd2dcf907 feat(server): support http long polling. 2021-07-24 21:38:51 +08:00
6d5bf84603 simply makeShowPic. 2021-07-22 23:08:54 +08:00
7274a46a3c feat: add api port to HTTP post headers. 2021-07-18 23:04:06 +08:00
decab775f9 update dependencies. fix #935 2021-07-18 22:57:52 +08:00
cd4ac2078b fix token login. 2021-07-18 22:48:38 +08:00
04fad744a5 update dependencies. 2021-07-18 22:42:22 +08:00
d28e6f6e82 fix typo. 2021-07-18 18:28:46 +08:00
9c9a469a16 ci(chore): Fix stylings 2021-07-18 10:04:18 +00:00
d7e0aaf827 feat: environment config support. 2021-07-18 18:03:23 +08:00
0a5e172a4a update dependencies & fix resource leak. 2021-07-18 16:03:03 +08:00
4894731422 rf(message): refactor show pic&flash pic.
Fixes: #470
2021-07-18 11:42:36 +08:00
85df77f9a5 feat: log aging & force new log file. close #963 2021-07-17 22:58:45 +08:00
b68bb0762f update dependencies. 2021-07-17 22:41:35 +08:00
2db968bbe3 feat(server): use a in-place removal to clean closed ws conn. 2021-07-17 22:07:21 +08:00
426aa5718f fix: form parse.
Fixes: #960
2021-07-15 10:13:24 +08:00
54cef1fd37 fix: private reply message incorrect display in android.
Fixes: #814
2021-07-14 21:45:21 +08:00
3663c2aed8 fix: build tag. 2021-07-14 21:43:36 +08:00
ca93d4de0d fix: data race && cgo build. 2021-07-12 11:39:46 +08:00
c6994ade94 chore(ci): fix commit back. 2021-07-11 15:30:46 +08:00
f21730e879 chore(ci): clean workflows. 2021-07-10 23:47:17 +08:00
ca8d28f913 clean ws client push event. 2021-07-10 23:29:42 +08:00
34c22bed9c refactor(server): lazy marshal json. 2021-07-10 23:10:32 +08:00
9390503683 chore(dep): update dependencies. 2021-07-10 11:07:06 +08:00
ccc344ab1c fix(server): panic on multi ws server. 2021-07-09 22:45:30 +08:00
e768ff0dc2 refactor(coolq): clean forward media upload. 2021-07-09 22:40:44 +08:00
404c39eb53 refactor(coolq): clean media upload. 2021-07-09 11:15:36 +08:00
2f05775101 style: small code clean. 2021-07-08 23:01:03 +08:00
d266242887 clear check authorization. 2021-07-07 21:19:02 +08:00
e9b302ad74 chore(dep): update dep. 2021-07-07 21:11:25 +08:00
6042d451e9 Merge branch 'master' into dev 2021-07-07 20:13:05 +08:00
b5fbc6f2d5 Merge pull request #936 from remiliacn/patch-1
Fix: Typo.
2021-07-04 22:06:05 +08:00
4a15138767 Merge pull request #948 from yume233/master
缓存依赖,优化CI编译速度,增加手动触发
2021-07-04 22:05:47 +08:00
f6e5e28a7a 增加手动触发 2021-07-03 20:07:24 +08:00
5d00bd134d 增加依赖缓存 2021-07-03 20:04:12 +08:00
a894ee91bc 增加依赖缓存 2021-07-03 20:03:39 +08:00
8bfbfc04f3 缓存依赖,优化CI编译速度,增加手动触发 2021-07-03 17:55:46 +08:00
e8ddc1f140 fix log message with invalid forward node. 2021-07-03 16:21:31 +08:00
5cfd53fd8a fix custom reply element param name. 2021-07-03 16:17:18 +08:00
48b442a281 fix send_group_forward_msg response message. 2021-07-03 16:09:59 +08:00
ad7e53009d fix device lock. 2021-07-02 09:46:15 +08:00
a87d0e9f8f fix wrong channel closed in SetupMainSignalHandler (#939) 2021-06-29 19:43:20 +08:00
70558dc965 Merge pull request #938 from povsister/printstack 2021-06-26 14:53:51 +08:00
752c82e8d6 deal with pipe close error 2021-06-26 14:53:19 +08:00
e2cafbd7d6 添加无需额外配置直接打印stack的功能
*unix: kill -USR1 <gocq_pid>
windows: echo dumpstack >\\.\pipe\go-cqhttp-<pid>

stackdump将直接以<exec_name>.<pid>.stacks.<timestamp>.log
的文件名保存在当前工作目录下
2021-06-26 14:33:37 +08:00
6e18969f96 Misc: 视频在图片文件夹里太怪了所以改一下 2021-06-25 18:25:13 -05:00
60297d1a83 Fix: Typo.
实例里的`video`写成了`image`
2021-06-25 18:18:14 -05:00
499108cb1b refactor(server): unify http & ws-server Authorization 2021-06-24 20:18:38 +08:00
2bbc75672f fix(filter): add missed code. 2021-06-23 13:38:03 +08:00
bddada66dc Merge pull request #930 from synodriver/unchanged-dev
typo
2021-06-23 13:27:49 +08:00
ee3580039b typo 2021-06-23 13:06:23 +08:00
63e1508815 style(filter): clean dead code. 2021-06-19 21:27:51 +08:00
7c5bd64e9b rf(api): simply get user_id 2021-06-19 21:25:38 +08:00
a117d67286 style: clear filter. 2021-06-19 21:08:13 +08:00
f40ff22c0e fix #923. 2021-06-17 15:11:29 +08:00
edde0c212c Merge pull request #906 from Shigma/patch-1
向 README 中添加适当的空格
2021-06-16 16:01:11 +08:00
8eb4b0388f Merge pull request #907 from Shigma/patch-2
修改 README 中的几处拼写细节
2021-06-16 16:00:38 +08:00
fad3bf1314 fix: private image upload
Updates: #914
2021-06-11 13:45:31 +08:00
da23fc1403 fix image source. 2021-06-08 13:24:36 +08:00
2f267e1926 remove: delete_group_file api folder_id param. 2021-06-06 14:48:16 +08:00
37d846c190 fix: private message recall (#669)
Fixes: #669
2021-05-31 18:39:16 +08:00
9c17a3e1c8 Update README.md 2021-05-31 00:59:17 +08:00
9d44697dcd Update README.md 2021-05-31 00:54:23 +08:00
139f053c5b Fix: Image won't load in README.md 2021-05-31 00:35:28 +08:00
19fdd34b7f style: clean code 2021-05-30 14:38:08 +08:00
9f7e31766b refactor(server): replace gin with net/http
reduce binary size about 2M
2021-05-30 14:05:50 +08:00
46 changed files with 1898 additions and 1214 deletions

View File

@ -37,6 +37,8 @@ go-cqhttp版本:
连接方式: 连接方式:
使用协议: 使用协议:
**在最新的release版本中能否复现**
**bug内容** **bug内容**
<!-- 请在这里详细描述bug的内容 --> <!-- 请在这里详细描述bug的内容 -->

View File

@ -1,6 +1,6 @@
name: CI name: CI
on: [push, pull_request] on: [push, pull_request,workflow_dispatch]
env: env:
BINARY_PREFIX: "go-cqhttp_" BINARY_PREFIX: "go-cqhttp_"
@ -31,6 +31,13 @@ jobs:
uses: actions/setup-go@v2.1.3 uses: actions/setup-go@v2.1.3
with: with:
go-version: 1.16 go-version: 1.16
- name: Cache downloaded module
uses: actions/cache@v2
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.goos }}-${{ matrix.goarch }}-${{ hashFiles('**/go.sum') }}
- name: Build binary file - name: Build binary file
env: env:
GOOS: ${{ matrix.goos }} GOOS: ${{ matrix.goos }}

View File

@ -1,6 +1,6 @@
name: Lint name: Lint
on: [push] on: [push,pull_request,workflow_dispatch]
jobs: jobs:
golangci: golangci:
@ -24,7 +24,7 @@ jobs:
go test $(go list ./...) go test $(go list ./...)
- name: Commit back - name: Commit back
if: github.repository_owner == 'Mrs4s' if: ${{ github.repository_owner == 'Mrs4s' && !github.event.pull_request }}
continue-on-error: true continue-on-error: true
run: | run: |
git config --local user.name 'github-actions[bot]' git config --local user.name 'github-actions[bot]'
@ -32,3 +32,9 @@ jobs:
git add --all git add --all
git commit -m "ci(chore): Fix stylings" git commit -m "ci(chore): Fix stylings"
git push git push
- name: Suggester
if: ${{ github.event.pull_request }}
uses: reviewdog/action-suggester@v1
with:
tool_name: golangci-lint

View File

@ -1,29 +0,0 @@
name: Lint
on: [pull_request]
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup Go environment
uses: actions/setup-go@v2.1.3
with:
go-version: 1.16
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: latest
- name: Tests
run: |
go test $(go list ./...)
- name: Suggester
uses: reviewdog/action-suggester@v1
with:
tool_name: golangci-lint

View File

@ -1,5 +1,7 @@
<p align="center"> <p align="center">
<a href="https://ishkong.github.io/go-cqhttp-docs/"><img src="https://i.loli.net/2020/12/20/qSLMDWxiocRQgu6.jpg" width="200" height="200" alt="go-cqhttp"></a> <a href="https://ishkong.github.io/go-cqhttp-docs/">
<img src="https://user-images.githubusercontent.com/25968335/120111974-8abef880-c139-11eb-99cd-fa928348b198.png" width="200" height="200" alt="go-cqhttp">
</a>
</p> </p>
<div align="center"> <div align="center">
@ -42,14 +44,14 @@ _✨ 基于 [Mirai](https://github.com/mamoe/mirai) 以及 [MiraiGo](https://git
## 兼容性 ## 兼容性
go-cqhttp兼容[OneBot-v11](https://github.com/howmanybots/onebot/tree/master/v11/specs)绝大多数内容并在其基础上做了一些扩展详情请看go-cqhttp的文档 go-cqhttp 兼容 [OneBot-v11](https://github.com/howmanybots/onebot/tree/master/v11/specs) 绝大多数内容,并在其基础上做了一些扩展,详情请看 go-cqhttp 的文档
### 接口 ### 接口
- [x] HTTP API - [x] HTTP API
- [x] 反向 HTTP POST - [x] 反向 HTTP POST
- [x] 正向 Websocket - [x] 正向 WebSocket
- [x] 反向 Websocket - [x] 反向 WebSocket
### 拓展支持 ### 拓展支持
@ -66,9 +68,9 @@ go-cqhttp兼容[OneBot-v11](https://github.com/howmanybots/onebot/tree/master/v1
### 实现 ### 实现
<details> <details>
<summary>已实现CQ码</summary> <summary>已实现 CQ </summary>
#### 符合 Onebot 标准的 CQ 码 #### 符合 OneBot 标准的 CQ 码
| CQ 码 | 功能 | | CQ 码 | 功能 |
| ------------ | --------------------------- | | ------------ | --------------------------- |
@ -97,7 +99,7 @@ go-cqhttp兼容[OneBot-v11](https://github.com/howmanybots/onebot/tree/master/v1
[xml 消息]: https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#xml-%E6%B6%88%E6%81%AF [xml 消息]: https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#xml-%E6%B6%88%E6%81%AF
[json 消息]: https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#json-%E6%B6%88%E6%81%AF [json 消息]: https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#json-%E6%B6%88%E6%81%AF
#### 拓展 CQ 码及与 Onebot 标准有略微差异的 CQ 码 #### 拓展 CQ 码及与 OneBot 标准有略微差异的 CQ 码
| 拓展 CQ 码 | 功能 | | 拓展 CQ 码 | 功能 |
| -------------- | --------------------------------- | | -------------- | --------------------------------- |
@ -120,9 +122,9 @@ go-cqhttp兼容[OneBot-v11](https://github.com/howmanybots/onebot/tree/master/v1
</details> </details>
<details> <details>
<summary>已实现API</summary> <summary>已实现 API</summary>
#### 符合 Onebot 标准的 API #### 符合 OneBot 标准的 API
| API | 功能 | | API | 功能 |
| ------------------------ | ---------------------- | | ------------------------ | ---------------------- |
@ -182,7 +184,7 @@ go-cqhttp兼容[OneBot-v11](https://github.com/howmanybots/onebot/tree/master/v1
[重启 go-cqhttp]: https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_restart-%E9%87%8D%E5%90%AF-onebot-%E5%AE%9E%E7%8E%B0 [重启 go-cqhttp]: https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_restart-%E9%87%8D%E5%90%AF-onebot-%E5%AE%9E%E7%8E%B0
[对事件执行快速操作]: https://github.com/howmanybots/onebot/blob/master/v11/specs/api/hidden.md#handle_quick_operation-%E5%AF%B9%E4%BA%8B%E4%BB%B6%E6%89%A7%E8%A1%8C%E5%BF%AB%E9%80%9F%E6%93%8D%E4%BD%9C [对事件执行快速操作]: https://github.com/howmanybots/onebot/blob/master/v11/specs/api/hidden.md#handle_quick_operation-%E5%AF%B9%E4%BA%8B%E4%BB%B6%E6%89%A7%E8%A1%8C%E5%BF%AB%E9%80%9F%E6%93%8D%E4%BD%9C
#### 拓展 API 及与 Onebot 标准有略微差异的 API #### 拓展 API 及与 OneBot 标准有略微差异的 API
| 拓展 API | 功能 | | 拓展 API | 功能 |
| --------------------------- | ---------------------- | | --------------------------- | ---------------------- |
@ -217,9 +219,9 @@ go-cqhttp兼容[OneBot-v11](https://github.com/howmanybots/onebot/tree/master/v1
</details> </details>
<details> <details>
<summary>已实现Event</summary> <summary>已实现 Event</summary>
#### 符合 Onebot 标准的 Event部分 Event 比 Onebot 标准多上报几个字段,不影响使用) #### 符合 OneBot 标准的 Event部分 Event 比 OneBot 标准多上报几个字段,不影响使用)
| 事件类型 | Event | | 事件类型 | Event |
| -------- | ---------------- | | -------- | ---------------- |
@ -285,4 +287,4 @@ go-cqhttp兼容[OneBot-v11](https://github.com/howmanybots/onebot/tree/master/v1
## 性能 ## 性能
在关闭数据库的情况下, 加载 25 个好友 128 个群运行 24 小时后内存使用为 15MB 左右. 开启数据库后内存使用将根据消息量增加 10-20MB, 如果系统内存小于 128M 建议关闭数据库使用. 在关闭数据库的情况下, 加载 25 个好友 128 个群运行 24 小时后内存使用为 15MB 左右. 开启数据库后内存使用将根据消息量增加 10-20MB, 如果系统内存小于 128M 建议关闭数据库使用.

View File

@ -4,7 +4,6 @@ import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io/ioutil"
"math" "math"
"os" "os"
"path" "path"
@ -14,12 +13,10 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"unicode/utf8"
"github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client" "github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/MiraiGo/message" "github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/MiraiGo/utils"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
@ -321,13 +318,13 @@ func (bot *CQBot) CQGroupFileDeleteFolder(groupID int64, id string) MSG {
// CQGroupFileDeleteFile 拓展API-删除群文件 // CQGroupFileDeleteFile 拓展API-删除群文件
// //
// //
func (bot *CQBot) CQGroupFileDeleteFile(groupID int64, parentID, id string, busID int32) MSG { func (bot *CQBot) CQGroupFileDeleteFile(groupID int64, id string, busID int32) MSG {
fs, err := bot.Client.GetGroupFileSystem(groupID) fs, err := bot.Client.GetGroupFileSystem(groupID)
if err != nil { if err != nil {
log.Errorf("获取群 %v 文件系统信息失败: %v", groupID, err) log.Errorf("获取群 %v 文件系统信息失败: %v", groupID, err)
return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error()) return Failed(100, "FILE_SYSTEM_API_ERROR", err.Error())
} }
if res := fs.DeleteFile(parentID, id, busID); res != "" { if res := fs.DeleteFile("", id, busID); res != "" {
log.Errorf("删除群 %v 文件 %v 时出现文件: %v", groupID, id, res) log.Errorf("删除群 %v 文件 %v 时出现文件: %v", groupID, id, res)
return Failed(200, "FILE_SYSTEM_API_ERROR", res) return Failed(200, "FILE_SYSTEM_API_ERROR", res)
} }
@ -351,8 +348,7 @@ func (bot *CQBot) CQGetWordSlices(content string) MSG {
// CQSendGroupMessage 发送群消息 // CQSendGroupMessage 发送群消息
// //
// https://git.io/Jtz1c // https://git.io/Jtz1c
func (bot *CQBot) CQSendGroupMessage(groupID int64, i interface{}, autoEscape bool) MSG { func (bot *CQBot) CQSendGroupMessage(groupID int64, m gjson.Result, autoEscape bool) MSG {
var str string
group := bot.Client.FindGroup(groupID) group := bot.Client.FindGroup(groupID)
if group == nil { if group == nil {
return Failed(100, "GROUP_NOT_FOUND", "群聊不存在") return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
@ -360,38 +356,29 @@ func (bot *CQBot) CQSendGroupMessage(groupID int64, i interface{}, autoEscape bo
fixAt := func(elem []message.IMessageElement) { fixAt := func(elem []message.IMessageElement) {
for _, e := range elem { for _, e := range elem {
if at, ok := e.(*message.AtElement); ok && at.Target != 0 && at.Display == "" { if at, ok := e.(*message.AtElement); ok && at.Target != 0 && at.Display == "" {
at.Display = "@" + func() string { mem := group.FindMember(at.Target)
mem := group.FindMember(at.Target) if mem != nil {
if mem != nil { at.Display = "@" + mem.DisplayName()
return mem.DisplayName() } else {
} at.Display = "@" + strconv.FormatInt(at.Target, 10)
return strconv.FormatInt(at.Target, 10) }
}()
} }
} }
} }
if m, ok := i.(gjson.Result); ok {
if m.Type == gjson.JSON { if m.Type == gjson.JSON {
elem := bot.ConvertObjectMessage(m, true) elem := bot.ConvertObjectMessage(m, true)
fixAt(elem) fixAt(elem)
mid := bot.SendGroupMessage(groupID, &message.SendingMessage{Elements: elem}) mid := bot.SendGroupMessage(groupID, &message.SendingMessage{Elements: elem})
if mid == -1 { if mid == -1 {
return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出") return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出")
}
log.Infof("发送群 %v(%v) 的消息: %v (%v)", group.Name, groupID, limitedString(ToStringMessage(elem, groupID)), mid)
return OK(MSG{"message_id": mid})
} }
str = func() string { log.Infof("发送群 %v(%v) 的消息: %v (%v)", group.Name, groupID, limitedString(ToStringMessage(elem, groupID)), mid)
if m.Str != "" { return OK(MSG{"message_id": mid})
return m.Str
}
return m.Raw
}()
} else if s, ok := i.(string); ok {
str = s
} }
str := m.String()
if str == "" { if str == "" {
log.Warnf("群消息发送失败: 信息为空. MSG: %v", i) log.Warn("群消息发送失败: 信息为空.")
return Failed(100, "EMPTY_MSG_ERROR", "消息为空") return Failed(100, "EMPTY_MSG_ERROR", "消息为空")
} }
var elem []message.IMessageElement var elem []message.IMessageElement
@ -416,28 +403,44 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) MSG {
if m.Type != gjson.JSON { if m.Type != gjson.JSON {
return Failed(100) return Failed(100)
} }
var sendNodes []*message.ForwardNode fm := message.NewForwardMessage()
ts := time.Now().Add(-time.Minute * 5) ts := time.Now().Add(-time.Minute * 5)
hasCustom := func() bool { hasCustom := false
for _, item := range m.Array() { m.ForEach(func(_, item gjson.Result) bool {
if item.Get("data.uin").Exists() { if item.Get("data.uin").Exists() || item.Get("data.user_id").Exists() {
return true hasCustom = true
return false
}
return true
})
var resolveElement = func(elems []message.IMessageElement) []message.IMessageElement {
for i, elem := range elems {
switch elem.(type) {
case *LocalImageElement, *LocalVideoElement:
gm, err := bot.uploadMedia(elem, groupID, true)
if err != nil {
log.Warnf("警告: 群 %d %s上传失败: %v", groupID, elem.Type().String(), err)
continue
}
elems[i] = gm
} }
} }
return false return elems
}() }
var convert func(e gjson.Result) []*message.ForwardNode
convert = func(e gjson.Result) (nodes []*message.ForwardNode) { var convert func(e gjson.Result) *message.ForwardNode
convert = func(e gjson.Result) *message.ForwardNode {
if e.Get("type").Str != "node" { if e.Get("type").Str != "node" {
return nil return nil
} }
ts.Add(time.Second) ts.Add(time.Second)
if e.Get("data.id").Exists() { if e.Get("data.id").Exists() {
i, _ := strconv.Atoi(e.Get("data.id").String()) i := e.Get("data.id").Int()
m := bot.GetMessage(int32(i)) m := bot.GetMessage(int32(i))
if m != nil { if m != nil {
sender := m["sender"].(message.Sender) sender := m["sender"].(message.Sender)
nodes = append(nodes, &message.ForwardNode{ return &message.ForwardNode{
SenderId: sender.Uin, SenderId: sender.Uin,
SenderName: (&sender).DisplayName(), SenderName: (&sender).DisplayName(),
Time: func() int32 { Time: func() int32 {
@ -447,87 +450,73 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) MSG {
} }
return msgTime return msgTime
}(), }(),
Message: bot.ConvertStringMessage(m["message"].(string), true), Message: resolveElement(bot.ConvertStringMessage(m["message"].(string), true)),
}) }
return
} }
log.Warnf("警告: 引用消息 %v 错误或数据库未开启.", e.Get("data.id").Str) log.Warnf("警告: 引用消息 %v 错误或数据库未开启.", e.Get("data.id").Str)
return return nil
} }
uin, _ := strconv.ParseInt(e.Get("data.uin").Str, 10, 64) uin := e.Get("data.[user_id,uin].0").Int()
msgTime, err := strconv.ParseInt(e.Get("data.time").Str, 10, 64) msgTime := e.Get("data.time").Int()
if err != nil { if msgTime == 0 {
msgTime = ts.Unix() msgTime = ts.Unix()
} }
name := e.Get("data.name").Str name := e.Get("data.name").Str
c := e.Get("data.content") c := e.Get("data.content")
if c.IsArray() { if c.IsArray() {
flag := false nested := false
c.ForEach(func(_, value gjson.Result) bool { c.ForEach(func(_, value gjson.Result) bool {
if value.Get("type").String() == "node" { if value.Get("type").Str == "node" {
flag = true nested = true
return false return false
} }
return true return true
}) })
if flag { if nested { // 处理嵌套
var taowa []*message.ForwardNode nest := message.NewForwardMessage()
for _, item := range c.Array() { for _, item := range c.Array() {
taowa = append(taowa, convert(item)...) node := convert(item)
if node != nil {
nest.AddNode(node)
}
} }
nodes = append(nodes, &message.ForwardNode{ elem := bot.Client.UploadGroupForwardMessage(groupID, nest)
return &message.ForwardNode{
SenderId: uin, SenderId: uin,
SenderName: name, SenderName: name,
Time: int32(msgTime), Time: int32(msgTime),
Message: []message.IMessageElement{bot.Client.UploadGroupForwardMessage(groupID, &message.ForwardMessage{Nodes: taowa})}, Message: []message.IMessageElement{elem},
}) }
return
} }
} }
content := bot.ConvertObjectMessage(e.Get("data.content"), true) content := bot.ConvertObjectMessage(e.Get("data.content"), true)
if uin != 0 && name != "" && len(content) > 0 { if uin != 0 && name != "" && len(content) > 0 {
var newElem []message.IMessageElement return &message.ForwardNode{
for _, elem := range content {
if img, ok := elem.(*LocalImageElement); ok {
gm, err := bot.UploadLocalImageAsGroup(groupID, img)
if err != nil {
log.Warnf("警告:群 %v 图片上传失败: %v", groupID, err)
continue
}
newElem = append(newElem, gm)
continue
}
if video, ok := elem.(*LocalVideoElement); ok {
gm, err := bot.UploadLocalVideo(groupID, video)
if err != nil {
log.Warnf("警告:群 %v 视频上传失败: %v", groupID, err)
continue
}
newElem = append(newElem, gm)
continue
}
newElem = append(newElem, elem)
}
nodes = append(nodes, &message.ForwardNode{
SenderId: uin, SenderId: uin,
SenderName: name, SenderName: name,
Time: int32(msgTime), Time: int32(msgTime),
Message: newElem, Message: resolveElement(content),
}) }
return
} }
log.Warnf("警告: 非法 Forward node 将跳过") log.Warnf("警告: 非法 Forward node 将跳过. uin: %v name: %v content count: %v", uin, name, len(content))
return return nil
} }
if m.IsArray() { if m.IsArray() {
for _, item := range m.Array() { for _, item := range m.Array() {
sendNodes = append(sendNodes, convert(item)...) node := convert(item)
if node != nil {
fm.AddNode(node)
}
} }
} else { } else {
sendNodes = convert(m) node := convert(m)
if node != nil {
fm.AddNode(node)
}
} }
if len(sendNodes) > 0 { if fm.Length() > 0 {
ret := bot.Client.SendGroupForwardMessage(groupID, &message.ForwardMessage{Nodes: sendNodes}) fe := bot.Client.UploadGroupForwardMessage(groupID, fm)
ret := bot.Client.SendGroupForwardMessage(groupID, fe)
if ret == nil || ret.Id == -1 { if ret == nil || ret.Id == -1 {
log.Warnf("合并转发(群)消息发送失败: 账号可能被风控.") log.Warnf("合并转发(群)消息发送失败: 账号可能被风控.")
return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出") return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出")
@ -536,33 +525,23 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) MSG {
"message_id": bot.InsertGroupMessage(ret), "message_id": bot.InsertGroupMessage(ret),
}) })
} }
return Failed(100) return Failed(100, "EMPTY_NODES", "未找到任何可发送的合并转发信息")
} }
// CQSendPrivateMessage 发送私聊消息 // CQSendPrivateMessage 发送私聊消息
// //
// https://git.io/Jtz1l // https://git.io/Jtz1l
func (bot *CQBot) CQSendPrivateMessage(userID int64, groupID int64, i interface{}, autoEscape bool) MSG { func (bot *CQBot) CQSendPrivateMessage(userID int64, groupID int64, m gjson.Result, autoEscape bool) MSG {
var str string if m.Type == gjson.JSON {
if m, ok := i.(gjson.Result); ok { elem := bot.ConvertObjectMessage(m, false)
if m.Type == gjson.JSON { mid := bot.SendPrivateMessage(userID, groupID, &message.SendingMessage{Elements: elem})
elem := bot.ConvertObjectMessage(m, false) if mid == -1 {
mid := bot.SendPrivateMessage(userID, groupID, &message.SendingMessage{Elements: elem}) return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出")
if mid == -1 {
return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出")
}
log.Infof("发送好友 %v(%v) 的消息: %v (%v)", userID, userID, limitedString(m.String()), mid)
return OK(MSG{"message_id": mid})
} }
str = func() string { log.Infof("发送好友 %v(%v) 的消息: %v (%v)", userID, userID, limitedString(m.String()), mid)
if m.Str != "" { return OK(MSG{"message_id": mid})
return m.Str
}
return m.Raw
}()
} else if s, ok := i.(string); ok {
str = s
} }
str := m.String()
if str == "" { if str == "" {
return Failed(100, "EMPTY_MSG_ERROR", "消息为空") return Failed(100, "EMPTY_MSG_ERROR", "消息为空")
} }
@ -1029,7 +1008,7 @@ func (bot *CQBot) CQGetImage(file string) MSG {
if !global.PathExists(path.Join(global.ImagePath, file)) { if !global.PathExists(path.Join(global.ImagePath, file)) {
return Failed(100) return Failed(100)
} }
b, err := ioutil.ReadFile(path.Join(global.ImagePath, file)) b, err := os.ReadFile(path.Join(global.ImagePath, file))
if err == nil { if err == nil {
r := binary.NewReader(b) r := binary.NewReader(b)
r.ReadBytes(16) r.ReadBytes(16)
@ -1352,9 +1331,9 @@ func (bot *CQBot) CQGetEssenceMessageList(groupCode int64) MSG {
"sender_time": m.SenderTime, "sender_time": m.SenderTime,
"operator_time": m.AddDigestTime, "operator_time": m.AddDigestTime,
"operator_nick": m.AddDigestNick, "operator_nick": m.AddDigestNick,
"sender_id": m.SenderUin,
"operator_id": m.AddDigestUin,
} }
msg["sender_id"], _ = strconv.ParseUint(m.SenderUin, 10, 64)
msg["operator_id"], _ = strconv.ParseUint(m.AddDigestUin, 10, 64)
msg["message_id"] = toGlobalID(groupCode, int32(m.MessageID)) msg["message_id"] = toGlobalID(groupCode, int32(m.MessageID))
list = append(list, msg) list = append(list, msg)
} }
@ -1439,6 +1418,23 @@ func (bot *CQBot) CQSetModelShow(modelName string, modelShow string) MSG {
return OK(nil) return OK(nil)
} }
// CQMarkMessageAsRead 标记消息已读
func (bot *CQBot) CQMarkMessageAsRead(msgID int32) MSG {
m := bot.GetMessage(msgID)
if m == nil {
return Failed(100, "MSG_NOT_FOUND", "消息不存在")
}
if _, ok := m["group"]; ok {
bot.Client.MarkGroupMessageReaded(m["group"].(int64), int64(m["message-id"].(int32)))
return OK(nil)
}
if _, ok := m["from-group"]; ok {
return Failed(100, "MSG_TYPE_ERROR", "不支持标记临时会话")
}
bot.Client.MarkPrivateMessageReaded(m["sender"].(*message.Sender).Uin, m["time"].(int64))
return OK(nil)
}
// OK 生成成功返回值 // OK 生成成功返回值
func OK(data interface{}) MSG { func OK(data interface{}) MSG {
return MSG{"data": data, "retcode": 0, "status": "ok"} return MSG{"data": data, "retcode": 0, "status": "ok"}
@ -1472,11 +1468,12 @@ func convertGroupMemberInfo(groupID int64, m *client.GroupMemberInfo) MSG {
// unknown = 0xff // unknown = 0xff
return "unknown" return "unknown"
}(), }(),
"age": 0, "age": 0,
"area": "", "area": "",
"join_time": m.JoinTime, "join_time": m.JoinTime,
"last_sent_time": m.LastSpeakTime, "last_sent_time": m.LastSpeakTime,
"level": strconv.FormatInt(int64(m.Level), 10), "shut_up_timestamp": m.ShutUpTimestamp,
"level": strconv.FormatInt(int64(m.Level), 10),
"role": func() string { "role": func() string {
switch m.Permission { switch m.Permission {
case client.Owner: case client.Owner:
@ -1497,15 +1494,17 @@ func convertGroupMemberInfo(groupID int64, m *client.GroupMemberInfo) MSG {
} }
func limitedString(str string) string { func limitedString(str string) string {
if utf8.RuneCountInString(str) <= 10 { limited := [14]rune{10: ' ', 11: '.', 12: '.', 13: '.'}
i := 0
for _, r := range str {
if i >= 10 {
break
}
limited[i] = r
i++
}
if i != 10 {
return str return str
} }
b := utils.S2B(str) return string(limited[:])
limited := make([]rune, 0, 10)
for i := 0; i < 10; i++ {
decodeRune, size := utf8.DecodeRune(b)
b = b[size:]
limited = append(limited, decodeRune)
}
return string(limited) + " ..."
} }

View File

@ -13,17 +13,20 @@ import (
"sync" "sync"
"time" "time"
"github.com/Mrs4s/go-cqhttp/global" "github.com/gabriel-vasile/mimetype"
"github.com/Mrs4s/go-cqhttp/global/config"
"github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/client" "github.com/Mrs4s/MiraiGo/client"
"github.com/Mrs4s/MiraiGo/message" "github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/MiraiGo/utils" "github.com/Mrs4s/MiraiGo/utils"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt" "github.com/syndtr/goleveldb/leveldb/opt"
"github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/global/config"
) )
var json = jsoniter.ConfigCompatibleWithStandardLibrary var json = jsoniter.ConfigCompatibleWithStandardLibrary
@ -32,7 +35,9 @@ var json = jsoniter.ConfigCompatibleWithStandardLibrary
type CQBot struct { type CQBot struct {
Client *client.QQClient Client *client.QQClient
events []func(*bytes.Buffer) lock sync.RWMutex
events []func(*Event)
db *leveldb.DB db *leveldb.DB
friendReqCache sync.Map friendReqCache sync.Map
tempSessionCache sync.Map tempSessionCache sync.Map
@ -42,9 +47,48 @@ type CQBot struct {
// MSG 消息Map // MSG 消息Map
type MSG map[string]interface{} type MSG map[string]interface{}
// Event 事件
type Event struct {
RawMsg MSG
once sync.Once
buffer *bytes.Buffer
}
func (e *Event) marshal() {
if e.buffer == nil {
e.buffer = global.NewBuffer()
}
_ = json.NewEncoder(e.buffer).Encode(e.RawMsg)
}
// JSONBytes return byes of json by lazy marshalling.
func (e *Event) JSONBytes() []byte {
e.once.Do(e.marshal)
return e.buffer.Bytes()
}
// JSONString return string of json without extra allocation
// by lazy marshalling.
func (e *Event) JSONString() string {
e.once.Do(e.marshal)
return utils.B2S(e.buffer.Bytes())
}
// ForceFragmented 是否启用强制分片 // ForceFragmented 是否启用强制分片
var ForceFragmented = false var ForceFragmented = false
// SkipMimeScan 是否跳过Mime扫描
var SkipMimeScan bool
var lawfulImageTypes = [...]string{"image/png", "image/jpeg", "image/gif", "image/bmp", "image/webp"}
var lawfulAudioTypes = [...]string{
"audio/mpeg", "audio/flac", "audio/midi", "audio/ogg",
"audio/ape", "audio/amr", "audio/wav", "audio/aiff",
"audio/mp4", "audio/aac", "audio/x-m4a",
}
// NewQQBot 初始化一个QQBot实例 // NewQQBot 初始化一个QQBot实例
func NewQQBot(cli *client.QQClient, conf *config.Config) *CQBot { func NewQQBot(cli *client.QQClient, conf *config.Config) *CQBot {
bot := &CQBot{ bot := &CQBot{
@ -82,6 +126,7 @@ func NewQQBot(cli *client.QQClient, conf *config.Config) *CQBot {
bot.Client.OnGroupMessageRecalled(bot.groupRecallEvent) bot.Client.OnGroupMessageRecalled(bot.groupRecallEvent)
bot.Client.OnGroupNotify(bot.groupNotifyEvent) bot.Client.OnGroupNotify(bot.groupNotifyEvent)
bot.Client.OnFriendNotify(bot.friendNotifyEvent) bot.Client.OnFriendNotify(bot.friendNotifyEvent)
bot.Client.OnMemberSpecialTitleUpdated(bot.memberTitleUpdatedEvent)
bot.Client.OnFriendMessageRecalled(bot.friendRecallEvent) bot.Client.OnFriendMessageRecalled(bot.friendRecallEvent)
bot.Client.OnReceivedOfflineFile(bot.offlineFileEvent) bot.Client.OnReceivedOfflineFile(bot.offlineFileEvent)
bot.Client.OnJoinGroup(bot.joinGroupEvent) bot.Client.OnJoinGroup(bot.joinGroupEvent)
@ -122,8 +167,10 @@ func NewQQBot(cli *client.QQClient, conf *config.Config) *CQBot {
} }
// OnEventPush 注册事件上报函数 // OnEventPush 注册事件上报函数
func (bot *CQBot) OnEventPush(f func(buf *bytes.Buffer)) { func (bot *CQBot) OnEventPush(f func(e *Event)) {
bot.lock.Lock()
bot.events = append(bot.events, f) bot.events = append(bot.events, f)
bot.lock.Unlock()
} }
// GetMessage 获取给定消息id对应的消息 // GetMessage 获取给定消息id对应的消息
@ -143,101 +190,96 @@ func (bot *CQBot) GetMessage(mid int32) MSG {
} }
// UploadLocalImageAsGroup 上传本地图片至群聊 // UploadLocalImageAsGroup 上传本地图片至群聊
func (bot *CQBot) UploadLocalImageAsGroup(groupCode int64, img *LocalImageElement) (*message.GroupImageElement, error) { func (bot *CQBot) UploadLocalImageAsGroup(groupCode int64, img *LocalImageElement) (i *message.GroupImageElement, err error) {
if img.Stream != nil { if img.File != "" {
return bot.Client.UploadGroupImage(groupCode, img.Stream) f, err := os.Open(img.File)
if err != nil {
return nil, errors.Wrap(err, "open image error")
}
defer func() { _ = f.Close() }()
img.Stream = f
} }
return bot.Client.UploadGroupImageByFile(groupCode, img.File) if lawful, mime := IsLawfulImage(img.Stream); !lawful {
return nil, errors.New("image type error: " + mime)
}
i, err = bot.Client.UploadGroupImage(groupCode, img.Stream)
if i != nil {
i.Flash = img.Flash
i.EffectID = img.EffectID
}
return
} }
// UploadLocalVideo 上传本地短视频至群聊 // UploadLocalVideo 上传本地短视频至群聊
func (bot *CQBot) UploadLocalVideo(target int64, v *LocalVideoElement) (*message.ShortVideoElement, error) { func (bot *CQBot) UploadLocalVideo(target int64, v *LocalVideoElement) (*message.ShortVideoElement, error) {
if v.File != "" { video, err := os.Open(v.File)
video, err := os.Open(v.File)
if err != nil {
return nil, err
}
defer video.Close()
hash, _ := utils.ComputeMd5AndLength(io.MultiReader(video, v.thumb))
cacheFile := path.Join(global.CachePath, hex.EncodeToString(hash)+".cache")
_, _ = video.Seek(0, io.SeekStart)
_, _ = v.thumb.Seek(0, io.SeekStart)
return bot.Client.UploadGroupShortVideo(target, video, v.thumb, cacheFile)
}
return &v.ShortVideoElement, nil
}
// UploadLocalImageAsPrivate 上传本地图片至私聊
func (bot *CQBot) UploadLocalImageAsPrivate(userID int64, img *LocalImageElement) (*message.FriendImageElement, error) {
if img.Stream != nil {
return bot.Client.UploadPrivateImage(userID, img.Stream)
}
// need update.
f, err := os.Open(img.File)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer f.Close() defer video.Close()
return bot.Client.UploadPrivateImage(userID, f) hash, _ := utils.ComputeMd5AndLength(io.MultiReader(video, v.thumb))
cacheFile := path.Join(global.CachePath, hex.EncodeToString(hash)+".cache")
_, _ = video.Seek(0, io.SeekStart)
_, _ = v.thumb.Seek(0, io.SeekStart)
return bot.Client.UploadGroupShortVideo(target, video, v.thumb, cacheFile)
}
// UploadLocalImageAsPrivate 上传本地图片至私聊
func (bot *CQBot) UploadLocalImageAsPrivate(userID int64, img *LocalImageElement) (i *message.FriendImageElement, err error) {
if img.File != "" {
f, err := os.Open(img.File)
if err != nil {
return nil, errors.Wrap(err, "open image error")
}
defer func() { _ = f.Close() }()
img.Stream = f
}
if lawful, mime := IsLawfulImage(img.Stream); !lawful {
return nil, errors.New("image type error: " + mime)
}
i, err = bot.Client.UploadPrivateImage(userID, img.Stream)
if i != nil {
i.Flash = img.Flash
}
return
} }
// SendGroupMessage 发送群消息 // SendGroupMessage 发送群消息
func (bot *CQBot) SendGroupMessage(groupID int64, m *message.SendingMessage) int32 { func (bot *CQBot) SendGroupMessage(groupID int64, m *message.SendingMessage) int32 {
newElem := make([]message.IMessageElement, 0, len(m.Elements)) newElem := make([]message.IMessageElement, 0, len(m.Elements))
group := bot.Client.FindGroup(groupID) group := bot.Client.FindGroup(groupID)
for _, elem := range m.Elements { for _, e := range m.Elements {
if i, ok := elem.(*LocalImageElement); ok { switch i := e.(type) {
gm, err := bot.UploadLocalImageAsGroup(groupID, i) case *LocalImageElement, *message.VoiceElement, *LocalVideoElement:
i, err := bot.uploadMedia(i, groupID, true)
if err != nil { if err != nil {
log.Warnf("警告: 群 %v 消息图片上传失败: %v", groupID, err) log.Warnf("警告: 群 %d 消息%s上传失败: %v", groupID, e.Type().String(), err)
continue continue
} }
newElem = append(newElem, gm) e = i
continue case *PokeElement:
} if group != nil {
if i, ok := elem.(*message.VoiceElement); ok {
gv, err := bot.Client.UploadGroupPtt(groupID, bytes.NewReader(i.Data))
if err != nil {
log.Warnf("警告: 群 %v 消息语音上传失败: %v", groupID, err)
continue
}
newElem = append(newElem, gv)
continue
}
if i, ok := elem.(*LocalVideoElement); ok {
gv, err := bot.UploadLocalVideo(groupID, i)
if err != nil {
log.Warnf("警告: 群 %v 消息短视频上传失败: %v", groupID, err)
continue
}
newElem = append(newElem, gv)
continue
}
if i, ok := elem.(*PokeElement); ok {
if group := bot.Client.FindGroup(groupID); group != nil {
if mem := group.FindMember(i.Target); mem != nil { if mem := group.FindMember(i.Target); mem != nil {
mem.Poke() mem.Poke()
return 0
} }
} }
} return 0
if i, ok := elem.(*GiftElement); ok { case *GiftElement:
bot.Client.SendGroupGift(uint64(groupID), uint64(i.Target), i.GiftID) bot.Client.SendGroupGift(uint64(groupID), uint64(i.Target), i.GiftID)
return 0 return 0
} case *message.MusicShareElement:
if i, ok := elem.(*message.MusicShareElement); ok {
ret, err := bot.Client.SendGroupMusicShare(groupID, i) ret, err := bot.Client.SendGroupMusicShare(groupID, i)
if err != nil { if err != nil {
log.Warnf("警告: 群 %v 富文本消息发送失败: %v", groupID, err) log.Warnf("警告: 群 %v 富文本消息发送失败: %v", groupID, err)
return -1 return -1
} }
return bot.InsertGroupMessage(ret) return bot.InsertGroupMessage(ret)
case *message.AtElement:
if i.Target == 0 && group.SelfPermission() == client.Member {
e = message.NewText("@全体成员")
}
} }
if i, ok := elem.(*message.AtElement); ok && i.Target == 0 && group.SelfPermission() == client.Member { newElem = append(newElem, e)
newElem = append(newElem, message.NewText("@全体成员"))
continue
}
newElem = append(newElem, elem)
} }
if len(newElem) == 0 { if len(newElem) == 0 {
log.Warnf("群消息发送失败: 消息为空.") log.Warnf("群消息发送失败: 消息为空.")
@ -256,43 +298,23 @@ func (bot *CQBot) SendGroupMessage(groupID int64, m *message.SendingMessage) int
// SendPrivateMessage 发送私聊消息 // SendPrivateMessage 发送私聊消息
func (bot *CQBot) SendPrivateMessage(target int64, groupID int64, m *message.SendingMessage) int32 { func (bot *CQBot) SendPrivateMessage(target int64, groupID int64, m *message.SendingMessage) int32 {
newElem := make([]message.IMessageElement, 0, len(m.Elements)) newElem := make([]message.IMessageElement, 0, len(m.Elements))
for _, elem := range m.Elements { for _, e := range m.Elements {
if i, ok := elem.(*LocalImageElement); ok { switch i := e.(type) {
fm, err := bot.UploadLocalImageAsPrivate(target, i) case *LocalImageElement, *message.VoiceElement, *LocalVideoElement:
i, err := bot.uploadMedia(i, groupID, false)
if err != nil { if err != nil {
log.Warnf("警告: 私聊 %v 消息图片上传失败.", target) log.Warnf("警告: 私聊 %d 消息%s上传失败: %v", target, e.Type().String(), err)
continue continue
} }
newElem = append(newElem, fm) e = i
continue case *PokeElement:
}
if i, ok := elem.(*PokeElement); ok {
bot.Client.SendFriendPoke(i.Target) bot.Client.SendFriendPoke(i.Target)
return 0 return 0
} case *message.MusicShareElement:
if i, ok := elem.(*message.VoiceElement); ok { bot.Client.SendFriendMusicShare(groupID, i)
fv, err := bot.Client.UploadPrivatePtt(target, bytes.NewReader(i.Data)) // todo: io.ReadSeeker
if err != nil {
log.Warnf("警告: 私聊 %v 消息语音上传失败: %v", target, err)
continue
}
newElem = append(newElem, fv)
continue
}
if i, ok := elem.(*LocalVideoElement); ok {
gv, err := bot.UploadLocalVideo(target, i)
if err != nil {
log.Warnf("警告: 私聊 %v 消息短视频上传失败: %v", target, err)
continue
}
newElem = append(newElem, gv)
continue
}
if i, ok := elem.(*message.MusicShareElement); ok {
bot.Client.SendFriendMusicShare(target, i)
return 0 return 0
} }
newElem = append(newElem, elem) newElem = append(newElem, e)
} }
if len(newElem) == 0 { if len(newElem) == 0 {
log.Warnf("好友消息发送失败: 消息为空.") log.Warnf("好友消息发送失败: 消息为空.")
@ -404,7 +426,7 @@ func (bot *CQBot) InsertTempMessage(target int64, m *message.TempMessage) int32
val := MSG{ val := MSG{
"message-id": m.Id, "message-id": m.Id,
// FIXME(InsertTempMessage) InternalId missing // FIXME(InsertTempMessage) InternalId missing
"group": m.GroupCode, "from-group": m.GroupCode,
"group-name": m.GroupName, "group-name": m.GroupName,
"target": target, "target": target,
"sender": m.Sender, "sender": m.Sender,
@ -440,12 +462,14 @@ func (bot *CQBot) Release() {
} }
func (bot *CQBot) dispatchEventMessage(m MSG) { func (bot *CQBot) dispatchEventMessage(m MSG) {
buf := global.NewBuffer() bot.lock.RLock()
defer bot.lock.RUnlock()
event := &Event{RawMsg: m}
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(len(bot.events)) wg.Add(len(bot.events))
_ = json.NewEncoder(buf).Encode(m)
for _, f := range bot.events { for _, f := range bot.events {
go func(fn func(*bytes.Buffer)) { go func(fn func(*Event)) {
defer func() { defer func() {
wg.Done() wg.Done()
if pan := recover(); pan != nil { if pan := recover(); pan != nil {
@ -454,7 +478,7 @@ func (bot *CQBot) dispatchEventMessage(m MSG) {
}() }()
start := time.Now() start := time.Now()
fn(buf) fn(event)
end := time.Now() end := time.Now()
if end.Sub(start) > time.Second*5 { if end.Sub(start) > time.Second*5 {
log.Debugf("警告: 事件处理耗时超过 5 秒 (%v), 请检查应用是否有堵塞.", end.Sub(start)) log.Debugf("警告: 事件处理耗时超过 5 秒 (%v), 请检查应用是否有堵塞.", end.Sub(start))
@ -462,7 +486,7 @@ func (bot *CQBot) dispatchEventMessage(m MSG) {
}(f) }(f)
} }
wg.Wait() wg.Wait()
global.PutBuffer(buf) global.PutBuffer(event.buffer)
} }
func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) MSG { func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) MSG {
@ -490,7 +514,7 @@ func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) MSG {
"user_id": m.Sender.Uin, "user_id": m.Sender.Uin,
}, },
"sub_type": "normal", "sub_type": "normal",
"time": time.Now().Unix(), "time": m.Time,
"user_id": m.Sender.Uin, "user_id": m.Sender.Uin,
} }
if m.Sender.IsAnonymous() { if m.Sender.IsAnonymous() {
@ -518,18 +542,16 @@ func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) MSG {
} }
} }
ms := gm["sender"].(MSG) ms := gm["sender"].(MSG)
ms["role"] = func() string { switch mem.Permission {
switch mem.Permission { case client.Owner:
case client.Owner: ms["role"] = "owner"
return "owner" case client.Administrator:
case client.Administrator: ms["role"] = "admin"
return "admin" case client.Member:
case client.Member: ms["role"] = "member"
return "member" default:
default: ms["role"] = "member"
return "member" }
}
}()
ms["nickname"] = mem.Nickname ms["nickname"] = mem.Nickname
ms["card"] = mem.CardName ms["card"] = mem.CardName
ms["title"] = mem.SpecialTitle ms["title"] = mem.SpecialTitle
@ -548,8 +570,42 @@ func formatMemberName(mem *client.GroupMemberInfo) string {
return fmt.Sprintf("%s(%d)", mem.DisplayName(), mem.Uin) return fmt.Sprintf("%s(%d)", mem.DisplayName(), mem.Uin)
} }
// ToJSON 生成JSON字符串 func (bot *CQBot) uploadMedia(raw message.IMessageElement, target int64, group bool) (message.IMessageElement, error) {
func (m MSG) ToJSON() string { switch m := raw.(type) {
b, _ := json.Marshal(m) case *LocalImageElement:
return string(b) if group {
return bot.UploadLocalImageAsGroup(target, m)
}
return bot.UploadLocalImageAsPrivate(target, m)
case *message.VoiceElement:
if group {
return bot.Client.UploadGroupPtt(target, bytes.NewReader(m.Data))
}
return bot.Client.UploadPrivatePtt(target, bytes.NewReader(m.Data))
case *LocalVideoElement:
return bot.UploadLocalVideo(target, m)
}
return nil, errors.New("unsupported message element type")
}
// IsLawfulImage 判断给定流是否为合法图片
// 返回 是否合法, 实际Mime
// 判断后会自动将 Stream Seek 至 0
func IsLawfulImage(r io.ReadSeeker) (bool, string) {
if SkipMimeScan {
return true, ""
}
_, _ = r.Seek(0, io.SeekStart)
defer func() { _, _ = r.Seek(0, io.SeekStart) }()
t, err := mimetype.DetectReader(r)
if err != nil {
log.Debugf("扫描 Mime 时出现问题: %v", err)
return false, ""
}
for _, lt := range lawfulImageTypes {
if t.Is(lt) {
return true, t.String()
}
}
return false, t.String()
} }

View File

@ -9,7 +9,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"math/rand" "math/rand"
"net/url" "net/url"
"os" "os"
@ -19,6 +18,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/gabriel-vasile/mimetype"
"github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/binary"
"github.com/Mrs4s/MiraiGo/message" "github.com/Mrs4s/MiraiGo/message"
"github.com/Mrs4s/MiraiGo/utils" "github.com/Mrs4s/MiraiGo/utils"
@ -65,9 +66,11 @@ type GiftElement struct {
// LocalImageElement 本地图片 // LocalImageElement 本地图片
type LocalImageElement struct { type LocalImageElement struct {
message.ImageElement
Stream io.ReadSeeker Stream io.ReadSeeker
File string File string
Flash bool
EffectID int32
} }
// LocalVoiceElement 本地语音 // LocalVoiceElement 本地语音
@ -78,17 +81,26 @@ type LocalVoiceElement struct {
// LocalVideoElement 本地视频 // LocalVideoElement 本地视频
type LocalVideoElement struct { type LocalVideoElement struct {
message.ShortVideoElement
File string File string
thumb io.ReadSeeker thumb io.ReadSeeker
} }
// Type implements the message.IMessageElement.
func (e *LocalImageElement) Type() message.ElementType {
return message.Image
}
// Type 获取元素类型ID // Type 获取元素类型ID
func (e *GiftElement) Type() message.ElementType { func (e *GiftElement) Type() message.ElementType {
// Make message.IMessageElement Happy // Make message.IMessageElement Happy
return message.At return message.At
} }
// Type impl message.IMessageElement
func (e *LocalVideoElement) Type() message.ElementType {
return message.Video
}
// GiftID 礼物ID数组 // GiftID 礼物ID数组
var GiftID = [...]message.GroupGift{ var GiftID = [...]message.GroupGift{
message.SweetWink, message.SweetWink,
@ -131,7 +143,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
r = append(r, MSG{ r = append(r, MSG{
"type": "reply", "type": "reply",
"data": map[string]string{ "data": map[string]string{
"id": fmt.Sprint(toGlobalID(rid, replyElem.ReplySeq)), "id": strconv.FormatInt(int64(toGlobalID(rid, replyElem.ReplySeq)), 10),
"seq": strconv.FormatInt(int64(replyElem.ReplySeq), 10), "seq": strconv.FormatInt(int64(replyElem.ReplySeq), 10),
"qq": strconv.FormatInt(replyElem.Sender, 10), "qq": strconv.FormatInt(replyElem.Sender, 10),
"time": strconv.FormatInt(int64(replyElem.Time), 10), "time": strconv.FormatInt(int64(replyElem.Time), 10),
@ -141,7 +153,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
} else { } else {
r = append(r, MSG{ r = append(r, MSG{
"type": "reply", "type": "reply",
"data": map[string]string{"id": fmt.Sprint(toGlobalID(rid, replyElem.ReplySeq))}, "data": map[string]string{"id": strconv.FormatInt(int64(toGlobalID(rid, replyElem.ReplySeq)), 10)},
}) })
} }
} }
@ -149,7 +161,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
var m MSG var m MSG
switch o := elem.(type) { switch o := elem.(type) {
case *message.ReplyElement: case *message.ReplyElement:
if RemoveReplyAt && len(e) > i+1 { if RemoveReplyAt && i+1 < len(e) && e[i+1].Type() == message.At {
elem, ok := e[i+1].(*message.AtElement) elem, ok := e[i+1].(*message.AtElement)
if ok && elem.Target == o.Sender { if ok && elem.Target == o.Sender {
e[i+1] = nil e[i+1] = nil
@ -174,7 +186,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
} else { } else {
m = MSG{ m = MSG{
"type": "at", "type": "at",
"data": map[string]string{"qq": fmt.Sprint(o.Target)}, "data": map[string]string{"qq": strconv.FormatInt(o.Target, 10)},
} }
} }
case *message.RedBagElement: case *message.RedBagElement:
@ -190,7 +202,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
case *message.FaceElement: case *message.FaceElement:
m = MSG{ m = MSG{
"type": "face", "type": "face",
"data": map[string]string{"id": fmt.Sprint(o.Index)}, "data": map[string]string{"id": strconv.FormatInt(int64(o.Index), 10)},
} }
case *message.VoiceElement: case *message.VoiceElement:
m = MSG{ m = MSG{
@ -202,41 +214,38 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
"type": "video", "type": "video",
"data": map[string]string{"file": o.Name, "url": o.Url}, "data": map[string]string{"file": o.Name, "url": o.Url},
} }
case *message.ImageElement:
m = MSG{
"type": "image",
"data": map[string]string{"file": o.Filename, "url": o.Url},
}
case *message.GroupImageElement: case *message.GroupImageElement:
data := map[string]string{"file": hex.EncodeToString(o.Md5) + ".image", "url": o.Url}
switch {
case o.Flash:
data["type"] = "flash"
case o.EffectID != 0:
data["type"] = "show"
data["id"] = strconv.FormatInt(int64(o.EffectID), 10)
}
m = MSG{ m = MSG{
"type": "image", "type": "image",
"data": map[string]string{"file": hex.EncodeToString(o.Md5) + ".image", "url": o.Url}, "data": data,
} }
case *message.FriendImageElement: case *message.FriendImageElement:
data := map[string]string{"file": hex.EncodeToString(o.Md5) + ".image", "url": o.Url}
if o.Flash {
data["type"] = "flash"
}
m = MSG{ m = MSG{
"type": "image", "type": "image",
"data": map[string]string{"file": hex.EncodeToString(o.Md5) + ".image", "url": o.Url}, "data": data,
} }
case *message.GroupFlashImgElement:
return []MSG{{
"type": "image",
"data": map[string]string{"file": o.Filename, "type": "flash"},
}}
case *message.FriendFlashImgElement:
return []MSG{{
"type": "image",
"data": map[string]string{"file": o.Filename, "type": "flash"},
}}
case *message.ServiceElement: case *message.ServiceElement:
if isOk := strings.Contains(o.Content, "<?xml"); isOk { if isOk := strings.Contains(o.Content, "<?xml"); isOk {
m = MSG{ m = MSG{
"type": "xml", "type": "xml",
"data": map[string]string{"data": o.Content, "resid": fmt.Sprintf("%d", o.Id)}, "data": map[string]string{"data": o.Content, "resid": strconv.FormatInt(int64(o.Id), 10)},
} }
} else { } else {
m = MSG{ m = MSG{
"type": "json", "type": "json",
"data": map[string]string{"data": o.Content, "resid": fmt.Sprintf("%d", o.Id)}, "data": map[string]string{"data": o.Content, "resid": strconv.FormatInt(int64(o.Id), 10)},
} }
} }
default: default:
@ -316,28 +325,28 @@ func ToStringMessage(e []message.IMessageElement, groupID int64, isRaw ...bool)
} else { } else {
write(`[CQ:video,file=%s,url=%s]`, o.Name, CQCodeEscapeValue(o.Url)) write(`[CQ:video,file=%s,url=%s]`, o.Name, CQCodeEscapeValue(o.Url))
} }
case *message.ImageElement:
if ur {
write(`[CQ:image,file=%s]`, o.Filename)
} else {
write(`[CQ:image,file=%s,url=%s]`, o.Filename, CQCodeEscapeValue(o.Url))
}
case *message.GroupImageElement: case *message.GroupImageElement:
var arg string
if o.Flash {
arg = ",type=flash"
} else if o.EffectID != 0 {
arg = ",type=show,id=" + strconv.FormatInt(int64(o.EffectID), 10)
}
if ur { if ur {
write("[CQ:image,file=%s]", hex.EncodeToString(o.Md5)+".image") write("[CQ:image,file=%s%s]", hex.EncodeToString(o.Md5)+".image", arg)
} else { } else {
write("[CQ:image,file=%s,url=%s]", hex.EncodeToString(o.Md5)+".image", CQCodeEscapeValue(o.Url)) write("[CQ:image,file=%s,url=%s%s]", hex.EncodeToString(o.Md5)+".image", CQCodeEscapeValue(o.Url), arg)
} }
case *message.FriendImageElement: case *message.FriendImageElement:
if ur { var arg string
write("[CQ:image,file=%s]", hex.EncodeToString(o.Md5)+".image") if o.Flash {
} else { arg = ",type=flash"
write("[CQ:image,file=%s,url=%s]", hex.EncodeToString(o.Md5)+".image", CQCodeEscapeValue(o.Url)) }
if ur {
write("[CQ:image,file=%s%s]", hex.EncodeToString(o.Md5)+".image", arg)
} else {
write("[CQ:image,file=%s,url=%s%s]", hex.EncodeToString(o.Md5)+".image", CQCodeEscapeValue(o.Url), arg)
} }
case *message.GroupFlashImgElement:
return fmt.Sprintf("[CQ:image,type=flash,file=%s]", o.Filename)
case *message.FriendFlashImgElement:
return fmt.Sprintf("[CQ:image,type=flash,file=%s]", o.Filename)
case *message.ServiceElement: case *message.ServiceElement:
if isOk := strings.Contains(o.Content, "<?xml"); isOk { if isOk := strings.Contains(o.Content, "<?xml"); isOk {
write(`[CQ:xml,data=%s,resid=%d]`, CQCodeEscapeValue(o.Content), o.Id) write(`[CQ:xml,data=%s,resid=%d]`, CQCodeEscapeValue(o.Content), o.Id)
@ -544,9 +553,9 @@ func (bot *CQBot) ConvertObjectMessage(m gjson.Result, isGroup bool) (r []messag
case customText != "": case customText != "":
var elem *message.ReplyElement var elem *message.ReplyElement
var org MSG var org MSG
sender, senderErr := strconv.ParseInt(e.Get("data.qq").String(), 10, 64) sender, senderErr := strconv.ParseInt(e.Get("data.[user_id,qq]").String(), 10, 64)
if senderErr != nil && err != nil { if senderErr != nil && err != nil {
log.Warnf("警告: 自定义 Reply 元素中必须包含 Uin 或 id") log.Warnf("警告: 自定义 Reply 元素中必须包含 user_id 或 id")
break break
} }
msgTime, timeErr := strconv.ParseInt(e.Get("data.time").String(), 10, 64) msgTime, timeErr := strconv.ParseInt(e.Get("data.time").String(), 10, 64)
@ -668,38 +677,29 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
return nil, err return nil, err
} }
tp := d["type"] tp := d["type"]
if tp != "show" && tp != "flash" { flash, id := false, int64(0)
return img, nil
}
if i, ok := img.(*LocalImageElement); ok { // 秀图,闪照什么的就直接传了吧
if isGroup {
img, err = bot.UploadLocalImageAsGroup(1, i)
} else {
img, err = bot.UploadLocalImageAsPrivate(1, i)
}
if err != nil {
return nil, err
}
}
switch tp { switch tp {
case "flash": case "flash":
if i, ok := img.(*message.GroupImageElement); ok { flash = true
return &message.GroupFlashPicElement{GroupImageElement: *i}, nil
}
if i, ok := img.(*message.FriendImageElement); ok {
return &message.FriendFlashPicElement{FriendImageElement: *i}, nil
}
case "show": case "show":
id, _ := strconv.ParseInt(d["id"], 10, 64) id, _ = strconv.ParseInt(d["id"], 10, 64)
if id < 40000 || id >= 40006 { if id < 40000 || id >= 40006 {
id = 40000 id = 40000
} }
if i, ok := img.(*message.GroupImageElement); ok { default:
return &message.GroupShowPicElement{GroupImageElement: *i, EffectId: int32(id)}, nil return img, err
}
return img, nil
} }
switch img := img.(type) {
case *LocalImageElement:
img.Flash = flash
img.EffectID = int32(id)
case *message.GroupImageElement:
img.Flash = flash
img.EffectID = int32(id)
case *message.FriendImageElement:
img.Flash = flash
}
return img, err
case "poke": case "poke":
t, _ := strconv.ParseInt(d["qq"], 10, 64) t, _ := strconv.ParseInt(d["qq"], 10, 64)
return &PokeElement{Target: t}, nil return &PokeElement{Target: t}, nil
@ -734,6 +734,19 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
if err != nil { if err != nil {
return nil, err return nil, err
} }
if !SkipMimeScan && !global.IsAMRorSILK(data) {
mt := mimetype.Detect(data)
lawful := false
for _, lt := range lawfulAudioTypes {
if mt.Is(lt) {
lawful = true
break
}
}
if !lawful {
return nil, errors.New("audio type error: " + mt.String())
}
}
if !global.IsAMRorSILK(data) { if !global.IsAMRorSILK(data) {
data, err = global.EncoderSilk(data) data, err = global.EncoderSilk(data)
if err != nil { if err != nil {
@ -835,7 +848,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
Summary: d["content"], Summary: d["content"],
Url: d["url"], Url: d["url"],
PictureUrl: d["image"], PictureUrl: d["image"],
MusicUrl: d["purl"], MusicUrl: d["audio"],
}, nil }, nil
} }
xml := fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="2" templateID="1" action="web" brief="[分享] %s" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="2"><audio cover="%s" src="%s"/><title>%s</title><summary>%s</summary></item><source name="音乐" icon="https://i.gtimg.cn/open/app_icon/01/07/98/56/1101079856_100_m.png" url="http://web.p.qq.com/qqmpmobile/aio/app.html?id=1101079856" action="app" a_actionData="com.tencent.qqmusic" i_actionData="tencent1101079856://" appid="1101079856" /></msg>`, xml := fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="2" templateID="1" action="web" brief="[分享] %s" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="2"><audio cover="%s" src="%s"/><title>%s</title><summary>%s</summary></item><source name="音乐" icon="https://i.gtimg.cn/open/app_icon/01/07/98/56/1101079856_100_m.png" url="http://web.p.qq.com/qqmpmobile/aio/app.html?id=1101079856" action="app" a_actionData="com.tencent.qqmusic" i_actionData="tencent1101079856://" appid="1101079856" /></msg>`,
@ -893,7 +906,10 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
if err != nil { if err != nil {
return nil, err return nil, err
} }
v := file.(*LocalVideoElement) v, ok := file.(*LocalVideoElement)
if !ok {
return file, nil
}
if v.File == "" { if v.File == "" {
return v, nil return v, nil
} }
@ -902,7 +918,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
data, _ = global.FindFile(cover, cache, global.ImagePath) data, _ = global.FindFile(cover, cache, global.ImagePath)
} else { } else {
_ = global.ExtractCover(v.File, v.File+".jpg") _ = global.ExtractCover(v.File, v.File+".jpg")
data, _ = ioutil.ReadFile(v.File + ".jpg") data, _ = os.ReadFile(v.File + ".jpg")
} }
v.thumb = bytes.NewReader(data) v.thumb = bytes.NewReader(data)
video, _ := os.Open(v.File) video, _ := os.Open(v.File)
@ -934,13 +950,13 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
default: default:
return nil, errors.New("unsupported cq code: " + t) return nil, errors.New("unsupported cq code: " + t)
} }
return nil, nil
} }
// XMLEscape 将字符串c转义为XML字符串 // XMLEscape 将字符串c转义为XML字符串
func XMLEscape(c string) string { func XMLEscape(c string) string {
buf := new(bytes.Buffer) buf := global.NewBuffer()
_ = xml2.EscapeText(buf, []byte(c)) defer global.PutBuffer(buf)
_ = xml2.EscapeText(buf, utils.S2B(c))
return buf.String() return buf.String()
} }
@ -953,12 +969,39 @@ func XMLEscape(c string) string {
] -> &#93; ] -> &#93;
*/ */
func CQCodeEscapeText(raw string) string { func CQCodeEscapeText(s string) string {
ret := raw count := strings.Count(s, "&")
ret = strings.ReplaceAll(ret, "&", "&amp;") count += strings.Count(s, "[")
ret = strings.ReplaceAll(ret, "[", "&#91;") count += strings.Count(s, "]")
ret = strings.ReplaceAll(ret, "]", "&#93;") if count == 0 {
return ret return s
}
// Apply replacements to buffer.
var b strings.Builder
b.Grow(len(s) + count*4)
start := 0
for i := 0; i < count; i++ {
j := start
for index, r := range s[start:] {
if r == '&' || r == '[' || r == ']' {
j += index
break
}
}
b.WriteString(s[start:j])
switch s[j] {
case '&':
b.WriteString("&amp;")
case '[':
b.WriteString("&#91;")
case ']':
b.WriteString("&#93;")
}
start = j + 1
}
b.WriteString(s[start:])
return b.String()
} }
/*CQCodeEscapeValue 将字符串value中部分字符转义 /*CQCodeEscapeValue 将字符串value中部分字符转义
@ -1086,16 +1129,16 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
return nil, errors.New("invalid video") return nil, errors.New("invalid video")
} }
if path.Ext(rawPath) == ".video" { if path.Ext(rawPath) == ".video" {
b, _ := ioutil.ReadFile(rawPath) b, _ := os.ReadFile(rawPath)
r := binary.NewReader(b) r := binary.NewReader(b)
return &LocalVideoElement{ShortVideoElement: message.ShortVideoElement{ // todo 检查缓存是否有效 return &message.ShortVideoElement{ // todo 检查缓存是否有效
Md5: r.ReadBytes(16), Md5: r.ReadBytes(16),
ThumbMd5: r.ReadBytes(16), ThumbMd5: r.ReadBytes(16),
Size: r.ReadInt32(), Size: r.ReadInt32(),
ThumbSize: r.ReadInt32(), ThumbSize: r.ReadInt32(),
Name: r.ReadString(), Name: r.ReadString(),
Uuid: r.ReadAvailable(), Uuid: r.ReadAvailable(),
}}, nil }, nil
} }
return &LocalVideoElement{File: rawPath}, nil return &LocalVideoElement{File: rawPath}, nil
} }
@ -1104,72 +1147,50 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
exist = true exist = true
rawPath = path.Join(global.ImagePathOld, f) rawPath = path.Join(global.ImagePathOld, f)
} }
if !exist && global.PathExists(rawPath+".cqimg") { if !exist {
exist = true if d["url"] != "" {
rawPath += ".cqimg" return bot.makeImageOrVideoElem(map[string]string{"file": d["url"]}, false, group)
}
return nil, errors.New("invalid image")
} }
if !exist && d["url"] != "" { if path.Ext(rawPath) != ".image" {
return bot.makeImageOrVideoElem(map[string]string{"file": d["url"]}, false, group) return &LocalImageElement{File: rawPath}, nil
} }
if exist { b, err := os.ReadFile(rawPath)
if path.Ext(rawPath) != ".image" && path.Ext(rawPath) != ".cqimg" { if err != nil {
return &LocalImageElement{File: rawPath}, nil return nil, err
}
b, err := os.ReadFile(rawPath)
if err != nil {
return nil, err
}
if len(b) < 20 {
return nil, errors.New("invalid local file")
}
var (
size int32
hash []byte
imageURL string
)
if path.Ext(rawPath) == ".cqimg" {
for _, line := range strings.Split(global.ReadAllText(rawPath), "\n") {
kv := strings.SplitN(line, "=", 2)
switch kv[0] {
case "md5":
hash, _ = hex.DecodeString(strings.ReplaceAll(kv[1], "\r", ""))
case "size":
t, _ := strconv.Atoi(strings.ReplaceAll(kv[1], "\r", ""))
size = int32(t)
}
}
} else {
r := binary.NewReader(b)
hash = r.ReadBytes(16)
size = r.ReadInt32()
r.ReadString()
imageURL = r.ReadString()
}
if size == 0 {
if imageURL != "" {
return bot.makeImageOrVideoElem(map[string]string{"file": imageURL}, false, group)
}
return nil, errors.New("img size is 0")
}
if len(hash) != 16 {
return nil, errors.New("invalid hash")
}
var rsp message.IMessageElement
if group {
rsp, err = bot.Client.QueryGroupImage(int64(rand.Uint32()), hash, size)
goto ok
}
rsp, err = bot.Client.QueryFriendImage(int64(rand.Uint32()), hash, size)
ok:
if err != nil {
if imageURL != "" {
return bot.makeImageOrVideoElem(map[string]string{"file": imageURL}, false, group)
}
return nil, err
}
return rsp, nil
} }
return nil, errors.New("invalid image") if len(b) < 20 {
return nil, errors.New("invalid local file")
}
r := binary.NewReader(b)
hash := r.ReadBytes(16)
size := r.ReadInt32()
r.ReadString()
imageURL := r.ReadString()
if size == 0 {
if imageURL != "" {
return bot.makeImageOrVideoElem(map[string]string{"file": imageURL}, false, group)
}
return nil, errors.New("img size is 0")
}
if len(hash) != 16 {
return nil, errors.New("invalid hash")
}
var rsp message.IMessageElement
if group {
rsp, err = bot.Client.QueryGroupImage(int64(rand.Uint32()), hash, size)
goto ok
}
rsp, err = bot.Client.QueryFriendImage(int64(rand.Uint32()), hash, size)
ok:
if err != nil {
if imageURL != "" {
return bot.makeImageOrVideoElem(map[string]string{"file": imageURL}, false, group)
}
return nil, err
}
return rsp, nil
} }
// makeShowPic 一种xml 方式发送的群消息图片 // makeShowPic 一种xml 方式发送的群消息图片
@ -1179,39 +1200,26 @@ func (bot *CQBot) makeShowPic(elem message.IMessageElement, source string, brief
if brief == "" { if brief == "" {
brief = "&#91;分享&#93;我看到一张很赞的图片,分享给你,快来看!" brief = "&#91;分享&#93;我看到一张很赞的图片,分享给你,快来看!"
} }
if i, ok := elem.(*LocalImageElement); ok { if _, ok := elem.(*LocalImageElement); ok {
if !group { r := rand.Uint32()
gm, err := bot.UploadLocalImageAsPrivate(1, i) e, err := bot.uploadMedia(elem, int64(r), group)
if err != nil { if err != nil {
log.Warnf("警告: 好友消息 %v 消息图片上传失败: %v", 1, err) log.Warnf("警告: 图片上传失败: %v", err)
return nil, err return nil, err
}
suf = gm
xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="%s" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, brief, "", gm.Md5, gm.Md5, len(i.Data), "", minWidth, minHeight, maxWidth, maxHeight, source, icon)
} else {
gm, err := bot.UploadLocalImageAsGroup(1, i)
if err != nil {
log.Warnf("警告: 群 %v 消息图片上传失败: %v", 1, err)
return nil, err
}
suf = gm
xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="%s" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, brief, "", gm.Md5, gm.Md5, len(i.Data), "", minWidth, minHeight, maxWidth, maxHeight, source, icon)
} }
elem = e
} }
switch i := elem.(type) {
if i, ok := elem.(*message.GroupImageElement); ok { case *message.GroupImageElement:
xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="%s" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, brief, "", i.Md5, i.Md5, 0, "", minWidth, minHeight, maxWidth, maxHeight, source, icon)
suf = i
case *message.FriendImageElement:
xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="%s" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, brief, "", i.Md5, i.Md5, 0, "", minWidth, minHeight, maxWidth, maxHeight, source, icon) xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="%s" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, brief, "", i.Md5, i.Md5, 0, "", minWidth, minHeight, maxWidth, maxHeight, source, icon)
suf = i suf = i
} }
if i, ok := elem.(*message.FriendImageElement); ok { if xml == "" {
xml = fmt.Sprintf(`<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="5" templateID="12345" action="" brief="%s" sourceMsgId="0" url="%s" flag="0" adverSign="0" multiMsgFlag="0"><item layout="0" advertiser_id="0" aid="0"><image uuid="%x" md5="%x" GroupFiledid="0" filesize="%d" local_path="%s" minWidth="%d" minHeight="%d" maxWidth="%d" maxHeight="%d" /></item><source name="%s" icon="%s" action="" appid="-1" /></msg>`, brief, "", i.Md5, i.Md5, 0, "", minWidth, minHeight, maxWidth, maxHeight, source, icon) return nil, errors.New("生成xml图片消息失败")
suf = i
} }
if xml != "" { ret := []message.IMessageElement{suf, message.NewRichXml(xml, 5)}
// log.Warn(xml) return ret, nil
ret := []message.IMessageElement{suf}
ret = append(ret, message.NewRichXml(xml, 5))
return ret, nil
}
return nil, errors.New("生成xml图片消息失败")
} }

View File

@ -2,8 +2,11 @@ package coolq
import ( import (
"fmt" "fmt"
"strings"
"testing" "testing"
"github.com/Mrs4s/MiraiGo/utils"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
@ -30,3 +33,32 @@ func BenchmarkCQBot_ConvertObjectMessage(b *testing.B) {
bot.ConvertObjectMessage(benchArray, false) bot.ConvertObjectMessage(benchArray, false)
} }
} }
const bText = `123456789[]&987654321[]&987654321[]&987654321[]&987654321[]&987654321[]&`
func BenchmarkCQCodeEscapeText(b *testing.B) {
for i := 0; i < b.N; i++ {
ret := bText
ret = CQCodeEscapeText(ret)
}
}
func BenchmarkCQCodeEscapeTextBefore(b *testing.B) {
for i := 0; i < b.N; i++ {
ret := bText
ret = strings.ReplaceAll(ret, "&", "&amp;")
ret = strings.ReplaceAll(ret, "[", "&#91;")
ret = strings.ReplaceAll(ret, "]", "&#93;")
}
}
func TestCQCodeEscapeText(t *testing.T) {
for i := 0; i < 200; i++ {
rs := utils.RandomStringRange(3000, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890[]&")
ret := rs
ret = strings.ReplaceAll(ret, "&", "&amp;")
ret = strings.ReplaceAll(ret, "[", "&#91;")
ret = strings.ReplaceAll(ret, "]", "&#93;")
assert.Equal(t, ret, CQCodeEscapeText(rs))
}
}

View File

@ -2,7 +2,7 @@ package coolq
import ( import (
"encoding/hex" "encoding/hex"
"io/ioutil" "os"
"path" "path"
"strconv" "strconv"
"strings" "strings"
@ -69,9 +69,6 @@ func (bot *CQBot) privateMessageEvent(c *client.QQClient, m *message.PrivateMess
}, },
} }
bot.dispatchEventMessage(fm) bot.dispatchEventMessage(fm)
if m.Sender.Uin != c.Uin {
c.MarkPrivateMessageReaded(m.Sender.Uin, int64(m.Time))
}
} }
func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage) { func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage) {
@ -109,9 +106,6 @@ func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage)
} }
gm["message_id"] = id gm["message_id"] = id
bot.dispatchEventMessage(gm) bot.dispatchEventMessage(gm)
if m.Sender.Uin != c.Uin {
c.MarkGroupMessageReaded(m.GroupCode, int64(m.Id))
}
} }
func (bot *CQBot) tempMessageEvent(c *client.QQClient, e *client.TempMessageEvent) { func (bot *CQBot) tempMessageEvent(c *client.QQClient, e *client.TempMessageEvent) {
@ -268,7 +262,11 @@ func (bot *CQBot) groupNotifyEvent(c *client.QQClient, e client.INotifyEvent) {
func (bot *CQBot) friendNotifyEvent(c *client.QQClient, e client.INotifyEvent) { func (bot *CQBot) friendNotifyEvent(c *client.QQClient, e client.INotifyEvent) {
friend := c.FindFriend(e.From()) friend := c.FindFriend(e.From())
if notify, ok := e.(*client.FriendPokeNotifyEvent); ok { if notify, ok := e.(*client.FriendPokeNotifyEvent); ok {
log.Infof("好友 %v 戳了戳你.", friend.Nickname) if notify.Receiver == notify.Sender {
log.Infof("好友 %v 戳了戳自己.", friend.Nickname)
} else {
log.Infof("好友 %v 戳了戳你.", friend.Nickname)
}
bot.dispatchEventMessage(MSG{ bot.dispatchEventMessage(MSG{
"post_type": "notice", "post_type": "notice",
"notice_type": "notify", "notice_type": "notify",
@ -282,6 +280,22 @@ func (bot *CQBot) friendNotifyEvent(c *client.QQClient, e client.INotifyEvent) {
} }
} }
func (bot *CQBot) memberTitleUpdatedEvent(c *client.QQClient, e *client.MemberSpecialTitleUpdatedEvent) {
group := c.FindGroup(e.GroupCode)
mem := group.FindMember(e.Uin)
log.Infof("群 %v(%v) 内成员 %v(%v) 获得了新的头衔: %v", group.Name, group.Code, mem.DisplayName(), mem.Uin, e.NewTitle)
bot.dispatchEventMessage(MSG{
"post_type": "notice",
"notice_type": "notify",
"sub_type": "title",
"group_id": group.Code,
"self_id": c.Uin,
"user_id": e.Uin,
"time": time.Now().Unix(),
"title": e.NewTitle,
})
}
func (bot *CQBot) friendRecallEvent(c *client.QQClient, e *client.FriendMessageRecalledEvent) { func (bot *CQBot) friendRecallEvent(c *client.QQClient, e *client.FriendMessageRecalledEvent) {
f := c.FindFriend(e.FriendUin) f := c.FindFriend(e.FriendUin)
gid := toGlobalID(e.FriendUin, e.MessageId) gid := toGlobalID(e.FriendUin, e.MessageId)
@ -542,59 +556,26 @@ func (bot *CQBot) groupDecrease(groupCode, userUin int64, operator *client.Group
func (bot *CQBot) checkMedia(e []message.IMessageElement) { func (bot *CQBot) checkMedia(e []message.IMessageElement) {
for _, elem := range e { for _, elem := range e {
switch i := elem.(type) { switch i := elem.(type) {
case *message.ImageElement:
filename := hex.EncodeToString(i.Md5) + ".image"
if !global.PathExists(path.Join(global.ImagePath, filename)) {
_ = ioutil.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
w.Write(i.Md5)
w.WriteUInt32(uint32(i.Size))
w.WriteString(i.Filename)
w.WriteString(i.Url)
}), 0o644)
}
i.Filename = filename
case *message.GroupImageElement: case *message.GroupImageElement:
filename := hex.EncodeToString(i.Md5) + ".image" filename := hex.EncodeToString(i.Md5) + ".image"
if !global.PathExists(path.Join(global.ImagePath, filename)) { if !global.PathExists(path.Join(global.ImagePath, filename)) {
_ = ioutil.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) { _ = os.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
w.Write(i.Md5) w.Write(i.Md5)
w.WriteUInt32(uint32(i.Size)) w.WriteUInt32(uint32(i.Size))
w.WriteString(filename) w.WriteString(i.ImageId)
w.WriteString(i.Url) w.WriteString(i.Url)
}), 0o644) }), 0o644)
} }
case *message.FriendImageElement: case *message.FriendImageElement:
filename := hex.EncodeToString(i.Md5) + ".image" filename := hex.EncodeToString(i.Md5) + ".image"
if !global.PathExists(path.Join(global.ImagePath, filename)) { if !global.PathExists(path.Join(global.ImagePath, filename)) {
_ = ioutil.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) { _ = os.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
w.Write(i.Md5) w.Write(i.Md5)
w.WriteUInt32(uint32(0)) // 发送时会调用url, 大概没事 w.WriteUInt32(uint32(i.Size))
w.WriteString(filename) w.WriteString(i.ImageId)
w.WriteString(i.Url) w.WriteString(i.Url)
}), 0o644) }), 0o644)
} }
case *message.GroupFlashImgElement:
filename := hex.EncodeToString(i.Md5) + ".image"
if !global.PathExists(path.Join(global.ImagePath, filename)) {
_ = ioutil.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
w.Write(i.Md5)
w.WriteUInt32(uint32(i.Size))
w.WriteString(i.Filename)
w.WriteString("")
}), 0o644)
}
i.Filename = filename
case *message.FriendFlashImgElement:
filename := hex.EncodeToString(i.Md5) + ".image"
if !global.PathExists(path.Join(global.ImagePath, filename)) {
_ = ioutil.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
w.Write(i.Md5)
w.WriteUInt32(uint32(i.Size))
w.WriteString(i.Filename)
w.WriteString("")
}), 0o644)
}
i.Filename = filename
case *message.VoiceElement: case *message.VoiceElement:
i.Name = strings.ReplaceAll(i.Name, "{", "") i.Name = strings.ReplaceAll(i.Name, "{", "")
i.Name = strings.ReplaceAll(i.Name, "}", "") i.Name = strings.ReplaceAll(i.Name, "}", "")
@ -604,12 +585,12 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
log.Warnf("语音文件 %v 下载失败: %v", i.Name, err) log.Warnf("语音文件 %v 下载失败: %v", i.Name, err)
continue continue
} }
_ = ioutil.WriteFile(path.Join(global.VoicePath, i.Name), b, 0o644) _ = os.WriteFile(path.Join(global.VoicePath, i.Name), b, 0o644)
} }
case *message.ShortVideoElement: case *message.ShortVideoElement:
filename := hex.EncodeToString(i.Md5) + ".video" filename := hex.EncodeToString(i.Md5) + ".video"
if !global.PathExists(path.Join(global.VideoPath, filename)) { if !global.PathExists(path.Join(global.VideoPath, filename)) {
_ = ioutil.WriteFile(path.Join(global.VideoPath, filename), binary.NewWriterF(func(w *binary.Writer) { _ = os.WriteFile(path.Join(global.VideoPath, filename), binary.NewWriterF(func(w *binary.Writer) {
w.Write(i.Md5) w.Write(i.Md5)
w.Write(i.ThumbMd5) w.Write(i.ThumbMd5)
w.WriteUInt32(uint32(i.Size)) w.WriteUInt32(uint32(i.Size))

View File

@ -119,6 +119,12 @@ servers:
host: 127.0.0.1 host: 127.0.0.1
# pprof服务器监听端口 # pprof服务器监听端口
port: 7700 port: 7700
# LambdaServer 配置
- lambda:
type: scf # 可用 scf,aws (aws未经过测试)
middlewares:
<<: *default # 引用默认中间件
# 可添加更多 # 可添加更多
#- ws-reverse: #- ws-reverse:
@ -208,4 +214,15 @@ database: # 数据库相关设置
```` ````
1.1.1.1:53 1.1.1.1:53
1.1.2.2:8899 1.1.2.2:8899
```` ````
## 云函数部署
使用CustomRuntime进行部署 bootstrap 文件在 `scripts/bootstrap` 中已给出。
在部署前,请在本地完成登录,并将 `config.yml` `device.json` `bootstrap` 和 `go-cqhttp`
一起打包。
在触发器中创建一个API网关触发器并启用集成响应创建完成后即可通过api网关访问go-cqhttp(建议配置 AccessToken)。
> scripts/bootstrap 中使用的工作路径为 /tmp, 这个目录最大能容下500M文件, 如需长期使用,
> 请挂载文件存储(CFS).

View File

@ -7,6 +7,7 @@
<p> <p>
##### CQCode ##### CQCode
- [图片](#图片) - [图片](#图片)
- [回复](#回复) - [回复](#回复)
- [红包](#红包) - [红包](#红包)
@ -21,6 +22,7 @@
- [图片](#图片) - [图片](#图片)
##### API ##### API
- [设置群名](#设置群名) - [设置群名](#设置群名)
- [设置群头像](#设置群头像) - [设置群头像](#设置群头像)
- [获取图片信息](#获取图片信息) - [获取图片信息](#获取图片信息)
@ -47,6 +49,7 @@
- [重载事件过滤器](#重载事件过滤器) - [重载事件过滤器](#重载事件过滤器)
##### 事件 ##### 事件
- [群消息撤回](#群消息撤回) - [群消息撤回](#群消息撤回)
- [好友消息撤回](#好友消息撤回) - [好友消息撤回](#好友消息撤回)
- [好友戳一戳](#好友戳一戳) - [好友戳一戳](#好友戳一戳)
@ -112,9 +115,7 @@ Type : `reply`
| `time` | int64 | 可选. 自定义回复时的时间, 格式为Unix时间 | | `time` | int64 | 可选. 自定义回复时的时间, 格式为Unix时间 |
| `seq` | int64 | 起始消息序号, 可通过 `get_msg` 获得 | | `seq` | int64 | 起始消息序号, 可通过 `get_msg` 获得 |
示例: `[CQ:reply,id=123456]`
示例: `[CQ:reply,id=123456]`
\ \
自定义回复示例: `[CQ:reply,text=Hello World,qq=10086,time=3376656000,seq=5123]` 自定义回复示例: `[CQ:reply,text=Hello World,qq=10086,time=3376656000,seq=5123]`
@ -122,11 +123,11 @@ Type : `reply`
```json ```json
{ {
"type": "music", "type": "music",
"data": { "data": {
"type": "163", "type": "163",
"id": "28949129" "id": "28949129"
} }
} }
``` ```
@ -143,13 +144,13 @@ Type : `reply`
```json ```json
{ {
"type": "music", "type": "music",
"data": { "data": {
"type": "custom", "type": "custom",
"url": "http://baidu.com", "url": "http://baidu.com",
"audio": "http://baidu.com/1.mp3", "audio": "http://baidu.com/1.mp3",
"title": "音乐标题" "title": "音乐标题"
} }
} }
``` ```
@ -231,11 +232,9 @@ Type: `gift`
| 12 | 我超忙的 | | 12 | 我超忙的 |
| 13 | 爱心口罩 | | 13 | 爱心口罩 |
示例: `[CQ:gift,qq=123456,id=8]` 示例: `[CQ:gift,qq=123456,id=8]`
### 合并转发 ### 合并转发
Type: `forward` Type: `forward`
@ -259,32 +258,33 @@ Type: `node`
| 参数名 | 类型 | 说明 | 特殊说明 | | 参数名 | 类型 | 说明 | 特殊说明 |
| --------- | ------- | -------------- | -------------------------------------------------------------------------------------- | | --------- | ------- | -------------- | -------------------------------------------------------------------------------------- |
| `id` | int32 | 转发消息id | 直接引用他人的消息合并转发, 实际查看顺序为原消息发送顺序 **与下面的自定义消息二选一** | | `id` | int32 | 转发消息id | 直接引用他人的消息合并转发, 实际查看顺序为原消息发送顺序 **与下面的自定义消息二选一** |
| `name` | string | 发送者显示名字 | 用于自定义消息 (自定义消息并合并转发,实际查看顺序为自定义消息段顺序) | | `name` | string | 发送者显示名字 | 用于自定义消息 (自定义消息并合并转发,实际查看顺序为自定义消息段顺序) |
| `uin` | int64 | 发送者QQ号 | 用于自定义消息 | | `uin` | int64 | 发送者QQ号 | 用于自定义消息 |
| `content` | message | 具体消息 | 用于自定义消息 | | `content` | message | 具体消息 | 用于自定义消息 |
| `seq` | message | 具体消息 | 用于自定义消息 | | `seq` | message | 具体消息 | 用于自定义消息 |
特殊说明: **需要使用单独的API `/send_group_forward_msg` 发送并且由于消息段较为复杂仅支持Array形式入参。 如果引用消息和自定义消息同时出现,实际查看顺序将取消息段顺序. 另外按 [CQHTTP](https://git.io/JtxtN) 文档说明, `data` 应全为字符串, 但由于需要接收`message` 类型的消息, 所以 *仅限此Type的content字段* 支持Array套娃** 特殊说明: **需要使用单独的API `/send_group_forward_msg` 发送并且由于消息段较为复杂仅支持Array形式入参。 如果引用消息和自定义消息同时出现,实际查看顺序将取消息段顺序.
另外按 [CQHTTP](https://git.io/JtxtN) 文档说明, `data` 应全为字符串, 但由于需要接收`message` 类型的消息, 所以 *仅限此Type的content字段* 支持Array套娃**
示例: 示例:
直接引用消息合并转发: 直接引用消息合并转发:
````json ````json
[ [
{ {
"type": "node", "type": "node",
"data": { "data": {
"id": "123" "id": "123"
}
},
{
"type": "node",
"data": {
"id": "456"
}
} }
},
{
"type": "node",
"data": {
"id": "456"
}
}
] ]
```` ````
@ -292,27 +292,29 @@ Type: `node`
````json ````json
[ [
{ {
"type": "node", "type": "node",
"data": { "data": {
"name": "消息发送者A", "name": "消息发送者A",
"uin": "10086", "uin": "10086",
"content": [ "content": [
{ {
"type": "text", "type": "text",
"data": {"text": "测试消息1"} "data": {
} "text": "测试消息1"
] }
}
},
{
"type": "node",
"data": {
"name": "消息发送者B",
"uin": "10087",
"content": "[CQ:image,file=xxxxx]测试消息2"
} }
]
} }
},
{
"type": "node",
"data": {
"name": "消息发送者B",
"uin": "10087",
"content": "[CQ:image,file=xxxxx]测试消息2"
}
}
] ]
```` ````
@ -320,24 +322,25 @@ Type: `node`
````json ````json
[ [
{ {
"type": "node", "type": "node",
"data": { "data": {
"name": "自定义发送者", "name": "自定义发送者",
"uin": "10086", "uin": "10086",
"content": "我是自定义消息", "content": "我是自定义消息",
"seq": "5123", "seq": "5123",
"time": "3376656000" "time": "3376656000"
}
},
{
"type": "node",
"data": {
"id": "123"
}
} }
},
{
"type": "node",
"data": {
"id": "123"
}
}
] ]
```` ````
### 短视频消息 ### 短视频消息
Type: `video` Type: `video`
@ -351,7 +354,8 @@ Type: `video`
| `file` | string | 支持http和file发送 | | `file` | string | 支持http和file发送 |
| `cover` | string | 视频封面支持httpfile和base64发送格式必须为jpg | | `cover` | string | 视频封面支持httpfile和base64发送格式必须为jpg |
| `c` | `2` `3` | 通过网络下载视频时的线程数, 默认单线程. (在资源不支持并发时会自动处理) | | `c` | `2` `3` | 通过网络下载视频时的线程数, 默认单线程. (在资源不支持并发时会自动处理) |
示例: `[CQ:image,file=file:///C:\\Users\Richard\Pictures\1.mp4]`
示例: `[CQ:video,file=file:///C:\\Users\Richard\Videos\1.mp4]`
### XML 消息 ### XML 消息
@ -375,30 +379,61 @@ Type: `xml`
#### qq音乐 #### qq音乐
```xml ```xml
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="2" templateID="1" action="web" brief="&#91;分享&#93; 十年" sourceMsgId="0" url="https://i.y.qq.com/v8/playsong.html?_wv=1&amp;songid=4830342&amp;souce=qqshare&amp;source=qqshare&amp;ADTAG=qqshare" flag="0" adverSign="0" multiMsgFlag="0" ><item layout="2"><audio cover="http://imgcache.qq.com/music/photo/album_500/26/500_albumpic_89526_0.jpg" src="http://ws.stream.qqmusic.qq.com/C400003mAan70zUy5O.m4a?guid=1535153710&amp;vkey=D5315B8C0603653592AD4879A8A3742177F59D582A7A86546E24DD7F282C3ACF81526C76E293E57EA1E42CF19881C561275D919233333ADE&amp;uin=&amp;fromtag=3" /><title>十年</title><summary>陈奕迅</summary></item><source name="QQ音乐" icon="https://i.gtimg.cn/open/app_icon/01/07/98/56/1101079856_100_m.png" url="http://web.p.qq.com/qqmpmobile/aio/app.html?id=1101079856" action="app" a_actionData="com.tencent.qqmusic" i_actionData="tencent1101079856://" appid="1101079856" /></msg> <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<msg serviceID="2" templateID="1" action="web" brief="&#91;分享&#93; 十年" sourceMsgId="0"
url="https://i.y.qq.com/v8/playsong.html?_wv=1&amp;songid=4830342&amp;souce=qqshare&amp;source=qqshare&amp;ADTAG=qqshare"
flag="0" adverSign="0" multiMsgFlag="0">
<item layout="2">
<audio cover="http://imgcache.qq.com/music/photo/album_500/26/500_albumpic_89526_0.jpg"
src="http://ws.stream.qqmusic.qq.com/C400003mAan70zUy5O.m4a?guid=1535153710&amp;vkey=D5315B8C0603653592AD4879A8A3742177F59D582A7A86546E24DD7F282C3ACF81526C76E293E57EA1E42CF19881C561275D919233333ADE&amp;uin=&amp;fromtag=3"/>
<title>十年</title>
<summary>陈奕迅</summary>
</item>
<source name="QQ音乐" icon="https://i.gtimg.cn/open/app_icon/01/07/98/56/1101079856_100_m.png"
url="http://web.p.qq.com/qqmpmobile/aio/app.html?id=1101079856" action="app"
a_actionData="com.tencent.qqmusic" i_actionData="tencent1101079856://" appid="1101079856"/>
</msg>
``` ```
#### 网易音乐 #### 网易音乐
```xml ```xml
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID="2" templateID="1" action="web" brief="&#91;分享&#93; 十年" sourceMsgId="0" url="http://music.163.com/m/song/409650368" flag="0" adverSign="0" multiMsgFlag="0" ><item layout="2"><audio cover="http://p2.music.126.net/g-Qgb9ibk9Wp_0HWra0xQQ==/16636710440565853.jpg?param=90y90" src="https://music.163.com/song/media/outer/url?id=409650368.mp3" /><title>十年</title><summary>黄梦之</summary></item><source name="网易云音乐" icon="https://pic.rmb.bdstatic.com/911423bee2bef937975b29b265d737b3.png" url="http://web.p.qq.com/qqmpmobile/aio/app.html?id=1101079856" action="app" a_actionData="com.netease.cloudmusic" i_actionData="tencent100495085://" appid="100495085" /></msg> <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<msg serviceID="2" templateID="1" action="web" brief="&#91;分享&#93; 十年" sourceMsgId="0"
url="http://music.163.com/m/song/409650368" flag="0" adverSign="0" multiMsgFlag="0">
<item layout="2">
<audio cover="http://p2.music.126.net/g-Qgb9ibk9Wp_0HWra0xQQ==/16636710440565853.jpg?param=90y90"
src="https://music.163.com/song/media/outer/url?id=409650368.mp3"/>
<title>十年</title>
<summary>黄梦之</summary>
</item>
<source name="网易云音乐" icon="https://pic.rmb.bdstatic.com/911423bee2bef937975b29b265d737b3.png"
url="http://web.p.qq.com/qqmpmobile/aio/app.html?id=1101079856" action="app"
a_actionData="com.netease.cloudmusic" i_actionData="tencent100495085://" appid="100495085"/>
</msg>
``` ```
#### 卡片消息1 #### 卡片消息1
```xml ```xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<msg serviceID="1"> <msg serviceID="1">
<item><title>生死8秒女司机高速急刹他一个操作救下一车性命</title></item> <item>
<source name="官方认证消息" icon="https://qzs.qq.com/ac/qzone_v5/client/auth_icon.png" action="" appid="-1" /> <title>生死8秒女司机高速急刹他一个操作救下一车性命</title>
</item>
<source name="官方认证消息" icon="https://qzs.qq.com/ac/qzone_v5/client/auth_icon.png" action="" appid="-1"/>
</msg> </msg>
``` ```
#### 卡片消息2 #### 卡片消息2
```xml ```xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<msg serviceID="1"> <msg serviceID="1">
<item layout="4"> <item layout="4">
<title>test title</title> <title>test title</title>
<picture cover="http://url.cn/5CEwIUy"/> <picture cover="http://url.cn/5CEwIUy"/>
</item> </item>
</msg> </msg>
``` ```
@ -417,23 +452,24 @@ Type: `json`
json中的字符串需要进行转义 json中的字符串需要进行转义
>","=> `&#44;` > ","=> `&#44;`
>"&"=> `&amp;` > "&"=> `&amp;`
>"["=> `&#91;` > "["=> `&#91;`
>"]"=> `&#93;` > "]"=> `&#93;`
否则无法正确得到解析 否则无法正确得到解析
示例json 的cq码 示例json 的cq码
```test ```test
[CQ:json,data={"app":"com.tencent.miniapp"&#44;"desc":""&#44;"view":"notification"&#44;"ver":"0.0.0.1"&#44;"prompt":"&#91;应用&#93;"&#44;"appID":""&#44;"sourceName":""&#44;"actionData":""&#44;"actionData_A":""&#44;"sourceUrl":""&#44;"meta":{"notification":{"appInfo":{"appName":"全国疫情数据统计"&#44;"appType":4&#44;"appid":1109659848&#44;"iconUrl":"http:\/\/gchat.qpic.cn\/gchatpic_new\/719328335\/-2010394141-6383A777BEB79B70B31CE250142D740F\/0"}&#44;"data":&#91;{"title":"确诊"&#44;"value":"80932"}&#44;{"title":"今日确诊"&#44;"value":"28"}&#44;{"title":"疑似"&#44;"value":"72"}&#44;{"title":"今日疑似"&#44;"value":"5"}&#44;{"title":"治愈"&#44;"value":"60197"}&#44;{"title":"今日治愈"&#44;"value":"1513"}&#44;{"title":"死亡"&#44;"value":"3140"}&#44;{"title":"今**亡"&#44;"value":"17"}&#93;&#44;"title":"中国加油,武汉加油"&#44;"button":&#91;{"name":"病毒SARS-CoV-2其导致疾病命名 COVID-19"&#44;"action":""}&#44;{"name":"传染源:新冠肺炎的患者。无症状感染者也可能成为传染源。"&#44;"action":""}&#93;&#44;"emphasis_keyword":""}}&#44;"text":""&#44;"sourceAd":""}] [CQ:json,data={"app":"com.tencent.miniapp"&#44;"desc":""&#44;"view":"notification"&#44;"ver":"0.0.0.1"&#44;"prompt":"&#91;应用&#93;"&#44;"appID":""&#44;"sourceName":""&#44;"actionData":""&#44;"actionData_A":""&#44;"sourceUrl":""&#44;"meta":{"notification":{"appInfo":{"appName":"全国疫情数据统计"&#44;"appType":4&#44;"appid":1109659848&#44;"iconUrl":"http:\/\/gchat.qpic.cn\/gchatpic_new\/719328335\/-2010394141-6383A777BEB79B70B31CE250142D740F\/0"}&#44;"data":&#91;{"title":"确诊"&#44;"value":"80932"}&#44;{"title":"今日确诊"&#44;"value":"28"}&#44;{"title":"疑似"&#44;"value":"72"}&#44;{"title":"今日疑似"&#44;"value":"5"}&#44;{"title":"治愈"&#44;"value":"60197"}&#44;{"title":"今日治愈"&#44;"value":"1513"}&#44;{"title":"死亡"&#44;"value":"3140"}&#44;{"title":"今**亡"&#44;"value":"17"}&#93;&#44;"title":"中国加油,武汉加油"&#44;"button":&#91;{"name":"病毒SARS-CoV-2其导致疾病命名 COVID-19"&#44;"action":""}&#44;{"name":"传染源:新冠肺炎的患者。无症状感染者也可能成为传染源。"&#44;"action":""}&#93;&#44;"emphasis_keyword":""}}&#44;"text":""&#44;"sourceAd":""}]
``` ```
### cardimage ### cardimage
一种xml的图片消息装逼大图 一种xml的图片消息装逼大图
ps: xml 接口的消息都存在风控风险,请自行兼容发送失败后的处理(可以失败后走普通图片模式) ps: xml 接口的消息都存在风控风险,请自行兼容发送失败后的处理(可以失败后走普通图片模式)
@ -454,8 +490,8 @@ Type: `cardimage`
| `source` | string | 分享来源的名称,可以留空 | | `source` | string | 分享来源的名称,可以留空 |
| `icon` | string | 分享来源的icon图标url可以留空 | | `icon` | string | 分享来源的icon图标url可以留空 |
示例cardimage 的cq码 示例cardimage 的cq码
```test ```test
[CQ:cardimage,file=https://i.pixiv.cat/img-master/img/2020/03/25/00/00/08/80334602_p0_master1200.jpg] [CQ:cardimage,file=https://i.pixiv.cat/img-master/img/2020/03/25/00/00/08/80334602_p0_master1200.jpg]
``` ```
@ -480,9 +516,9 @@ Type: `tts`
### 设置群名 ### 设置群名
终结点: `/set_group_name` 终结点: `/set_group_name`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ------------ | ------ | ---- | | ------------ | ------ | ---- |
@ -491,9 +527,9 @@ Type: `tts`
### 设置群头像 ### 设置群头像
终结点: `/set_group_portrait` 终结点: `/set_group_portrait`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ---------- | ------ | ------------------------ | | ---------- | ------ | ------------------------ |
@ -505,7 +541,8 @@ Type: `tts`
- 绝对路径,例如 `file:///C:\\Users\Richard\Pictures\1.png`,格式使用 [`file` URI](https://tools.ietf.org/html/rfc8089) - 绝对路径,例如 `file:///C:\\Users\Richard\Pictures\1.png`,格式使用 [`file` URI](https://tools.ietf.org/html/rfc8089)
- 网络 URL例如 `http://i1.piimg.com/567571/fdd6e7b6d93f1ef0.jpg` - 网络 URL例如 `http://i1.piimg.com/567571/fdd6e7b6d93f1ef0.jpg`
- Base64 编码,例如 `base64://iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAIAAADJt1n/AAAAKElEQVQ4EWPk5+RmIBcwkasRpG9UM4mhNxpgowFGMARGEwnBIEJVAAAdBgBNAZf+QAAAAABJRU5ErkJggg==` - Base64
编码,例如 `base64://iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAIAAADJt1n/AAAAKElEQVQ4EWPk5+RmIBcwkasRpG9UM4mhNxpgowFGMARGEwnBIEJVAAAdBgBNAZf+QAAAAABJRU5ErkJggg==`
[2]`cache`参数: 通过网络 URL 发送时有效,`1`表示使用缓存,`0`关闭关闭缓存,默认 为`1` [2]`cache`参数: 通过网络 URL 发送时有效,`1`表示使用缓存,`0`关闭关闭缓存,默认 为`1`
@ -513,7 +550,7 @@ Type: `tts`
### 获取图片信息 ### 获取图片信息
终结点: `/get_image` 终结点: `/get_image`
> 该接口为 CQHTTP 接口修改 > 该接口为 CQHTTP 接口修改
@ -533,7 +570,7 @@ Type: `tts`
### 获取消息 ### 获取消息
终结点: `/get_msg` 终结点: `/get_msg`
参数 参数
@ -571,28 +608,29 @@ Type: `tts`
````json5 ````json5
{ {
"data": { "data": {
"messages": [ "messages": [
{ {
"content": "合并转发1", "content": "合并转发1",
"sender": { "sender": {
"nickname": "发送者A", "nickname": "发送者A",
"user_id": 10086 "user_id": 10086
}, },
"time": 1595694374 "time": 1595694374
}, },
{ {
"content": "合并转发2[CQ:image,file=xxxx,url=xxxx]", "content": "合并转发2[CQ:image,file=xxxx,url=xxxx]",
"sender": { "sender": {
"nickname": "发送者B", "nickname": "发送者B",
"user_id": 10087 "user_id": 10087
}, },
"time": 1595694393 // 可选 "time": 1595694393
} // 可选
] }
}, ]
"retcode": 0, },
"status": "ok" "retcode": 0,
"status": "ok"
} }
```` ````
@ -600,7 +638,7 @@ Type: `tts`
终结点: `/send_group_forward_msg` 终结点: `/send_group_forward_msg`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ---------- | -------------- | ---------------------------- | | ---------- | -------------- | ---------------------------- |
@ -608,16 +646,16 @@ Type: `tts`
| `messages` | forward node[] | 自定义转发消息, 具体看 [CQCode](https://github.com/Mrs4s/go-cqhttp/blob/master/docs/cqhttp.md#%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91%E6%B6%88%E6%81%AF%E8%8A%82%E7%82%B9) | | `messages` | forward node[] | 自定义转发消息, 具体看 [CQCode](https://github.com/Mrs4s/go-cqhttp/blob/master/docs/cqhttp.md#%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91%E6%B6%88%E6%81%AF%E8%8A%82%E7%82%B9) |
响应数据 响应数据
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ------------ | ------ | ------ | | ------------ | ------ | ------ |
| `message_id` | string | 消息id | | `message_id` | string | 消息id |
### 获取中文分词 ### 获取中文分词
终结点: `/.get_word_slices` 终结点: `/.get_word_slices`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| --------- | ------ | ---- | | --------- | ------ | ---- |
@ -685,9 +723,9 @@ Type: `tts`
> 注意: 目前图片OCR接口仅支持接受的图片 > 注意: 目前图片OCR接口仅支持接受的图片
终结点: `/ocr_image` 终结点: `/ocr_image`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ------- | ------ | ------ | | ------- | ------ | ------ |
@ -708,7 +746,6 @@ Type: `tts`
| `confidence` | int32 | 置信度 | | `confidence` | int32 | 置信度 |
| `coordinates` | vector2 | 坐标 | | `coordinates` | vector2 | 坐标 |
### 获取群系统消息 ### 获取群系统消息
终结点: `/get_group_system_msg` 终结点: `/get_group_system_msg`
@ -720,9 +757,9 @@ Type: `tts`
| `invited_requests` | InvitedRequest[] | 邀请消息列表 | | `invited_requests` | InvitedRequest[] | 邀请消息列表 |
| `join_requests` | JoinRequest[] | 进群消息列表 | | `join_requests` | JoinRequest[] | 进群消息列表 |
> 注意: 如果列表不存在任何消息, 将返回 `null` > 注意: 如果列表不存在任何消息, 将返回 `null`
**InvitedRequest** **InvitedRequest**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| -------------- | ------ | ----------------- | | -------------- | ------ | ----------------- |
@ -734,7 +771,7 @@ Type: `tts`
| `checked` | bool | 是否已被处理 | | `checked` | bool | 是否已被处理 |
| `actor` | int64 | 处理者, 未处理为0 | | `actor` | int64 | 处理者, 未处理为0 |
**JoinRequest** **JoinRequest**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ---------------- | ------ | ----------------- | | ---------------- | ------ | ----------------- |
@ -751,7 +788,7 @@ Type: `tts`
终结点: `/get_group_file_system_info` 终结点: `/get_group_file_system_info`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ---------- | ----- | ---- | | ---------- | ----- | ---- |
@ -772,7 +809,7 @@ Type: `tts`
终结点: `/get_group_root_files` 终结点: `/get_group_root_files`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ---------- | ----- | ---- | | ---------- | ----- | ---- |
@ -791,7 +828,7 @@ Type: `tts`
终结点: `/get_group_files_by_folder` 终结点: `/get_group_files_by_folder`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ----------- | ------ | --------------------------- | | ----------- | ------ | --------------------------- |
@ -811,7 +848,7 @@ Type: `tts`
终结点: `/get_group_file_url` 终结点: `/get_group_file_url`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ---------- | ------ | ------------------------- | | ---------- | ------ | ------------------------- |
@ -825,7 +862,7 @@ Type: `tts`
| ----- | ------ | ------------ | | ----- | ------ | ------------ |
| `url` | string | 文件下载链接 | | `url` | string | 文件下载链接 |
**File** **File**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ---------------- | ------ | ---------------------- | | ---------------- | ------ | ---------------------- |
@ -840,7 +877,7 @@ Type: `tts`
| `uploader` | int64 | 上传者ID | | `uploader` | int64 | 上传者ID |
| `uploader_name` | string | 上传者名字 | | `uploader_name` | string | 上传者名字 |
**Folder** **Folder**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ------------------ | ------ | ---------- | | ------------------ | ------ | ---------- |
@ -855,7 +892,7 @@ Type: `tts`
终结点: `/upload_group_file` 终结点: `/upload_group_file`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ---------- | ------ | ------------------------- | | ---------- | ------ | ------------------------- |
@ -885,7 +922,6 @@ Type: `tts`
**Statistics** **Statistics**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ------------------ | ------ | ---------------- | | ------------------ | ------ | ---------------- |
| `packet_received` | uint64 | 收到的数据包总数 | | `packet_received` | uint64 | 收到的数据包总数 |
@ -902,7 +938,7 @@ Type: `tts`
终结点: `/get_group_at_all_remain` 终结点: `/get_group_at_all_remain`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ---------- | ----- | ---- | | ---------- | ----- | ---- |
@ -920,7 +956,7 @@ Type: `tts`
终结点: `/download_file` 终结点: `/download_file`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| -------------- | --------------- | ------------ | | -------------- | --------------- | ------------ |
@ -960,7 +996,7 @@ JSON数组:
终结点:`/get_group_msg_history` 终结点:`/get_group_msg_history`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ------------- | ----- | ----------------------------------- | | ------------- | ----- | ----------------------------------- |
@ -979,7 +1015,7 @@ JSON数组:
终结点:`/get_online_clients` 终结点:`/get_online_clients`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ---------- | ---- | ------------ | | ---------- | ---- | ------------ |
@ -1003,7 +1039,7 @@ JSON数组:
终结点:`/check_url_safely` 终结点:`/check_url_safely`
**参数** **参数**
| 字段 | 类型 | 说明 | | 字段 | 类型 | 说明 |
| ---------- | ------ | ------------------------- | | ---------- | ------ | ------------------------- |
@ -1076,13 +1112,22 @@ JSON数组:
| `ext_name` | string | 用户昵称 | | `ext_name` | string | 用户昵称 |
| `create_time` | int64 | 账号创建时间 | | `create_time` | int64 | 账号创建时间 |
### 标记消息已读
终结点: `/mark_msg_as_read`
**参数**
| 字段名 | 数据类型 | 默认值 | 说明 |
| ---------- | -------- | ------ | -------- |
| `message_id` | int32 | | 消息ID |
### 重载事件过滤器 ### 重载事件过滤器
终结点:`/reload_event_filter` 终结点:`/reload_event_filter`
`该 API 无需参数也没有响应数据` `该 API 无需参数也没有响应数据`
## 事件 ## 事件
### 群消息撤回 ### 群消息撤回
@ -1181,11 +1226,23 @@ JSON数组:
| `notice_type` | string | `group_card` | 消息类型 | | `notice_type` | string | `group_card` | 消息类型 |
| `group_id` | int64 | | 群号 | | `group_id` | int64 | | 群号 |
| `user_id` | int64 | | 成员id | | `user_id` | int64 | | 成员id |
| `card_new` | int64 | | 新名片 | | `card_new` | string | | 新名片 |
| `card_old` | int64 | | 旧名片 | | `card_old` | string | | 旧名片 |
> PS: 当名片为空时 `card_xx` 字段为空字符串, 并不是昵称 > PS: 当名片为空时 `card_xx` 字段为空字符串, 并不是昵称
### 群成员头衔更新事件
**上报数据**
| 字段 | 类型 | 可能的值 | 说明 |
| ------------- | ------ | ------------ | -------- |
| `post_type` | string | `notice` | 上报类型 |
| `notice_type` | string | `notify` | 消息类型 |
| `group_id` | int64 | | 群号 |
| `user_id` | int64 | | 成员id |
| `title` | string | | 新头衔 |
### 接收到离线文件 ### 接收到离线文件
**上报数据** **上报数据**

View File

@ -1,38 +1,40 @@
# 文件 # 文件
go-cqhttp 默认生成的文件树如下所示: go-cqhttp 默认生成的文件树如下所示:
```` ```
. .
├── go-cqhttp ├── go-cqhttp
├── config.hjson ├── config.yml
├── device.json ├── device.json
├── logs ├── logs
│ └── xx-xx-xx.log │ └── xx-xx-xx.log
└── data └── data
├── images ├── images
│ └── xxxx.image │ └── xxxx.image
└── db └── levleldb
```` ```
| 文件 | 用途 | | 文件 | 用途 |
| ----------- | ------------------- | | ------------ | -------------------- |
| go-cqhttp | go-cqhttp可执行文件 | | go-cqhttp | go-cqhttp 可执行文件 |
| config.hjson | 运行配置文件 | | config.yml | 运行配置文件 |
| device.json | 虚拟设备配置文件 | | device.json | 虚拟设备配置文件 |
| logs | 日志存放目录 | | logs | 日志存放目录 |
| data | 数据目录 | | data | 数据目录 |
| data/images | 图片缓存目录 | | data/leveldb | 数据库目录 |
| data/db | 数据库目录 | | data/images | 图片缓存目录 |
| data/voices | 语音缓存目录 |
| data/videos | 视频缓存目录 |
| data/cache | 发送图片缓存目录 |
## 图片缓存文件 ## 图片缓存文件
出于性能考虑go-cqhttp 并不会将图片源文件下载到本地而是生成一个可以和QQ服务器对应的缓存文件 (.image),该缓存文件结构如下: 出于性能考虑go-cqhttp 并不会将图片源文件下载到本地,而是生成一个可以和 QQ 服务器对应的缓存文件 (.image),该缓存文件结构如下:
| 偏移 | 类型 | 说明 | | 偏移 | 类型 | 说明 |
| --------------- | -------- | ------------------ | | --------------- | -------- | -------------------- |
| 0x00 | [16]byte | 图片源文件MD5 HASH | | 0x00 | [16]byte | 图片源文件 MD5 HASH |
| 0x10 | uint32 | 图片源文件大小 | | 0x10 | uint32 | 图片源文件大小 |
| 0x14 | string | 图片原名(QQ内部ID) | | 0x14 | string | 图片原名(QQ内部ID) |
| 0x14 + 原名长度 | string | 图片下载链接 | | 0x14 + 原名长度 | string | 图片下载链接 |

View File

@ -1,5 +1,7 @@
# 滑块验证码 # 滑块验证码
> 该文档已过期, 最新版本下可直接使用手机扫描二维码通过验证.
由于TX最新的限制, 所有协议在陌生设备/IP登录时都有可能被要求通过滑块验证码, 否则将会出现 `当前上网环境异常` 的错误. 目前我们准备了两个临时方案应对该验证码. 由于TX最新的限制, 所有协议在陌生设备/IP登录时都有可能被要求通过滑块验证码, 否则将会出现 `当前上网环境异常` 的错误. 目前我们准备了两个临时方案应对该验证码.
> 如果您有一台运行Windows的PC/Server 并且不会抓包操作, 我们建议直接使用方案B > 如果您有一台运行Windows的PC/Server 并且不会抓包操作, 我们建议直接使用方案B

View File

@ -11,7 +11,7 @@ var bufferPool = sync.Pool{
}, },
} }
// NewBuffer 从池获取新 bytes.Buffer // NewBuffer 从池获取新 bytes.Buffer
func NewBuffer() *bytes.Buffer { func NewBuffer() *bytes.Buffer {
return bufferPool.Get().(*bytes.Buffer) return bufferPool.Get().(*bytes.Buffer)
} }
@ -20,7 +20,7 @@ func NewBuffer() *bytes.Buffer {
func PutBuffer(buf *bytes.Buffer) { func PutBuffer(buf *bytes.Buffer) {
// See https://golang.org/issue/23199 // See https://golang.org/issue/23199
const maxSize = 1 << 16 const maxSize = 1 << 16
if buf.Cap() < maxSize { // 对于大Buffer直接丢弃 if buf != nil && buf.Cap() < maxSize { // 对于大Buffer直接丢弃
buf.Reset() buf.Reset()
bufferPool.Put(buf) bufferPool.Put(buf)
} }

View File

@ -2,8 +2,8 @@ package global
import ( import (
"crypto/md5" "crypto/md5"
"fmt" "encoding/hex"
"io/ioutil" "os"
"os/exec" "os/exec"
"path" "path"
@ -19,9 +19,9 @@ func EncoderSilk(data []byte) ([]byte, error) {
if err != nil { if err != nil {
return nil, errors.Wrap(err, "calc md5 failed") return nil, errors.Wrap(err, "calc md5 failed")
} }
tempName := fmt.Sprintf("%x", h.Sum(nil)) tempName := hex.EncodeToString(h.Sum(nil))
if silkPath := path.Join("data/cache", tempName+".silk"); PathExists(silkPath) { if silkPath := path.Join("data/cache", tempName+".silk"); PathExists(silkPath) {
return ioutil.ReadFile(silkPath) return os.ReadFile(silkPath)
} }
slk, err := codec.EncodeToSilk(data, tempName, true) slk, err := codec.EncodeToSilk(data, tempName, true)
if err != nil { if err != nil {

View File

@ -1,11 +1,11 @@
//go:build (linux || (windows && !arm) || darwin) && (386 || amd64 || arm || arm64) && !race
// +build linux windows,!arm darwin // +build linux windows,!arm darwin
// +build 386 amd64 arm arm64 // +build 386 amd64 arm arm64
// +build !race
// Package codec Slik编码核心模块
package codec package codec
import ( import (
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -14,15 +14,13 @@ import (
"github.com/wdvxdr1123/go-silk" "github.com/wdvxdr1123/go-silk"
) )
const ( const silkCachePath = "data/cache"
silkCachePath = "data/cache"
)
// EncodeToSilk 将音频编码为Silk // EncodeToSilk 将音频编码为Silk
func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte, err error) { func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte, err error) {
// 1. 写入缓存文件 // 1. 写入缓存文件
rawPath := path.Join(silkCachePath, tempName+".wav") rawPath := path.Join(silkCachePath, tempName+".wav")
err = ioutil.WriteFile(rawPath, record, os.ModePerm) err = os.WriteFile(rawPath, record, os.ModePerm)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "write temp file error") return nil, errors.Wrap(err, "write temp file error")
} }
@ -31,13 +29,17 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte
// 2.转换pcm // 2.转换pcm
pcmPath := path.Join(silkCachePath, tempName+".pcm") pcmPath := path.Join(silkCachePath, tempName+".pcm")
cmd := exec.Command("ffmpeg", "-i", rawPath, "-f", "s16le", "-ar", "24000", "-ac", "1", pcmPath) cmd := exec.Command("ffmpeg", "-i", rawPath, "-f", "s16le", "-ar", "24000", "-ac", "1", pcmPath)
if Debug {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
if err = cmd.Run(); err != nil { if err = cmd.Run(); err != nil {
return nil, errors.Wrap(err, "convert pcm file error") return nil, errors.Wrap(err, "convert pcm file error")
} }
defer os.Remove(pcmPath) defer os.Remove(pcmPath)
// 3. 转silk // 3. 转silk
pcm, err := ioutil.ReadFile(pcmPath) pcm, err := os.ReadFile(pcmPath)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "read pcm file err") return nil, errors.Wrap(err, "read pcm file err")
} }
@ -47,7 +49,7 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte
} }
if useCache { if useCache {
silkPath := path.Join(silkCachePath, tempName+".silk") silkPath := path.Join(silkCachePath, tempName+".silk")
err = ioutil.WriteFile(silkPath, silkWav, 0o666) err = os.WriteFile(silkPath, silkWav, 0o666)
} }
return return
} }

View File

@ -1,4 +1,5 @@
// +build !arm,!arm64,!amd64,!386 //go:build (!arm && !arm64 && !amd64 && !386) || race || (!windows && !linux && !darwin) || (windows && arm)
// +build !arm,!arm64,!amd64,!386 race !windows,!linux,!darwin windows,arm
package codec package codec

View File

@ -1,15 +0,0 @@
// +build !windows,!linux,!darwin
package codec
import "errors"
// EncodeToSilk 将音频编码为Silk
func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) {
return nil, errors.New("not supported now")
}
// RecodeTo24K 将silk重新编码为 24000 bit rate
func RecodeTo24K(data []byte) []byte {
return data
}

View File

@ -1,13 +0,0 @@
package codec
import "errors"
// EncodeToSilk 将音频编码为Silk
func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) {
return nil, errors.New("not supported now")
}
// RecodeTo24K 将silk重新编码为 24000 bit rate
func RecodeTo24K(data []byte) []byte {
return data
}

2
global/codec/doc.go Normal file
View File

@ -0,0 +1,2 @@
// Package codec Slik编码核心模块
package codec

4
global/codec/stubs.go Normal file
View File

@ -0,0 +1,4 @@
package codec
// Debug mode controls the ffmpeg output.
var Debug bool

View File

@ -7,9 +7,12 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"strconv"
"strings" "strings"
"sync" "sync"
"github.com/Mrs4s/go-cqhttp/global"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -53,11 +56,14 @@ type Config struct {
ReportSelfMessage bool `yaml:"report-self-message"` ReportSelfMessage bool `yaml:"report-self-message"`
RemoveReplyAt bool `yaml:"remove-reply-at"` RemoveReplyAt bool `yaml:"remove-reply-at"`
ExtraReplyData bool `yaml:"extra-reply-data"` ExtraReplyData bool `yaml:"extra-reply-data"`
SkipMimeScan bool `yaml:"skip-mime-scan"`
} `yaml:"message"` } `yaml:"message"`
Output struct { Output struct {
LogLevel string `yaml:"log-level"` LogLevel string `yaml:"log-level"`
Debug bool `yaml:"debug"` LogAging int `yaml:"log-aging"`
LogForceNew bool `yaml:"log-force-new"`
Debug bool `yaml:"debug"`
} `yaml:"output"` } `yaml:"output"`
Servers []map[string]yaml.Node `yaml:"servers"` Servers []map[string]yaml.Node `yaml:"servers"`
@ -77,11 +83,15 @@ type MiddleWares struct {
// HTTPServer HTTP通信相关配置 // HTTPServer HTTP通信相关配置
type HTTPServer struct { type HTTPServer struct {
Disabled bool `yaml:"disabled"` Disabled bool `yaml:"disabled"`
Host string `yaml:"host"` Host string `yaml:"host"`
Port int `yaml:"port"` Port int `yaml:"port"`
Timeout int32 `yaml:"timeout"` Timeout int32 `yaml:"timeout"`
Post []struct { LongPolling struct {
Enabled bool `yaml:"enabled"`
MaxQueueSize int `yaml:"max-queue-size"`
} `yaml:"long-polling"`
Post []struct {
URL string `yaml:"url"` URL string `yaml:"url"`
Secret string `yaml:"secret"` Secret string `yaml:"secret"`
} }
@ -116,6 +126,14 @@ type WebsocketReverse struct {
MiddleWares `yaml:"middlewares"` MiddleWares `yaml:"middlewares"`
} }
// LambdaServer 云函数配置
type LambdaServer struct {
Disabled bool `yaml:"disabled"`
Type string `yaml:"type"`
MiddleWares `yaml:"middlewares"`
}
// LevelDBConfig leveldb 相关配置 // LevelDBConfig leveldb 相关配置
type LevelDBConfig struct { type LevelDBConfig struct {
Enable bool `yaml:"enable"` Enable bool `yaml:"enable"`
@ -129,15 +147,89 @@ var (
// Get 从默认配置文件路径中获取 // Get 从默认配置文件路径中获取
func Get() *Config { func Get() *Config {
once.Do(func() { once.Do(func() {
hasEnvironmentConf := os.Getenv("GCQ_UIN") != ""
file, err := os.Open(DefaultConfigFile) file, err := os.Open(DefaultConfigFile)
if err != nil { config = &Config{}
if err == nil {
defer func() { _ = file.Close() }()
if err = yaml.NewDecoder(file).Decode(config); err != nil && !hasEnvironmentConf {
log.Fatal("配置文件不合法!", err)
}
} else if !hasEnvironmentConf {
generateConfig() generateConfig()
os.Exit(0) os.Exit(0)
} }
defer func() { _ = file.Close() }()
config = &Config{} // type convert tools
if err = yaml.NewDecoder(file).Decode(config); err != nil { toInt64 := func(str string) int64 {
log.Fatal("配置文件不合法!", err) i, _ := strconv.ParseInt(str, 10, 64)
return i
}
// load config from environment variable
global.SetAtDefault(&config.Account.Uin, toInt64(os.Getenv("GCQ_UIN")), int64(0))
global.SetAtDefault(&config.Account.Password, os.Getenv("GCQ_PWD"), "")
global.SetAtDefault(&config.Account.Status, int32(toInt64(os.Getenv("GCQ_STATUS"))), int32(0))
global.SetAtDefault(&config.Account.ReLogin.Disabled, !global.EnsureBool(os.Getenv("GCQ_RELOGIN"), false), false)
global.SetAtDefault(&config.Account.ReLogin.Delay, uint(toInt64(os.Getenv("GCQ_RELOGIN_DELAY"))), uint(0))
global.SetAtDefault(&config.Account.ReLogin.MaxTimes, uint(toInt64(os.Getenv("GCQ_RELOGIN_MAX_TIMES"))), uint(0))
dbConf := &LevelDBConfig{Enable: global.EnsureBool(os.Getenv("GCQ_LEVELDB"), true)}
config.Database["leveldb"] = func() yaml.Node {
n := &yaml.Node{}
_ = n.Encode(dbConf)
return *n
}()
accessTokenEnv := os.Getenv("GCQ_ACCESS_TOKEN")
if os.Getenv("GCQ_HTTP_PORT") != "" {
node := &yaml.Node{}
httpConf := &HTTPServer{
Host: "0.0.0.0",
Port: 5700,
MiddleWares: MiddleWares{
AccessToken: accessTokenEnv,
},
}
global.SetExcludeDefault(&httpConf.Disabled, global.EnsureBool(os.Getenv("GCQ_HTTP_DISABLE"), false), false)
global.SetExcludeDefault(&httpConf.Host, os.Getenv("GCQ_HTTP_HOST"), "")
global.SetExcludeDefault(&httpConf.Port, int(toInt64(os.Getenv("GCQ_HTTP_PORT"))), 0)
if os.Getenv("GCQ_HTTP_POST_URL") != "" {
httpConf.Post = append(httpConf.Post, struct {
URL string `yaml:"url"`
Secret string `yaml:"secret"`
}{os.Getenv("GCQ_HTTP_POST_URL"), os.Getenv("GCQ_HTTP_POST_SECRET")})
}
_ = node.Encode(httpConf)
config.Servers = append(config.Servers, map[string]yaml.Node{"http": *node})
}
if os.Getenv("GCQ_WS_PORT") != "" {
node := &yaml.Node{}
wsServerConf := &WebsocketServer{
Host: "0.0.0.0",
Port: 6700,
MiddleWares: MiddleWares{
AccessToken: accessTokenEnv,
},
}
global.SetExcludeDefault(&wsServerConf.Disabled, global.EnsureBool(os.Getenv("GCQ_WS_DISABLE"), false), false)
global.SetExcludeDefault(&wsServerConf.Host, os.Getenv("GCQ_WS_HOST"), "")
global.SetExcludeDefault(&wsServerConf.Port, int(toInt64(os.Getenv("GCQ_WS_PORT"))), 0)
_ = node.Encode(wsServerConf)
config.Servers = append(config.Servers, map[string]yaml.Node{"ws": *node})
}
if os.Getenv("GCQ_RWS_API") != "" || os.Getenv("GCQ_RWS_EVENT") != "" || os.Getenv("GCQ_RWS_UNIVERSAL") != "" {
node := &yaml.Node{}
rwsConf := &WebsocketReverse{
MiddleWares: MiddleWares{
AccessToken: accessTokenEnv,
},
}
global.SetExcludeDefault(&rwsConf.Disabled, global.EnsureBool(os.Getenv("GCQ_RWS_DISABLE"), false), false)
global.SetExcludeDefault(&rwsConf.API, os.Getenv("GCQ_RWS_API"), "")
global.SetExcludeDefault(&rwsConf.Event, os.Getenv("GCQ_RWS_EVENT"), "")
global.SetExcludeDefault(&rwsConf.Universal, os.Getenv("GCQ_RWS_UNIVERSAL"), "")
_ = node.Encode(rwsConf)
config.Servers = append(config.Servers, map[string]yaml.Node{"ws-reverse": *node})
} }
}) })
return config return config
@ -162,6 +254,7 @@ func generateConfig() {
> 2: 正向 Websocket 通信 > 2: 正向 Websocket 通信
> 3: 反向 Websocket 通信 > 3: 反向 Websocket 通信
> 4: pprof 性能分析服务器 > 4: pprof 性能分析服务器
> 5: 云函数服务
请输入你需要的编号,可输入多个,同一编号也可输入多个(如: 233) 请输入你需要的编号,可输入多个,同一编号也可输入多个(如: 233)
您的选择是:`) 您的选择是:`)
input := bufio.NewReader(os.Stdin) input := bufio.NewReader(os.Stdin)
@ -179,6 +272,8 @@ func generateConfig() {
sb.WriteString(wsReverseDefault) sb.WriteString(wsReverseDefault)
case '4': case '4':
sb.WriteString(pprofDefault) sb.WriteString(pprofDefault)
case '5':
sb.WriteString(lambdaDefault)
} }
} }
_ = os.WriteFile("config.yml", []byte(sb.String()), 0o644) _ = os.WriteFile("config.yml", []byte(sb.String()), 0o644)
@ -195,6 +290,12 @@ const httpDefault = ` # HTTP 通信设置
# 反向HTTP超时时间, 单位秒 # 反向HTTP超时时间, 单位秒
# 最小值为5小于5将会忽略本项设置 # 最小值为5小于5将会忽略本项设置
timeout: 5 timeout: 5
# 长轮询拓展
long-polling:
# 是否开启
enabled: false
# 消息队列大小0 表示不限制队列大小,谨慎使用
max-queue-size: 2000
middlewares: middlewares:
<<: *default # 引用默认中间件 <<: *default # 引用默认中间件
# 反向HTTP POST地址列表 # 反向HTTP POST地址列表
@ -205,6 +306,13 @@ const httpDefault = ` # HTTP 通信设置
# secret: '' # 密钥 # secret: '' # 密钥
` `
const lambdaDefault = ` # LambdaServer 配置
- lambda:
type: scf # scf: 腾讯云函数 aws: aws Lambda
middlewares:
<<: *default # 引用默认中间件
`
const wsDefault = ` # 正向WS设置 const wsDefault = ` # 正向WS设置
- ws: - ws:
# 正向WS服务器监听地址 # 正向WS服务器监听地址

View File

@ -39,10 +39,16 @@ message:
remove-reply-at: false remove-reply-at: false
# 为Reply附加更多信息 # 为Reply附加更多信息
extra-reply-data: false extra-reply-data: false
# 跳过 Mime 扫描, 忽略错误数据
skip-mime-scan: false
output: output:
# 日志等级 trace,debug,info,warn,error # 日志等级 trace,debug,info,warn,error
log-level: warn log-level: warn
# 日志时效 单位天. 超过这个时间之前的日志将会被自动删除. 设置为 0 表示永久保留.
log-aging: 15
# 是否在每次启动时强制创建全新的文件储存日志. 为 false 的情况下将会在上次启动时创建的日志文件续写
log-force-new: true
# 是否启用 DEBUG # 是否启用 DEBUG
debug: false # 开启调试模式 debug: false # 开启调试模式

View File

@ -17,35 +17,33 @@ type operationNode struct {
filter Filter filter Filter
} }
// NotOperator 定义了过滤器中Not操作符 // notOperator 定义了过滤器中Not操作符
type NotOperator struct { type notOperator struct {
operand Filter operand Filter
} }
func notOperatorConstruct(argument gjson.Result) *NotOperator { func newNotOp(argument gjson.Result) Filter {
if !argument.IsObject() { if !argument.IsObject() {
panic("the argument of 'not' operator must be an object") panic("the argument of 'not' operator must be an object")
} }
op := new(NotOperator) return &notOperator{operand: Generate("and", argument)}
op.operand = Generate("and", argument)
return op
} }
// Eval 对payload执行Not过滤 // Eval 对payload执行Not过滤
func (op *NotOperator) Eval(payload gjson.Result) bool { func (op *notOperator) Eval(payload gjson.Result) bool {
return !op.operand.Eval(payload) return !op.operand.Eval(payload)
} }
// AndOperator 定义了过滤器中And操作符 // andOperator 定义了过滤器中And操作符
type AndOperator struct { type andOperator struct {
operands []operationNode operands []operationNode
} }
func andOperatorConstruct(argument gjson.Result) *AndOperator { func newAndOp(argument gjson.Result) Filter {
if !argument.IsObject() { if !argument.IsObject() {
panic("the argument of 'and' operator must be an object") panic("the argument of 'and' operator must be an object")
} }
op := new(AndOperator) op := new(andOperator)
argument.ForEach(func(key, value gjson.Result) bool { argument.ForEach(func(key, value gjson.Result) bool {
switch { switch {
case key.Str[0] == '.': case key.Str[0] == '.':
@ -74,7 +72,7 @@ func andOperatorConstruct(argument gjson.Result) *AndOperator {
} }
// Eval 对payload执行And过滤 // Eval 对payload执行And过滤
func (op *AndOperator) Eval(payload gjson.Result) bool { func (op *andOperator) Eval(payload gjson.Result) bool {
res := true res := true
for _, operand := range op.operands { for _, operand := range op.operands {
if len(operand.key) == 0 { if len(operand.key) == 0 {
@ -93,16 +91,16 @@ func (op *AndOperator) Eval(payload gjson.Result) bool {
return res return res
} }
// OrOperator 定义了过滤器中Or操作符 // orOperator 定义了过滤器中Or操作符
type OrOperator struct { type orOperator struct {
operands []Filter operands []Filter
} }
func orOperatorConstruct(argument gjson.Result) *OrOperator { func newOrOp(argument gjson.Result) Filter {
if !argument.IsArray() { if !argument.IsArray() {
panic("the argument of 'or' operator must be an array") panic("the argument of 'or' operator must be an array")
} }
op := new(OrOperator) op := new(orOperator)
argument.ForEach(func(_, value gjson.Result) bool { argument.ForEach(func(_, value gjson.Result) bool {
op.operands = append(op.operands, Generate("and", value)) op.operands = append(op.operands, Generate("and", value))
return true return true
@ -111,7 +109,7 @@ func orOperatorConstruct(argument gjson.Result) *OrOperator {
} }
// Eval 对payload执行Or过滤 // Eval 对payload执行Or过滤
func (op *OrOperator) Eval(payload gjson.Result) bool { func (op *orOperator) Eval(payload gjson.Result) bool {
res := false res := false
for _, operand := range op.operands { for _, operand := range op.operands {
res = res || operand.Eval(payload) res = res || operand.Eval(payload)
@ -122,49 +120,45 @@ func (op *OrOperator) Eval(payload gjson.Result) bool {
return res return res
} }
// EqualOperator 定义了过滤器中Equal操作符 // eqOperator 定义了过滤器中Equal操作符
type EqualOperator struct { type eqOperator struct {
operand string operand string
} }
func equalOperatorConstruct(argument gjson.Result) *EqualOperator { func newEqOp(argument gjson.Result) Filter {
op := new(EqualOperator) return &eqOperator{operand: argument.String()}
op.operand = argument.String()
return op
} }
// Eval 对payload执行Equal过滤 // Eval 对payload执行Equal过滤
func (op *EqualOperator) Eval(payload gjson.Result) bool { func (op *eqOperator) Eval(payload gjson.Result) bool {
return payload.String() == op.operand return payload.String() == op.operand
} }
// NotEqualOperator 定义了过滤器中NotEqual操作符 // neqOperator 定义了过滤器中NotEqual操作符
type NotEqualOperator struct { type neqOperator struct {
operand string operand string
} }
func notEqualOperatorConstruct(argument gjson.Result) *NotEqualOperator { func newNeqOp(argument gjson.Result) Filter {
op := new(NotEqualOperator) return &neqOperator{operand: argument.String()}
op.operand = argument.String()
return op
} }
// Eval 对payload执行NotEqual过滤 // Eval 对payload执行NotEqual过滤
func (op *NotEqualOperator) Eval(payload gjson.Result) bool { func (op *neqOperator) Eval(payload gjson.Result) bool {
return !(payload.String() == op.operand) return !(payload.String() == op.operand)
} }
// InOperator 定义了过滤器中In操作符 // inOperator 定义了过滤器中In操作符
type InOperator struct { type inOperator struct {
operandString string operandString string
operandArray []string operandArray []string
} }
func inOperatorConstruct(argument gjson.Result) *InOperator { func newInOp(argument gjson.Result) Filter {
if argument.IsObject() { if argument.IsObject() {
panic("the argument of 'in' operator must be an array or a string") panic("the argument of 'in' operator must be an array or a string")
} }
op := new(InOperator) op := new(inOperator)
if argument.IsArray() { if argument.IsArray() {
op.operandArray = []string{} op.operandArray = []string{}
argument.ForEach(func(_, value gjson.Result) bool { argument.ForEach(func(_, value gjson.Result) bool {
@ -178,7 +172,7 @@ func inOperatorConstruct(argument gjson.Result) *InOperator {
} }
// Eval 对payload执行In过滤 // Eval 对payload执行In过滤
func (op *InOperator) Eval(payload gjson.Result) bool { func (op *inOperator) Eval(payload gjson.Result) bool {
payloadStr := payload.String() payloadStr := payload.String()
if op.operandArray != nil { if op.operandArray != nil {
for _, value := range op.operandArray { for _, value := range op.operandArray {
@ -191,64 +185,59 @@ func (op *InOperator) Eval(payload gjson.Result) bool {
return strings.Contains(op.operandString, payloadStr) return strings.Contains(op.operandString, payloadStr)
} }
// ContainsOperator 定义了过滤器中Contains操作符 // containsOperator 定义了过滤器中Contains操作符
type ContainsOperator struct { type containsOperator struct {
operand string operand string
} }
func containsOperatorConstruct(argument gjson.Result) *ContainsOperator { func newContainOp(argument gjson.Result) Filter {
if argument.IsArray() || argument.IsObject() { if argument.IsArray() || argument.IsObject() {
panic("the argument of 'contains' operator must be a string") panic("the argument of 'contains' operator must be a string")
} }
op := new(ContainsOperator) return &containsOperator{operand: argument.String()}
op.operand = argument.String()
return op
} }
// Eval 对payload执行Contains过滤 // Eval 对payload执行Contains过滤
func (op *ContainsOperator) Eval(payload gjson.Result) bool { func (op *containsOperator) Eval(payload gjson.Result) bool {
return strings.Contains(payload.String(), op.operand) return strings.Contains(payload.String(), op.operand)
} }
// RegexOperator 定义了过滤器中Regex操作符 // regexOperator 定义了过滤器中Regex操作符
type RegexOperator struct { type regexOperator struct {
regex *regexp.Regexp regex *regexp.Regexp
} }
func regexOperatorConstruct(argument gjson.Result) *RegexOperator { func newRegexOp(argument gjson.Result) Filter {
if argument.IsArray() || argument.IsObject() { if argument.IsArray() || argument.IsObject() {
panic("the argument of 'regex' operator must be a string") panic("the argument of 'regex' operator must be a string")
} }
op := new(RegexOperator) return &regexOperator{regex: regexp.MustCompile(argument.String())}
op.regex = regexp.MustCompile(argument.String())
return op
} }
// Eval 对payload执行RegexO过滤 // Eval 对payload执行RegexO过滤
func (op *RegexOperator) Eval(payload gjson.Result) bool { func (op *regexOperator) Eval(payload gjson.Result) bool {
matched := op.regex.MatchString(payload.String()) return op.regex.MatchString(payload.String())
return matched
} }
// Generate 根据给定操作符名opName及操作符参数argument创建一个过滤器实例 // Generate 根据给定操作符名opName及操作符参数argument创建一个过滤器实例
func Generate(opName string, argument gjson.Result) Filter { func Generate(opName string, argument gjson.Result) Filter {
switch opName { switch opName {
case "not": case "not":
return notOperatorConstruct(argument) return newNotOp(argument)
case "and": case "and":
return andOperatorConstruct(argument) return newAndOp(argument)
case "or": case "or":
return orOperatorConstruct(argument) return newOrOp(argument)
case "neq":
return notEqualOperatorConstruct(argument)
case "eq": case "eq":
return equalOperatorConstruct(argument) return newEqOp(argument)
case "neq":
return newNeqOp(argument)
case "in": case "in":
return inOperatorConstruct(argument) return newInOp(argument)
case "contains": case "contains":
return containsOperatorConstruct(argument) return newContainOp(argument)
case "regex": case "regex":
return regexOperatorConstruct(argument) return newRegexOp(argument)
default: default:
panic("the operator " + opName + " is not supported") panic("the operator " + opName + " is not supported")
} }

View File

@ -6,7 +6,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"errors" "errors"
"io/ioutil"
"net" "net"
"net/url" "net/url"
"os" "os"
@ -51,7 +50,7 @@ func PathExists(path string) bool {
// ReadAllText 读取给定path对应文件无法读取时返回空值 // ReadAllText 读取给定path对应文件无法读取时返回空值
func ReadAllText(path string) string { func ReadAllText(path string) string {
b, err := ioutil.ReadFile(path) b, err := os.ReadFile(path)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return "" return ""
@ -61,7 +60,7 @@ func ReadAllText(path string) string {
// WriteAllText 将给定text写入给定path // WriteAllText 将给定text写入给定path
func WriteAllText(path, text string) error { func WriteAllText(path, text string) error {
return ioutil.WriteFile(path, utils.S2B(text), 0o644) return os.WriteFile(path, utils.S2B(text), 0o644)
} }
// Check 检测err是否为nil // Check 检测err是否为nil
@ -91,10 +90,10 @@ func FindFile(file, cache, p string) (data []byte, err error) {
hash := md5.Sum([]byte(file)) hash := md5.Sum([]byte(file))
cacheFile := path.Join(CachePath, hex.EncodeToString(hash[:])+".cache") cacheFile := path.Join(CachePath, hex.EncodeToString(hash[:])+".cache")
if PathExists(cacheFile) && cache == "1" { if PathExists(cacheFile) && cache == "1" {
return ioutil.ReadFile(cacheFile) return os.ReadFile(cacheFile)
} }
data, err = GetBytes(file) data, err = GetBytes(file)
_ = ioutil.WriteFile(cacheFile, data, 0o644) _ = os.WriteFile(cacheFile, data, 0o644)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -112,12 +111,12 @@ func FindFile(file, cache, p string) (data []byte, err error) {
if strings.HasPrefix(fu.Path, "/") && runtime.GOOS == `windows` { if strings.HasPrefix(fu.Path, "/") && runtime.GOOS == `windows` {
fu.Path = fu.Path[1:] fu.Path = fu.Path[1:]
} }
data, err = ioutil.ReadFile(fu.Path) data, err = os.ReadFile(fu.Path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
case PathExists(path.Join(p, file)): case PathExists(path.Join(p, file)):
data, err = ioutil.ReadFile(path.Join(p, file)) data, err = os.ReadFile(path.Join(p, file))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -140,7 +139,7 @@ func DelFile(path string) bool {
// ReadAddrFile 从给定path中读取合法的IP地址与端口,每个IP地址以换行符"\n"作为分隔 // ReadAddrFile 从给定path中读取合法的IP地址与端口,每个IP地址以换行符"\n"作为分隔
func ReadAddrFile(path string) []*net.TCPAddr { func ReadAddrFile(path string) []*net.TCPAddr {
d, err := ioutil.ReadFile(path) d, err := os.ReadFile(path)
if err != nil { if err != nil {
return nil return nil
} }

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings"
"sync" "sync"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -174,3 +175,21 @@ func GetLogLevel(level string) []logrus.Level {
} }
} }
} }
// LogFormat specialize for go-cqhttp
type LogFormat struct{}
// Format implements logrus.Formatter
func (f LogFormat) Format(entry *logrus.Entry) ([]byte, error) {
buf := NewBuffer()
defer PutBuffer(buf)
buf.WriteByte('[')
buf.WriteString(entry.Time.Format("2006-01-02 15:04:05"))
buf.WriteString("] [")
buf.WriteString(strings.ToUpper(entry.Level.String()))
buf.WriteString("]: ")
buf.WriteString(entry.Message)
buf.WriteString(" \n")
ret := append([]byte(nil), buf.Bytes()...) // copy buffer
return ret, nil
}

View File

@ -2,6 +2,7 @@ package global
import ( import (
"math" "math"
"reflect"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -11,18 +12,6 @@ import (
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
) )
var trueSet = map[string]struct{}{
"true": {},
"yes": {},
"1": {},
}
var falseSet = map[string]struct{}{
"false": {},
"no": {},
"0": {},
}
// EnsureBool 判断给定的p是否可表示为合法Bool类型,否则返回defaultVal // EnsureBool 判断给定的p是否可表示为合法Bool类型,否则返回defaultVal
// //
// 支持的合法类型有 // 支持的合法类型有
@ -41,27 +30,28 @@ func EnsureBool(p interface{}, defaultVal bool) bool {
if !j.Exists() { if !j.Exists() {
return defaultVal return defaultVal
} }
if j.Type == gjson.True { switch j.Type { // nolint
case gjson.True:
return true return true
} case gjson.False:
if j.Type == gjson.False {
return false return false
} case gjson.String:
if j.Type != gjson.String { str = j.Str
default:
return defaultVal return defaultVal
} }
str = j.Str
} else if s, ok := p.(string); ok { } else if s, ok := p.(string); ok {
str = s str = s
} }
str = strings.ToLower(str) str = strings.ToLower(str)
if _, ok := trueSet[str]; ok { switch str {
case "true", "yes", "1":
return true return true
} case "false", "no", "0":
if _, ok := falseSet[str]; ok {
return false return false
default:
return defaultVal
} }
return defaultVal
} }
// VersionNameCompare 检查版本名是否需要更新, 仅适用于 go-cqhttp 的版本命名规则 // VersionNameCompare 检查版本名是否需要更新, 仅适用于 go-cqhttp 的版本命名规则
@ -99,6 +89,40 @@ func VersionNameCompare(current, remote string) bool {
return cur[4] < re[4] return cur[4] < re[4]
} }
// SetAtDefault 在变量 variable 为默认值 defaultValue 的时候修改为 value
func SetAtDefault(variable, value, defaultValue interface{}) {
v := reflect.ValueOf(variable)
v2 := reflect.ValueOf(value)
if v.Kind() != reflect.Ptr || v.IsNil() {
return
}
v = v.Elem()
if v.Interface() != defaultValue {
return
}
if v.Kind() != v2.Kind() {
return
}
v.Set(v2)
}
// SetExcludeDefault 在目标值 value 不为默认值 defaultValue 时修改 variable 为 value
func SetExcludeDefault(variable, value, defaultValue interface{}) {
v := reflect.ValueOf(variable)
v2 := reflect.ValueOf(value)
if v.Kind() != reflect.Ptr || v.IsNil() {
return
}
v = v.Elem()
if reflect.Indirect(v2).Interface() != defaultValue {
return
}
if v.Kind() != v2.Kind() {
return
}
v.Set(v2)
}
var ( var (
// once lazy compile the reg // once lazy compile the reg
once sync.Once once sync.Once

52
global/signal.go Normal file
View File

@ -0,0 +1,52 @@
package global
import (
"fmt"
"os"
"path/filepath"
"runtime"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
var (
mainStopCh chan struct{}
mainOnce sync.Once
dumpMutex sync.Mutex
)
func dumpStack() {
dumpMutex.Lock()
defer dumpMutex.Unlock()
log.Info("开始 dump 当前 goroutine stack 信息")
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
buf = buf[:n]
break
}
buf = make([]byte, 2*len(buf))
}
fileName := fmt.Sprintf("%s.%d.stacks.%d.log", filepath.Base(os.Args[0]), os.Getpid(), time.Now().Unix())
fd, err := os.Create(fileName)
if err != nil {
log.Errorf("保存 stackdump 到文件时出现错误: %v", err)
log.Warnf("无法保存 stackdump. 将直接打印\n %s", buf)
return
}
defer fd.Close()
_, err = fd.Write(buf)
if err != nil {
log.Errorf("写入 stackdump 失败: %v", err)
log.Warnf("无法保存 stackdump. 将直接打印\n %s", buf)
return
}
log.Infof("stackdump 已保存至 %s", fileName)
}

34
global/signal_unix.go Normal file
View File

@ -0,0 +1,34 @@
//go:build !windows
// +build !windows
package global
import (
"os"
"os/signal"
"sync"
"syscall"
)
// SetupMainSignalHandler is for main to use at last
func SetupMainSignalHandler() <-chan struct{} {
mainOnce.Do(func() {
mainStopCh = make(chan struct{})
mc := make(chan os.Signal, 3)
closeOnce := sync.Once{}
signal.Notify(mc, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1)
go func() {
for {
switch <-mc {
case os.Interrupt, syscall.SIGTERM:
closeOnce.Do(func() {
close(mainStopCh)
})
case syscall.SIGUSR1:
dumpStack()
}
}
}()
})
return mainStopCh
}

91
global/signal_windows.go Normal file
View File

@ -0,0 +1,91 @@
//go:build windows
// +build windows
package global
import (
"errors"
"fmt"
"net"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/Microsoft/go-winio"
log "github.com/sirupsen/logrus"
)
var (
validTasks = map[string]func(){
"dumpstack": dumpStack,
}
)
// SetupMainSignalHandler is for main to use at last
func SetupMainSignalHandler() <-chan struct{} {
mainOnce.Do(func() {
// for stack trace collecting on windows
pipeName := fmt.Sprintf(`\\.\pipe\go-cqhttp-%d`, os.Getpid())
pipe, err := winio.ListenPipe(pipeName, &winio.PipeConfig{})
if err != nil {
log.Errorf("创建 named pipe 失败. 将无法使用 dumpstack 功能: %v", err)
} else {
maxTaskLen := 0
for t := range validTasks {
if l := len(t); l > maxTaskLen {
maxTaskLen = l
}
}
go func() {
for {
c, err := pipe.Accept()
if err != nil {
if errors.Is(err, net.ErrClosed) || strings.Contains(err.Error(), "closed") {
return
}
log.Errorf("accept named pipe 失败: %v", err)
continue
}
go func() {
defer c.Close()
_ = c.SetReadDeadline(time.Now().Add(5 * time.Second))
buf := make([]byte, maxTaskLen)
n, err := c.Read(buf)
if err != nil {
log.Errorf("读取 named pipe 失败: %v", err)
return
}
cmd := string(buf[:n])
if task, ok := validTasks[cmd]; ok {
task()
return
}
log.Warnf("named pipe 读取到未知指令: %q", cmd)
}()
}
}()
}
// setup the main stop channel
mainStopCh = make(chan struct{})
mc := make(chan os.Signal, 2)
closeOnce := sync.Once{}
signal.Notify(mc, os.Interrupt, syscall.SIGTERM)
go func() {
for {
switch <-mc {
case os.Interrupt, syscall.SIGTERM:
closeOnce.Do(func() {
close(mainStopCh)
if pipe != nil {
_ = pipe.Close()
}
})
}
}
}()
})
return mainStopCh
}

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows // +build !windows
package terminal package terminal

View File

@ -1,3 +1,4 @@
//go:build windows
// +build windows // +build windows
package terminal package terminal

View File

@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"hash" "hash"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -42,7 +41,7 @@ func FromStream(updateWith io.Reader) (err error, errRecover error) {
// no patch to apply, go on through // no patch to apply, go on through
bufBytes := bufio.NewReader(updateWith) bufBytes := bufio.NewReader(updateWith)
updateWith = io.Reader(bufBytes) updateWith = io.Reader(bufBytes)
newBytes, err = ioutil.ReadAll(updateWith) newBytes, err = io.ReadAll(updateWith)
if err != nil { if err != nil {
return return
} }

View File

@ -1,3 +1,4 @@
//go:build !windows
// +build !windows // +build !windows
package update package update

24
go.mod
View File

@ -4,14 +4,14 @@ go 1.16
require ( require (
github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f
github.com/Mrs4s/MiraiGo v0.0.0-20210525010101-8f0cd9494d64 github.com/Microsoft/go-winio v0.5.0
github.com/Mrs4s/MiraiGo v0.0.0-20210810070836-6614d2383adb
github.com/dustin/go-humanize v1.0.0 github.com/dustin/go-humanize v1.0.0
github.com/gin-contrib/pprof v1.3.0 github.com/gabriel-vasile/mimetype v1.3.1
github.com/gin-gonic/gin v1.7.1
github.com/gorilla/websocket v1.4.2 github.com/gorilla/websocket v1.4.2
github.com/guonaihong/gout v0.1.9 github.com/guonaihong/gout v0.2.1
github.com/jonboulle/clockwork v0.2.2 // indirect github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/json-iterator/go v1.1.10 github.com/json-iterator/go v1.1.11
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/lestrrat-go/strftime v1.0.4 // indirect github.com/lestrrat-go/strftime v1.0.4 // indirect
@ -21,13 +21,15 @@ require (
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
github.com/stretchr/testify v1.7.0 github.com/stretchr/testify v1.7.0
github.com/syndtr/goleveldb v1.0.0 github.com/syndtr/goleveldb v1.0.0
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 github.com/tidwall/gjson v1.8.1
github.com/tidwall/gjson v1.7.5
github.com/tuotoo/qrcode v0.0.0-20190222102259-ac9c44189bf2 github.com/tuotoo/qrcode v0.0.0-20190222102259-ac9c44189bf2
github.com/wdvxdr1123/go-silk v0.0.0-20210316130616-d47b553def60 github.com/wdvxdr1123/go-silk v0.0.0-20210316130616-d47b553def60
github.com/willf/bitset v1.1.11 // indirect github.com/willf/bitset v1.2.0 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b
golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
) )
replace github.com/willf/bitset v1.2.0 => github.com/bits-and-blooms/bitset v1.2.0

72
go.sum
View File

@ -2,8 +2,12 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f h1:2dk3eOnYllh+wUOuDhOoC2vUVoJF/5z478ryJ+wzEII= github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f h1:2dk3eOnYllh+wUOuDhOoC2vUVoJF/5z478ryJ+wzEII=
github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f/go.mod h1:4a58ifQTEe2uwwsaqbh3i2un5/CBPg+At/qHpt18Tmk= github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f/go.mod h1:4a58ifQTEe2uwwsaqbh3i2un5/CBPg+At/qHpt18Tmk=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Mrs4s/MiraiGo v0.0.0-20210525010101-8f0cd9494d64 h1:36RqrFJS7TCicTLc995qgJI5NDFy93YR/zrqn4GI6nQ= github.com/Microsoft/go-winio v0.5.0 h1:Elr9Wn+sGKPlkaBvwu4mTrxtmOp3F3yV9qhaHbXGjwU=
github.com/Mrs4s/MiraiGo v0.0.0-20210525010101-8f0cd9494d64/go.mod h1:NjiWhlvGxwv1ftOWIoiFa/OzklnAYI4YqNexFOKSZKw= github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/Mrs4s/MiraiGo v0.0.0-20210810070836-6614d2383adb h1:agCxYd/ZemDwrEYKpnpKqA0cubxfpkQ/b+GpIlIJK0U=
github.com/Mrs4s/MiraiGo v0.0.0-20210810070836-6614d2383adb/go.mod h1:5V3f/+mTYtrI/+hLqbdzZQXuLMl2RyLfx0XYYjCQ90Q=
github.com/bits-and-blooms/bitset v1.2.0 h1:Kn4yilvwNtMACtf1eYDlG8H77R07mZSPbMjLyS07ChA=
github.com/bits-and-blooms/bitset v1.2.0/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -14,23 +18,19 @@ github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25Kn
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gin-contrib/pprof v1.3.0 h1:G9eK6HnbkSqDZBYbzG4wrjCsA4e+cvYAHUZw6W+W9K0= github.com/gabriel-vasile/mimetype v1.3.1 h1:qevA6c2MtE1RorlScnixeG0VA1H4xrXyhyX3oWBynNQ=
github.com/gin-contrib/pprof v1.3.0/go.mod h1:waMjT1H9b179t3CxuG1cV3DHpga6ybizwfBaM5OXaB0= github.com/gabriel-vasile/mimetype v1.3.1/go.mod h1:fA8fi6KUiG7MgQQ+mEWotXoEOvmxRtOJlERCzSmRvr8=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.6.0 h1:Lb3veSYoGaNck69fV2+Vf2juLSsHpMTf3Vk5+X+EDJg=
github.com/gin-gonic/gin v1.6.0/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= github.com/gin-gonic/gin v1.6.0/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/gin-gonic/gin v1.6.2/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/gin-gonic/gin v1.7.1 h1:qC89GU3p8TvKWMAVhEpmpB2CIb1hnqt2UdKZaP93mS8=
github.com/gin-gonic/gin v1.7.1/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY=
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
@ -42,7 +42,6 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4= github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w=
@ -60,18 +59,18 @@ github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/guonaihong/gout v0.1.9 h1:splZ4PPe5Ju40akL29drcTmG75TLtzqVlBv7GeoCzH8= github.com/guonaihong/gout v0.2.1 h1:XnaS92y/Mpcu4MOQU5cx4hTKhbqFznbIE/uUI5sMY9E=
github.com/guonaihong/gout v0.1.9/go.mod h1:JkjNv1G2oRWvFgP/r4DUbYhoeIBB0zMP2j1ID+5CYpU= github.com/guonaihong/gout v0.2.1/go.mod h1:JkjNv1G2oRWvFgP/r4DUbYhoeIBB0zMP2j1ID+5CYpU=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ= github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ=
github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8= github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA= github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8= github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8= github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8=
@ -104,13 +103,12 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
@ -118,10 +116,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE= github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 h1:J6v8awz+me+xeb/cUTotKgceAYouhIB3pjzgRd6IlGk= github.com/tidwall/gjson v1.8.1 h1:8j5EE9Hrh3l9Od1OIEDAb7IpezNA20UdRngNAj5N0WU=
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA= github.com/tidwall/gjson v1.8.1/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/gjson v1.7.5 h1:zmAN/xmX7OtpAkv4Ovfso60r/BiCi5IErCDYGNJu+uc=
github.com/tidwall/gjson v1.7.5/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE= github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8= github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=
@ -134,13 +130,12 @@ github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
github.com/wdvxdr1123/go-silk v0.0.0-20210316130616-d47b553def60 h1:lRKf10iIOW0VsH5WDF621ihzR+R2wEBZVtNRHuLLCb4= github.com/wdvxdr1123/go-silk v0.0.0-20210316130616-d47b553def60 h1:lRKf10iIOW0VsH5WDF621ihzR+R2wEBZVtNRHuLLCb4=
github.com/wdvxdr1123/go-silk v0.0.0-20210316130616-d47b553def60/go.mod h1:ecFKZPX81BaB70I6ruUgEwYcDOtuNgJGnjdK+MIl5ko= github.com/wdvxdr1123/go-silk v0.0.0-20210316130616-d47b553def60/go.mod h1:ecFKZPX81BaB70I6ruUgEwYcDOtuNgJGnjdK+MIl5ko=
github.com/willf/bitset v1.1.11 h1:N7Z7E9UvjW+sGsEl7k/SJrvY2reP1A07MrGuCjIOjRE=
github.com/willf/bitset v1.1.11/go.mod h1:83CECat5yLh5zVOf4P1ErAgKA5UDvKtgyUABdr3+MjI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d h1:RNPAfi2nHY7C2srAV8A49jpsYr0ADedCk1wq6fTMTvs=
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
@ -149,34 +144,38 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125 h1:Ugb8sMTWuWRC3+sz5WeN/4kejDx9BvIwnPUiJBjJE+8=
golang.org/x/net v0.0.0-20210505024714-0287a6fb4125/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201126233918-771906719818 h1:f1CIuDlJhwANEC2MM87MBEVMr3jl5bifgsfj90XAF9c=
golang.org/x/sys v0.0.0-20201126233918-771906719818/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201126233918-771906719818/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed h1:Ei4bQjjpYUsS4efOUz+5Nz++IVkHk87n2zBA0NxBWc0= golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba h1:O8mE0/t419eoIwhTFpKVkHiTs/Igowgfkj25AcZrtiE= golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6 h1:Vv0JUPWTyeqUq42B2WJ1FeIDjjvGKoA2Ss+Ts0lAVbs=
golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
@ -198,7 +197,6 @@ google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQ
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=

View File

@ -3,7 +3,6 @@ package main
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"io/ioutil"
"os" "os"
"strings" "strings"
"time" "time"
@ -63,9 +62,13 @@ func qrcodeLogin() error {
if err != nil { if err != nil {
return err return err
} }
_ = ioutil.WriteFile("qrcode.png", rsp.ImageData, 0o644) _ = os.WriteFile("qrcode.png", rsp.ImageData, 0o644)
defer func() { _ = os.Remove("qrcode.png") }() defer func() { _ = os.Remove("qrcode.png") }()
log.Infof("请使用手机QQ扫描二维码 (qrcode.png) : ") if cli.Uin != 0 {
log.Infof("请使用账号 %v 登录手机QQ扫描二维码 (qrcode.png) : ", cli.Uin)
} else {
log.Infof("请使用手机QQ扫描二维码 (qrcode.png) : ")
}
time.Sleep(time.Second) time.Sleep(time.Second)
qrcodeTerminal.New().Get(fi.Content).Print() qrcodeTerminal.New().Get(fi.Content).Print()
s, err := cli.QueryQRCodeStatus(rsp.Sig) s, err := cli.QueryQRCodeStatus(rsp.Sig)
@ -114,26 +117,14 @@ func loginResponseProcessor(res *client.LoginResponse) error {
var text string var text string
switch res.Error { switch res.Error {
case client.SliderNeededError: case client.SliderNeededError:
log.Warnf("登录需要滑条验证码. ") log.Warnf("登录需要滑条验证码, 请使用手机QQ扫描二维码以继续登录.")
log.Warnf("请参考文档 -> https://docs.go-cqhttp.org/faq/slider.html <- 进行处理")
log.Warnf("1. 自行抓包并获取 Ticket 输入.")
log.Warnf("2. 使用手机QQ扫描二维码登入. (推荐)")
log.Warn("请输入(1 - 2) (将在10秒后自动选择2)")
text = readLineTimeout(time.Second*10, "2")
if strings.Contains(text, "1") {
println()
log.Warnf("请用浏览器打开 -> %v <- 并获取Ticket.", res.VerifyUrl)
println()
log.Warn("请输入Ticket (Enter 提交)")
text = readLine()
res, err = cli.SubmitTicket(text)
continue
}
cli.Disconnect() cli.Disconnect()
cli.Release()
cli = client.NewClientEmpty()
return qrcodeLogin() return qrcodeLogin()
case client.NeedCaptcha: case client.NeedCaptcha:
log.Warnf("登录需要验证码.") log.Warnf("登录需要验证码.")
_ = ioutil.WriteFile("captcha.jpg", res.CaptchaImage, 0o644) _ = os.WriteFile("captcha.jpg", res.CaptchaImage, 0o644)
log.Warnf("请输入验证码 (captcha.jpg) (Enter 提交)") log.Warnf("请输入验证码 (captcha.jpg) (Enter 提交)")
text = readLine() text = readLine()
global.DelFile("captcha.jpg") global.DelFile("captcha.jpg")

206
main.go
View File

@ -8,19 +8,18 @@ import (
"encoding/hex" "encoding/hex"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"path" "path"
"path/filepath"
"runtime" "runtime"
"strings" "strings"
"sync" "sync"
"syscall"
"time" "time"
"github.com/Mrs4s/go-cqhttp/coolq" "github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/global/codec"
"github.com/Mrs4s/go-cqhttp/global/config" "github.com/Mrs4s/go-cqhttp/global/config"
"github.com/Mrs4s/go-cqhttp/global/terminal" "github.com/Mrs4s/go-cqhttp/global/terminal"
"github.com/Mrs4s/go-cqhttp/global/update" "github.com/Mrs4s/go-cqhttp/global/update"
@ -31,7 +30,6 @@ import (
"github.com/guonaihong/gout" "github.com/guonaihong/gout"
rotatelogs "github.com/lestrrat-go/file-rotatelogs" rotatelogs "github.com/lestrrat-go/file-rotatelogs"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
easy "github.com/t-tomalak/logrus-easy-formatter"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"golang.org/x/crypto/pbkdf2" "golang.org/x/crypto/pbkdf2"
"golang.org/x/term" "golang.org/x/term"
@ -40,11 +38,6 @@ import (
var ( var (
conf *config.Config conf *config.Config
isFastStart = false isFastStart = false
c string
d bool
h bool
wd string // reset work dir
// PasswordHash 存储QQ密码哈希供登录使用 // PasswordHash 存储QQ密码哈希供登录使用
PasswordHash [16]byte PasswordHash [16]byte
@ -61,70 +54,65 @@ var (
} }
) )
func init() { func main() {
var debug bool c := flag.String("c", config.DefaultConfigFile, "configuration filename default is config.hjson")
flag.StringVar(&c, "c", config.DefaultConfigFile, "configuration filename default is config.hjson") d := flag.Bool("d", false, "running as a daemon")
flag.BoolVar(&d, "d", false, "running as a daemon") h := flag.Bool("h", false, "this help")
flag.BoolVar(&debug, "D", false, "debug mode") wd := flag.String("w", "", "cover the working directory")
flag.BoolVar(&h, "h", false, "this help") debug := flag.Bool("D", false, "debug mode")
flag.StringVar(&wd, "w", "", "cover the working directory")
flag.Parse() flag.Parse()
// 通过-c 参数替换 配置文件路径 switch {
config.DefaultConfigFile = c case *h:
logFormatter := &easy.Formatter{ help()
TimestampFormat: "2006-01-02 15:04:05", case *d:
LogFormat: "[%time%] [%lvl%]: %msg% \n", server.Daemon()
case *wd != "":
resetWorkDir(*wd)
} }
w, err := rotatelogs.New(path.Join("logs", "%Y-%m-%d.log"), rotatelogs.WithRotationTime(time.Hour*24)) // 通过-c 参数替换 配置文件路径
config.DefaultConfigFile = *c
conf = config.Get()
if *debug {
conf.Output.Debug = true
}
if conf.Output.Debug {
log.SetReportCaller(true)
codec.Debug = true
}
rotateOptions := []rotatelogs.Option{
rotatelogs.WithRotationTime(time.Hour * 24),
}
if conf.Output.LogAging > 0 {
rotateOptions = append(rotateOptions, rotatelogs.WithMaxAge(time.Hour*24*time.Duration(conf.Output.LogAging)))
}
if conf.Output.LogForceNew {
rotateOptions = append(rotateOptions, rotatelogs.ForceNewFile())
}
w, err := rotatelogs.New(path.Join("logs", "%Y-%m-%d.log"), rotateOptions...)
if err != nil { if err != nil {
log.Errorf("rotatelogs init err: %v", err) log.Errorf("rotatelogs init err: %v", err)
panic(err) panic(err)
} }
conf = config.Get() log.AddHook(global.NewLocalHook(w, global.LogFormat{}, global.GetLogLevel(conf.Output.LogLevel)...))
if debug {
conf.Output.Debug = true
}
// 在debug模式下,将在标准输出中打印当前执行行数
if conf.Output.Debug {
log.SetReportCaller(true)
}
log.AddHook(global.NewLocalHook(w, logFormatter, global.GetLogLevel(conf.Output.LogLevel)...))
if !global.PathExists(global.ImagePath) { mkCacheDir := func(path string, _type string) {
if err := os.MkdirAll(global.ImagePath, 0o755); err != nil { if !global.PathExists(path) {
log.Fatalf("创建图片缓存文件夹失败: %v", err) if err := os.MkdirAll(path, 0o755); err != nil {
log.Fatalf("创建%s缓存文件夹失败: %v", _type, err)
}
} }
} }
if !global.PathExists(global.VoicePath) { mkCacheDir(global.ImagePath, "图片")
if err := os.MkdirAll(global.VoicePath, 0o755); err != nil { mkCacheDir(global.VoicePath, "语音")
log.Fatalf("创建语音缓存文件夹失败: %v", err) mkCacheDir(global.VideoPath, "视频")
} mkCacheDir(global.CachePath, "发送图片")
}
if !global.PathExists(global.VideoPath) {
if err := os.MkdirAll(global.VideoPath, 0o755); err != nil {
log.Fatalf("创建视频缓存文件夹失败: %v", err)
}
}
if !global.PathExists(global.CachePath) {
if err := os.MkdirAll(global.CachePath, 0o755); err != nil {
log.Fatalf("创建发送图片缓存文件夹失败: %v", err)
}
}
}
func main() {
if h {
help()
}
if d {
server.Daemon()
}
if wd != "" {
resetWorkDir()
}
var byteKey []byte var byteKey []byte
arg := os.Args arg := os.Args
if len(arg) > 1 { if len(arg) > 1 {
@ -169,7 +157,7 @@ func main() {
if !global.PathExists("device.json") { if !global.PathExists("device.json") {
log.Warn("虚拟设备信息不存在, 将自动生成随机设备.") log.Warn("虚拟设备信息不存在, 将自动生成随机设备.")
client.GenRandomDevice() client.GenRandomDevice()
_ = ioutil.WriteFile("device.json", client.SystemDeviceInfo.ToJson(), 0o644) _ = os.WriteFile("device.json", client.SystemDeviceInfo.ToJson(), 0o644)
log.Info("已生成设备信息并保存到 device.json 文件.") log.Info("已生成设备信息并保存到 device.json 文件.")
} else { } else {
log.Info("将使用 device.json 内的设备信息运行Bot.") log.Info("将使用 device.json 内的设备信息运行Bot.")
@ -250,46 +238,16 @@ func main() {
} }
return "未知" return "未知"
}()) }())
cli = client.NewClientEmpty() cli = newClient()
if conf.Account.Uin != 0 && PasswordHash != [16]byte{} {
cli.Uin = conf.Account.Uin
cli.PasswordMd5 = PasswordHash
}
cli.OnLog(func(c *client.QQClient, e *client.LogEvent) {
switch e.Type {
case "INFO":
log.Info("Protocol -> " + e.Message)
case "ERROR":
log.Error("Protocol -> " + e.Message)
case "DEBUG":
log.Debug("Protocol -> " + e.Message)
}
})
if global.PathExists("address.txt") {
log.Infof("检测到 address.txt 文件. 将覆盖目标IP.")
addr := global.ReadAddrFile("address.txt")
if len(addr) > 0 {
cli.SetCustomServer(addr)
}
log.Infof("读取到 %v 个自定义地址.", len(addr))
}
cli.OnServerUpdated(func(bot *client.QQClient, e *client.ServerUpdatedEvent) bool {
if !conf.Account.UseSSOAddress {
log.Infof("收到服务器地址更新通知, 根据配置文件已忽略.")
return false
}
log.Infof("收到服务器地址更新通知, 将在下一次重连时应用. ")
return true
})
global.Proxy = conf.Message.ProxyRewrite global.Proxy = conf.Message.ProxyRewrite
isQRCodeLogin := (conf.Account.Uin == 0 || len(conf.Account.Password) == 0) && !conf.Account.Encrypt isQRCodeLogin := (conf.Account.Uin == 0 || len(conf.Account.Password) == 0) && !conf.Account.Encrypt
isTokenLogin := false isTokenLogin := false
saveToken := func() { saveToken := func() {
AccountToken = cli.GenToken() AccountToken = cli.GenToken()
_ = ioutil.WriteFile("session.token", AccountToken, 0o677) _ = os.WriteFile("session.token", AccountToken, 0o644)
} }
if global.PathExists("session.token") { if global.PathExists("session.token") {
token, err := ioutil.ReadFile("session.token") token, err := os.ReadFile("session.token")
if err == nil { if err == nil {
if conf.Account.Uin != 0 { if conf.Account.Uin != 0 {
r := binary.NewReader(token) r := binary.NewReader(token)
@ -302,6 +260,7 @@ func main() {
text := readLineTimeout(time.Second*5, "1") text := readLineTimeout(time.Second*5, "1")
if text == "2" { if text == "2" {
_ = os.Remove("session.token") _ = os.Remove("session.token")
log.Infof("缓存已删除.")
os.Exit(0) os.Exit(0)
} }
} }
@ -310,11 +269,18 @@ func main() {
_ = os.Remove("session.token") _ = os.Remove("session.token")
log.Warnf("恢复会话失败: %v , 尝试使用正常流程登录.", err) log.Warnf("恢复会话失败: %v , 尝试使用正常流程登录.", err)
time.Sleep(time.Second) time.Sleep(time.Second)
cli.Disconnect()
cli.Release()
cli = newClient()
} else { } else {
isTokenLogin = true isTokenLogin = true
} }
} }
} }
if conf.Account.Uin != 0 && PasswordHash != [16]byte{} {
cli.Uin = conf.Account.Uin
cli.PasswordMd5 = PasswordHash
}
if !isTokenLogin { if !isTokenLogin {
if !isQRCodeLogin { if !isQRCodeLogin {
if err := commonLogin(); err != nil { if err := commonLogin(); err != nil {
@ -392,12 +358,12 @@ func main() {
} else { } else {
coolq.SetMessageFormat(conf.Message.PostFormat) coolq.SetMessageFormat(conf.Message.PostFormat)
} }
log.Info("正在加载事件过滤器.")
coolq.IgnoreInvalidCQCode = conf.Message.IgnoreInvalidCQCode coolq.IgnoreInvalidCQCode = conf.Message.IgnoreInvalidCQCode
coolq.SplitURL = conf.Message.FixURL coolq.SplitURL = conf.Message.FixURL
coolq.ForceFragmented = conf.Message.ForceFragment coolq.ForceFragmented = conf.Message.ForceFragment
coolq.RemoveReplyAt = conf.Message.RemoveReplyAt coolq.RemoveReplyAt = conf.Message.RemoveReplyAt
coolq.ExtraReplyData = conf.Message.ExtraReplyData coolq.ExtraReplyData = conf.Message.ExtraReplyData
coolq.SkipMimeScan = conf.Message.SkipMimeScan
for _, m := range conf.Servers { for _, m := range conf.Servers {
if h, ok := m["http"]; ok { if h, ok := m["http"]; ok {
hc := new(config.HTTPServer) hc := new(config.HTTPServer)
@ -431,13 +397,21 @@ func main() {
go server.RunPprofServer(pc) go server.RunPprofServer(pc)
} }
} }
if p, ok := m["lambda"]; ok {
lc := new(config.LambdaServer)
if err := p.Decode(lc); err != nil {
log.Warn("读取pprof配置失败 :", err)
} else {
go server.RunLambdaClient(bot, lc)
}
}
} }
log.Info("资源初始化完成, 开始处理信息.") log.Info("资源初始化完成, 开始处理信息.")
log.Info("アトリは、高性能ですから!") log.Info("アトリは、高性能ですから!")
c := make(chan os.Signal, 1)
go checkUpdate() go checkUpdate()
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c <-global.SetupMainSignalHandler()
} }
// PasswordHashEncrypt 使用key加密给定passwordHash // PasswordHashEncrypt 使用key加密给定passwordHash
@ -617,7 +591,7 @@ Options:
os.Exit(0) os.Exit(0)
} }
func resetWorkDir() { func resetWorkDir(wd string) {
args := make([]string, 0, len(os.Args)) args := make([]string, 0, len(os.Args))
for i := 1; i < len(os.Args); i++ { for i := 1; i < len(os.Args); i++ {
if os.Args[i] == "-w" { if os.Args[i] == "-w" {
@ -626,7 +600,8 @@ func resetWorkDir() {
args = append(args, os.Args[i]) args = append(args, os.Args[i])
} }
} }
proc := exec.Command(os.Args[0], args...) p, _ := filepath.Abs(os.Args[0])
proc := exec.Command(p, args...)
proc.Stdin = os.Stdin proc.Stdin = os.Stdin
proc.Stdout = os.Stdout proc.Stdout = os.Stdout
proc.Stderr = os.Stderr proc.Stderr = os.Stderr
@ -637,3 +612,34 @@ func resetWorkDir() {
} }
os.Exit(0) os.Exit(0)
} }
func newClient() *client.QQClient {
c := client.NewClientEmpty()
c.OnServerUpdated(func(bot *client.QQClient, e *client.ServerUpdatedEvent) bool {
if !conf.Account.UseSSOAddress {
log.Infof("收到服务器地址更新通知, 根据配置文件已忽略.")
return false
}
log.Infof("收到服务器地址更新通知, 将在下一次重连时应用. ")
return true
})
if global.PathExists("address.txt") {
log.Infof("检测到 address.txt 文件. 将覆盖目标IP.")
addr := global.ReadAddrFile("address.txt")
if len(addr) > 0 {
cli.SetCustomServer(addr)
}
log.Infof("读取到 %v 个自定义地址.", len(addr))
}
c.OnLog(func(c *client.QQClient, e *client.LogEvent) {
switch e.Type {
case "INFO":
log.Info("Protocol -> " + e.Message)
case "ERROR":
log.Error("Protocol -> " + e.Message)
case "DEBUG":
log.Debug("Protocol -> " + e.Message)
}
})
return c
}

6
scripts/bootstrap Normal file
View File

@ -0,0 +1,6 @@
#!/bin/sh
echo "Start GOCQHTTP~~~"
cp -f config.yml /tmp/config.yml
cp -f device.json /tmp/device.json
./go-cqhttp -w="/tmp/" faststart

View File

@ -271,7 +271,7 @@ func deleteGroupFolder(bot *coolq.CQBot, p resultGetter) coolq.MSG {
} }
func deleteGroupFile(bot *coolq.CQBot, p resultGetter) coolq.MSG { func deleteGroupFile(bot *coolq.CQBot, p resultGetter) coolq.MSG {
return bot.CQGroupFileDeleteFile(p.Get("group_id").Int(), p.Get("folder_id").Str, p.Get("file_id").Str, int32(p.Get("bus_id").Int())) return bot.CQGroupFileDeleteFile(p.Get("group_id").Int(), p.Get("file_id").Str, int32(p.Get("bus_id").Int()))
} }
func getGroupMsgHistory(bot *coolq.CQBot, p resultGetter) coolq.MSG { func getGroupMsgHistory(bot *coolq.CQBot, p resultGetter) coolq.MSG {
@ -350,6 +350,10 @@ func setModelShow(bot *coolq.CQBot, p resultGetter) coolq.MSG {
return bot.CQSetModelShow(p.Get("model").String(), p.Get("model_show").String()) return bot.CQSetModelShow(p.Get("model").String(), p.Get("model_show").String())
} }
func markMSGAsRead(bot *coolq.CQBot, p resultGetter) coolq.MSG {
return bot.CQMarkMessageAsRead(int32(p.Get("message_id").Int()))
}
// API 是go-cqhttp当前支持的所有api的映射表 // API 是go-cqhttp当前支持的所有api的映射表
var API = map[string]func(*coolq.CQBot, resultGetter) coolq.MSG{ var API = map[string]func(*coolq.CQBot, resultGetter) coolq.MSG{
"get_login_info": getLoginInfo, "get_login_info": getLoginInfo,
@ -413,6 +417,7 @@ var API = map[string]func(*coolq.CQBot, resultGetter) coolq.MSG{
"qidian_get_account_info": getQiDianAccountInfo, "qidian_get_account_info": getQiDianAccountInfo,
"_get_model_show": getModelShow, "_get_model_show": getModelShow,
"_set_model_show": setModelShow, "_set_model_show": setModelShow,
"mark_msg_as_read": markMSGAsRead,
} }
func (api *apiCaller) callAPI(action string, p resultGetter) coolq.MSG { func (api *apiCaller) callAPI(action string, p resultGetter) coolq.MSG {

View File

@ -1,46 +1,145 @@
package server package server
import ( import (
"bytes"
"context" "context"
"crypto/hmac" "crypto/hmac"
"crypto/sha1" "crypto/sha1"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io"
"net/http" "net/http"
"net/url"
"os" "os"
"strings" "strings"
"time" "time"
"github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global/config"
"github.com/Mrs4s/MiraiGo/utils" "github.com/Mrs4s/MiraiGo/utils"
"github.com/gin-gonic/gin"
"github.com/guonaihong/gout" "github.com/guonaihong/gout"
"github.com/guonaihong/gout/dataflow" "github.com/guonaihong/gout/dataflow"
jsoniter "github.com/json-iterator/go"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global/config"
) )
type httpServer struct { type httpServer struct {
engine *gin.Engine HTTP *http.Server
bot *coolq.CQBot api *apiCaller
HTTP *http.Server accessToken string
api *apiCaller
} }
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// HTTPClient 反向HTTP上报客户端 // HTTPClient 反向HTTP上报客户端
type HTTPClient struct { type HTTPClient struct {
bot *coolq.CQBot bot *coolq.CQBot
secret string secret string
addr string addr string
filter string filter string
apiPort int
timeout int32 timeout int32
} }
type httpContext struct { type httpCtx struct {
ctx *gin.Context json gjson.Result
query url.Values
postForm url.Values
}
func (h *httpCtx) Get(s string) gjson.Result {
j := h.json.Get(s)
if j.Exists() {
return j
}
if h.postForm != nil {
if form := h.postForm.Get(s); form != "" {
return gjson.Result{Type: gjson.String, Str: form}
}
}
if h.query != nil {
if query := h.query.Get(s); query != "" {
return gjson.Result{Type: gjson.String, Str: query}
}
}
return gjson.Result{}
}
func (s *httpServer) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
var ctx httpCtx
contentType := request.Header.Get("Content-Type")
switch request.Method {
case http.MethodPost:
if strings.Contains(contentType, "application/json") {
body, err := io.ReadAll(request.Body)
if err != nil {
log.Warnf("获取请求 %v 的Body时出现错误: %v", request.RequestURI, err)
writer.WriteHeader(http.StatusBadRequest)
return
}
if !gjson.ValidBytes(body) {
log.Warnf("已拒绝客户端 %v 的请求: 非法Json", request.RemoteAddr)
writer.WriteHeader(http.StatusBadRequest)
return
}
ctx.json = gjson.Parse(utils.B2S(body))
}
if strings.Contains(contentType, "application/x-www-form-urlencoded") {
err := request.ParseForm()
if err != nil {
log.Warnf("已拒绝客户端 %v 的请求: %v", request.RemoteAddr, err)
writer.WriteHeader(http.StatusBadRequest)
}
ctx.postForm = request.PostForm
}
fallthrough
case http.MethodGet:
ctx.query = request.URL.Query()
default:
log.Warnf("已拒绝客户端 %v 的请求: 方法错误", request.RemoteAddr)
writer.WriteHeader(http.StatusNotFound)
return
}
if status := checkAuth(request, s.accessToken); status != http.StatusOK {
writer.WriteHeader(status)
return
}
action := strings.TrimPrefix(request.URL.Path, "/")
action = strings.TrimSuffix(action, "_async")
log.Debugf("HTTPServer接收到API调用: %v", action)
ret := s.api.callAPI(action, &ctx)
writer.Header().Set("Content-Type", "application/json; charset=utf-8")
writer.WriteHeader(http.StatusOK)
_ = json.NewEncoder(writer).Encode(ret)
}
func checkAuth(req *http.Request, token string) int {
if token == "" { // quick path
return http.StatusOK
}
auth := req.Header.Get("Authorization")
if auth == "" {
auth = req.URL.Query().Get("access_token")
} else {
authN := strings.SplitN(auth, " ", 2)
if len(authN) == 2 {
auth = authN[1]
}
}
switch auth {
case token:
return http.StatusOK
case "":
return http.StatusUnauthorized
default:
return http.StatusForbidden
}
} }
// RunHTTPServerAndClients 启动HTTP服务器与HTTP上报客户端 // RunHTTPServerAndClients 启动HTTP服务器与HTTP上报客户端
@ -49,78 +148,27 @@ func RunHTTPServerAndClients(bot *coolq.CQBot, conf *config.HTTPServer) {
return return
} }
var ( var (
s = new(httpServer) s = new(httpServer)
authToken = conf.AccessToken addr string
addr string
) )
s.accessToken = conf.AccessToken
if conf.Host == "" || conf.Port == 0 { if conf.Host == "" || conf.Port == 0 {
goto client goto client
} }
addr = fmt.Sprintf("%s:%d", conf.Host, conf.Port) addr = fmt.Sprintf("%s:%d", conf.Host, conf.Port)
gin.SetMode(gin.ReleaseMode) s.api = newAPICaller(bot)
s.engine = gin.New()
s.bot = bot
s.api = newAPICaller(s.bot)
if conf.RateLimit.Enabled { if conf.RateLimit.Enabled {
s.api.use(rateLimit(conf.RateLimit.Frequency, conf.RateLimit.Bucket)) s.api.use(rateLimit(conf.RateLimit.Frequency, conf.RateLimit.Bucket))
} }
s.engine.Use(func(c *gin.Context) { if conf.LongPolling.Enabled {
if c.Request.Method != "GET" && c.Request.Method != "POST" { s.api.use(longPolling(bot, conf.LongPolling.MaxQueueSize))
log.Warnf("已拒绝客户端 %v 的请求: 方法错误", c.Request.RemoteAddr)
c.Status(404)
return
}
if c.Request.Method == "POST" && strings.Contains(c.Request.Header.Get("Content-Type"), "application/json") {
d, err := c.GetRawData()
if err != nil {
log.Warnf("获取请求 %v 的Body时出现错误: %v", c.Request.RequestURI, err)
c.Status(400)
return
}
if !gjson.ValidBytes(d) {
log.Warnf("已拒绝客户端 %v 的请求: 非法Json", c.Request.RemoteAddr)
c.Status(400)
return
}
c.Set("json_body", gjson.ParseBytes(d))
}
c.Next()
})
if authToken != "" {
s.engine.Use(func(c *gin.Context) {
auth := c.Request.Header.Get("Authorization")
if auth == "" {
headAuth := c.Query("access_token")
switch {
case headAuth == "":
c.AbortWithStatus(401)
return
case headAuth != authToken:
c.AbortWithStatus(403)
return
}
} else {
auth := strings.SplitN(auth, " ", 2)
switch {
case len(auth) != 2 || auth[1] == "":
c.AbortWithStatus(401)
return
case auth[1] != authToken:
c.AbortWithStatus(403)
return
}
}
})
} }
s.engine.Any("/:action", s.HandleActions)
go func() { go func() {
log.Infof("CQ HTTP 服务器已启动: %v", addr) log.Infof("CQ HTTP 服务器已启动: %v", addr)
s.HTTP = &http.Server{ s.HTTP = &http.Server{
Addr: addr, Addr: addr,
Handler: s.engine, Handler: s,
} }
if err := s.HTTP.ListenAndServe(); err != nil && err != http.ErrServerClosed { if err := s.HTTP.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Error(err) log.Error(err)
@ -133,55 +181,55 @@ func RunHTTPServerAndClients(bot *coolq.CQBot, conf *config.HTTPServer) {
client: client:
for _, c := range conf.Post { for _, c := range conf.Post {
if c.URL != "" { if c.URL != "" {
go newHTTPClient().Run(c.URL, c.Secret, conf.Filter, conf.Timeout, bot) go HTTPClient{
bot: bot,
secret: c.Secret,
addr: c.URL,
apiPort: conf.Port,
filter: conf.Filter,
timeout: conf.Timeout,
}.Run()
} }
} }
} }
// newHTTPClient 返回反向HTTP客户端
func newHTTPClient() *HTTPClient {
return &HTTPClient{}
}
// Run 运行反向HTTP服务 // Run 运行反向HTTP服务
func (c *HTTPClient) Run(addr, secret, filter string, timeout int32, bot *coolq.CQBot) { func (c HTTPClient) Run() {
c.bot = bot addFilter(c.filter)
c.secret = secret
c.addr = addr
c.timeout = timeout
c.filter = filter
addFilter(filter)
if c.timeout < 5 { if c.timeout < 5 {
c.timeout = 5 c.timeout = 5
} }
bot.OnEventPush(c.onBotPushEvent) c.bot.OnEventPush(c.onBotPushEvent)
log.Infof("HTTP POST上报器已启动: %v", addr) log.Infof("HTTP POST上报器已启动: %v", c.addr)
} }
func (c *HTTPClient) onBotPushEvent(m *bytes.Buffer) { func (c *HTTPClient) onBotPushEvent(e *coolq.Event) {
var res string var res string
if c.filter != "" { if c.filter != "" {
filter := findFilter(c.filter) filter := findFilter(c.filter)
if filter != nil && !filter.Eval(gjson.Parse(utils.B2S(m.Bytes()))) { if filter != nil && !filter.Eval(gjson.Parse(e.JSONString())) {
log.Debugf("上报Event %v 到 HTTP 服务器 %v 时被过滤.", c.addr, utils.B2S(m.Bytes())) log.Debugf("上报Event %v 到 HTTP 服务器 %s 时被过滤.", c.addr, e.JSONBytes())
return return
} }
} }
err := gout.POST(c.addr).SetJSON(m.Bytes()).BindBody(&res).SetHeader(func() gout.H { err := gout.POST(c.addr).SetJSON(e.JSONBytes()).BindBody(&res).SetHeader(func() gout.H {
h := gout.H{ h := gout.H{
"X-Self-ID": c.bot.Client.Uin, "X-Self-ID": c.bot.Client.Uin,
"User-Agent": "CQHttp/4.15.0", "User-Agent": "CQHttp/4.15.0",
} }
if c.secret != "" { if c.secret != "" {
mac := hmac.New(sha1.New, []byte(c.secret)) mac := hmac.New(sha1.New, []byte(c.secret))
_, err := mac.Write(m.Bytes()) _, err := mac.Write(e.JSONBytes())
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return nil return nil
} }
h["X-Signature"] = "sha1=" + hex.EncodeToString(mac.Sum(nil)) h["X-Signature"] = "sha1=" + hex.EncodeToString(mac.Sum(nil))
} }
if c.apiPort != 0 {
h["X-API-Port"] = c.apiPort
}
return h return h
}()).SetTimeout(time.Second * time.Duration(c.timeout)).F().Retry().Attempt(5). }()).SetTimeout(time.Second * time.Duration(c.timeout)).F().Retry().Attempt(5).
WaitTime(time.Millisecond * 500).MaxWaitTime(time.Second * 5). WaitTime(time.Millisecond * 500).MaxWaitTime(time.Second * 5).
@ -193,43 +241,15 @@ func (c *HTTPClient) onBotPushEvent(m *bytes.Buffer) {
return nil return nil
}).Do() }).Do()
if err != nil { if err != nil {
log.Warnf("上报Event数据 %v 到 %v 失败: %v", utils.B2S(m.Bytes()), c.addr, err) log.Warnf("上报Event数据 %s 到 %v 失败: %v", e.JSONBytes(), c.addr, err)
return return
} }
log.Debugf("上报Event数据 %v 到 %v", utils.B2S(m.Bytes()), c.addr) log.Debugf("上报Event数据 %s 到 %v", e.JSONBytes(), c.addr)
if gjson.Valid(res) { if gjson.Valid(res) {
c.bot.CQHandleQuickOperation(gjson.Parse(utils.B2S(m.Bytes())), gjson.Parse(res)) c.bot.CQHandleQuickOperation(gjson.Parse(e.JSONString()), gjson.Parse(res))
} }
} }
func (s *httpServer) HandleActions(c *gin.Context) {
action := strings.TrimSuffix(c.Param("action"), "_async")
log.Debugf("HTTPServer接收到API调用: %v", action)
c.JSON(200, s.api.callAPI(action, httpContext{ctx: c}))
}
func (h httpContext) Get(k string) gjson.Result {
c := h.ctx
if q := c.Query(k); q != "" {
return gjson.Result{Type: gjson.String, Str: q}
}
if c.Request.Method == "POST" {
if h := c.Request.Header.Get("Content-Type"); h != "" {
if strings.Contains(h, "application/x-www-form-urlencoded") {
if p, ok := c.GetPostForm(k); ok {
return gjson.Result{Type: gjson.String, Str: p}
}
}
if strings.Contains(h, "application/json") {
if obj, ok := c.Get("json_body"); ok {
return obj.(gjson.Result).Get(k)
}
}
}
}
return gjson.Result{Type: gjson.Null, Str: ""}
}
func (s *httpServer) ShutDown() { func (s *httpServer) ShutDown() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel() defer cancel()

View File

@ -1,6 +1,7 @@
package server package server
import ( import (
"container/list"
"context" "context"
"os" "os"
"sync" "sync"
@ -54,3 +55,37 @@ func findFilter(file string) global.Filter {
defer filterMutex.RUnlock() defer filterMutex.RUnlock()
return filters[file] return filters[file]
} }
func longPolling(bot *coolq.CQBot, maxSize int) handler {
var mutex sync.Mutex
cond := sync.NewCond(&mutex)
queue := list.New()
bot.OnEventPush(func(event *coolq.Event) {
mutex.Lock()
defer mutex.Unlock()
queue.PushBack(event.RawMsg)
for maxSize != 0 && queue.Len() > maxSize {
queue.Remove(queue.Front())
}
cond.Signal()
})
return func(action string, p resultGetter) coolq.MSG {
if action != "get_updates" {
return nil
}
mutex.Lock()
defer mutex.Unlock()
if queue.Len() == 0 {
cond.Wait()
}
limit := int(p.Get("limit").Int())
if limit <= 0 || queue.Len() < limit {
limit = queue.Len()
}
ret := make([]interface{}, limit)
for i := 0; i < limit; i++ {
ret[i] = queue.Remove(queue.Front())
}
return coolq.OK(ret)
}
}

View File

@ -3,11 +3,10 @@ package server
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"net/http/pprof"
"os" "os"
"time" "time"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/Mrs4s/go-cqhttp/global/config" "github.com/Mrs4s/go-cqhttp/global/config"
@ -18,14 +17,18 @@ func RunPprofServer(conf *config.PprofServer) {
if conf.Disabled { if conf.Disabled {
return return
} }
gin.SetMode(gin.ReleaseMode)
engine := gin.New()
addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port) addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port)
pprof.Register(engine) mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
server := http.Server{Addr: addr, Handler: mux}
go func() { go func() {
log.Infof("pprof debug 服务器已启动: %v/debug/pprof", addr) log.Infof("pprof debug 服务器已启动: %v/debug/pprof", addr)
log.Warnf("警告: pprof 服务不支持鉴权, 请不要运行在公网.") log.Warnf("警告: pprof 服务不支持鉴权, 请不要运行在公网.")
if err := engine.Run(addr); err != nil && err != http.ErrServerClosed { if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Error(err) log.Error(err)
log.Infof("pprof 服务启动失败, 请检查端口是否被占用.") log.Infof("pprof 服务启动失败, 请检查端口是否被占用.")
log.Warnf("将在五秒后退出.") log.Warnf("将在五秒后退出.")

185
server/scf.go Normal file
View File

@ -0,0 +1,185 @@
package server
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"os"
"runtime/debug"
"strings"
"github.com/Mrs4s/MiraiGo/utils"
log "github.com/sirupsen/logrus"
"github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/global/config"
)
type lambdaClient struct {
nextURL string
responseURL string
lambdaType string
client http.Client
}
type lambdaResponse struct {
IsBase64Encoded bool `json:"isBase64Encoded"`
StatusCode int `json:"statusCode"`
Headers map[string]string `json:"headers"`
Body string `json:"body"`
}
type lambdaResponseWriter struct {
statusCode int
buf bytes.Buffer
header http.Header
}
func (l *lambdaResponseWriter) Write(p []byte) (n int, err error) {
return l.buf.Write(p)
}
func (l *lambdaResponseWriter) Header() http.Header {
return l.header
}
func (l *lambdaResponseWriter) flush() error {
buffer := global.NewBuffer()
defer global.PutBuffer(buffer)
body := utils.B2S(l.buf.Bytes())
header := make(map[string]string)
for k, v := range l.header {
header[k] = v[0]
}
_ = json.NewEncoder(buffer).Encode(&lambdaResponse{
IsBase64Encoded: false,
StatusCode: l.statusCode,
Headers: header,
Body: body,
})
r, _ := http.NewRequest("POST", cli.responseURL, buffer)
do, err := cli.client.Do(r)
if err != nil {
return err
}
return do.Body.Close()
}
func (l *lambdaResponseWriter) WriteHeader(statusCode int) {
l.statusCode = statusCode
}
var cli *lambdaClient
// RunLambdaClient type: [scf,aws]
func RunLambdaClient(bot *coolq.CQBot, conf *config.LambdaServer) {
cli = &lambdaClient{
lambdaType: conf.Type,
client: http.Client{Timeout: 0},
}
switch cli.lambdaType { // todo: aws
case "scf": // tencent serverless function
base := fmt.Sprintf("http://%s:%s/runtime/",
os.Getenv("SCF_RUNTIME_API"),
os.Getenv("SCF_RUNTIME_API_PORT"),
)
cli.nextURL = base + "invocation/next"
cli.responseURL = base + "invocation/response"
post, err := http.Post(base+"init/ready", "", nil)
if err != nil {
log.Warnf("lambda 初始化失败: %v", err)
return
}
_ = post.Body.Close()
case "aws": // aws lambda
const apiVersion = "2018-06-01"
base := fmt.Sprintf("http://%s/%s/runtime/", os.Getenv("AWS_LAMBDA_RUNTIME_API"), apiVersion)
cli.nextURL = base + "invocation/next"
cli.responseURL = base + "invocation/response"
default:
log.Fatal("unknown lambda type:", conf.Type)
}
api := newAPICaller(bot)
if conf.RateLimit.Enabled {
api.use(rateLimit(conf.RateLimit.Frequency, conf.RateLimit.Bucket))
}
server := &httpServer{
api: api,
accessToken: conf.AccessToken,
}
for {
req := cli.next()
if req == nil {
writer := lambdaResponseWriter{statusCode: 200}
_, _ = writer.Write(nil)
continue
}
func() {
defer func() {
if e := recover(); e != nil {
log.Warnf("Lambda 出现不可恢复错误: %v\n%s", e, debug.Stack())
}
}()
writer := lambdaResponseWriter{header: make(http.Header)}
server.ServeHTTP(&writer, req)
if err := writer.flush(); err != nil {
log.Warnf("Lambda 发送响应失败: %v", err)
}
}()
}
}
type lambdaInvoke struct {
Headers map[string]string
HTTPMethod string `json:"httpMethod"`
Body string `json:"body"`
Path string `json:"path"`
QueryString map[string]string
RequestContext struct {
Path string `json:"path"`
} `json:"requestContext"`
}
func (c *lambdaClient) next() *http.Request {
r, err := http.NewRequest(http.MethodGet, c.nextURL, nil)
if err != nil {
return nil
}
resp, err := c.client.Do(r)
if err != nil {
return nil
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil
}
var req = new(http.Request)
var invoke = new(lambdaInvoke)
_ = json.NewDecoder(resp.Body).Decode(invoke)
if invoke.HTTPMethod == "" { // 不是 api 网关
return nil
}
req.Method = invoke.HTTPMethod
req.Body = io.NopCloser(strings.NewReader(invoke.Body))
req.Header = make(map[string][]string)
for k, v := range invoke.Headers {
req.Header.Set(k, v)
}
req.URL = new(url.URL)
req.URL.Path = strings.TrimPrefix(invoke.Path, invoke.RequestContext.Path)
// todo: avoid encoding
query := make(url.Values)
for k, v := range invoke.QueryString {
query[k] = []string{v}
}
req.URL.RawQuery = query.Encode()
return req
}

View File

@ -31,8 +31,8 @@ type webSocketServer struct {
filter string filter string
} }
// WebSocketClient WebSocket客户端实例 // websocketClient WebSocket客户端实例
type WebSocketClient struct { type websocketClient struct {
bot *coolq.CQBot bot *coolq.CQBot
conf *config.WebsocketReverse conf *config.WebsocketReverse
@ -59,22 +59,24 @@ func RunWebSocketServer(b *coolq.CQBot, conf *config.WebsocketServer) {
if conf.Disabled { if conf.Disabled {
return return
} }
s := new(webSocketServer) s := &webSocketServer{
s.conf = conf bot: b,
s.bot = b conf: conf,
s.token = conf.AccessToken token: conf.AccessToken,
s.filter = conf.Filter filter: conf.Filter,
}
addFilter(s.filter) addFilter(s.filter)
addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port) addr := fmt.Sprintf("%s:%d", conf.Host, conf.Port)
s.handshake = fmt.Sprintf(`{"_post_method":2,"meta_event_type":"lifecycle","post_type":"meta_event","self_id":%d,"sub_type":"connect","time":%d}`, s.handshake = fmt.Sprintf(`{"_post_method":2,"meta_event_type":"lifecycle","post_type":"meta_event","self_id":%d,"sub_type":"connect","time":%d}`,
b.Client.Uin, time.Now().Unix()) b.Client.Uin, time.Now().Unix())
b.OnEventPush(s.onBotPushEvent) b.OnEventPush(s.onBotPushEvent)
http.HandleFunc("/event", s.event) mux := http.ServeMux{}
http.HandleFunc("/api", s.api) mux.HandleFunc("/event", s.event)
http.HandleFunc("/", s.any) mux.HandleFunc("/api", s.api)
mux.HandleFunc("/", s.any)
go func() { go func() {
log.Infof("CQ WebSocket 服务器已启动: %v", addr) log.Infof("CQ WebSocket 服务器已启动: %v", addr)
log.Fatal(http.ListenAndServe(addr, nil)) log.Fatal(http.ListenAndServe(addr, &mux))
}() }()
} }
@ -83,11 +85,12 @@ func RunWebSocketClient(b *coolq.CQBot, conf *config.WebsocketReverse) {
if conf.Disabled { if conf.Disabled {
return return
} }
c := new(WebSocketClient) c := &websocketClient{
c.bot = b bot: b,
c.conf = conf conf: conf,
c.token = conf.AccessToken token: conf.AccessToken,
c.filter = conf.Filter filter: conf.Filter,
}
addFilter(c.filter) addFilter(c.filter)
if c.conf.Universal != "" { if c.conf.Universal != "" {
c.connectUniversal() c.connectUniversal()
@ -102,7 +105,7 @@ func RunWebSocketClient(b *coolq.CQBot, conf *config.WebsocketReverse) {
c.bot.OnEventPush(c.onBotPushEvent) c.bot.OnEventPush(c.onBotPushEvent)
} }
func (c *WebSocketClient) connectAPI() { func (c *websocketClient) connectAPI() {
log.Infof("开始尝试连接到反向WebSocket API服务器: %v", c.conf.API) log.Infof("开始尝试连接到反向WebSocket API服务器: %v", c.conf.API)
header := http.Header{ header := http.Header{
"X-Client-Role": []string{"API"}, "X-Client-Role": []string{"API"},
@ -129,7 +132,7 @@ func (c *WebSocketClient) connectAPI() {
go c.listenAPI(wrappedConn, false) go c.listenAPI(wrappedConn, false)
} }
func (c *WebSocketClient) connectEvent() { func (c *websocketClient) connectEvent() {
log.Infof("开始尝试连接到反向WebSocket Event服务器: %v", c.conf.Event) log.Infof("开始尝试连接到反向WebSocket Event服务器: %v", c.conf.Event)
header := http.Header{ header := http.Header{
"X-Client-Role": []string{"Event"}, "X-Client-Role": []string{"Event"},
@ -157,10 +160,15 @@ func (c *WebSocketClient) connectEvent() {
} }
log.Infof("已连接到反向WebSocket Event服务器 %v", c.conf.Event) log.Infof("已连接到反向WebSocket Event服务器 %v", c.conf.Event)
c.eventConn = &webSocketConn{Conn: conn, apiCaller: newAPICaller(c.bot)} if c.eventConn == nil {
wrappedConn := &webSocketConn{Conn: conn, apiCaller: newAPICaller(c.bot)}
c.eventConn = wrappedConn
} else {
c.eventConn.Conn = conn
}
} }
func (c *WebSocketClient) connectUniversal() { func (c *websocketClient) connectUniversal() {
log.Infof("开始尝试连接到反向WebSocket Universal服务器: %v", c.conf.Universal) log.Infof("开始尝试连接到反向WebSocket Universal服务器: %v", c.conf.Universal)
header := http.Header{ header := http.Header{
"X-Client-Role": []string{"Universal"}, "X-Client-Role": []string{"Universal"},
@ -186,15 +194,19 @@ func (c *WebSocketClient) connectUniversal() {
log.Warnf("反向WebSocket 握手时出现错误: %v", err) log.Warnf("反向WebSocket 握手时出现错误: %v", err)
} }
wrappedConn := &webSocketConn{Conn: conn, apiCaller: newAPICaller(c.bot)} if c.universalConn == nil {
if c.conf.RateLimit.Enabled { wrappedConn := &webSocketConn{Conn: conn, apiCaller: newAPICaller(c.bot)}
wrappedConn.apiCaller.use(rateLimit(c.conf.RateLimit.Frequency, c.conf.RateLimit.Bucket)) if c.conf.RateLimit.Enabled {
wrappedConn.apiCaller.use(rateLimit(c.conf.RateLimit.Frequency, c.conf.RateLimit.Bucket))
}
c.universalConn = wrappedConn
} else {
c.universalConn.Conn = conn
} }
go c.listenAPI(wrappedConn, true) go c.listenAPI(c.universalConn, true)
c.universalConn = wrappedConn
} }
func (c *WebSocketClient) listenAPI(conn *webSocketConn, u bool) { func (c *websocketClient) listenAPI(conn *webSocketConn, u bool) {
defer func() { _ = conn.Close() }() defer func() { _ = conn.Close() }()
for { for {
buffer := global.NewBuffer() buffer := global.NewBuffer()
@ -225,66 +237,39 @@ func (c *WebSocketClient) listenAPI(conn *webSocketConn, u bool) {
} }
} }
func (c *WebSocketClient) onBotPushEvent(m *bytes.Buffer) { func (c *websocketClient) onBotPushEvent(e *coolq.Event) {
filter := findFilter(c.filter) filter := findFilter(c.filter)
if filter != nil && !filter.Eval(gjson.Parse(utils.B2S(m.Bytes()))) { if filter != nil && !filter.Eval(gjson.Parse(e.JSONString())) {
log.Debugf("上报Event %v 到 WS客户端 时被过滤.", utils.B2S(m.Bytes())) log.Debugf("上报Event %s 到 WS服务器 时被过滤.", e.JSONBytes())
return return
} }
if c.eventConn != nil { push := func(conn *webSocketConn, reconnect func()) {
log.Debugf("向WS服务器 %v 推送Event: %v", c.eventConn.RemoteAddr().String(), utils.B2S(m.Bytes())) log.Debugf("向WS服务器 %v 推送Event: %s", conn.RemoteAddr().String(), e.JSONBytes())
conn := c.eventConn
conn.Lock() conn.Lock()
defer conn.Unlock() defer conn.Unlock()
_ = c.eventConn.SetWriteDeadline(time.Now().Add(time.Second * 15)) _ = conn.SetWriteDeadline(time.Now().Add(time.Second * 15))
if err := c.eventConn.WriteMessage(websocket.TextMessage, m.Bytes()); err != nil { if err := conn.WriteMessage(websocket.TextMessage, e.JSONBytes()); err != nil {
log.Warnf("向WS服务器 %v 推送Event时出现错误: %v", c.eventConn.RemoteAddr().String(), err) log.Warnf("向WS服务器 %v 推送Event时出现错误: %v", conn.RemoteAddr().String(), err)
_ = c.eventConn.Close() _ = conn.Close()
if c.conf.ReconnectInterval != 0 { if c.conf.ReconnectInterval != 0 {
time.Sleep(time.Millisecond * time.Duration(c.conf.ReconnectInterval)) time.Sleep(time.Millisecond * time.Duration(c.conf.ReconnectInterval))
c.connectEvent() reconnect()
} }
} }
} }
if c.eventConn != nil {
push(c.eventConn, c.connectEvent)
}
if c.universalConn != nil { if c.universalConn != nil {
log.Debugf("向WS服务器 %v 推送Event: %v", c.universalConn.RemoteAddr().String(), utils.B2S(m.Bytes())) push(c.universalConn, c.connectUniversal)
conn := c.universalConn
conn.Lock()
defer conn.Unlock()
_ = c.universalConn.SetWriteDeadline(time.Now().Add(time.Second * 15))
if err := c.universalConn.WriteMessage(websocket.TextMessage, m.Bytes()); err != nil {
log.Warnf("向WS服务器 %v 推送Event时出现错误: %v", c.universalConn.RemoteAddr().String(), err)
_ = c.universalConn.Close()
if c.conf.ReconnectInterval != 0 {
time.Sleep(time.Millisecond * time.Duration(c.conf.ReconnectInterval))
c.connectUniversal()
}
}
} }
} }
func (s *webSocketServer) auth(r *http.Request) (bool, int) {
if s.token != "" { // s.token == s.conf.AccessToken
var auth string
if auth = r.URL.Query().Get("access_token"); auth == "" {
headAuth := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(headAuth) != 2 || headAuth[1] == "" {
return false, 401
}
auth = headAuth[1]
}
if auth != s.token {
log.Warnf("已拒绝 %v 的 WebSocket 请求: Token鉴权失败", r.RemoteAddr)
return false, 403
}
}
return true, 0
}
func (s *webSocketServer) event(w http.ResponseWriter, r *http.Request) { func (s *webSocketServer) event(w http.ResponseWriter, r *http.Request) {
isAuth, errReason := s.auth(r) status := checkAuth(r, s.token)
if !isAuth { if status != http.StatusOK {
w.WriteHeader(errReason) log.Warnf("已拒绝 %v 的 WebSocket 请求: Token鉴权失败(code:%d)", r.RemoteAddr, status)
w.WriteHeader(status)
return return
} }
c, err := upgrader.Upgrade(w, r, nil) c, err := upgrader.Upgrade(w, r, nil)
@ -309,9 +294,10 @@ func (s *webSocketServer) event(w http.ResponseWriter, r *http.Request) {
} }
func (s *webSocketServer) api(w http.ResponseWriter, r *http.Request) { func (s *webSocketServer) api(w http.ResponseWriter, r *http.Request) {
isAuth, errReason := s.auth(r) status := checkAuth(r, s.token)
if !isAuth { if status != http.StatusOK {
w.WriteHeader(errReason) log.Warnf("已拒绝 %v 的 WebSocket 请求: Token鉴权失败(code:%d)", r.RemoteAddr, status)
w.WriteHeader(status)
return return
} }
c, err := upgrader.Upgrade(w, r, nil) c, err := upgrader.Upgrade(w, r, nil)
@ -328,9 +314,10 @@ func (s *webSocketServer) api(w http.ResponseWriter, r *http.Request) {
} }
func (s *webSocketServer) any(w http.ResponseWriter, r *http.Request) { func (s *webSocketServer) any(w http.ResponseWriter, r *http.Request) {
isAuth, errReason := s.auth(r) status := checkAuth(r, s.token)
if !isAuth { if status != http.StatusOK {
w.WriteHeader(errReason) log.Warnf("已拒绝 %v 的 WebSocket 请求: Token鉴权失败(code:%d)", r.RemoteAddr, status)
w.WriteHeader(status)
return return
} }
c, err := upgrader.Upgrade(w, r, nil) c, err := upgrader.Upgrade(w, r, nil)
@ -398,33 +385,32 @@ func (c *webSocketConn) handleRequest(_ *coolq.CQBot, payload []byte) {
_ = c.WriteJSON(ret) _ = c.WriteJSON(ret)
} }
func (s *webSocketServer) onBotPushEvent(m *bytes.Buffer) { func (s *webSocketServer) onBotPushEvent(e *coolq.Event) {
s.eventConnMutex.Lock() s.eventConnMutex.Lock()
defer s.eventConnMutex.Unlock() defer s.eventConnMutex.Unlock()
filter := findFilter(s.filter) filter := findFilter(s.filter)
if filter != nil && !filter.Eval(gjson.Parse(utils.B2S(m.Bytes()))) { if filter != nil && !filter.Eval(gjson.Parse(e.JSONString())) {
log.Debugf("上报Event %v 到 WS客户端 时被过滤.", utils.B2S(m.Bytes())) log.Debugf("上报Event %s 到 WS客户端 时被过滤.", e.JSONBytes())
return return
} }
j := 0
for i, l := 0, len(s.eventConn); i < l; i++ { for i := 0; i < len(s.eventConn); i++ {
conn := s.eventConn[i] conn := s.eventConn[i]
log.Debugf("向WS客户端 %v 推送Event: %v", conn.RemoteAddr().String(), utils.B2S(m.Bytes())) log.Debugf("向WS客户端 %v 推送Event: %s", conn.RemoteAddr().String(), e.JSONBytes())
conn.Lock() conn.Lock()
if err := conn.WriteMessage(websocket.TextMessage, m.Bytes()); err != nil { if err := conn.WriteMessage(websocket.TextMessage, e.JSONBytes()); err != nil {
_ = conn.Close() _ = conn.Close()
next := i + 1
if next >= l {
next = l - 1
}
s.eventConn[i], s.eventConn[next] = s.eventConn[next], s.eventConn[i]
s.eventConn = append(s.eventConn[:next], s.eventConn[next+1:]...)
i--
l--
conn = nil conn = nil
continue continue
} }
if i != j {
// i != j means that some connection has been closed.
// use a in-place removal to avoid copying.
s.eventConn[j] = conn
}
conn.Unlock() conn.Unlock()
j++
} }
s.eventConn = s.eventConn[:j]
} }