Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from Mrs4s:master #207

Closed
wants to merge 35 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8ea182a
sync: master to dev (#2340)
fumiama Aug 1, 2023
ce119b7
Merge branch 'master' of https://github.com/Mrs4s/go-cqhttp into dev
fumiama Aug 1, 2023
ca20a3d
update deps
fumiama Aug 1, 2023
0714aac
fix: panic after -10005
fumiama Aug 1, 2023
94a3ff5
dowgrade mongo-driver to latest static
fumiama Aug 1, 2023
3b99a82
optimize(login): log打印
fumiama Aug 3, 2023
5db03c7
fix #2347: This mutex is not locked
fumiama Aug 4, 2023
977030e
Revert #2207 (#2397)
xiangxiangxiong9 Aug 23, 2023
da9f03f
fix #2368, which causes system lacked sufficient buffer space (#2372)
KomeiDiSanXian Aug 23, 2023
d85d697
Fix: SignServer TCP ping for custom port (#2353)
xiwangly2 Aug 23, 2023
f8354ec
修复TCP缓冲区不足问题;重构 qsign 签名服务对接部分;支持配置多个签名服务器 (#2389)
1umine Aug 27, 2023
79a194f
ci(chore): Fix stylings
github-actions[bot] Aug 27, 2023
fd6ef4a
optimize(qisgn): async operations
fumiama Aug 28, 2023
77b54fc
fix: nil pointer
fumiama Aug 29, 2023
417a0f2
fix: nil pointer
fumiama Aug 29, 2023
9e6d7b7
fix: nil pointer
fumiama Aug 29, 2023
f16d72f
!fixup: optimize(qisgn): async operations (#2415)
YumeMichi Aug 31, 2023
1c34643
fix nil pointer dereference caused by nil 'cs' (#2440)
1umine Oct 1, 2023
07214e3
尝试实现 #2421 (#2422)
xiangxiangxiong9 Oct 1, 2023
517d323
ci(chore): Fix stylings
github-actions[bot] Oct 1, 2023
bd785d3
尝试修复首次登录时容易出现 code -10005 和 packet timed out 的问题 (#2463)
1umine Oct 9, 2023
6ac7a8f
Merge pull request #2470 from Mrs4s/dev
fumiama Oct 9, 2023
fcf79de
fix(cmd/main): -update-protocol 不保存新文件
fumiama Oct 9, 2023
5cbbcda
Update README.md
Mrs4s Oct 9, 2023
8607542
Update README.md
Mrs4s Oct 9, 2023
e1a4293
fix: switch ghproxy.com to mirror.ghproxy.com
zhullyb Nov 14, 2023
9cccd0e
Merge pull request #2492 from zhullyb/master
fumiama Nov 22, 2023
b8d622b
fix #2494
Akegarasu Jan 3, 2024
d25e320
fix: log error when reading version file. (#2503)
Akegarasu Jan 4, 2024
7563573
Merge pull request #2504 from Akegarasu/fix-2494
fumiama Jan 7, 2024
5aca1f4
chore: make lint happy
fumiama Feb 26, 2024
e06edd2
chore(deps): bump golang.org/x/crypto from 0.11.0 to 0.17.0 (#2502)
dependabot[bot] Feb 26, 2024
12e0cb4
chore(deps): bump golang.org/x/image from 0.9.0 to 0.10.0 (#2484)
dependabot[bot] Feb 26, 2024
730d01c
chore: make lint happy
fumiama Feb 26, 2024
a5923f1
Merge pull request #2526 from Mrs4s/dev
fumiama May 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ _✨ 基于 [Mirai](https://github.com/mamoe/mirai) 以及 [MiraiGo](https://git
<a href="https://github.com/Mrs4s/go-cqhttp/blob/master/CONTRIBUTING.md">参与贡献</a>
</p>

## 重要信息
由于QQ官方针对协议库的围追堵截, 不断更新加密方案, 我们已无力继续维护此项目.
建议Bot开发者尽快迁移至无头NTQQ项目 -> https://github.com/Mrs4s/go-cqhttp/issues/2471

## 兼容性
go-cqhttp 兼容 [OneBot-v11](https://github.com/botuniverse/onebot-11) 绝大多数内容,并在其基础上做了一些扩展,详情请看 go-cqhttp 的文档。
Expand Down
284 changes: 0 additions & 284 deletions cmd/gocq/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,21 @@ package gocq
import (
"bufio"
"bytes"
"encoding/hex"
"fmt"
"image"
"image/png"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"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"
"gopkg.ilharper.com/x/isatty"

"github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/internal/base"
"github.com/Mrs4s/go-cqhttp/internal/download"
)

Expand Down Expand Up @@ -268,279 +260,3 @@ func fetchCaptcha(id string) string {
}
return ""
}

func energy(uin uint64, id string, _ string, salt []byte) ([]byte, error) {
signServer := base.SignServer
if !strings.HasSuffix(signServer, "/") {
signServer += "/"
}
headers := make(map[string]string)
signServerBearer := base.SignServerBearer
if signServerBearer != "-" && signServerBearer != "" {
headers["Authorization"] = "Bearer " + signServerBearer
}
req := download.Request{
Method: http.MethodGet,
Header: headers,
URL: signServer + "custom_energy" + fmt.Sprintf("?data=%v&salt=%v&uin=%v&android_id=%v&guid=%v",
id, hex.EncodeToString(salt), uin, utils.B2S(device.AndroidId), hex.EncodeToString(device.Guid)),
}.WithTimeout(time.Duration(base.SignServerTimeout) * time.Second)
if base.IsBelow110 {
req.URL = signServer + "custom_energy" + fmt.Sprintf("?data=%v&salt=%v", id, hex.EncodeToString(salt))
}
response, err := req.Bytes()
if err != nil {
log.Warnf("获取T544 sign时出现错误: %v server: %v", err, signServer)
return nil, err
}
data, err := hex.DecodeString(gjson.GetBytes(response, "data").String())
if err != nil {
log.Warnf("获取T544 sign时出现错误: %v", err)
return nil, err
}
if len(data) == 0 {
log.Warnf("获取T544 sign时出现错误: %v", "data is empty")
return nil, errors.New("data is empty")
}
return data, nil
}

// signSubmit 提交的操作类型
func signSubmit(uin string, cmd string, callbackID int64, buffer []byte, t string) {
signServer := base.SignServer
if !strings.HasSuffix(signServer, "/") {
signServer += "/"
}
buffStr := hex.EncodeToString(buffer)
log.Infof("submit %v: uin=%v, cmd=%v, callbackID=%v, buffer-end=%v", t, uin, cmd, callbackID,
buffStr[len(buffStr)-10:])
_, err := download.Request{
Method: http.MethodGet,
URL: signServer + "submit" + fmt.Sprintf("?uin=%v&cmd=%v&callback_id=%v&buffer=%v",
uin, cmd, callbackID, buffStr),
}.WithTimeout(time.Duration(base.SignServerTimeout) * time.Second).Bytes()
if err != nil {
log.Warnf("提交 callback 时出现错误: %v server: %v", err, signServer)
}
}

// signCallback request token 和签名的回调
func signCallback(uin string, results []gjson.Result, t string) {
for _, result := range results {
cmd := result.Get("cmd").String()
callbackID := result.Get("callbackId").Int()
body, _ := hex.DecodeString(result.Get("body").String())
ret, err := cli.SendSsoPacket(cmd, body)
if err != nil {
log.Warnf("callback error: %v", err)
}
signSubmit(uin, cmd, callbackID, ret, t)
}
}

func signRequset(seq uint64, uin string, cmd string, qua string, buff []byte) (sign []byte, extra []byte, token []byte, err error) {
signServer := base.SignServer
if !strings.HasSuffix(signServer, "/") {
signServer += "/"
}
headers := map[string]string{"Content-Type": "application/x-www-form-urlencoded"}
signServerBearer := base.SignServerBearer
if signServerBearer != "-" && signServerBearer != "" {
headers["Authorization"] = "Bearer " + signServerBearer
}
response, err := download.Request{
Method: http.MethodPost,
URL: signServer + "sign",
Header: headers,
Body: bytes.NewReader([]byte(fmt.Sprintf("uin=%v&qua=%s&cmd=%s&seq=%v&buffer=%v&android_id=%v&guid=%v",
uin, qua, cmd, seq, hex.EncodeToString(buff), utils.B2S(device.AndroidId), hex.EncodeToString(device.Guid)))),
}.WithTimeout(time.Duration(base.SignServerTimeout) * time.Second).Bytes()
if err != nil {
return nil, nil, nil, err
}
sign, _ = hex.DecodeString(gjson.GetBytes(response, "data.sign").String())
extra, _ = hex.DecodeString(gjson.GetBytes(response, "data.extra").String())
token, _ = hex.DecodeString(gjson.GetBytes(response, "data.token").String())
if !base.IsBelow110 {
go signCallback(uin, gjson.GetBytes(response, "data.requestCallback").Array(), "sign")
}
return sign, extra, token, nil
}

var registerLock sync.Mutex

func signRegister(uin int64, androidID, guid []byte, qimei36, key string) {
if base.IsBelow110 {
log.Warn("签名服务器版本低于1.1.0, 跳过实例注册")
return
}
signServer := base.SignServer
if !strings.HasSuffix(signServer, "/") {
signServer += "/"
}
resp, err := download.Request{
Method: http.MethodGet,
URL: signServer + "register" + fmt.Sprintf("?uin=%v&android_id=%v&guid=%v&qimei36=%v&key=%s",
uin, utils.B2S(androidID), hex.EncodeToString(guid), qimei36, key),
}.WithTimeout(time.Duration(base.SignServerTimeout) * time.Second).Bytes()
if err != nil {
log.Warnf("注册QQ实例时出现错误: %v server: %v", err, signServer)
return
}
msg := gjson.GetBytes(resp, "msg")
if gjson.GetBytes(resp, "code").Int() != 0 {
log.Warnf("注册QQ实例时出现错误: %v server: %v", msg, signServer)
return
}
log.Infof("注册QQ实例 %v 成功: %v", uin, msg)
}

func signRefreshToken(uin string) error {
signServer := base.SignServer
if !strings.HasSuffix(signServer, "/") {
signServer += "/"
}
log.Info("正在刷新 token")
resp, err := download.Request{
Method: http.MethodGet,
URL: signServer + "request_token" + fmt.Sprintf("?uin=%v", uin),
}.WithTimeout(time.Duration(base.SignServerTimeout) * time.Second).Bytes()
if err != nil {
return err
}
msg := gjson.GetBytes(resp, "msg")
if gjson.GetBytes(resp, "code").Int() != 0 {
return errors.New(msg.String())
}
go signCallback(uin, gjson.GetBytes(resp, "data").Array(), "request token")
return nil
}

var missTokenCount = uint64(0)

func sign(seq uint64, uin string, cmd string, qua string, buff []byte) (sign []byte, extra []byte, token []byte, err error) {
i := 0
for {
sign, extra, token, err = signRequset(seq, uin, cmd, qua, buff)
if err != nil {
log.Warnf("获取sso sign时出现错误: %v server: %v", err, base.SignServer)
}
if i > 0 {
break
}
i++
if (!base.IsBelow110) && base.Account.AutoRegister && err == nil && len(sign) == 0 {
if registerLock.TryLock() { // 避免并发时多处同时销毁并重新注册
log.Warn("获取签名为空,实例可能丢失,正在尝试重新注册")
defer registerLock.Unlock()
err := signServerDestroy(uin)
if err != nil {
log.Warnln(err)
return nil, nil, nil, err
}
signRegister(base.Account.Uin, device.AndroidId, device.Guid, device.QImei36, base.Key)
}
continue
}
if (!base.IsBelow110) && base.Account.AutoRefreshToken && len(token) == 0 {
log.Warnf("token 已过期, 总丢失 token 次数为 %v", atomic.AddUint64(&missTokenCount, 1))
if registerLock.TryLock() {
defer registerLock.Unlock()
if err := signRefreshToken(uin); err != nil {
log.Warnf("刷新 token 出现错误: %v server: %v", err, base.SignServer)
} else {
log.Info("刷新 token 成功")
}
}
continue
}
break
}
return sign, extra, token, err
}

func signServerDestroy(uin string) error {
signServer := base.SignServer
if !strings.HasSuffix(signServer, "/") {
signServer += "/"
}
signVersion, err := signVersion()
if err != nil {
return errors.Wrapf(err, "获取签名服务版本出现错误, server: %v", signServer)
}
if global.VersionNameCompare("v"+signVersion, "v1.1.6") {
return errors.Errorf("当前签名服务器版本 %v 低于 1.1.6,无法使用 destroy 接口", signVersion)
}
resp, err := download.Request{
Method: http.MethodGet,
URL: signServer + "destroy" + fmt.Sprintf("?uin=%v&key=%v", uin, base.Key),
}.WithTimeout(time.Duration(base.SignServerTimeout) * time.Second).Bytes()
if err != nil || gjson.GetBytes(resp, "code").Int() != 0 {
return errors.Wrapf(err, "destroy 实例出现错误, server: %v", signServer)
}
return nil
}

func signVersion() (version string, err error) {
signServer := base.SignServer
resp, err := download.Request{
Method: http.MethodGet,
URL: signServer,
}.WithTimeout(time.Duration(base.SignServerTimeout) * time.Second).Bytes()
if err != nil {
return "", err
}
if gjson.GetBytes(resp, "code").Int() == 0 {
return gjson.GetBytes(resp, "data.version").String(), nil
}
return "", errors.New("empty version")
}

// 定时刷新 token, interval 为间隔时间(分钟)
func signStartRefreshToken(interval int64) {
if interval <= 0 {
log.Warn("定时刷新 token 已关闭")
return
}
log.Infof("每 %v 分钟将刷新一次签名 token", interval)
if interval < 10 {
log.Warnf("间隔时间 %v 分钟较短,推荐 30~40 分钟", interval)
}
if interval > 60 {
log.Warn("间隔时间不能超过 60 分钟,已自动设置为 60 分钟")
interval = 60
}
t := time.NewTicker(time.Duration(interval) * time.Minute)
defer t.Stop()
for range t.C {
err := signRefreshToken(strconv.FormatInt(base.Account.Uin, 10))
if err != nil {
log.Warnf("刷新 token 出现错误: %v server: %v", err, base.SignServer)
}
}
}

func signWaitServer() bool {
t := time.NewTicker(time.Second * 5)
defer t.Stop()
i := 0
for range t.C {
if i > 3 {
return false
}
i++
u, err := url.Parse(base.SignServer)
if err != nil {
log.Warnf("连接到签名服务器出现错误: %v", err)
continue
}
r := utils.RunTCPPingLoop(u.Host, 4)
if r.PacketsLoss > 0 {
log.Warnf("连接到签名服务器出现错误: 丢包%d/%d 时延%dms", r.PacketsLoss, r.PacketsSent, r.AvgTimeMill)
continue
}
break
}
log.Infof("连接至签名服务器: %s", base.SignServer)
return true
}
Loading
Loading