1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-08 04:55:55 +08:00

Merge remote-tracking branch 'upstream/dev' into test/goreleaser

This commit is contained in:
Ink33 2021-04-03 00:20:53 +08:00
commit 33864b6d07
No known key found for this signature in database
GPG Key ID: 5D8B1D036EFB0D2E
13 changed files with 302 additions and 192 deletions

View File

@ -27,15 +27,15 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout Dist
uses: actions/checkout@v2
with:
repository: 'gocq/dist'
ref: master
ssh-key: ${{ secrets.SSH_KEY }}
path: upstream/dist
#- name: Checkout Dist
# uses: actions/checkout@v2
# with:
# repository: 'gocq/dist'
# ref: master
# ssh-key: ${{ secrets.SSH_KEY }}
# path: upstream/dist
- name: Update Dist
run: |
chmod +x scripts/upload_dist.sh
./scripts/upload_dist.sh
#- name: Update Dist
# run: |
# chmod +x scripts/upload_dist.sh
# ./scripts/upload_dist.sh

View File

@ -14,12 +14,14 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/Mrs4s/go-cqhttp/global"
"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"
)
@ -71,6 +73,9 @@ func (bot *CQBot) CQGetGroupList(noCache bool) MSG {
gs = append(gs, MSG{
"group_id": g.Code,
"group_name": g.Name,
"group_memo": g.Memo,
"group_create_time": g.GroupCreateTime,
"group_level": g.GroupLevel,
"max_member_count": g.MaxMemberCount,
"member_count": g.MemberCount,
})
@ -83,6 +88,9 @@ func (bot *CQBot) CQGetGroupList(noCache bool) MSG {
// https://git.io/Jtz1O
func (bot *CQBot) CQGetGroupInfo(groupID int64, noCache bool) MSG {
group := bot.Client.FindGroup(groupID)
if group == nil || noCache {
group, _ = bot.Client.GetGroupInfo(groupID)
}
if group == nil {
gid := strconv.FormatInt(groupID, 10)
info, err := bot.Client.SearchGroupByKeyword(gid)
@ -94,27 +102,27 @@ func (bot *CQBot) CQGetGroupInfo(groupID int64, noCache bool) MSG {
return OK(MSG{
"group_id": g.Code,
"group_name": g.Name,
"group_memo": g.Memo,
"group_create_time": 0,
"group_level": 0,
"max_member_count": 0,
"member_count": 0,
})
}
}
return Failed(100, "GROUP_NOT_FOUND", "群聊不存在失败")
}
if noCache {
var err error
group, err = bot.Client.GetGroupInfo(groupID)
if err != nil {
return Failed(100, "GET_GROUP_INFO_API_ERROR", err.Error())
}
}
} else {
return OK(MSG{
"group_id": group.Code,
"group_name": group.Name,
"group_memo": group.Memo,
"group_create_time": group.GroupCreateTime,
"group_level": group.GroupLevel,
"max_member_count": group.MaxMemberCount,
"member_count": group.MemberCount,
})
}
return Failed(100, "GROUP_NOT_FOUND", "群聊不存在")
}
// CQGetGroupMemberList 获取群成员列表
//
@ -852,12 +860,7 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
if reply.Exists() {
autoEscape := global.EnsureBool(operation.Get("auto_escape"), false)
at := !isAnonymous && msgType == "group" // 除匿名消息场合外默认 true
if operation.Get("at_sender").Exists() {
at = operation.Get("at_sender").Bool() && !isAnonymous && msgType == "group"
}
at := operation.Get("at_sender").Bool() && !isAnonymous && msgType == "group"
if at && reply.IsArray() {
// 在 reply 数组头部插入CQ码
replySegments := make([]MSG, 0)
@ -1378,10 +1381,15 @@ func convertGroupMemberInfo(groupID int64, m *client.GroupMemberInfo) MSG {
}
func limitedString(str string) string {
if strings.Count(str, "") <= 10 {
if utf8.RuneCountInString(str) <= 10 {
return str
}
limited := []rune(str)
limited = limited[:10]
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) + " ..."
}

View File

@ -299,7 +299,7 @@ func (bot *CQBot) SendPrivateMessage(target int64, groupID int64, m *message.Sen
}
} else if code, ok := bot.tempMsgCache.Load(target); ok || groupID != 0 { // 临时会话
switch {
case bot.Client.FindGroup(groupID) == nil:
case groupID != 0 && bot.Client.FindGroup(groupID) == nil:
log.Errorf("错误: 找不到群(%v)", groupID)
id = -1
case groupID != 0 && !bot.Client.FindGroup(groupID).AdministratorOrOwner():
@ -309,7 +309,7 @@ func (bot *CQBot) SendPrivateMessage(target int64, groupID int64, m *message.Sen
log.Errorf("错误: 群员(%v) 不在 群(%v), 无法发起临时会话", target, groupID)
id = -1
default:
if code != nil {
if code != nil && groupID == 0 {
groupID = code.(int64)
}
msg := bot.Client.SendTempMessage(groupID, target, m)

View File

@ -443,8 +443,14 @@ S1: // Plain Text
if *(*byte)(add(ptr, uintptr(i))) == '[' && i+4 < l &&
*(*uint32)(add(ptr, uintptr(i))) == magicCQ { // Magic :uint32([]byte("[CQ:"))
if i > j {
if SplitURL {
for _, str := range global.SplitURL(CQCodeUnescapeText(s[j:i])) {
r = append(r, message.NewText(str))
}
} else {
r = append(r, message.NewText(CQCodeUnescapeText(s[j:i])))
}
}
CQBegin = i
i += 4
j = i
@ -501,8 +507,14 @@ S4: // CQCode param value
goto End
End:
if i > j {
if SplitURL {
for _, str := range global.SplitURL(CQCodeUnescapeText(s[j:i])) {
r = append(r, message.NewText(str))
}
} else {
r = append(r, message.NewText(CQCodeUnescapeText(s[j:i])))
}
}
return
}

View File

@ -15,6 +15,7 @@ var json = jsoniter.ConfigCompatibleWithStandardLibrary
var currentPath = getCurrentPath()
var DefaultConfFile = path.Join(currentPath, "config.hjson")
var AccountToken []byte
// DefaultConfigWithComments 为go-cqhttp的默认配置文件
var DefaultConfigWithComments = `

View File

@ -1,28 +1,20 @@
package global
import (
"bufio"
"bytes"
"compress/bzip2"
"crypto/md5"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/url"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/kardianos/osext"
"github.com/dustin/go-humanize"
log "github.com/sirupsen/logrus"
)
@ -160,95 +152,3 @@ func ReadAddrFile(path string) []*net.TCPAddr {
}
return ret
}
// WriteCounter 写入量计算实例
type WriteCounter struct {
Total uint64
}
// Write 方法将写入的byte长度追加至写入的总长度Total中
func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
wc.Total += uint64(n)
wc.PrintProgress()
return n, nil
}
// PrintProgress 方法将打印当前的总写入量
func (wc *WriteCounter) PrintProgress() {
fmt.Printf("\r%s", strings.Repeat(" ", 35))
fmt.Printf("\rDownloading... %s complete", humanize.Bytes(wc.Total))
}
// UpdateFromStream copy form getlantern/go-update
func UpdateFromStream(updateWith io.Reader) (err error, errRecover error) {
updatePath, err := osext.Executable()
if err != nil {
return
}
var newBytes []byte
// no patch to apply, go on through
var fileHeader []byte
bufBytes := bufio.NewReader(updateWith)
fileHeader, err = bufBytes.Peek(2)
if err != nil {
return
}
// The content is always bzip2 compressed except when running test, in
// which case is not prefixed with the magic byte sequence for sure.
if bytes.Equal([]byte{0x42, 0x5a}, fileHeader) {
// Identifying bzip2 files.
updateWith = bzip2.NewReader(bufBytes)
} else {
updateWith = io.Reader(bufBytes)
}
newBytes, err = ioutil.ReadAll(updateWith)
if err != nil {
return
}
// get the directory the executable exists in
updateDir := filepath.Dir(updatePath)
filename := filepath.Base(updatePath)
// Copy the contents of of newbinary to a the new executable file
newPath := filepath.Join(updateDir, fmt.Sprintf(".%s.new", filename))
fp, err := os.OpenFile(newPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
if err != nil {
return
}
// We won't log this error, because it's always going to happen.
defer func() { _ = fp.Close() }()
if _, err = io.Copy(fp, bytes.NewReader(newBytes)); err != nil {
log.Errorf("Unable to copy data: %v\n", err)
}
// if we don't call fp.Close(), windows won't let us move the new executable
// because the file will still be "in use"
if err := fp.Close(); err != nil {
log.Errorf("Unable to close file: %v\n", err)
}
// this is where we'll move the executable to so that we can swap in the updated replacement
oldPath := filepath.Join(updateDir, fmt.Sprintf(".%s.old", filename))
// delete any existing old exec file - this is necessary on Windows for two reasons:
// 1. after a successful update, Windows can't remove the .old file because the process is still running
// 2. windows rename operations fail if the destination file already exists
_ = os.Remove(oldPath)
// move the existing executable to a new file in the same directory
err = os.Rename(updatePath, oldPath)
if err != nil {
return
}
// move the new executable in to become the new program
err = os.Rename(newPath, updatePath)
if err != nil {
// copy unsuccessful
errRecover = os.Rename(oldPath, updatePath)
} else {
// copy successful, remove the old binary
_ = os.Remove(oldPath)
}
return
}

96
global/update/update.go Normal file
View File

@ -0,0 +1,96 @@
package update
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/dustin/go-humanize"
"github.com/kardianos/osext"
log "github.com/sirupsen/logrus"
)
// WriteCounter 写入量计算实例
type WriteCounter struct {
Total uint64
}
// Write 方法将写入的byte长度追加至写入的总长度Total中
func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
wc.Total += uint64(n)
wc.PrintProgress()
return n, nil
}
// PrintProgress 方法将打印当前的总写入量
func (wc *WriteCounter) PrintProgress() {
fmt.Printf("\r%s", strings.Repeat(" ", 35))
fmt.Printf("\rDownloading... %s complete", humanize.Bytes(wc.Total))
}
// UpdateFromStream copy form getlantern/go-update
func UpdateFromStream(updateWith io.Reader) (err error, errRecover error) {
updatePath, err := osext.Executable()
if err != nil {
return
}
var newBytes []byte
// no patch to apply, go on through
bufBytes := bufio.NewReader(updateWith)
updateWith = io.Reader(bufBytes)
newBytes, err = ioutil.ReadAll(updateWith)
if err != nil {
return
}
// get the directory the executable exists in
updateDir := filepath.Dir(updatePath)
filename := filepath.Base(updatePath)
// Copy the contents of of newbinary to a the new executable file
newPath := filepath.Join(updateDir, fmt.Sprintf(".%s.new", filename))
fp, err := os.OpenFile(newPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
if err != nil {
return
}
// We won't log this error, because it's always going to happen.
defer func() { _ = fp.Close() }()
if _, err = io.Copy(fp, bytes.NewReader(newBytes)); err != nil {
log.Errorf("Unable to copy data: %v\n", err)
}
// if we don't call fp.Close(), windows won't let us move the new executable
// because the file will still be "in use"
if err := fp.Close(); err != nil {
log.Errorf("Unable to close file: %v\n", err)
}
// this is where we'll move the executable to so that we can swap in the updated replacement
oldPath := filepath.Join(updateDir, fmt.Sprintf(".%s.old", filename))
// delete any existing old exec file - this is necessary on Windows for two reasons:
// 1. after a successful update, Windows can't remove the .old file because the process is still running
// 2. windows rename operations fail if the destination file already exists
_ = os.Remove(oldPath)
// move the existing executable to a new file in the same directory
err = os.Rename(updatePath, oldPath)
if err != nil {
return
}
// move the new executable in to become the new program
err = os.Rename(newPath, updatePath)
if err != nil {
// copy unsuccessful
errRecover = os.Rename(oldPath, updatePath)
} else {
// copy successful, remove the old binary
_ = os.Remove(oldPath)
}
return
}

View File

@ -0,0 +1,50 @@
// +build !windows
package update
import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"io"
"net/http"
log "github.com/sirupsen/logrus"
)
func Update(url string) {
resp, err := http.Get(url)
if err != nil {
log.Error("更新失败: ", err)
return
}
defer resp.Body.Close()
wc := WriteCounter{}
data, err := io.ReadAll(io.TeeReader(resp.Body, &wc))
if err != nil {
log.Error("更新失败: ", err)
return
}
gr, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
log.Error("更新失败: ", err)
return
}
tr := tar.NewReader(gr)
for {
header, err := tr.Next()
if err == io.EOF {
return
}
if header.Name == "go-cqhttp" {
err, _ := UpdateFromStream(tr)
fmt.Println()
if err != nil {
log.Error("更新失败!", err)
return
}
log.Info("更新完成!")
}
}
}

View File

@ -0,0 +1,35 @@
package update
import (
"archive/zip"
"bytes"
"fmt"
"io"
"net/http"
log "github.com/sirupsen/logrus"
)
func Update(url string) {
resp, err := http.Get(url)
if err != nil {
log.Error("更新失败: ", err)
return
}
defer resp.Body.Close()
wc := WriteCounter{}
rsp, _ := io.ReadAll(io.TeeReader(resp.Body, &wc))
reader, _ := zip.NewReader(bytes.NewReader(rsp), resp.ContentLength)
file, err := reader.Open("go-cqhttp.exe")
if err != nil {
log.Error("更新失败!", err)
return
}
err, _ = UpdateFromStream(file)
fmt.Println()
if err != nil {
log.Error("更新失败!", err)
return
}
log.Info("更新完成!")
}

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.16
require (
github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f
github.com/Mrs4s/MiraiGo v0.0.0-20210327070734-ede433b4f625
github.com/Mrs4s/MiraiGo v0.0.0-20210328053212-557c05319718
github.com/dustin/go-humanize v1.0.0
github.com/gin-gonic/gin v1.6.3
github.com/gorilla/websocket v1.4.2

View File

@ -78,7 +78,7 @@ func qrcodeLogin() error {
if s.State == client.QRCodeConfirmed {
res, err := cli.QRCodeLogin(s.LoginInfo)
if err != nil {
return nil
return err
}
return loginResponseProcessor(res)
}

75
main.go
View File

@ -9,9 +9,7 @@ import (
"encoding/hex"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/signal"
"path"
@ -25,6 +23,7 @@ import (
"github.com/Mrs4s/go-cqhttp/coolq"
"github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/global/terminal"
"github.com/Mrs4s/go-cqhttp/global/update"
"github.com/Mrs4s/go-cqhttp/server"
"github.com/Mrs4s/MiraiGo/binary"
@ -166,7 +165,7 @@ func main() {
log.Warning("将等待10s后启动")
time.Sleep(time.Second * 10)
}
if conf.Uin == 0 || (conf.Password == "" && conf.PasswordEncrypted == "") {
if (conf.Uin == 0 || (conf.Password == "" && conf.PasswordEncrypted == "")) && !global.PathExists("session.token") {
log.Warn("账号密码未配置, 将使用二维码登录.")
if !isFastStart {
log.Warn("将在 5秒 后继续.")
@ -314,6 +313,18 @@ func main() {
// b := server.WebServer.Run(fmt.Sprintf("%s:%d", conf.WebUI.Host, conf.WebUI.WebUIPort), cli)
// c := server.Console
isQRCodeLogin := (conf.Uin == 0 || len(conf.Password) == 0) && len(conf.PasswordEncrypted) == 0
isTokenLogin := false
if global.PathExists("session.token") {
token, err := ioutil.ReadFile("session.token")
if err == nil {
if err = cli.TokenLogin(token); err != nil {
log.Warnf("恢复会话失败: %v , 尝试使用正常流程登录.", err)
} else {
isTokenLogin = true
}
}
}
if !isTokenLogin {
if !isQRCodeLogin {
if err := commonLogin(); err != nil {
log.Fatalf("登录时发生致命错误: %v", err)
@ -323,6 +334,12 @@ func main() {
log.Fatalf("登录时发生致命错误: %v", err)
}
}
}
saveToken := func() {
global.AccountToken = cli.GenToken()
_ = ioutil.WriteFile("session.token", global.AccountToken, 0677)
}
saveToken()
var times uint = 1 // 重试次数
var reLoginLock sync.Mutex
cli.OnDisconnected(func(q *client.QQClient, e *client.ClientDisconnectedEvent) {
@ -332,9 +349,6 @@ func main() {
if !conf.ReLogin.Enabled {
os.Exit(1)
}
if isQRCodeLogin {
log.Fatalf("二维码登录暂不支持重连.")
}
if times > conf.ReLogin.MaxReloginTimes && conf.ReLogin.MaxReloginTimes != 0 {
log.Fatalf("Bot重连次数超过限制, 停止")
}
@ -345,6 +359,13 @@ func main() {
if cli.Online {
return
}
if err := cli.TokenLogin(global.AccountToken); err == nil {
saveToken()
return
}
if isQRCodeLogin {
log.Fatalf("快速重连失败")
}
if err := commonLogin(); err != nil {
log.Fatalf("登录时发生致命错误: %v", err)
}
@ -488,45 +509,31 @@ func selfUpdate(imageURL string) {
log.Info("当前最新版本为 ", version)
log.Warn("是否更新(y/N): ")
r := strings.TrimSpace(readLine())
doUpdate := func() {
if r != "y" && r != "Y" {
log.Warn("已取消更新!")
} else {
log.Info("正在更新,请稍等...")
url := fmt.Sprintf(
"%v/Mrs4s/go-cqhttp/releases/download/%v/go-cqhttp-%v-%v-%v",
"%v/Mrs4s/go-cqhttp/releases/download/%v/go-cqhttp_%v_%v",
func() string {
if imageURL != "" {
return imageURL
}
return "https://github.com"
}(),
version,
version,
runtime.GOOS,
runtime.GOARCH,
version, runtime.GOOS, func() string {
if runtime.GOARCH == "arm" {
return "armv7"
}
return runtime.GOARCH
}(),
)
if runtime.GOOS == "windows" {
url += ".exe"
}
resp, err := http.Get(url)
if err != nil {
log.Error("更新失败: ", err)
return
}
defer func() { _ = resp.Body.Close() }()
wc := global.WriteCounter{}
err, _ = global.UpdateFromStream(io.TeeReader(resp.Body, &wc))
fmt.Println()
if err != nil {
log.Error("更新失败!")
return
}
log.Info("更新完成!")
}
if r == "y" || r == "Y" {
doUpdate()
url += ".zip"
} else {
log.Warn("已取消更新!")
url += ".tar.gz"
}
update.Update(url)
}
} else {
log.Info("当前版本已经是最新版本!")

View File

@ -79,9 +79,10 @@ func (s *httpServer) Run(addr, authToken string, bot *coolq.CQBot) {
s.engine.Use(func(c *gin.Context) {
auth := c.Request.Header.Get("Authorization")
switch {
case auth != "" && strings.SplitN(auth, " ", 2)[1] != authToken:
case auth != "":
if strings.SplitN(auth, " ", 2)[1] != authToken {
c.AbortWithStatus(401)
return
}
case c.Query("access_token") != authToken:
c.AbortWithStatus(401)
return