From 23d594be2917b3449dac20a23818e04ec8cb0e99 Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Mon, 20 Jun 2022 16:11:31 +0800 Subject: [PATCH 1/4] coolq: support upload_private_file pc client can't receive file sent by this api. --- coolq/api.go | 24 ++++++++++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- modules/api/api.go | 5 +++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/coolq/api.go b/coolq/api.go index 1eeb6be..9a6c150 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -601,6 +601,30 @@ func (bot *CQBot) CQUploadGroupFile(groupID int64, file, name, folder string) gl return OK(nil) } +// CQUploadPrivateFile 扩展API-上传私聊文件 +// +// @route(upload_private_file) +func (bot *CQBot) CQUploadPrivateFile(userID int64, file, name string) global.MSG { + target := message.Source{ + SourceType: message.SourcePrivate, + PrimaryID: userID, + } + fileBody, err := os.Open(file) + if err != nil { + log.Warnf("上传私聊文件 %v 失败: %+v", file, err) + return Failed(100, "OPEN_FILE_ERROR", "打开文件失败") + } + localFile := &client.LocalFile{ + FileName: name, + Body: fileBody, + } + if err := bot.Client.UploadFile(target, localFile); err != nil { + log.Warnf("上传私聊 %v 文件 %v 失败: %+v", userID, file, err) + return Failed(100, "FILE_SYSTEM_UPLOAD_API_ERROR", err.Error()) + } + return OK(nil) +} + // CQGroupFileCreateFolder 拓展API-创建群文件文件夹 // // @route(create_group_file_folder) diff --git a/go.mod b/go.mod index bd31bcf..cd0ec42 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/Microsoft/go-winio v0.5.1 - github.com/Mrs4s/MiraiGo v0.0.0-20220614093722-87ff2f95910c + github.com/Mrs4s/MiraiGo v0.0.0-20220620032456-d09215e943bb github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc github.com/fumiama/go-base16384 v1.5.2 diff --git a/go.sum b/go.sum index 755c917..87b6e7a 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY= github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84= -github.com/Mrs4s/MiraiGo v0.0.0-20220614093722-87ff2f95910c h1:LGJoAua1bYxx28255h02owv66v9eWkSwcZx/jEO79Z0= -github.com/Mrs4s/MiraiGo v0.0.0-20220614093722-87ff2f95910c/go.mod h1:mZp8Lt7uqLCUwSLouB2yuiP467Cwl4mnG9IMAaXUKA0= +github.com/Mrs4s/MiraiGo v0.0.0-20220620032456-d09215e943bb h1:UAfqKJ+xXbJObUQQAl1TYYwRcLBPYfy67D25lAD4Nl8= +github.com/Mrs4s/MiraiGo v0.0.0-20220620032456-d09215e943bb/go.mod h1:mZp8Lt7uqLCUwSLouB2yuiP467Cwl4mnG9IMAaXUKA0= github.com/RomiChan/protobuf v0.1.1-0.20220602121309-9e3b8cbefd7a h1:WIfEWYj82oEuPtm5pqlyQmCJCoiw00C6ugZFqHA0cC8= github.com/RomiChan/protobuf v0.1.1-0.20220602121309-9e3b8cbefd7a/go.mod h1:2Ie+hdBFQpQFDHfeklgxoFmQRCE7O+KwFpISeXq7OwA= github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c h1:cNPOdTNiVwxLpROLjXCgbIPvdkE+BwvxDvgmdYmWx6Q= diff --git a/modules/api/api.go b/modules/api/api.go index ae4e928..c57ce26 100644 --- a/modules/api/api.go +++ b/modules/api/api.go @@ -337,5 +337,10 @@ func (c *Caller) call(action string, p Getter) global.MSG { p2 := p.Get("name").String() p3 := p.Get("folder").String() return c.bot.CQUploadGroupFile(p0, p1, p2, p3) + case "upload_private_file": + p0 := p.Get("user_id").Int() + p1 := p.Get("file").String() + p2 := p.Get("name").String() + return c.bot.CQUploadPrivateFile(p0, p1, p2) } } From ae7fefad137f2a309361e124fb9605657431a4b3 Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Tue, 21 Jun 2022 13:55:41 +0800 Subject: [PATCH 2/4] coolq: fix upload_private_file pc client now can receive file sent by this api. --- cmd/gocq/login.go | 3 +-- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cmd/gocq/login.go b/cmd/gocq/login.go index bc19146..90be038 100644 --- a/cmd/gocq/login.go +++ b/cmd/gocq/login.go @@ -192,8 +192,7 @@ func loginResponseProcessor(res *client.LoginResponse) error { msg := res.ErrorMessage if strings.Contains(msg, "版本") { msg = "密码错误或账号被冻结" - } - if strings.Contains(msg, "冻结") { + } else if strings.Contains(msg, "冻结") { log.Fatalf("账号被冻结") } log.Warnf("登录失败: %v", msg) diff --git a/go.mod b/go.mod index cd0ec42..cace355 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/Microsoft/go-winio v0.5.1 - github.com/Mrs4s/MiraiGo v0.0.0-20220620032456-d09215e943bb + github.com/Mrs4s/MiraiGo v0.0.0-20220621055318-d1a66c42b67e github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc github.com/fumiama/go-base16384 v1.5.2 diff --git a/go.sum b/go.sum index 87b6e7a..365a233 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY= github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84= -github.com/Mrs4s/MiraiGo v0.0.0-20220620032456-d09215e943bb h1:UAfqKJ+xXbJObUQQAl1TYYwRcLBPYfy67D25lAD4Nl8= -github.com/Mrs4s/MiraiGo v0.0.0-20220620032456-d09215e943bb/go.mod h1:mZp8Lt7uqLCUwSLouB2yuiP467Cwl4mnG9IMAaXUKA0= +github.com/Mrs4s/MiraiGo v0.0.0-20220621055318-d1a66c42b67e h1:xPHtwF8AYB2N055t1sNPC6+2FMy2dujV2Rp7Bf928w4= +github.com/Mrs4s/MiraiGo v0.0.0-20220621055318-d1a66c42b67e/go.mod h1:mZp8Lt7uqLCUwSLouB2yuiP467Cwl4mnG9IMAaXUKA0= github.com/RomiChan/protobuf v0.1.1-0.20220602121309-9e3b8cbefd7a h1:WIfEWYj82oEuPtm5pqlyQmCJCoiw00C6ugZFqHA0cC8= github.com/RomiChan/protobuf v0.1.1-0.20220602121309-9e3b8cbefd7a/go.mod h1:2Ie+hdBFQpQFDHfeklgxoFmQRCE7O+KwFpISeXq7OwA= github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c h1:cNPOdTNiVwxLpROLjXCgbIPvdkE+BwvxDvgmdYmWx6Q= From 2a0babad99a9f565c64ca8106391aebedd2b8cf6 Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Tue, 21 Jun 2022 21:26:33 +0800 Subject: [PATCH 3/4] feat: slider captcha processor --- cmd/gocq/login.go | 37 ++++++++++++++++++++++++++++++++++++- go.mod | 2 +- go.sum | 4 ++-- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/cmd/gocq/login.go b/cmd/gocq/login.go index 90be038..8f72712 100644 --- a/cmd/gocq/login.go +++ b/cmd/gocq/login.go @@ -3,6 +3,7 @@ package gocq import ( "bufio" "bytes" + "fmt" "image" "image/png" "os" @@ -10,9 +11,11 @@ import ( "time" "github.com/Mrs4s/MiraiGo/client" + "github.com/Mrs4s/MiraiGo/utils" "github.com/mattn/go-colorable" "github.com/pkg/errors" log "github.com/sirupsen/logrus" + "github.com/tidwall/gjson" "github.com/Mrs4s/go-cqhttp/global" ) @@ -142,7 +145,19 @@ func loginResponseProcessor(res *client.LoginResponse) error { var text string switch res.Error { case client.SliderNeededError: - log.Warnf("登录需要滑条验证码, 请使用手机QQ扫描二维码以继续登录.") + log.Warnf("登录需要滑条验证码, 请选择验证方式: ") + log.Warnf("1. 使用浏览器抓取滑条并登录") + log.Warnf("2. 使用手机QQ扫码验证 (需要手Q和gocq在同一网络下).") + log.Warn("请输入(1 - 2) (将在10秒后自动选择1):") + text = readLineTimeout(time.Second*10, "1") + if strings.Contains(text, "1") { + ticket := sliderCaptchaProcessor(res.VerifyUrl) + if ticket == "" { + os.Exit(0) + } + res, err = cli.SubmitTicket(ticket) + continue + } cli.Disconnect() cli.Release() cli = client.NewClientEmpty() @@ -202,3 +217,23 @@ func loginResponseProcessor(res *client.LoginResponse) error { } } } + +func sliderCaptchaProcessor(u string) string { + id := utils.RandomString(8) + log.Warnf("请前往该地址验证 -> %v", strings.ReplaceAll(u, "https://ssl.captcha.qq.com/template/wireless_mqq_captcha.html?", fmt.Sprintf("https://captcha.go-cqhttp.org/captcha?id=%v&", id))) + start := time.Now() + for time.Now().Sub(start).Minutes() < 2 { + time.Sleep(time.Second) + data, err := global.GetBytes("https://captcha.go-cqhttp.org/captcha/ticket?id=" + id) + if err != nil { + log.Warnf("获取 Ticket 时出现错误: %v", err) + return "" + } + g := gjson.ParseBytes(data) + if g.Get("ticket").Exists() { + return g.Get("ticket").String() + } + } + log.Warnf("验证超时") + return "" +} diff --git a/go.mod b/go.mod index cace355..db48f45 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/Microsoft/go-winio v0.5.1 - github.com/Mrs4s/MiraiGo v0.0.0-20220621055318-d1a66c42b67e + github.com/Mrs4s/MiraiGo v0.0.0-20220621083050-ae8c187aa59d github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc github.com/fumiama/go-base16384 v1.5.2 diff --git a/go.sum b/go.sum index 365a233..d70e916 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY= github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84= -github.com/Mrs4s/MiraiGo v0.0.0-20220621055318-d1a66c42b67e h1:xPHtwF8AYB2N055t1sNPC6+2FMy2dujV2Rp7Bf928w4= -github.com/Mrs4s/MiraiGo v0.0.0-20220621055318-d1a66c42b67e/go.mod h1:mZp8Lt7uqLCUwSLouB2yuiP467Cwl4mnG9IMAaXUKA0= +github.com/Mrs4s/MiraiGo v0.0.0-20220621083050-ae8c187aa59d h1:Cq8HMtyL3PRpvOynuwi9WSdek2+5UTOd0zJ+JTq5hPM= +github.com/Mrs4s/MiraiGo v0.0.0-20220621083050-ae8c187aa59d/go.mod h1:mZp8Lt7uqLCUwSLouB2yuiP467Cwl4mnG9IMAaXUKA0= github.com/RomiChan/protobuf v0.1.1-0.20220602121309-9e3b8cbefd7a h1:WIfEWYj82oEuPtm5pqlyQmCJCoiw00C6ugZFqHA0cC8= github.com/RomiChan/protobuf v0.1.1-0.20220602121309-9e3b8cbefd7a/go.mod h1:2Ie+hdBFQpQFDHfeklgxoFmQRCE7O+KwFpISeXq7OwA= github.com/RomiChan/syncx v0.0.0-20220404072119-d7ea0ae15a4c h1:cNPOdTNiVwxLpROLjXCgbIPvdkE+BwvxDvgmdYmWx6Q= From 177ba9d8c2b1d86301f0bf3e0714710fe3c40af4 Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Tue, 21 Jun 2022 21:29:41 +0800 Subject: [PATCH 4/4] make lint happy --- cmd/gocq/login.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/gocq/login.go b/cmd/gocq/login.go index 8f72712..20c10e1 100644 --- a/cmd/gocq/login.go +++ b/cmd/gocq/login.go @@ -222,7 +222,7 @@ func sliderCaptchaProcessor(u string) string { id := utils.RandomString(8) log.Warnf("请前往该地址验证 -> %v", strings.ReplaceAll(u, "https://ssl.captcha.qq.com/template/wireless_mqq_captcha.html?", fmt.Sprintf("https://captcha.go-cqhttp.org/captcha?id=%v&", id))) start := time.Now() - for time.Now().Sub(start).Minutes() < 2 { + for time.Since(start).Minutes() < 2 { time.Sleep(time.Second) data, err := global.GetBytes("https://captcha.go-cqhttp.org/captcha/ticket?id=" + id) if err != nil {