From ea1e9876d71f40f74446ad3a15da2abe48beca56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=9A=E5=90=83=E7=82=B9=E8=8B=B9=E6=9E=9C?= <73388495+eatmoreapple@users.noreply.github.com> Date: Mon, 20 Feb 2023 22:26:40 +0800 Subject: [PATCH] =?UTF-8?q?[update]:=20=E6=9B=B4=E6=96=B0=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E7=99=BB=E5=BD=95=20v2=20=E7=89=88=E6=9C=AC=20(#282)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.go | 9 +++------ caller.go | 16 +++++++++++----- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/bot.go b/bot.go index 449e652..4fd4e3a 100644 --- a/bot.go +++ b/bot.go @@ -8,7 +8,6 @@ import ( "net/url" "os/exec" "runtime" - "sync" ) type Bot struct { @@ -22,7 +21,6 @@ type Bot struct { Serializer Serializer // 序列化器, 默认为json Storage *Storage Caller *Caller - once sync.Once err error context context.Context cancel context.CancelFunc @@ -184,8 +182,7 @@ func (b *Bot) WebInit() error { } // 开启协程,轮询获取是否有新的消息返回 - // FIX: 当bot在线的情况下执行热登录,会开启多次事件监听 - go b.once.Do(func() { + go func() { if b.MessageErrorHandler == nil { b.MessageErrorHandler = defaultSyncCheckErrHandler(b) } @@ -201,7 +198,7 @@ func (b *Bot) WebInit() error { break } } - }) + }() return nil } @@ -293,7 +290,7 @@ func (b *Bot) Block() error { return errors.New("`Block` must be called after user login") } <-b.Context().Done() - return nil + return b.CrashReason() } // Exit 主动退出,让 Block 不在阻塞 diff --git a/caller.go b/caller.go index 1ee1e10..a8e11a6 100644 --- a/caller.go +++ b/caller.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "io" - "net/http" "net/url" "os" ) @@ -67,16 +66,23 @@ func (c *Caller) CheckLogin(uuid, tip string) (CheckLoginResponse, error) { // GetLoginInfo 获取登录信息 func (c *Caller) GetLoginInfo(path *url.URL) (*LoginInfo, error) { // 从响应体里面获取需要跳转的url + query := path.Query() + query.Set("version", "v2") + path.RawQuery = query.Encode() resp, err := c.Client.GetLoginInfo(path) if err != nil { return nil, err } - // 判断是否重定向 - if resp.StatusCode != http.StatusMovedPermanently { - return nil, fmt.Errorf("%w: try to login with Desktop Mode", ErrForbidden) - } + // 微信 v2 版本修复了301 response missing Location header 的问题 defer func() { _ = resp.Body.Close() }() + if _, exists := CookieGroup(resp.Cookies()).GetByName("wxuin"); !exists { + err = ErrForbidden + if c.Client.mode != desktop { + err = fmt.Errorf("%w: try to login with desktop mode", err) + } + return nil, err + } var loginInfo LoginInfo // xml结构体序列化储存 if err := scanXml(resp.Body, &loginInfo); err != nil {