From 1ab1cba84c4307c178c80b81c8b57d3a1d06ea9b Mon Sep 17 00:00:00 2001 From: Mrs4s Date: Sun, 2 Apr 2023 18:04:13 +0800 Subject: [PATCH] rf: change protocol auto-update to manual update --- cmd/gocq/main.go | 37 ++++++++++++++++++------------- internal/base/flag.go | 2 ++ modules/config/config.go | 15 ++++++------- modules/config/default_config.yml | 2 -- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/cmd/gocq/main.go b/cmd/gocq/main.go index b0e2e67..efd6479 100644 --- a/cmd/gocq/main.go +++ b/cmd/gocq/main.go @@ -18,6 +18,7 @@ import ( rotatelogs "github.com/lestrrat-go/file-rotatelogs" "github.com/pkg/errors" log "github.com/sirupsen/logrus" + "github.com/tidwall/gjson" "golang.org/x/crypto/pbkdf2" "golang.org/x/term" @@ -281,24 +282,30 @@ func LoginInteract() { cli.Uin = base.Account.Uin cli.PasswordMd5 = base.PasswordHash } - if !isTokenLogin { - if !base.Account.DisableProtocolUpdate { - log.Infof("正在检查协议更新...") - oldVersionName := device.Protocol.Version().String() - remoteVersion, err := getRemoteLatestProtocolVersion(int(device.Protocol.Version().Protocol)) - if err == nil { - if err = device.Protocol.Version().UpdateFromJson(remoteVersion); err == nil { - if device.Protocol.Version().String() != oldVersionName { - log.Infof("已自动更新协议版本: %s -> %s", oldVersionName, device.Protocol.Version().String()) - } else { - log.Infof("协议已经是最新版本") - } - _ = os.WriteFile(versionFile, remoteVersion, 0o644) + if !base.FastStart { + log.Infof("正在检查协议更新...") + currentVersionName := device.Protocol.Version().SortVersionName + remoteVersion, err := getRemoteLatestProtocolVersion(int(device.Protocol.Version().Protocol)) + if err == nil { + remoteVersionName := gjson.GetBytes(remoteVersion, "sort_version_name").String() + if remoteVersionName != currentVersionName { + switch { + case !base.UpdateProtocol: + log.Infof("检测到协议更新: %s -> %s", currentVersionName, remoteVersionName) + log.Infof("如果登录时出现版本过低错误, 可尝试使用 -update-protocol 参数启动") + case !isTokenLogin: + _ = device.Protocol.Version().UpdateFromJson(remoteVersion) + log.Infof("协议版本已更新: %s -> %s", currentVersionName, remoteVersionName) + default: + log.Infof("检测到协议更新: %s -> %s", currentVersionName, remoteVersionName) + log.Infof("由于使用了会话缓存, 无法自动更新协议, 请删除缓存后重试") } - } else if err.Error() != "remote version unavailable" { - log.Warnf("检查协议更新失败: %v", err) } + } else if err.Error() != "remote version unavailable" { + log.Warnf("检查协议更新失败: %v", err) } + } + if !isTokenLogin { if !isQRCodeLogin { if err := commonLogin(); err != nil { log.Fatalf("登录时发生致命错误: %v", err) diff --git a/internal/base/flag.go b/internal/base/flag.go index 5e546b5..44e018c 100644 --- a/internal/base/flag.go +++ b/internal/base/flag.go @@ -37,6 +37,7 @@ var ( LogColorful bool // 是否启用日志颜色 FastStart bool // 是否为快速启动 AllowTempSession bool // 是否允许发送临时会话信息 + UpdateProtocol bool // 是否更新协议 SignServerOverwrite string // 使用特定的服务器进行签名 PostFormat string // 上报格式 string or array @@ -61,6 +62,7 @@ func Parse() { flag.StringVar(&LittleWD, "w", "", "cover the working directory") d := flag.Bool("D", false, "debug mode") flag.BoolVar(&FastStart, "faststart", false, "skip waiting 5 seconds") + flag.BoolVar(&UpdateProtocol, "update-protocol", false, "update protocol") flag.StringVar(&SignServerOverwrite, "sign-server", "", "use special server to sign tlv") flag.Parse() diff --git a/modules/config/config.go b/modules/config/config.go index 53e2abf..22d9879 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -28,14 +28,13 @@ type Reconnect struct { // Account 账号配置 type Account struct { - Uin int64 `yaml:"uin"` - Password string `yaml:"password"` - Encrypt bool `yaml:"encrypt"` - Status int `yaml:"status"` - ReLogin *Reconnect `yaml:"relogin"` - UseSSOAddress bool `yaml:"use-sso-address"` - AllowTempSession bool `yaml:"allow-temp-session"` - DisableProtocolUpdate bool `yaml:"disable-protocol-update"` + Uin int64 `yaml:"uin"` + Password string `yaml:"password"` + Encrypt bool `yaml:"encrypt"` + Status int `yaml:"status"` + ReLogin *Reconnect `yaml:"relogin"` + UseSSOAddress bool `yaml:"use-sso-address"` + AllowTempSession bool `yaml:"allow-temp-session"` } // Config 总配置文件 diff --git a/modules/config/default_config.yml b/modules/config/default_config.yml index 56d51f7..9e4acd8 100644 --- a/modules/config/default_config.yml +++ b/modules/config/default_config.yml @@ -15,8 +15,6 @@ account: # 账号相关 use-sso-address: true # 是否允许发送临时会话消息 allow-temp-session: false - # 是否禁用协议更新 - disable-protocol-update: false heartbeat: # 心跳频率, 单位秒