mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-06-30 03:43:25 +00:00
Compare commits
80 Commits
v1.0.0-bet
...
v1.0.0-bet
Author | SHA1 | Date | |
---|---|---|---|
6c52734324 | |||
a2f5be2166 | |||
cf307f455e | |||
58d96004a3 | |||
c951caba9f | |||
f4e38cb416 | |||
6fd104a8b9 | |||
86202d9d90 | |||
a3906a5d33 | |||
0be4c79d2c | |||
d705dd0617 | |||
e5a8a406a4 | |||
89eaf91371 | |||
19b2a86c7c | |||
e56e3b5036 | |||
cdd2dcf907 | |||
6d5bf84603 | |||
7274a46a3c | |||
decab775f9 | |||
cd4ac2078b | |||
04fad744a5 | |||
d28e6f6e82 | |||
9c9a469a16 | |||
d7e0aaf827 | |||
0a5e172a4a | |||
4894731422 | |||
85df77f9a5 | |||
b68bb0762f | |||
2db968bbe3 | |||
426aa5718f | |||
54cef1fd37 | |||
3663c2aed8 | |||
ca93d4de0d | |||
c6994ade94 | |||
f21730e879 | |||
ca8d28f913 | |||
34c22bed9c | |||
9390503683 | |||
ccc344ab1c | |||
e768ff0dc2 | |||
404c39eb53 | |||
2f05775101 | |||
d266242887 | |||
e9b302ad74 | |||
6042d451e9 | |||
b5fbc6f2d5 | |||
4a15138767 | |||
f6e5e28a7a | |||
5d00bd134d | |||
a894ee91bc | |||
8bfbfc04f3 | |||
e8ddc1f140 | |||
5cfd53fd8a | |||
48b442a281 | |||
ad7e53009d | |||
a87d0e9f8f | |||
70558dc965 | |||
752c82e8d6 | |||
e2cafbd7d6 | |||
6e18969f96 | |||
60297d1a83 | |||
499108cb1b | |||
2bbc75672f | |||
bddada66dc | |||
ee3580039b | |||
63e1508815 | |||
7c5bd64e9b | |||
a117d67286 | |||
f40ff22c0e | |||
edde0c212c | |||
8eb4b0388f | |||
fad3bf1314 | |||
da23fc1403 | |||
2f267e1926 | |||
37d846c190 | |||
9c17a3e1c8 | |||
9d44697dcd | |||
139f053c5b | |||
19fdd34b7f | |||
9f7e31766b |
9
.github/workflows/ci.yml
vendored
9
.github/workflows/ci.yml
vendored
@ -1,6 +1,6 @@
|
||||
name: CI
|
||||
|
||||
on: [push, pull_request]
|
||||
on: [push, pull_request,workflow_dispatch]
|
||||
|
||||
env:
|
||||
BINARY_PREFIX: "go-cqhttp_"
|
||||
@ -31,6 +31,13 @@ jobs:
|
||||
uses: actions/setup-go@v2.1.3
|
||||
with:
|
||||
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
|
||||
env:
|
||||
GOOS: ${{ matrix.goos }}
|
||||
|
10
.github/workflows/golint.yml
vendored
10
.github/workflows/golint.yml
vendored
@ -1,6 +1,6 @@
|
||||
name: Lint
|
||||
|
||||
on: [push]
|
||||
on: [push,pull_request,workflow_dispatch]
|
||||
|
||||
jobs:
|
||||
golangci:
|
||||
@ -24,7 +24,7 @@ jobs:
|
||||
go test $(go list ./...)
|
||||
|
||||
- name: Commit back
|
||||
if: github.repository_owner == 'Mrs4s'
|
||||
if: ${{ github.repository_owner == 'Mrs4s' && !github.event.pull_request }}
|
||||
continue-on-error: true
|
||||
run: |
|
||||
git config --local user.name 'github-actions[bot]'
|
||||
@ -32,3 +32,9 @@ jobs:
|
||||
git add --all
|
||||
git commit -m "ci(chore): Fix stylings"
|
||||
git push
|
||||
|
||||
- name: Suggester
|
||||
if: ${{ github.event.pull_request }}
|
||||
uses: reviewdog/action-suggester@v1
|
||||
with:
|
||||
tool_name: golangci-lint
|
||||
|
29
.github/workflows/suggester.yml
vendored
29
.github/workflows/suggester.yml
vendored
@ -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
|
28
README.md
28
README.md
@ -1,5 +1,7 @@
|
||||
<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>
|
||||
|
||||
<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 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>
|
||||
<summary>已实现CQ码</summary>
|
||||
<summary>已实现 CQ 码</summary>
|
||||
|
||||
#### 符合 Onebot 标准的 CQ 码
|
||||
#### 符合 OneBot 标准的 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
|
||||
[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 码 | 功能 |
|
||||
| -------------- | --------------------------------- |
|
||||
@ -120,9 +122,9 @@ go-cqhttp兼容[OneBot-v11](https://github.com/howmanybots/onebot/tree/master/v1
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>已实现API</summary>
|
||||
<summary>已实现 API</summary>
|
||||
|
||||
#### 符合 Onebot 标准的 API
|
||||
#### 符合 OneBot 标准的 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
|
||||
[对事件执行快速操作]: 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 | 功能 |
|
||||
| --------------------------- | ---------------------- |
|
||||
@ -217,9 +219,9 @@ go-cqhttp兼容[OneBot-v11](https://github.com/howmanybots/onebot/tree/master/v1
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>已实现Event</summary>
|
||||
<summary>已实现 Event</summary>
|
||||
|
||||
#### 符合 Onebot 标准的 Event(部分 Event 比 Onebot 标准多上报几个字段,不影响使用)
|
||||
#### 符合 OneBot 标准的 Event(部分 Event 比 OneBot 标准多上报几个字段,不影响使用)
|
||||
|
||||
| 事件类型 | 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 建议关闭数据库使用.
|
||||
|
155
coolq/api.go
155
coolq/api.go
@ -4,7 +4,6 @@ import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"os"
|
||||
"path"
|
||||
@ -14,12 +13,10 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/Mrs4s/MiraiGo/binary"
|
||||
"github.com/Mrs4s/MiraiGo/client"
|
||||
"github.com/Mrs4s/MiraiGo/message"
|
||||
"github.com/Mrs4s/MiraiGo/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/tidwall/gjson"
|
||||
|
||||
@ -321,13 +318,13 @@ func (bot *CQBot) CQGroupFileDeleteFolder(groupID int64, id string) MSG {
|
||||
// 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)
|
||||
if err != nil {
|
||||
log.Errorf("获取群 %v 文件系统信息失败: %v", groupID, err)
|
||||
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)
|
||||
return Failed(200, "FILE_SYSTEM_API_ERROR", res)
|
||||
}
|
||||
@ -351,8 +348,7 @@ func (bot *CQBot) CQGetWordSlices(content string) MSG {
|
||||
// CQSendGroupMessage 发送群消息
|
||||
//
|
||||
// https://git.io/Jtz1c
|
||||
func (bot *CQBot) CQSendGroupMessage(groupID int64, i interface{}, autoEscape bool) MSG {
|
||||
var str string
|
||||
func (bot *CQBot) CQSendGroupMessage(groupID int64, m gjson.Result, autoEscape bool) MSG {
|
||||
group := bot.Client.FindGroup(groupID)
|
||||
if group == nil {
|
||||
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) {
|
||||
for _, e := range elem {
|
||||
if at, ok := e.(*message.AtElement); ok && at.Target != 0 && at.Display == "" {
|
||||
at.Display = "@" + func() string {
|
||||
mem := group.FindMember(at.Target)
|
||||
if mem != nil {
|
||||
return mem.DisplayName()
|
||||
}
|
||||
return strconv.FormatInt(at.Target, 10)
|
||||
}()
|
||||
mem := group.FindMember(at.Target)
|
||||
if mem != nil {
|
||||
at.Display = "@" + mem.DisplayName()
|
||||
} else {
|
||||
at.Display = "@" + strconv.FormatInt(at.Target, 10)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if m, ok := i.(gjson.Result); ok {
|
||||
if m.Type == gjson.JSON {
|
||||
elem := bot.ConvertObjectMessage(m, true)
|
||||
fixAt(elem)
|
||||
mid := bot.SendGroupMessage(groupID, &message.SendingMessage{Elements: elem})
|
||||
if mid == -1 {
|
||||
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})
|
||||
|
||||
if m.Type == gjson.JSON {
|
||||
elem := bot.ConvertObjectMessage(m, true)
|
||||
fixAt(elem)
|
||||
mid := bot.SendGroupMessage(groupID, &message.SendingMessage{Elements: elem})
|
||||
if mid == -1 {
|
||||
return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出")
|
||||
}
|
||||
str = func() string {
|
||||
if m.Str != "" {
|
||||
return m.Str
|
||||
}
|
||||
return m.Raw
|
||||
}()
|
||||
} else if s, ok := i.(string); ok {
|
||||
str = s
|
||||
log.Infof("发送群 %v(%v) 的消息: %v (%v)", group.Name, groupID, limitedString(ToStringMessage(elem, groupID)), mid)
|
||||
return OK(MSG{"message_id": mid})
|
||||
}
|
||||
str := m.String()
|
||||
if str == "" {
|
||||
log.Warnf("群消息发送失败: 信息为空. MSG: %v", i)
|
||||
log.Warn("群消息发送失败: 信息为空.")
|
||||
return Failed(100, "EMPTY_MSG_ERROR", "消息为空")
|
||||
}
|
||||
var elem []message.IMessageElement
|
||||
@ -418,14 +405,15 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) MSG {
|
||||
}
|
||||
var sendNodes []*message.ForwardNode
|
||||
ts := time.Now().Add(-time.Minute * 5)
|
||||
hasCustom := func() bool {
|
||||
for _, item := range m.Array() {
|
||||
if item.Get("data.uin").Exists() {
|
||||
return true
|
||||
}
|
||||
hasCustom := false
|
||||
m.ForEach(func(_, item gjson.Result) bool {
|
||||
if item.Get("data.uin").Exists() || item.Get("data.user_id").Exists() {
|
||||
hasCustom = true
|
||||
return false
|
||||
}
|
||||
return false
|
||||
}()
|
||||
return true
|
||||
})
|
||||
|
||||
var convert func(e gjson.Result) []*message.ForwardNode
|
||||
convert = func(e gjson.Result) (nodes []*message.ForwardNode) {
|
||||
if e.Get("type").Str != "node" {
|
||||
@ -433,7 +421,7 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) MSG {
|
||||
}
|
||||
ts.Add(time.Second)
|
||||
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))
|
||||
if m != nil {
|
||||
sender := m["sender"].(message.Sender)
|
||||
@ -454,9 +442,9 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) MSG {
|
||||
log.Warnf("警告: 引用消息 %v 错误或数据库未开启.", e.Get("data.id").Str)
|
||||
return
|
||||
}
|
||||
uin, _ := strconv.ParseInt(e.Get("data.uin").Str, 10, 64)
|
||||
msgTime, err := strconv.ParseInt(e.Get("data.time").Str, 10, 64)
|
||||
if err != nil {
|
||||
uin := e.Get("data.[user_id,uin].0").Int()
|
||||
msgTime := e.Get("data.time").Int()
|
||||
if msgTime == 0 {
|
||||
msgTime = ts.Unix()
|
||||
}
|
||||
name := e.Get("data.name").Str
|
||||
@ -464,7 +452,7 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) MSG {
|
||||
if c.IsArray() {
|
||||
flag := false
|
||||
c.ForEach(func(_, value gjson.Result) bool {
|
||||
if value.Get("type").String() == "node" {
|
||||
if value.Get("type").Str == "node" {
|
||||
flag = true
|
||||
return false
|
||||
}
|
||||
@ -488,23 +476,14 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) MSG {
|
||||
if uin != 0 && name != "" && len(content) > 0 {
|
||||
var newElem []message.IMessageElement
|
||||
for _, elem := range content {
|
||||
if img, ok := elem.(*LocalImageElement); ok {
|
||||
gm, err := bot.UploadLocalImageAsGroup(groupID, img)
|
||||
switch elem.(type) {
|
||||
case *LocalImageElement, *LocalVideoElement:
|
||||
gm, err := bot.uploadMedia(elem, groupID, true)
|
||||
if err != nil {
|
||||
log.Warnf("警告:群 %v 图片上传失败: %v", groupID, err)
|
||||
log.Warnf("警告: 群 %d %s上传失败: %v", groupID, elem.Type().String(), 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
|
||||
elem = gm
|
||||
}
|
||||
newElem = append(newElem, elem)
|
||||
}
|
||||
@ -516,7 +495,7 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) MSG {
|
||||
})
|
||||
return
|
||||
}
|
||||
log.Warnf("警告: 非法 Forward node 将跳过")
|
||||
log.Warnf("警告: 非法 Forward node 将跳过. uin: %v name: %v content count: %v", uin, name, len(content))
|
||||
return
|
||||
}
|
||||
if m.IsArray() {
|
||||
@ -536,33 +515,23 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) MSG {
|
||||
"message_id": bot.InsertGroupMessage(ret),
|
||||
})
|
||||
}
|
||||
return Failed(100)
|
||||
return Failed(100, "EMPTY_NODES", "未找到任何可发送的合并转发信息")
|
||||
}
|
||||
|
||||
// CQSendPrivateMessage 发送私聊消息
|
||||
//
|
||||
// https://git.io/Jtz1l
|
||||
func (bot *CQBot) CQSendPrivateMessage(userID int64, groupID int64, i interface{}, autoEscape bool) MSG {
|
||||
var str string
|
||||
if m, ok := i.(gjson.Result); ok {
|
||||
if m.Type == gjson.JSON {
|
||||
elem := bot.ConvertObjectMessage(m, false)
|
||||
mid := bot.SendPrivateMessage(userID, groupID, &message.SendingMessage{Elements: elem})
|
||||
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})
|
||||
func (bot *CQBot) CQSendPrivateMessage(userID int64, groupID int64, m gjson.Result, autoEscape bool) MSG {
|
||||
if m.Type == gjson.JSON {
|
||||
elem := bot.ConvertObjectMessage(m, false)
|
||||
mid := bot.SendPrivateMessage(userID, groupID, &message.SendingMessage{Elements: elem})
|
||||
if mid == -1 {
|
||||
return Failed(100, "SEND_MSG_API_ERROR", "请参考 go-cqhttp 端输出")
|
||||
}
|
||||
str = func() string {
|
||||
if m.Str != "" {
|
||||
return m.Str
|
||||
}
|
||||
return m.Raw
|
||||
}()
|
||||
} else if s, ok := i.(string); ok {
|
||||
str = s
|
||||
log.Infof("发送好友 %v(%v) 的消息: %v (%v)", userID, userID, limitedString(m.String()), mid)
|
||||
return OK(MSG{"message_id": mid})
|
||||
}
|
||||
str := m.String()
|
||||
if str == "" {
|
||||
return Failed(100, "EMPTY_MSG_ERROR", "消息为空")
|
||||
}
|
||||
@ -1029,7 +998,7 @@ func (bot *CQBot) CQGetImage(file string) MSG {
|
||||
if !global.PathExists(path.Join(global.ImagePath, file)) {
|
||||
return Failed(100)
|
||||
}
|
||||
b, err := ioutil.ReadFile(path.Join(global.ImagePath, file))
|
||||
b, err := os.ReadFile(path.Join(global.ImagePath, file))
|
||||
if err == nil {
|
||||
r := binary.NewReader(b)
|
||||
r.ReadBytes(16)
|
||||
@ -1352,9 +1321,9 @@ func (bot *CQBot) CQGetEssenceMessageList(groupCode int64) MSG {
|
||||
"sender_time": m.SenderTime,
|
||||
"operator_time": m.AddDigestTime,
|
||||
"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))
|
||||
list = append(list, msg)
|
||||
}
|
||||
@ -1497,15 +1466,17 @@ func convertGroupMemberInfo(groupID int64, m *client.GroupMemberInfo) MSG {
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
b := utils.S2B(str)
|
||||
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) + " ..."
|
||||
return string(limited[:])
|
||||
}
|
||||
|
262
coolq/bot.go
262
coolq/bot.go
@ -13,17 +13,20 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/Mrs4s/go-cqhttp/global"
|
||||
"github.com/Mrs4s/go-cqhttp/global/config"
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
|
||||
"github.com/Mrs4s/MiraiGo/binary"
|
||||
"github.com/Mrs4s/MiraiGo/client"
|
||||
"github.com/Mrs4s/MiraiGo/message"
|
||||
"github.com/Mrs4s/MiraiGo/utils"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
"github.com/syndtr/goleveldb/leveldb/opt"
|
||||
|
||||
"github.com/Mrs4s/go-cqhttp/global"
|
||||
"github.com/Mrs4s/go-cqhttp/global/config"
|
||||
)
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
@ -32,7 +35,9 @@ var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
type CQBot struct {
|
||||
Client *client.QQClient
|
||||
|
||||
events []func(*bytes.Buffer)
|
||||
lock sync.RWMutex
|
||||
events []func(*Event)
|
||||
|
||||
db *leveldb.DB
|
||||
friendReqCache sync.Map
|
||||
tempSessionCache sync.Map
|
||||
@ -42,9 +47,48 @@ type CQBot struct {
|
||||
// MSG 消息Map
|
||||
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 是否启用强制分片
|
||||
var ForceFragmented = false
|
||||
|
||||
// SkipMimeScan 是否跳过Mime扫描
|
||||
var SkipMimeScan bool
|
||||
|
||||
var lawfulImageTypes = []string{"image/png", "image/jpeg", "image/gif", "image/bmp"}
|
||||
|
||||
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实例
|
||||
func NewQQBot(cli *client.QQClient, conf *config.Config) *CQBot {
|
||||
bot := &CQBot{
|
||||
@ -122,8 +166,10 @@ func NewQQBot(cli *client.QQClient, conf *config.Config) *CQBot {
|
||||
}
|
||||
|
||||
// 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.lock.Unlock()
|
||||
}
|
||||
|
||||
// GetMessage 获取给定消息id对应的消息
|
||||
@ -143,11 +189,24 @@ func (bot *CQBot) GetMessage(mid int32) MSG {
|
||||
}
|
||||
|
||||
// UploadLocalImageAsGroup 上传本地图片至群聊
|
||||
func (bot *CQBot) UploadLocalImageAsGroup(groupCode int64, img *LocalImageElement) (*message.GroupImageElement, error) {
|
||||
if img.Stream != nil {
|
||||
return bot.Client.UploadGroupImage(groupCode, img.Stream)
|
||||
func (bot *CQBot) UploadLocalImageAsGroup(groupCode int64, img *LocalImageElement) (i *message.GroupImageElement, 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
|
||||
}
|
||||
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 上传本地短视频至群聊
|
||||
@ -168,76 +227,61 @@ func (bot *CQBot) UploadLocalVideo(target int64, v *LocalVideoElement) (*message
|
||||
}
|
||||
|
||||
// UploadLocalImageAsPrivate 上传本地图片至私聊
|
||||
func (bot *CQBot) UploadLocalImageAsPrivate(userID int64, img *LocalImageElement) (*message.FriendImageElement, error) {
|
||||
func (bot *CQBot) UploadLocalImageAsPrivate(userID int64, img *LocalImageElement) (i *message.FriendImageElement, err error) {
|
||||
if img.Stream != nil {
|
||||
return bot.Client.UploadPrivateImage(userID, img.Stream)
|
||||
i, err = bot.Client.UploadPrivateImage(userID, img.Stream)
|
||||
} else {
|
||||
// need update.
|
||||
f, e := os.Open(img.File)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
defer f.Close()
|
||||
i, err = bot.Client.UploadPrivateImage(userID, f)
|
||||
}
|
||||
// need update.
|
||||
f, err := os.Open(img.File)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
if i != nil {
|
||||
i.Flash = img.Flash
|
||||
}
|
||||
defer f.Close()
|
||||
return bot.Client.UploadPrivateImage(userID, f)
|
||||
return
|
||||
}
|
||||
|
||||
// SendGroupMessage 发送群消息
|
||||
func (bot *CQBot) SendGroupMessage(groupID int64, m *message.SendingMessage) int32 {
|
||||
newElem := make([]message.IMessageElement, 0, len(m.Elements))
|
||||
group := bot.Client.FindGroup(groupID)
|
||||
for _, elem := range m.Elements {
|
||||
if i, ok := elem.(*LocalImageElement); ok {
|
||||
gm, err := bot.UploadLocalImageAsGroup(groupID, i)
|
||||
for _, e := range m.Elements {
|
||||
switch i := e.(type) {
|
||||
case *LocalImageElement, *message.VoiceElement, *LocalVideoElement:
|
||||
i, err := bot.uploadMedia(i, groupID, true)
|
||||
if err != nil {
|
||||
log.Warnf("警告: 群 %v 消息图片上传失败: %v", groupID, err)
|
||||
log.Warnf("警告: 群 %d 消息%s上传失败: %v", groupID, e.Type().String(), err)
|
||||
continue
|
||||
}
|
||||
newElem = append(newElem, gm)
|
||||
continue
|
||||
}
|
||||
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 {
|
||||
e = i
|
||||
case *PokeElement:
|
||||
if group != nil {
|
||||
if mem := group.FindMember(i.Target); mem != nil {
|
||||
mem.Poke()
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
||||
if i, ok := elem.(*GiftElement); ok {
|
||||
return 0
|
||||
case *GiftElement:
|
||||
bot.Client.SendGroupGift(uint64(groupID), uint64(i.Target), i.GiftID)
|
||||
return 0
|
||||
}
|
||||
if i, ok := elem.(*message.MusicShareElement); ok {
|
||||
case *message.MusicShareElement:
|
||||
ret, err := bot.Client.SendGroupMusicShare(groupID, i)
|
||||
if err != nil {
|
||||
log.Warnf("警告: 群 %v 富文本消息发送失败: %v", groupID, err)
|
||||
return -1
|
||||
}
|
||||
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, message.NewText("@全体成员"))
|
||||
continue
|
||||
}
|
||||
newElem = append(newElem, elem)
|
||||
newElem = append(newElem, e)
|
||||
}
|
||||
if len(newElem) == 0 {
|
||||
log.Warnf("群消息发送失败: 消息为空.")
|
||||
@ -256,43 +300,23 @@ func (bot *CQBot) SendGroupMessage(groupID int64, m *message.SendingMessage) int
|
||||
// SendPrivateMessage 发送私聊消息
|
||||
func (bot *CQBot) SendPrivateMessage(target int64, groupID int64, m *message.SendingMessage) int32 {
|
||||
newElem := make([]message.IMessageElement, 0, len(m.Elements))
|
||||
for _, elem := range m.Elements {
|
||||
if i, ok := elem.(*LocalImageElement); ok {
|
||||
fm, err := bot.UploadLocalImageAsPrivate(target, i)
|
||||
for _, e := range m.Elements {
|
||||
switch i := e.(type) {
|
||||
case *LocalImageElement, *message.VoiceElement, *LocalVideoElement:
|
||||
i, err := bot.uploadMedia(i, groupID, false)
|
||||
if err != nil {
|
||||
log.Warnf("警告: 私聊 %v 消息图片上传失败.", target)
|
||||
log.Warnf("警告: 私聊 %d 消息%s上传失败: %v", target, e.Type().String(), err)
|
||||
continue
|
||||
}
|
||||
newElem = append(newElem, fm)
|
||||
continue
|
||||
}
|
||||
if i, ok := elem.(*PokeElement); ok {
|
||||
e = i
|
||||
case *PokeElement:
|
||||
bot.Client.SendFriendPoke(i.Target)
|
||||
return 0
|
||||
}
|
||||
if i, ok := elem.(*message.VoiceElement); ok {
|
||||
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)
|
||||
case *message.MusicShareElement:
|
||||
bot.Client.SendFriendMusicShare(groupID, i)
|
||||
return 0
|
||||
}
|
||||
newElem = append(newElem, elem)
|
||||
newElem = append(newElem, e)
|
||||
}
|
||||
if len(newElem) == 0 {
|
||||
log.Warnf("好友消息发送失败: 消息为空.")
|
||||
@ -440,12 +464,14 @@ func (bot *CQBot) Release() {
|
||||
}
|
||||
|
||||
func (bot *CQBot) dispatchEventMessage(m MSG) {
|
||||
buf := global.NewBuffer()
|
||||
bot.lock.RLock()
|
||||
defer bot.lock.RUnlock()
|
||||
|
||||
event := &Event{RawMsg: m}
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(len(bot.events))
|
||||
_ = json.NewEncoder(buf).Encode(m)
|
||||
for _, f := range bot.events {
|
||||
go func(fn func(*bytes.Buffer)) {
|
||||
go func(fn func(*Event)) {
|
||||
defer func() {
|
||||
wg.Done()
|
||||
if pan := recover(); pan != nil {
|
||||
@ -454,7 +480,7 @@ func (bot *CQBot) dispatchEventMessage(m MSG) {
|
||||
}()
|
||||
|
||||
start := time.Now()
|
||||
fn(buf)
|
||||
fn(event)
|
||||
end := time.Now()
|
||||
if end.Sub(start) > time.Second*5 {
|
||||
log.Debugf("警告: 事件处理耗时超过 5 秒 (%v), 请检查应用是否有堵塞.", end.Sub(start))
|
||||
@ -462,7 +488,7 @@ func (bot *CQBot) dispatchEventMessage(m MSG) {
|
||||
}(f)
|
||||
}
|
||||
wg.Wait()
|
||||
global.PutBuffer(buf)
|
||||
global.PutBuffer(event.buffer)
|
||||
}
|
||||
|
||||
func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) MSG {
|
||||
@ -518,18 +544,16 @@ func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) MSG {
|
||||
}
|
||||
}
|
||||
ms := gm["sender"].(MSG)
|
||||
ms["role"] = func() string {
|
||||
switch mem.Permission {
|
||||
case client.Owner:
|
||||
return "owner"
|
||||
case client.Administrator:
|
||||
return "admin"
|
||||
case client.Member:
|
||||
return "member"
|
||||
default:
|
||||
return "member"
|
||||
}
|
||||
}()
|
||||
switch mem.Permission {
|
||||
case client.Owner:
|
||||
ms["role"] = "owner"
|
||||
case client.Administrator:
|
||||
ms["role"] = "admin"
|
||||
case client.Member:
|
||||
ms["role"] = "member"
|
||||
default:
|
||||
ms["role"] = "member"
|
||||
}
|
||||
ms["nickname"] = mem.Nickname
|
||||
ms["card"] = mem.CardName
|
||||
ms["title"] = mem.SpecialTitle
|
||||
@ -548,8 +572,38 @@ func formatMemberName(mem *client.GroupMemberInfo) string {
|
||||
return fmt.Sprintf("%s(%d)", mem.DisplayName(), mem.Uin)
|
||||
}
|
||||
|
||||
// ToJSON 生成JSON字符串
|
||||
func (m MSG) ToJSON() string {
|
||||
b, _ := json.Marshal(m)
|
||||
return string(b)
|
||||
func (bot *CQBot) uploadMedia(raw message.IMessageElement, target int64, group bool) (message.IMessageElement, error) {
|
||||
switch m := raw.(type) {
|
||||
case *LocalImageElement:
|
||||
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, ""
|
||||
}
|
||||
mime := t.String()
|
||||
return mimetype.EqualsAny(mime, lawfulImageTypes...), mime
|
||||
}
|
||||
|
236
coolq/cqcode.go
236
coolq/cqcode.go
@ -9,7 +9,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"os"
|
||||
@ -19,6 +18,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
|
||||
"github.com/Mrs4s/MiraiGo/binary"
|
||||
"github.com/Mrs4s/MiraiGo/message"
|
||||
"github.com/Mrs4s/MiraiGo/utils"
|
||||
@ -65,9 +66,11 @@ type GiftElement struct {
|
||||
|
||||
// LocalImageElement 本地图片
|
||||
type LocalImageElement struct {
|
||||
message.ImageElement
|
||||
Stream io.ReadSeeker
|
||||
File string
|
||||
|
||||
Flash bool
|
||||
EffectID int32
|
||||
}
|
||||
|
||||
// LocalVoiceElement 本地语音
|
||||
@ -83,6 +86,11 @@ type LocalVideoElement struct {
|
||||
thumb io.ReadSeeker
|
||||
}
|
||||
|
||||
// Type implements the message.IMessageElement.
|
||||
func (e *LocalImageElement) Type() message.ElementType {
|
||||
return message.Image
|
||||
}
|
||||
|
||||
// Type 获取元素类型ID
|
||||
func (e *GiftElement) Type() message.ElementType {
|
||||
// Make message.IMessageElement Happy
|
||||
@ -131,7 +139,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
|
||||
r = append(r, MSG{
|
||||
"type": "reply",
|
||||
"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),
|
||||
"qq": strconv.FormatInt(replyElem.Sender, 10),
|
||||
"time": strconv.FormatInt(int64(replyElem.Time), 10),
|
||||
@ -141,7 +149,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
|
||||
} else {
|
||||
r = append(r, MSG{
|
||||
"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 +157,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
|
||||
var m MSG
|
||||
switch o := elem.(type) {
|
||||
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)
|
||||
if ok && elem.Target == o.Sender {
|
||||
e[i+1] = nil
|
||||
@ -174,7 +182,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
|
||||
} else {
|
||||
m = MSG{
|
||||
"type": "at",
|
||||
"data": map[string]string{"qq": fmt.Sprint(o.Target)},
|
||||
"data": map[string]string{"qq": strconv.FormatInt(o.Target, 10)},
|
||||
}
|
||||
}
|
||||
case *message.RedBagElement:
|
||||
@ -190,7 +198,7 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
|
||||
case *message.FaceElement:
|
||||
m = MSG{
|
||||
"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:
|
||||
m = MSG{
|
||||
@ -202,41 +210,38 @@ func ToArrayMessage(e []message.IMessageElement, groupID int64) (r []MSG) {
|
||||
"type": "video",
|
||||
"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:
|
||||
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{
|
||||
"type": "image",
|
||||
"data": map[string]string{"file": hex.EncodeToString(o.Md5) + ".image", "url": o.Url},
|
||||
"data": data,
|
||||
}
|
||||
case *message.FriendImageElement:
|
||||
data := map[string]string{"file": hex.EncodeToString(o.Md5) + ".image", "url": o.Url}
|
||||
if o.Flash {
|
||||
data["type"] = "flash"
|
||||
}
|
||||
m = MSG{
|
||||
"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:
|
||||
if isOk := strings.Contains(o.Content, "<?xml"); isOk {
|
||||
m = MSG{
|
||||
"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 {
|
||||
m = MSG{
|
||||
"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:
|
||||
@ -316,28 +321,28 @@ func ToStringMessage(e []message.IMessageElement, groupID int64, isRaw ...bool)
|
||||
} else {
|
||||
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:
|
||||
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 {
|
||||
write("[CQ:image,file=%s]", hex.EncodeToString(o.Md5)+".image")
|
||||
write("[CQ:image,file=%s%s]", hex.EncodeToString(o.Md5)+".image", arg)
|
||||
} 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:
|
||||
if ur {
|
||||
write("[CQ:image,file=%s]", hex.EncodeToString(o.Md5)+".image")
|
||||
} else {
|
||||
write("[CQ:image,file=%s,url=%s]", hex.EncodeToString(o.Md5)+".image", CQCodeEscapeValue(o.Url))
|
||||
var arg string
|
||||
if o.Flash {
|
||||
arg = ",type=flash"
|
||||
}
|
||||
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:
|
||||
if isOk := strings.Contains(o.Content, "<?xml"); isOk {
|
||||
write(`[CQ:xml,data=%s,resid=%d]`, CQCodeEscapeValue(o.Content), o.Id)
|
||||
@ -544,9 +549,9 @@ func (bot *CQBot) ConvertObjectMessage(m gjson.Result, isGroup bool) (r []messag
|
||||
case customText != "":
|
||||
var elem *message.ReplyElement
|
||||
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 {
|
||||
log.Warnf("警告: 自定义 Reply 元素中必须包含 Uin 或 id")
|
||||
log.Warnf("警告: 自定义 Reply 元素中必须包含 user_id 或 id")
|
||||
break
|
||||
}
|
||||
msgTime, timeErr := strconv.ParseInt(e.Get("data.time").String(), 10, 64)
|
||||
@ -668,38 +673,29 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
|
||||
return nil, err
|
||||
}
|
||||
tp := d["type"]
|
||||
if tp != "show" && tp != "flash" {
|
||||
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
|
||||
}
|
||||
}
|
||||
flash, id := false, int64(0)
|
||||
switch tp {
|
||||
case "flash":
|
||||
if i, ok := img.(*message.GroupImageElement); ok {
|
||||
return &message.GroupFlashPicElement{GroupImageElement: *i}, nil
|
||||
}
|
||||
if i, ok := img.(*message.FriendImageElement); ok {
|
||||
return &message.FriendFlashPicElement{FriendImageElement: *i}, nil
|
||||
}
|
||||
flash = true
|
||||
case "show":
|
||||
id, _ := strconv.ParseInt(d["id"], 10, 64)
|
||||
id, _ = strconv.ParseInt(d["id"], 10, 64)
|
||||
if id < 40000 || id >= 40006 {
|
||||
id = 40000
|
||||
}
|
||||
if i, ok := img.(*message.GroupImageElement); ok {
|
||||
return &message.GroupShowPicElement{GroupImageElement: *i, EffectId: int32(id)}, nil
|
||||
}
|
||||
return img, nil
|
||||
default:
|
||||
return img, err
|
||||
}
|
||||
|
||||
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":
|
||||
t, _ := strconv.ParseInt(d["qq"], 10, 64)
|
||||
return &PokeElement{Target: t}, nil
|
||||
@ -734,6 +730,12 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !SkipMimeScan && !global.IsAMRorSILK(data) {
|
||||
mt := mimetype.Detect(data).String()
|
||||
if !mimetype.EqualsAny(mt, lawfulAudioTypes...) {
|
||||
return nil, errors.New("audio type error: " + mt)
|
||||
}
|
||||
}
|
||||
if !global.IsAMRorSILK(data) {
|
||||
data, err = global.EncoderSilk(data)
|
||||
if err != nil {
|
||||
@ -835,7 +837,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
|
||||
Summary: d["content"],
|
||||
Url: d["url"],
|
||||
PictureUrl: d["image"],
|
||||
MusicUrl: d["purl"],
|
||||
MusicUrl: d["audio"],
|
||||
}, 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>`,
|
||||
@ -902,7 +904,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
|
||||
data, _ = global.FindFile(cover, cache, global.ImagePath)
|
||||
} else {
|
||||
_ = global.ExtractCover(v.File, v.File+".jpg")
|
||||
data, _ = ioutil.ReadFile(v.File + ".jpg")
|
||||
data, _ = os.ReadFile(v.File + ".jpg")
|
||||
}
|
||||
v.thumb = bytes.NewReader(data)
|
||||
video, _ := os.Open(v.File)
|
||||
@ -934,13 +936,13 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
|
||||
default:
|
||||
return nil, errors.New("unsupported cq code: " + t)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// XMLEscape 将字符串c转义为XML字符串
|
||||
func XMLEscape(c string) string {
|
||||
buf := new(bytes.Buffer)
|
||||
_ = xml2.EscapeText(buf, []byte(c))
|
||||
buf := global.NewBuffer()
|
||||
defer global.PutBuffer(buf)
|
||||
_ = xml2.EscapeText(buf, utils.S2B(c))
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
@ -953,12 +955,39 @@ func XMLEscape(c string) string {
|
||||
] -> ]
|
||||
|
||||
*/
|
||||
func CQCodeEscapeText(raw string) string {
|
||||
ret := raw
|
||||
ret = strings.ReplaceAll(ret, "&", "&")
|
||||
ret = strings.ReplaceAll(ret, "[", "[")
|
||||
ret = strings.ReplaceAll(ret, "]", "]")
|
||||
return ret
|
||||
func CQCodeEscapeText(s string) string {
|
||||
count := strings.Count(s, "&")
|
||||
count += strings.Count(s, "[")
|
||||
count += strings.Count(s, "]")
|
||||
if count == 0 {
|
||||
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("&")
|
||||
case '[':
|
||||
b.WriteString("[")
|
||||
case ']':
|
||||
b.WriteString("]")
|
||||
}
|
||||
start = j + 1
|
||||
}
|
||||
b.WriteString(s[start:])
|
||||
return b.String()
|
||||
}
|
||||
|
||||
/*CQCodeEscapeValue 将字符串value中部分字符转义
|
||||
@ -1086,7 +1115,7 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
|
||||
return nil, errors.New("invalid video")
|
||||
}
|
||||
if path.Ext(rawPath) == ".video" {
|
||||
b, _ := ioutil.ReadFile(rawPath)
|
||||
b, _ := os.ReadFile(rawPath)
|
||||
r := binary.NewReader(b)
|
||||
return &LocalVideoElement{ShortVideoElement: message.ShortVideoElement{ // todo 检查缓存是否有效
|
||||
Md5: r.ReadBytes(16),
|
||||
@ -1179,39 +1208,26 @@ func (bot *CQBot) makeShowPic(elem message.IMessageElement, source string, brief
|
||||
if brief == "" {
|
||||
brief = "[分享]我看到一张很赞的图片,分享给你,快来看!"
|
||||
}
|
||||
if i, ok := elem.(*LocalImageElement); ok {
|
||||
if !group {
|
||||
gm, err := bot.UploadLocalImageAsPrivate(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)
|
||||
} 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)
|
||||
if _, ok := elem.(*LocalImageElement); ok {
|
||||
r := rand.Uint32()
|
||||
e, err := bot.uploadMedia(elem, int64(r), group)
|
||||
if err != nil {
|
||||
log.Warnf("警告: 图片上传失败: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
elem = e
|
||||
}
|
||||
|
||||
if i, ok := elem.(*message.GroupImageElement); ok {
|
||||
switch i := elem.(type) {
|
||||
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)
|
||||
suf = i
|
||||
}
|
||||
if i, ok := elem.(*message.FriendImageElement); ok {
|
||||
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
|
||||
if xml == "" {
|
||||
return nil, errors.New("生成xml图片消息失败")
|
||||
}
|
||||
if xml != "" {
|
||||
// log.Warn(xml)
|
||||
ret := []message.IMessageElement{suf}
|
||||
ret = append(ret, message.NewRichXml(xml, 5))
|
||||
return ret, nil
|
||||
}
|
||||
return nil, errors.New("生成xml图片消息失败")
|
||||
ret := []message.IMessageElement{suf, message.NewRichXml(xml, 5)}
|
||||
return ret, nil
|
||||
}
|
||||
|
@ -2,8 +2,11 @@ package coolq
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Mrs4s/MiraiGo/utils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
@ -30,3 +33,32 @@ func BenchmarkCQBot_ConvertObjectMessage(b *testing.B) {
|
||||
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, "&", "&")
|
||||
ret = strings.ReplaceAll(ret, "[", "[")
|
||||
ret = strings.ReplaceAll(ret, "]", "]")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCQCodeEscapeText(t *testing.T) {
|
||||
for i := 0; i < 200; i++ {
|
||||
rs := utils.RandomStringRange(3000, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890[]&")
|
||||
ret := rs
|
||||
ret = strings.ReplaceAll(ret, "&", "&")
|
||||
ret = strings.ReplaceAll(ret, "[", "[")
|
||||
ret = strings.ReplaceAll(ret, "]", "]")
|
||||
assert.Equal(t, ret, CQCodeEscapeText(rs))
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package coolq
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -542,59 +542,26 @@ func (bot *CQBot) groupDecrease(groupCode, userUin int64, operator *client.Group
|
||||
func (bot *CQBot) checkMedia(e []message.IMessageElement) {
|
||||
for _, elem := range e {
|
||||
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:
|
||||
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) {
|
||||
_ = os.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
w.Write(i.Md5)
|
||||
w.WriteUInt32(uint32(i.Size))
|
||||
w.WriteString(filename)
|
||||
w.WriteString(i.ImageId)
|
||||
w.WriteString(i.Url)
|
||||
}), 0o644)
|
||||
}
|
||||
case *message.FriendImageElement:
|
||||
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) {
|
||||
_ = os.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
w.Write(i.Md5)
|
||||
w.WriteUInt32(uint32(0)) // 发送时会调用url, 大概没事
|
||||
w.WriteString(filename)
|
||||
w.WriteUInt32(uint32(i.Size))
|
||||
w.WriteString(i.ImageId)
|
||||
w.WriteString(i.Url)
|
||||
}), 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:
|
||||
i.Name = strings.ReplaceAll(i.Name, "{", "")
|
||||
i.Name = strings.ReplaceAll(i.Name, "}", "")
|
||||
@ -604,12 +571,12 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
|
||||
log.Warnf("语音文件 %v 下载失败: %v", i.Name, err)
|
||||
continue
|
||||
}
|
||||
_ = ioutil.WriteFile(path.Join(global.VoicePath, i.Name), b, 0o644)
|
||||
_ = os.WriteFile(path.Join(global.VoicePath, i.Name), b, 0o644)
|
||||
}
|
||||
case *message.ShortVideoElement:
|
||||
filename := hex.EncodeToString(i.Md5) + ".video"
|
||||
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.ThumbMd5)
|
||||
w.WriteUInt32(uint32(i.Size))
|
||||
|
@ -119,6 +119,12 @@ servers:
|
||||
host: 127.0.0.1
|
||||
# pprof服务器监听端口
|
||||
port: 7700
|
||||
|
||||
# LambdaServer 配置
|
||||
- lambda:
|
||||
type: scf # 可用 scf,aws (aws未经过测试)
|
||||
middlewares:
|
||||
<<: *default # 引用默认中间件
|
||||
|
||||
# 可添加更多
|
||||
#- ws-reverse:
|
||||
@ -208,4 +214,15 @@ database: # 数据库相关设置
|
||||
````
|
||||
1.1.1.1:53
|
||||
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).
|
@ -351,7 +351,7 @@ Type: `video`
|
||||
| `file` | string | 支持http和file发送 |
|
||||
| `cover` | string | 视频封面,支持http,file和base64发送,格式必须为jpg |
|
||||
| `c` | `2` `3` | 通过网络下载视频时的线程数, 默认单线程. (在资源不支持并发时会自动处理) |
|
||||
示例: `[CQ:image,file=file:///C:\\Users\Richard\Pictures\1.mp4]`
|
||||
示例: `[CQ:video,file=file:///C:\\Users\Richard\Videos\1.mp4]`
|
||||
|
||||
### XML 消息
|
||||
|
||||
|
@ -11,7 +11,7 @@ var bufferPool = sync.Pool{
|
||||
},
|
||||
}
|
||||
|
||||
// NewBuffer 从池钟获取新 bytes.Buffer
|
||||
// NewBuffer 从池中获取新 bytes.Buffer
|
||||
func NewBuffer() *bytes.Buffer {
|
||||
return bufferPool.Get().(*bytes.Buffer)
|
||||
}
|
||||
@ -20,7 +20,7 @@ func NewBuffer() *bytes.Buffer {
|
||||
func PutBuffer(buf *bytes.Buffer) {
|
||||
// See https://golang.org/issue/23199
|
||||
const maxSize = 1 << 16
|
||||
if buf.Cap() < maxSize { // 对于大Buffer直接丢弃
|
||||
if buf != nil && buf.Cap() < maxSize { // 对于大Buffer直接丢弃
|
||||
buf.Reset()
|
||||
bufferPool.Put(buf)
|
||||
}
|
||||
|
@ -2,8 +2,8 @@ package global
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"encoding/hex"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
|
||||
@ -19,9 +19,9 @@ func EncoderSilk(data []byte) ([]byte, error) {
|
||||
if err != nil {
|
||||
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) {
|
||||
return ioutil.ReadFile(silkPath)
|
||||
return os.ReadFile(silkPath)
|
||||
}
|
||||
slk, err := codec.EncodeToSilk(data, tempName, true)
|
||||
if err != nil {
|
||||
|
@ -1,11 +1,11 @@
|
||||
//go:build (linux || (windows && !arm) || darwin) && (386 || amd64 || arm || arm64) && !race
|
||||
// +build linux windows,!arm darwin
|
||||
// +build 386 amd64 arm arm64
|
||||
// +build !race
|
||||
|
||||
// Package codec Slik编码核心模块
|
||||
package codec
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
@ -14,15 +14,13 @@ import (
|
||||
"github.com/wdvxdr1123/go-silk"
|
||||
)
|
||||
|
||||
const (
|
||||
silkCachePath = "data/cache"
|
||||
)
|
||||
const silkCachePath = "data/cache"
|
||||
|
||||
// EncodeToSilk 将音频编码为Silk
|
||||
func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte, err error) {
|
||||
// 1. 写入缓存文件
|
||||
rawPath := path.Join(silkCachePath, tempName+".wav")
|
||||
err = ioutil.WriteFile(rawPath, record, os.ModePerm)
|
||||
err = os.WriteFile(rawPath, record, os.ModePerm)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "write temp file error")
|
||||
}
|
||||
@ -37,7 +35,7 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte
|
||||
defer os.Remove(pcmPath)
|
||||
|
||||
// 3. 转silk
|
||||
pcm, err := ioutil.ReadFile(pcmPath)
|
||||
pcm, err := os.ReadFile(pcmPath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "read pcm file err")
|
||||
}
|
||||
@ -47,7 +45,7 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte
|
||||
}
|
||||
if useCache {
|
||||
silkPath := path.Join(silkCachePath, tempName+".silk")
|
||||
err = ioutil.WriteFile(silkPath, silkWav, 0o666)
|
||||
err = os.WriteFile(silkPath, silkWav, 0o666)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
// +build !arm,!arm64,!amd64,!386
|
||||
//go:build (!arm && !arm64 && !amd64 && !386) || race
|
||||
// +build !arm,!arm64,!amd64,!386 race
|
||||
|
||||
package codec
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
//go:build !windows && !linux && !darwin
|
||||
// +build !windows,!linux,!darwin
|
||||
|
||||
package codec
|
||||
|
2
global/codec/doc.go
Normal file
2
global/codec/doc.go
Normal file
@ -0,0 +1,2 @@
|
||||
// Package codec Slik编码核心模块
|
||||
package codec
|
@ -7,9 +7,12 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/Mrs4s/go-cqhttp/global"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
@ -53,11 +56,14 @@ type Config struct {
|
||||
ReportSelfMessage bool `yaml:"report-self-message"`
|
||||
RemoveReplyAt bool `yaml:"remove-reply-at"`
|
||||
ExtraReplyData bool `yaml:"extra-reply-data"`
|
||||
SkipMimeScan bool `yaml:"skip-mime-scan"`
|
||||
} `yaml:"message"`
|
||||
|
||||
Output struct {
|
||||
LogLevel string `yaml:"log-level"`
|
||||
Debug bool `yaml:"debug"`
|
||||
LogLevel string `yaml:"log-level"`
|
||||
LogAging int `yaml:"log-aging"`
|
||||
LogForceNew bool `yaml:"log-force-new"`
|
||||
Debug bool `yaml:"debug"`
|
||||
} `yaml:"output"`
|
||||
|
||||
Servers []map[string]yaml.Node `yaml:"servers"`
|
||||
@ -77,11 +83,15 @@ type MiddleWares struct {
|
||||
|
||||
// HTTPServer HTTP通信相关配置
|
||||
type HTTPServer struct {
|
||||
Disabled bool `yaml:"disabled"`
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
Timeout int32 `yaml:"timeout"`
|
||||
Post []struct {
|
||||
Disabled bool `yaml:"disabled"`
|
||||
Host string `yaml:"host"`
|
||||
Port int `yaml:"port"`
|
||||
Timeout int32 `yaml:"timeout"`
|
||||
LongPolling struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
MaxQueueSize int `yaml:"max-queue-size"`
|
||||
} `yaml:"long-polling"`
|
||||
Post []struct {
|
||||
URL string `yaml:"url"`
|
||||
Secret string `yaml:"secret"`
|
||||
}
|
||||
@ -116,6 +126,14 @@ type WebsocketReverse struct {
|
||||
MiddleWares `yaml:"middlewares"`
|
||||
}
|
||||
|
||||
// LambdaServer 云函数配置
|
||||
type LambdaServer struct {
|
||||
Disabled bool `yaml:"disabled"`
|
||||
Type string `yaml:"type"`
|
||||
|
||||
MiddleWares `yaml:"middlewares"`
|
||||
}
|
||||
|
||||
// LevelDBConfig leveldb 相关配置
|
||||
type LevelDBConfig struct {
|
||||
Enable bool `yaml:"enable"`
|
||||
@ -129,15 +147,83 @@ var (
|
||||
// Get 从默认配置文件路径中获取
|
||||
func Get() *Config {
|
||||
once.Do(func() {
|
||||
hasEnvironmentConf := os.Getenv("GCQ_UIN") != ""
|
||||
|
||||
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()
|
||||
os.Exit(0)
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
config = &Config{}
|
||||
if err = yaml.NewDecoder(file).Decode(config); err != nil {
|
||||
log.Fatal("配置文件不合法!", err)
|
||||
|
||||
// type convert tools
|
||||
toInt64 := func(str string) int64 {
|
||||
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))
|
||||
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
|
||||
@ -162,6 +248,7 @@ func generateConfig() {
|
||||
> 2: 正向 Websocket 通信
|
||||
> 3: 反向 Websocket 通信
|
||||
> 4: pprof 性能分析服务器
|
||||
> 5: 云函数服务
|
||||
请输入你需要的编号,可输入多个,同一编号也可输入多个(如: 233)
|
||||
您的选择是:`)
|
||||
input := bufio.NewReader(os.Stdin)
|
||||
@ -179,6 +266,8 @@ func generateConfig() {
|
||||
sb.WriteString(wsReverseDefault)
|
||||
case '4':
|
||||
sb.WriteString(pprofDefault)
|
||||
case '5':
|
||||
sb.WriteString(lambdaDefault)
|
||||
}
|
||||
}
|
||||
_ = os.WriteFile("config.yml", []byte(sb.String()), 0o644)
|
||||
@ -195,6 +284,12 @@ const httpDefault = ` # HTTP 通信设置
|
||||
# 反向HTTP超时时间, 单位秒
|
||||
# 最小值为5,小于5将会忽略本项设置
|
||||
timeout: 5
|
||||
# 长轮询拓展
|
||||
long-polling:
|
||||
# 是否开启
|
||||
enabled: false
|
||||
# 消息队列大小,0 表示不限制队列大小,谨慎使用
|
||||
max-queue-size: 2000
|
||||
middlewares:
|
||||
<<: *default # 引用默认中间件
|
||||
# 反向HTTP POST地址列表
|
||||
@ -205,6 +300,13 @@ const httpDefault = ` # HTTP 通信设置
|
||||
# secret: '' # 密钥
|
||||
`
|
||||
|
||||
const lambdaDefault = ` # LambdaServer 配置
|
||||
- lambda:
|
||||
type: scf # scf: 腾讯云函数 aws: aws Lambda
|
||||
middlewares:
|
||||
<<: *default # 引用默认中间件
|
||||
`
|
||||
|
||||
const wsDefault = ` # 正向WS设置
|
||||
- ws:
|
||||
# 正向WS服务器监听地址
|
||||
|
@ -39,10 +39,16 @@ message:
|
||||
remove-reply-at: false
|
||||
# 为Reply附加更多信息
|
||||
extra-reply-data: false
|
||||
# 跳过 Mime 扫描, 忽略错误数据
|
||||
skip-mime-scan: false
|
||||
|
||||
output:
|
||||
# 日志等级 trace,debug,info,warn,error
|
||||
log-level: warn
|
||||
# 日志时效 单位天. 超过这个时间之前的日志将会被自动删除. 设置为 0 表示永久保留.
|
||||
log-aging: 15
|
||||
# 是否在每次启动时强制创建全新的文件储存日志. 为 false 的情况下将会在上次启动时创建的日志文件续写
|
||||
log-force-new: true
|
||||
# 是否启用 DEBUG
|
||||
debug: false # 开启调试模式
|
||||
|
||||
|
111
global/filter.go
111
global/filter.go
@ -17,35 +17,33 @@ type operationNode struct {
|
||||
filter Filter
|
||||
}
|
||||
|
||||
// NotOperator 定义了过滤器中Not操作符
|
||||
type NotOperator struct {
|
||||
// notOperator 定义了过滤器中Not操作符
|
||||
type notOperator struct {
|
||||
operand Filter
|
||||
}
|
||||
|
||||
func notOperatorConstruct(argument gjson.Result) *NotOperator {
|
||||
func newNotOp(argument gjson.Result) Filter {
|
||||
if !argument.IsObject() {
|
||||
panic("the argument of 'not' operator must be an object")
|
||||
}
|
||||
op := new(NotOperator)
|
||||
op.operand = Generate("and", argument)
|
||||
return op
|
||||
return ¬Operator{operand: Generate("and", argument)}
|
||||
}
|
||||
|
||||
// Eval 对payload执行Not过滤
|
||||
func (op *NotOperator) Eval(payload gjson.Result) bool {
|
||||
func (op *notOperator) Eval(payload gjson.Result) bool {
|
||||
return !op.operand.Eval(payload)
|
||||
}
|
||||
|
||||
// AndOperator 定义了过滤器中And操作符
|
||||
type AndOperator struct {
|
||||
// andOperator 定义了过滤器中And操作符
|
||||
type andOperator struct {
|
||||
operands []operationNode
|
||||
}
|
||||
|
||||
func andOperatorConstruct(argument gjson.Result) *AndOperator {
|
||||
func newAndOp(argument gjson.Result) Filter {
|
||||
if !argument.IsObject() {
|
||||
panic("the argument of 'and' operator must be an object")
|
||||
}
|
||||
op := new(AndOperator)
|
||||
op := new(andOperator)
|
||||
argument.ForEach(func(key, value gjson.Result) bool {
|
||||
switch {
|
||||
case key.Str[0] == '.':
|
||||
@ -74,7 +72,7 @@ func andOperatorConstruct(argument gjson.Result) *AndOperator {
|
||||
}
|
||||
|
||||
// Eval 对payload执行And过滤
|
||||
func (op *AndOperator) Eval(payload gjson.Result) bool {
|
||||
func (op *andOperator) Eval(payload gjson.Result) bool {
|
||||
res := true
|
||||
for _, operand := range op.operands {
|
||||
if len(operand.key) == 0 {
|
||||
@ -93,16 +91,16 @@ func (op *AndOperator) Eval(payload gjson.Result) bool {
|
||||
return res
|
||||
}
|
||||
|
||||
// OrOperator 定义了过滤器中Or操作符
|
||||
type OrOperator struct {
|
||||
// orOperator 定义了过滤器中Or操作符
|
||||
type orOperator struct {
|
||||
operands []Filter
|
||||
}
|
||||
|
||||
func orOperatorConstruct(argument gjson.Result) *OrOperator {
|
||||
func newOrOp(argument gjson.Result) Filter {
|
||||
if !argument.IsArray() {
|
||||
panic("the argument of 'or' operator must be an array")
|
||||
}
|
||||
op := new(OrOperator)
|
||||
op := new(orOperator)
|
||||
argument.ForEach(func(_, value gjson.Result) bool {
|
||||
op.operands = append(op.operands, Generate("and", value))
|
||||
return true
|
||||
@ -111,7 +109,7 @@ func orOperatorConstruct(argument gjson.Result) *OrOperator {
|
||||
}
|
||||
|
||||
// Eval 对payload执行Or过滤
|
||||
func (op *OrOperator) Eval(payload gjson.Result) bool {
|
||||
func (op *orOperator) Eval(payload gjson.Result) bool {
|
||||
res := false
|
||||
for _, operand := range op.operands {
|
||||
res = res || operand.Eval(payload)
|
||||
@ -122,49 +120,45 @@ func (op *OrOperator) Eval(payload gjson.Result) bool {
|
||||
return res
|
||||
}
|
||||
|
||||
// EqualOperator 定义了过滤器中Equal操作符
|
||||
type EqualOperator struct {
|
||||
// eqOperator 定义了过滤器中Equal操作符
|
||||
type eqOperator struct {
|
||||
operand string
|
||||
}
|
||||
|
||||
func equalOperatorConstruct(argument gjson.Result) *EqualOperator {
|
||||
op := new(EqualOperator)
|
||||
op.operand = argument.String()
|
||||
return op
|
||||
func newEqOp(argument gjson.Result) Filter {
|
||||
return &eqOperator{operand: argument.String()}
|
||||
}
|
||||
|
||||
// Eval 对payload执行Equal过滤
|
||||
func (op *EqualOperator) Eval(payload gjson.Result) bool {
|
||||
func (op *eqOperator) Eval(payload gjson.Result) bool {
|
||||
return payload.String() == op.operand
|
||||
}
|
||||
|
||||
// NotEqualOperator 定义了过滤器中NotEqual操作符
|
||||
type NotEqualOperator struct {
|
||||
// neqOperator 定义了过滤器中NotEqual操作符
|
||||
type neqOperator struct {
|
||||
operand string
|
||||
}
|
||||
|
||||
func notEqualOperatorConstruct(argument gjson.Result) *NotEqualOperator {
|
||||
op := new(NotEqualOperator)
|
||||
op.operand = argument.String()
|
||||
return op
|
||||
func newNeqOp(argument gjson.Result) Filter {
|
||||
return &neqOperator{operand: argument.String()}
|
||||
}
|
||||
|
||||
// Eval 对payload执行NotEqual过滤
|
||||
func (op *NotEqualOperator) Eval(payload gjson.Result) bool {
|
||||
func (op *neqOperator) Eval(payload gjson.Result) bool {
|
||||
return !(payload.String() == op.operand)
|
||||
}
|
||||
|
||||
// InOperator 定义了过滤器中In操作符
|
||||
type InOperator struct {
|
||||
// inOperator 定义了过滤器中In操作符
|
||||
type inOperator struct {
|
||||
operandString string
|
||||
operandArray []string
|
||||
}
|
||||
|
||||
func inOperatorConstruct(argument gjson.Result) *InOperator {
|
||||
func newInOp(argument gjson.Result) Filter {
|
||||
if argument.IsObject() {
|
||||
panic("the argument of 'in' operator must be an array or a string")
|
||||
}
|
||||
op := new(InOperator)
|
||||
op := new(inOperator)
|
||||
if argument.IsArray() {
|
||||
op.operandArray = []string{}
|
||||
argument.ForEach(func(_, value gjson.Result) bool {
|
||||
@ -178,7 +172,7 @@ func inOperatorConstruct(argument gjson.Result) *InOperator {
|
||||
}
|
||||
|
||||
// Eval 对payload执行In过滤
|
||||
func (op *InOperator) Eval(payload gjson.Result) bool {
|
||||
func (op *inOperator) Eval(payload gjson.Result) bool {
|
||||
payloadStr := payload.String()
|
||||
if op.operandArray != nil {
|
||||
for _, value := range op.operandArray {
|
||||
@ -191,64 +185,59 @@ func (op *InOperator) Eval(payload gjson.Result) bool {
|
||||
return strings.Contains(op.operandString, payloadStr)
|
||||
}
|
||||
|
||||
// ContainsOperator 定义了过滤器中Contains操作符
|
||||
type ContainsOperator struct {
|
||||
// containsOperator 定义了过滤器中Contains操作符
|
||||
type containsOperator struct {
|
||||
operand string
|
||||
}
|
||||
|
||||
func containsOperatorConstruct(argument gjson.Result) *ContainsOperator {
|
||||
func newContainOp(argument gjson.Result) Filter {
|
||||
if argument.IsArray() || argument.IsObject() {
|
||||
panic("the argument of 'contains' operator must be a string")
|
||||
}
|
||||
op := new(ContainsOperator)
|
||||
op.operand = argument.String()
|
||||
return op
|
||||
return &containsOperator{operand: argument.String()}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// RegexOperator 定义了过滤器中Regex操作符
|
||||
type RegexOperator struct {
|
||||
// regexOperator 定义了过滤器中Regex操作符
|
||||
type regexOperator struct {
|
||||
regex *regexp.Regexp
|
||||
}
|
||||
|
||||
func regexOperatorConstruct(argument gjson.Result) *RegexOperator {
|
||||
func newRegexOp(argument gjson.Result) Filter {
|
||||
if argument.IsArray() || argument.IsObject() {
|
||||
panic("the argument of 'regex' operator must be a string")
|
||||
}
|
||||
op := new(RegexOperator)
|
||||
op.regex = regexp.MustCompile(argument.String())
|
||||
return op
|
||||
return ®exOperator{regex: regexp.MustCompile(argument.String())}
|
||||
}
|
||||
|
||||
// Eval 对payload执行RegexO过滤
|
||||
func (op *RegexOperator) Eval(payload gjson.Result) bool {
|
||||
matched := op.regex.MatchString(payload.String())
|
||||
return matched
|
||||
func (op *regexOperator) Eval(payload gjson.Result) bool {
|
||||
return op.regex.MatchString(payload.String())
|
||||
}
|
||||
|
||||
// Generate 根据给定操作符名opName及操作符参数argument创建一个过滤器实例
|
||||
func Generate(opName string, argument gjson.Result) Filter {
|
||||
switch opName {
|
||||
case "not":
|
||||
return notOperatorConstruct(argument)
|
||||
return newNotOp(argument)
|
||||
case "and":
|
||||
return andOperatorConstruct(argument)
|
||||
return newAndOp(argument)
|
||||
case "or":
|
||||
return orOperatorConstruct(argument)
|
||||
case "neq":
|
||||
return notEqualOperatorConstruct(argument)
|
||||
return newOrOp(argument)
|
||||
case "eq":
|
||||
return equalOperatorConstruct(argument)
|
||||
return newEqOp(argument)
|
||||
case "neq":
|
||||
return newNeqOp(argument)
|
||||
case "in":
|
||||
return inOperatorConstruct(argument)
|
||||
return newInOp(argument)
|
||||
case "contains":
|
||||
return containsOperatorConstruct(argument)
|
||||
return newContainOp(argument)
|
||||
case "regex":
|
||||
return regexOperatorConstruct(argument)
|
||||
return newRegexOp(argument)
|
||||
default:
|
||||
panic("the operator " + opName + " is not supported")
|
||||
}
|
||||
|
15
global/fs.go
15
global/fs.go
@ -6,7 +6,6 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
@ -51,7 +50,7 @@ func PathExists(path string) bool {
|
||||
|
||||
// ReadAllText 读取给定path对应文件,无法读取时返回空值
|
||||
func ReadAllText(path string) string {
|
||||
b, err := ioutil.ReadFile(path)
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return ""
|
||||
@ -61,7 +60,7 @@ func ReadAllText(path string) string {
|
||||
|
||||
// WriteAllText 将给定text写入给定path
|
||||
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
|
||||
@ -91,10 +90,10 @@ func FindFile(file, cache, p string) (data []byte, err error) {
|
||||
hash := md5.Sum([]byte(file))
|
||||
cacheFile := path.Join(CachePath, hex.EncodeToString(hash[:])+".cache")
|
||||
if PathExists(cacheFile) && cache == "1" {
|
||||
return ioutil.ReadFile(cacheFile)
|
||||
return os.ReadFile(cacheFile)
|
||||
}
|
||||
data, err = GetBytes(file)
|
||||
_ = ioutil.WriteFile(cacheFile, data, 0o644)
|
||||
_ = os.WriteFile(cacheFile, data, 0o644)
|
||||
if err != nil {
|
||||
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` {
|
||||
fu.Path = fu.Path[1:]
|
||||
}
|
||||
data, err = ioutil.ReadFile(fu.Path)
|
||||
data, err = os.ReadFile(fu.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
@ -140,7 +139,7 @@ func DelFile(path string) bool {
|
||||
|
||||
// ReadAddrFile 从给定path中读取合法的IP地址与端口,每个IP地址以换行符"\n"作为分隔
|
||||
func ReadAddrFile(path string) []*net.TCPAddr {
|
||||
d, err := ioutil.ReadFile(path)
|
||||
d, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package global
|
||||
|
||||
import (
|
||||
"math"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -11,18 +12,6 @@ import (
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
var trueSet = map[string]struct{}{
|
||||
"true": {},
|
||||
"yes": {},
|
||||
"1": {},
|
||||
}
|
||||
|
||||
var falseSet = map[string]struct{}{
|
||||
"false": {},
|
||||
"no": {},
|
||||
"0": {},
|
||||
}
|
||||
|
||||
// EnsureBool 判断给定的p是否可表示为合法Bool类型,否则返回defaultVal
|
||||
//
|
||||
// 支持的合法类型有
|
||||
@ -41,27 +30,28 @@ func EnsureBool(p interface{}, defaultVal bool) bool {
|
||||
if !j.Exists() {
|
||||
return defaultVal
|
||||
}
|
||||
if j.Type == gjson.True {
|
||||
switch j.Type { // nolint
|
||||
case gjson.True:
|
||||
return true
|
||||
}
|
||||
if j.Type == gjson.False {
|
||||
case gjson.False:
|
||||
return false
|
||||
}
|
||||
if j.Type != gjson.String {
|
||||
case gjson.String:
|
||||
str = j.Str
|
||||
default:
|
||||
return defaultVal
|
||||
}
|
||||
str = j.Str
|
||||
} else if s, ok := p.(string); ok {
|
||||
str = s
|
||||
}
|
||||
str = strings.ToLower(str)
|
||||
if _, ok := trueSet[str]; ok {
|
||||
switch str {
|
||||
case "true", "yes", "1":
|
||||
return true
|
||||
}
|
||||
if _, ok := falseSet[str]; ok {
|
||||
case "false", "no", "0":
|
||||
return false
|
||||
default:
|
||||
return defaultVal
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
// VersionNameCompare 检查版本名是否需要更新, 仅适用于 go-cqhttp 的版本命名规则
|
||||
@ -99,6 +89,38 @@ func VersionNameCompare(current, remote string) bool {
|
||||
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
|
||||
}
|
||||
if v.Elem().Interface() != defaultValue {
|
||||
return
|
||||
}
|
||||
if v.Elem().Kind() != v2.Kind() {
|
||||
return
|
||||
}
|
||||
v.Elem().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
|
||||
}
|
||||
if v2.Elem().Interface() != defaultValue {
|
||||
return
|
||||
}
|
||||
if v.Elem().Kind() != v2.Kind() {
|
||||
return
|
||||
}
|
||||
v.Elem().Set(v2)
|
||||
}
|
||||
|
||||
var (
|
||||
// once lazy compile the reg
|
||||
once sync.Once
|
||||
|
52
global/signal.go
Normal file
52
global/signal.go
Normal 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
34
global/signal_unix.go
Normal 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
91
global/signal_windows.go
Normal 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
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package terminal
|
||||
|
@ -1,3 +1,4 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package terminal
|
||||
|
@ -7,7 +7,6 @@ import (
|
||||
"fmt"
|
||||
"hash"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
@ -42,7 +41,7 @@ func FromStream(updateWith io.Reader) (err error, errRecover error) {
|
||||
// no patch to apply, go on through
|
||||
bufBytes := bufio.NewReader(updateWith)
|
||||
updateWith = io.Reader(bufBytes)
|
||||
newBytes, err = ioutil.ReadAll(updateWith)
|
||||
newBytes, err = io.ReadAll(updateWith)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package update
|
||||
|
23
go.mod
23
go.mod
@ -4,14 +4,14 @@ go 1.16
|
||||
|
||||
require (
|
||||
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-20210726103104-1d68826cef0e
|
||||
github.com/dustin/go-humanize v1.0.0
|
||||
github.com/gin-contrib/pprof v1.3.0
|
||||
github.com/gin-gonic/gin v1.7.1
|
||||
github.com/gabriel-vasile/mimetype v1.3.1 // indirect
|
||||
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/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/lestrrat-go/file-rotatelogs v2.4.0+incompatible
|
||||
github.com/lestrrat-go/strftime v1.0.4 // indirect
|
||||
@ -22,12 +22,15 @@ require (
|
||||
github.com/stretchr/testify v1.7.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.7.5
|
||||
github.com/tidwall/gjson v1.8.1
|
||||
github.com/tuotoo/qrcode v0.0.0-20190222102259-ac9c44189bf2
|
||||
github.com/wdvxdr1123/go-silk v0.0.0-20210316130616-d47b553def60
|
||||
github.com/willf/bitset v1.1.11 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b
|
||||
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed
|
||||
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
|
||||
github.com/willf/bitset v1.2.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e
|
||||
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
|
||||
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
|
||||
)
|
||||
|
||||
replace github.com/willf/bitset v1.2.0 => github.com/bits-and-blooms/bitset v1.2.0
|
||||
|
63
go.sum
63
go.sum
@ -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/go.mod h1:4a58ifQTEe2uwwsaqbh3i2un5/CBPg+At/qHpt18Tmk=
|
||||
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/Mrs4s/MiraiGo v0.0.0-20210525010101-8f0cd9494d64/go.mod h1:NjiWhlvGxwv1ftOWIoiFa/OzklnAYI4YqNexFOKSZKw=
|
||||
github.com/Microsoft/go-winio v0.5.0 h1:Elr9Wn+sGKPlkaBvwu4mTrxtmOp3F3yV9qhaHbXGjwU=
|
||||
github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
|
||||
github.com/Mrs4s/MiraiGo v0.0.0-20210726103104-1d68826cef0e h1:PgFshw1L5TVdiDRLgr/bSotPGaGXYzbtn5cDBBvpL6U=
|
||||
github.com/Mrs4s/MiraiGo v0.0.0-20210726103104-1d68826cef0e/go.mod h1:CPaznIPn415uQqxJgjyMHLqGLkvLS6R6+bkW3/fe08Q=
|
||||
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/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
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/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/gin-contrib/pprof v1.3.0 h1:G9eK6HnbkSqDZBYbzG4wrjCsA4e+cvYAHUZw6W+W9K0=
|
||||
github.com/gin-contrib/pprof v1.3.0/go.mod h1:waMjT1H9b179t3CxuG1cV3DHpga6ybizwfBaM5OXaB0=
|
||||
github.com/gabriel-vasile/mimetype v1.3.1 h1:qevA6c2MtE1RorlScnixeG0VA1H4xrXyhyX3oWBynNQ=
|
||||
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/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.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/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/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/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.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/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
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/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||
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/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w=
|
||||
@ -60,15 +59,16 @@ 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/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
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.1.9/go.mod h1:JkjNv1G2oRWvFgP/r4DUbYhoeIBB0zMP2j1ID+5CYpU=
|
||||
github.com/guonaihong/gout v0.2.1 h1:XnaS92y/Mpcu4MOQU5cx4hTKhbqFznbIE/uUI5sMY9E=
|
||||
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/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/jonboulle/clockwork v0.2.2 h1:UOGuzwb1PwsrDAObMuhUnj0p5ULPj8V/xJ7Kx9qUBdQ=
|
||||
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.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.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/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=
|
||||
@ -105,6 +105,7 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
|
||||
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/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/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
|
||||
@ -120,8 +121,8 @@ github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFd
|
||||
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/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA=
|
||||
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/gjson v1.8.1 h1:8j5EE9Hrh3l9Od1OIEDAb7IpezNA20UdRngNAj5N0WU=
|
||||
github.com/tidwall/gjson v1.8.1/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
|
||||
github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE=
|
||||
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
|
||||
github.com/tidwall/pretty v1.1.0 h1:K3hMW5epkdAVwibsQEfR/7Zj0Qgt4DxtNumTq/VloO8=
|
||||
@ -134,13 +135,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/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/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-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI=
|
||||
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
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-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
@ -149,10 +149,11 @@ 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-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-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-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-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/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=
|
||||
@ -160,23 +161,26 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
|
||||
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-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-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-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-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-20210422114643-f5beecf764ed h1:Ei4bQjjpYUsS4efOUz+5Nz++IVkHk87n2zBA0NxBWc0=
|
||||
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b h1:9zKuko04nR4gjZ4+DNjHqRlAJqbJETHwiNKDqTfOjfE=
|
||||
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.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/time v0.0.0-20210220033141-f8bda1e9f3ba h1:O8mE0/t419eoIwhTFpKVkHiTs/Igowgfkj25AcZrtiE=
|
||||
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
||||
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-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
@ -198,7 +202,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.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||
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.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
|
13
login.go
13
login.go
@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
@ -63,9 +62,13 @@ func qrcodeLogin() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = ioutil.WriteFile("qrcode.png", rsp.ImageData, 0o644)
|
||||
_ = os.WriteFile("qrcode.png", rsp.ImageData, 0o644)
|
||||
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)
|
||||
qrcodeTerminal.New().Get(fi.Content).Print()
|
||||
s, err := cli.QueryQRCodeStatus(rsp.Sig)
|
||||
@ -130,10 +133,12 @@ func loginResponseProcessor(res *client.LoginResponse) error {
|
||||
continue
|
||||
}
|
||||
cli.Disconnect()
|
||||
cli.Release()
|
||||
cli = client.NewClientEmpty()
|
||||
return qrcodeLogin()
|
||||
case client.NeedCaptcha:
|
||||
log.Warnf("登录需要验证码.")
|
||||
_ = ioutil.WriteFile("captcha.jpg", res.CaptchaImage, 0o644)
|
||||
_ = os.WriteFile("captcha.jpg", res.CaptchaImage, 0o644)
|
||||
log.Warnf("请输入验证码 (captcha.jpg): (Enter 提交)")
|
||||
text = readLine()
|
||||
global.DelFile("captcha.jpg")
|
||||
|
196
main.go
196
main.go
@ -8,15 +8,13 @@ import (
|
||||
"encoding/hex"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/signal"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||
@ -40,11 +38,6 @@ import (
|
||||
var (
|
||||
conf *config.Config
|
||||
isFastStart = false
|
||||
c string
|
||||
d bool
|
||||
h bool
|
||||
wd string // reset work dir
|
||||
|
||||
// PasswordHash 存储QQ密码哈希供登录使用
|
||||
PasswordHash [16]byte
|
||||
|
||||
@ -61,70 +54,68 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
var debug bool
|
||||
flag.StringVar(&c, "c", config.DefaultConfigFile, "configuration filename default is config.hjson")
|
||||
flag.BoolVar(&d, "d", false, "running as a daemon")
|
||||
flag.BoolVar(&debug, "D", false, "debug mode")
|
||||
flag.BoolVar(&h, "h", false, "this help")
|
||||
flag.StringVar(&wd, "w", "", "cover the working directory")
|
||||
func main() {
|
||||
c := flag.String("c", config.DefaultConfigFile, "configuration filename default is config.hjson")
|
||||
d := flag.Bool("d", false, "running as a daemon")
|
||||
h := flag.Bool("h", false, "this help")
|
||||
wd := flag.String("w", "", "cover the working directory")
|
||||
debug := flag.Bool("D", false, "debug mode")
|
||||
flag.Parse()
|
||||
|
||||
switch {
|
||||
case *h:
|
||||
help()
|
||||
case *d:
|
||||
server.Daemon()
|
||||
case *wd != "":
|
||||
resetWorkDir(*wd)
|
||||
}
|
||||
|
||||
// 通过-c 参数替换 配置文件路径
|
||||
config.DefaultConfigFile = c
|
||||
config.DefaultConfigFile = *c
|
||||
conf = config.Get()
|
||||
if *debug {
|
||||
conf.Output.Debug = true
|
||||
}
|
||||
if conf.Output.Debug {
|
||||
log.SetReportCaller(true)
|
||||
}
|
||||
|
||||
logFormatter := &easy.Formatter{
|
||||
TimestampFormat: "2006-01-02 15:04:05",
|
||||
LogFormat: "[%time%] [%lvl%]: %msg% \n",
|
||||
}
|
||||
rotateOptions := []rotatelogs.Option{
|
||||
rotatelogs.WithRotationTime(time.Hour * 24),
|
||||
}
|
||||
|
||||
w, err := rotatelogs.New(path.Join("logs", "%Y-%m-%d.log"), 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 {
|
||||
log.Errorf("rotatelogs init err: %v", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
conf = config.Get()
|
||||
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) {
|
||||
if err := os.MkdirAll(global.ImagePath, 0o755); err != nil {
|
||||
log.Fatalf("创建图片缓存文件夹失败: %v", err)
|
||||
mkCacheDir := func(path string, _type string) {
|
||||
if !global.PathExists(path) {
|
||||
if err := os.MkdirAll(path, 0o755); err != nil {
|
||||
log.Fatalf("创建%s缓存文件夹失败: %v", _type, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if !global.PathExists(global.VoicePath) {
|
||||
if err := os.MkdirAll(global.VoicePath, 0o755); err != nil {
|
||||
log.Fatalf("创建语音缓存文件夹失败: %v", err)
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
mkCacheDir(global.ImagePath, "图片")
|
||||
mkCacheDir(global.VoicePath, "语音")
|
||||
mkCacheDir(global.VideoPath, "视频")
|
||||
mkCacheDir(global.CachePath, "发送图片")
|
||||
|
||||
func main() {
|
||||
if h {
|
||||
help()
|
||||
}
|
||||
if d {
|
||||
server.Daemon()
|
||||
}
|
||||
if wd != "" {
|
||||
resetWorkDir()
|
||||
}
|
||||
var byteKey []byte
|
||||
arg := os.Args
|
||||
if len(arg) > 1 {
|
||||
@ -169,7 +160,7 @@ func main() {
|
||||
if !global.PathExists("device.json") {
|
||||
log.Warn("虚拟设备信息不存在, 将自动生成随机设备.")
|
||||
client.GenRandomDevice()
|
||||
_ = ioutil.WriteFile("device.json", client.SystemDeviceInfo.ToJson(), 0o644)
|
||||
_ = os.WriteFile("device.json", client.SystemDeviceInfo.ToJson(), 0o644)
|
||||
log.Info("已生成设备信息并保存到 device.json 文件.")
|
||||
} else {
|
||||
log.Info("将使用 device.json 内的设备信息运行Bot.")
|
||||
@ -250,46 +241,16 @@ func main() {
|
||||
}
|
||||
return "未知"
|
||||
}())
|
||||
cli = client.NewClientEmpty()
|
||||
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
|
||||
})
|
||||
cli = newClient()
|
||||
global.Proxy = conf.Message.ProxyRewrite
|
||||
isQRCodeLogin := (conf.Account.Uin == 0 || len(conf.Account.Password) == 0) && !conf.Account.Encrypt
|
||||
isTokenLogin := false
|
||||
saveToken := func() {
|
||||
AccountToken = cli.GenToken()
|
||||
_ = ioutil.WriteFile("session.token", AccountToken, 0o677)
|
||||
_ = os.WriteFile("session.token", AccountToken, 0o644)
|
||||
}
|
||||
if global.PathExists("session.token") {
|
||||
token, err := ioutil.ReadFile("session.token")
|
||||
token, err := os.ReadFile("session.token")
|
||||
if err == nil {
|
||||
if conf.Account.Uin != 0 {
|
||||
r := binary.NewReader(token)
|
||||
@ -310,11 +271,18 @@ func main() {
|
||||
_ = os.Remove("session.token")
|
||||
log.Warnf("恢复会话失败: %v , 尝试使用正常流程登录.", err)
|
||||
time.Sleep(time.Second)
|
||||
cli.Disconnect()
|
||||
cli.Release()
|
||||
cli = newClient()
|
||||
} else {
|
||||
isTokenLogin = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if conf.Account.Uin != 0 && PasswordHash != [16]byte{} {
|
||||
cli.Uin = conf.Account.Uin
|
||||
cli.PasswordMd5 = PasswordHash
|
||||
}
|
||||
if !isTokenLogin {
|
||||
if !isQRCodeLogin {
|
||||
if err := commonLogin(); err != nil {
|
||||
@ -392,12 +360,12 @@ func main() {
|
||||
} else {
|
||||
coolq.SetMessageFormat(conf.Message.PostFormat)
|
||||
}
|
||||
log.Info("正在加载事件过滤器.")
|
||||
coolq.IgnoreInvalidCQCode = conf.Message.IgnoreInvalidCQCode
|
||||
coolq.SplitURL = conf.Message.FixURL
|
||||
coolq.ForceFragmented = conf.Message.ForceFragment
|
||||
coolq.RemoveReplyAt = conf.Message.RemoveReplyAt
|
||||
coolq.ExtraReplyData = conf.Message.ExtraReplyData
|
||||
coolq.SkipMimeScan = conf.Message.SkipMimeScan
|
||||
for _, m := range conf.Servers {
|
||||
if h, ok := m["http"]; ok {
|
||||
hc := new(config.HTTPServer)
|
||||
@ -431,13 +399,21 @@ func main() {
|
||||
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("アトリは、高性能ですから!")
|
||||
c := make(chan os.Signal, 1)
|
||||
|
||||
go checkUpdate()
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
<-c
|
||||
|
||||
<-global.SetupMainSignalHandler()
|
||||
}
|
||||
|
||||
// PasswordHashEncrypt 使用key加密给定passwordHash
|
||||
@ -617,7 +593,7 @@ Options:
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func resetWorkDir() {
|
||||
func resetWorkDir(wd string) {
|
||||
args := make([]string, 0, len(os.Args))
|
||||
for i := 1; i < len(os.Args); i++ {
|
||||
if os.Args[i] == "-w" {
|
||||
@ -626,7 +602,8 @@ func resetWorkDir() {
|
||||
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.Stdout = os.Stdout
|
||||
proc.Stderr = os.Stderr
|
||||
@ -637,3 +614,34 @@ func resetWorkDir() {
|
||||
}
|
||||
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
6
scripts/bootstrap
Normal 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
|
@ -271,7 +271,7 @@ func deleteGroupFolder(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 {
|
||||
|
260
server/http.go
260
server/http.go
@ -1,46 +1,145 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||
"github.com/Mrs4s/go-cqhttp/global/config"
|
||||
|
||||
"github.com/Mrs4s/MiraiGo/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/guonaihong/gout"
|
||||
"github.com/guonaihong/gout/dataflow"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/tidwall/gjson"
|
||||
|
||||
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||
"github.com/Mrs4s/go-cqhttp/global/config"
|
||||
)
|
||||
|
||||
type httpServer struct {
|
||||
engine *gin.Engine
|
||||
bot *coolq.CQBot
|
||||
HTTP *http.Server
|
||||
api *apiCaller
|
||||
HTTP *http.Server
|
||||
api *apiCaller
|
||||
accessToken string
|
||||
}
|
||||
|
||||
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
// HTTPClient 反向HTTP上报客户端
|
||||
type HTTPClient struct {
|
||||
bot *coolq.CQBot
|
||||
secret string
|
||||
addr string
|
||||
filter string
|
||||
apiPort int
|
||||
timeout int32
|
||||
}
|
||||
|
||||
type httpContext struct {
|
||||
ctx *gin.Context
|
||||
type httpCtx struct {
|
||||
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上报客户端
|
||||
@ -49,78 +148,27 @@ func RunHTTPServerAndClients(bot *coolq.CQBot, conf *config.HTTPServer) {
|
||||
return
|
||||
}
|
||||
var (
|
||||
s = new(httpServer)
|
||||
authToken = conf.AccessToken
|
||||
addr string
|
||||
s = new(httpServer)
|
||||
addr string
|
||||
)
|
||||
s.accessToken = conf.AccessToken
|
||||
if conf.Host == "" || conf.Port == 0 {
|
||||
goto client
|
||||
}
|
||||
addr = fmt.Sprintf("%s:%d", conf.Host, conf.Port)
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
s.engine = gin.New()
|
||||
s.bot = bot
|
||||
s.api = newAPICaller(s.bot)
|
||||
s.api = newAPICaller(bot)
|
||||
if conf.RateLimit.Enabled {
|
||||
s.api.use(rateLimit(conf.RateLimit.Frequency, conf.RateLimit.Bucket))
|
||||
}
|
||||
s.engine.Use(func(c *gin.Context) {
|
||||
if c.Request.Method != "GET" && c.Request.Method != "POST" {
|
||||
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
|
||||
}
|
||||
}
|
||||
})
|
||||
if conf.LongPolling.Enabled {
|
||||
s.api.use(longPolling(bot, conf.LongPolling.MaxQueueSize))
|
||||
}
|
||||
|
||||
s.engine.Any("/:action", s.HandleActions)
|
||||
|
||||
go func() {
|
||||
log.Infof("CQ HTTP 服务器已启动: %v", addr)
|
||||
s.HTTP = &http.Server{
|
||||
Addr: addr,
|
||||
Handler: s.engine,
|
||||
Handler: s,
|
||||
}
|
||||
if err := s.HTTP.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||
log.Error(err)
|
||||
@ -133,55 +181,55 @@ func RunHTTPServerAndClients(bot *coolq.CQBot, conf *config.HTTPServer) {
|
||||
client:
|
||||
for _, c := range conf.Post {
|
||||
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服务
|
||||
func (c *HTTPClient) Run(addr, secret, filter string, timeout int32, bot *coolq.CQBot) {
|
||||
c.bot = bot
|
||||
c.secret = secret
|
||||
c.addr = addr
|
||||
c.timeout = timeout
|
||||
c.filter = filter
|
||||
addFilter(filter)
|
||||
func (c HTTPClient) Run() {
|
||||
addFilter(c.filter)
|
||||
if c.timeout < 5 {
|
||||
c.timeout = 5
|
||||
}
|
||||
bot.OnEventPush(c.onBotPushEvent)
|
||||
log.Infof("HTTP POST上报器已启动: %v", addr)
|
||||
c.bot.OnEventPush(c.onBotPushEvent)
|
||||
log.Infof("HTTP POST上报器已启动: %v", c.addr)
|
||||
}
|
||||
|
||||
func (c *HTTPClient) onBotPushEvent(m *bytes.Buffer) {
|
||||
func (c *HTTPClient) onBotPushEvent(e *coolq.Event) {
|
||||
var res string
|
||||
if c.filter != "" {
|
||||
filter := findFilter(c.filter)
|
||||
if filter != nil && !filter.Eval(gjson.Parse(utils.B2S(m.Bytes()))) {
|
||||
log.Debugf("上报Event %v 到 HTTP 服务器 %v 时被过滤.", c.addr, utils.B2S(m.Bytes()))
|
||||
if filter != nil && !filter.Eval(gjson.Parse(e.JSONString())) {
|
||||
log.Debugf("上报Event %v 到 HTTP 服务器 %s 时被过滤.", c.addr, e.JSONBytes())
|
||||
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{
|
||||
"X-Self-ID": c.bot.Client.Uin,
|
||||
"User-Agent": "CQHttp/4.15.0",
|
||||
}
|
||||
if c.secret != "" {
|
||||
mac := hmac.New(sha1.New, []byte(c.secret))
|
||||
_, err := mac.Write(m.Bytes())
|
||||
_, err := mac.Write(e.JSONBytes())
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil
|
||||
}
|
||||
h["X-Signature"] = "sha1=" + hex.EncodeToString(mac.Sum(nil))
|
||||
}
|
||||
if c.apiPort != 0 {
|
||||
h["X-API-Port"] = c.apiPort
|
||||
}
|
||||
return h
|
||||
}()).SetTimeout(time.Second * time.Duration(c.timeout)).F().Retry().Attempt(5).
|
||||
WaitTime(time.Millisecond * 500).MaxWaitTime(time.Second * 5).
|
||||
@ -193,43 +241,15 @@ func (c *HTTPClient) onBotPushEvent(m *bytes.Buffer) {
|
||||
return nil
|
||||
}).Do()
|
||||
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
|
||||
}
|
||||
log.Debugf("上报Event数据 %v 到 %v", utils.B2S(m.Bytes()), c.addr)
|
||||
log.Debugf("上报Event数据 %s 到 %v", e.JSONBytes(), c.addr)
|
||||
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() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"context"
|
||||
"os"
|
||||
"sync"
|
||||
@ -54,3 +55,37 @@ func findFilter(file string) global.Filter {
|
||||
defer filterMutex.RUnlock()
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,10 @@ package server
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/pprof"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/pprof"
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/Mrs4s/go-cqhttp/global/config"
|
||||
@ -18,14 +17,18 @@ func RunPprofServer(conf *config.PprofServer) {
|
||||
if conf.Disabled {
|
||||
return
|
||||
}
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
engine := gin.New()
|
||||
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() {
|
||||
log.Infof("pprof debug 服务器已启动: %v/debug/pprof", addr)
|
||||
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.Infof("pprof 服务启动失败, 请检查端口是否被占用.")
|
||||
log.Warnf("将在五秒后退出.")
|
||||
|
179
server/scf.go
Normal file
179
server/scf.go
Normal file
@ -0,0 +1,179 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"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
|
||||
header http.Header
|
||||
}
|
||||
|
||||
func (l *lambdaResponseWriter) Header() http.Header {
|
||||
return l.header
|
||||
}
|
||||
|
||||
func (l *lambdaResponseWriter) Write(data []byte) (int, error) {
|
||||
buffer := global.NewBuffer()
|
||||
defer global.PutBuffer(buffer)
|
||||
body := ""
|
||||
if data != nil {
|
||||
body = utils.B2S(data)
|
||||
}
|
||||
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 0, err
|
||||
}
|
||||
_ = do.Body.Close()
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}()
|
||||
server.ServeHTTP(&lambdaResponseWriter{header: make(http.Header)}, req)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
@ -31,8 +31,8 @@ type webSocketServer struct {
|
||||
filter string
|
||||
}
|
||||
|
||||
// WebSocketClient WebSocket客户端实例
|
||||
type WebSocketClient struct {
|
||||
// websocketClient WebSocket客户端实例
|
||||
type websocketClient struct {
|
||||
bot *coolq.CQBot
|
||||
conf *config.WebsocketReverse
|
||||
|
||||
@ -59,22 +59,24 @@ func RunWebSocketServer(b *coolq.CQBot, conf *config.WebsocketServer) {
|
||||
if conf.Disabled {
|
||||
return
|
||||
}
|
||||
s := new(webSocketServer)
|
||||
s.conf = conf
|
||||
s.bot = b
|
||||
s.token = conf.AccessToken
|
||||
s.filter = conf.Filter
|
||||
s := &webSocketServer{
|
||||
bot: b,
|
||||
conf: conf,
|
||||
token: conf.AccessToken,
|
||||
filter: conf.Filter,
|
||||
}
|
||||
addFilter(s.filter)
|
||||
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}`,
|
||||
b.Client.Uin, time.Now().Unix())
|
||||
b.OnEventPush(s.onBotPushEvent)
|
||||
http.HandleFunc("/event", s.event)
|
||||
http.HandleFunc("/api", s.api)
|
||||
http.HandleFunc("/", s.any)
|
||||
mux := http.ServeMux{}
|
||||
mux.HandleFunc("/event", s.event)
|
||||
mux.HandleFunc("/api", s.api)
|
||||
mux.HandleFunc("/", s.any)
|
||||
go func() {
|
||||
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 {
|
||||
return
|
||||
}
|
||||
c := new(WebSocketClient)
|
||||
c.bot = b
|
||||
c.conf = conf
|
||||
c.token = conf.AccessToken
|
||||
c.filter = conf.Filter
|
||||
c := &websocketClient{
|
||||
bot: b,
|
||||
conf: conf,
|
||||
token: conf.AccessToken,
|
||||
filter: conf.Filter,
|
||||
}
|
||||
addFilter(c.filter)
|
||||
if c.conf.Universal != "" {
|
||||
c.connectUniversal()
|
||||
@ -102,7 +105,7 @@ func RunWebSocketClient(b *coolq.CQBot, conf *config.WebsocketReverse) {
|
||||
c.bot.OnEventPush(c.onBotPushEvent)
|
||||
}
|
||||
|
||||
func (c *WebSocketClient) connectAPI() {
|
||||
func (c *websocketClient) connectAPI() {
|
||||
log.Infof("开始尝试连接到反向WebSocket API服务器: %v", c.conf.API)
|
||||
header := http.Header{
|
||||
"X-Client-Role": []string{"API"},
|
||||
@ -129,7 +132,7 @@ func (c *WebSocketClient) connectAPI() {
|
||||
go c.listenAPI(wrappedConn, false)
|
||||
}
|
||||
|
||||
func (c *WebSocketClient) connectEvent() {
|
||||
func (c *websocketClient) connectEvent() {
|
||||
log.Infof("开始尝试连接到反向WebSocket Event服务器: %v", c.conf.Event)
|
||||
header := http.Header{
|
||||
"X-Client-Role": []string{"Event"},
|
||||
@ -160,7 +163,7 @@ func (c *WebSocketClient) connectEvent() {
|
||||
c.eventConn = &webSocketConn{Conn: conn, apiCaller: newAPICaller(c.bot)}
|
||||
}
|
||||
|
||||
func (c *WebSocketClient) connectUniversal() {
|
||||
func (c *websocketClient) connectUniversal() {
|
||||
log.Infof("开始尝试连接到反向WebSocket Universal服务器: %v", c.conf.Universal)
|
||||
header := http.Header{
|
||||
"X-Client-Role": []string{"Universal"},
|
||||
@ -194,7 +197,7 @@ func (c *WebSocketClient) connectUniversal() {
|
||||
c.universalConn = wrappedConn
|
||||
}
|
||||
|
||||
func (c *WebSocketClient) listenAPI(conn *webSocketConn, u bool) {
|
||||
func (c *websocketClient) listenAPI(conn *webSocketConn, u bool) {
|
||||
defer func() { _ = conn.Close() }()
|
||||
for {
|
||||
buffer := global.NewBuffer()
|
||||
@ -225,66 +228,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)
|
||||
if filter != nil && !filter.Eval(gjson.Parse(utils.B2S(m.Bytes()))) {
|
||||
log.Debugf("上报Event %v 到 WS客户端 时被过滤.", utils.B2S(m.Bytes()))
|
||||
if filter != nil && !filter.Eval(gjson.Parse(e.JSONString())) {
|
||||
log.Debugf("上报Event %s 到 WS服务器 时被过滤.", e.JSONBytes())
|
||||
return
|
||||
}
|
||||
if c.eventConn != nil {
|
||||
log.Debugf("向WS服务器 %v 推送Event: %v", c.eventConn.RemoteAddr().String(), utils.B2S(m.Bytes()))
|
||||
conn := c.eventConn
|
||||
push := func(conn *webSocketConn, reconnect func()) {
|
||||
log.Debugf("向WS服务器 %v 推送Event: %s", conn.RemoteAddr().String(), e.JSONBytes())
|
||||
conn.Lock()
|
||||
defer conn.Unlock()
|
||||
_ = c.eventConn.SetWriteDeadline(time.Now().Add(time.Second * 15))
|
||||
if err := c.eventConn.WriteMessage(websocket.TextMessage, m.Bytes()); err != nil {
|
||||
log.Warnf("向WS服务器 %v 推送Event时出现错误: %v", c.eventConn.RemoteAddr().String(), err)
|
||||
_ = c.eventConn.Close()
|
||||
_ = conn.SetWriteDeadline(time.Now().Add(time.Second * 15))
|
||||
if err := conn.WriteMessage(websocket.TextMessage, e.JSONBytes()); err != nil {
|
||||
log.Warnf("向WS服务器 %v 推送Event时出现错误: %v", conn.RemoteAddr().String(), err)
|
||||
_ = conn.Close()
|
||||
if c.conf.ReconnectInterval != 0 {
|
||||
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 {
|
||||
log.Debugf("向WS服务器 %v 推送Event: %v", c.universalConn.RemoteAddr().String(), utils.B2S(m.Bytes()))
|
||||
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()
|
||||
}
|
||||
}
|
||||
push(c.universalConn, 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) {
|
||||
isAuth, errReason := s.auth(r)
|
||||
if !isAuth {
|
||||
w.WriteHeader(errReason)
|
||||
status := checkAuth(r, s.token)
|
||||
if status != http.StatusOK {
|
||||
log.Warnf("已拒绝 %v 的 WebSocket 请求: Token鉴权失败(code:%d)", r.RemoteAddr, status)
|
||||
w.WriteHeader(status)
|
||||
return
|
||||
}
|
||||
c, err := upgrader.Upgrade(w, r, nil)
|
||||
@ -309,9 +285,10 @@ func (s *webSocketServer) event(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (s *webSocketServer) api(w http.ResponseWriter, r *http.Request) {
|
||||
isAuth, errReason := s.auth(r)
|
||||
if !isAuth {
|
||||
w.WriteHeader(errReason)
|
||||
status := checkAuth(r, s.token)
|
||||
if status != http.StatusOK {
|
||||
log.Warnf("已拒绝 %v 的 WebSocket 请求: Token鉴权失败(code:%d)", r.RemoteAddr, status)
|
||||
w.WriteHeader(status)
|
||||
return
|
||||
}
|
||||
c, err := upgrader.Upgrade(w, r, nil)
|
||||
@ -328,9 +305,10 @@ func (s *webSocketServer) api(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (s *webSocketServer) any(w http.ResponseWriter, r *http.Request) {
|
||||
isAuth, errReason := s.auth(r)
|
||||
if !isAuth {
|
||||
w.WriteHeader(errReason)
|
||||
status := checkAuth(r, s.token)
|
||||
if status != http.StatusOK {
|
||||
log.Warnf("已拒绝 %v 的 WebSocket 请求: Token鉴权失败(code:%d)", r.RemoteAddr, status)
|
||||
w.WriteHeader(status)
|
||||
return
|
||||
}
|
||||
c, err := upgrader.Upgrade(w, r, nil)
|
||||
@ -398,33 +376,32 @@ func (c *webSocketConn) handleRequest(_ *coolq.CQBot, payload []byte) {
|
||||
_ = c.WriteJSON(ret)
|
||||
}
|
||||
|
||||
func (s *webSocketServer) onBotPushEvent(m *bytes.Buffer) {
|
||||
func (s *webSocketServer) onBotPushEvent(e *coolq.Event) {
|
||||
s.eventConnMutex.Lock()
|
||||
defer s.eventConnMutex.Unlock()
|
||||
|
||||
filter := findFilter(s.filter)
|
||||
if filter != nil && !filter.Eval(gjson.Parse(utils.B2S(m.Bytes()))) {
|
||||
log.Debugf("上报Event %v 到 WS客户端 时被过滤.", utils.B2S(m.Bytes()))
|
||||
if filter != nil && !filter.Eval(gjson.Parse(e.JSONString())) {
|
||||
log.Debugf("上报Event %s 到 WS客户端 时被过滤.", e.JSONBytes())
|
||||
return
|
||||
}
|
||||
|
||||
for i, l := 0, len(s.eventConn); i < l; i++ {
|
||||
j := 0
|
||||
for i := 0; i < len(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()
|
||||
if err := conn.WriteMessage(websocket.TextMessage, m.Bytes()); err != nil {
|
||||
if err := conn.WriteMessage(websocket.TextMessage, e.JSONBytes()); err != nil {
|
||||
_ = 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
|
||||
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()
|
||||
j++
|
||||
}
|
||||
s.eventConn = s.eventConn[:j]
|
||||
}
|
||||
|
Reference in New Issue
Block a user