From 69e5a26e0178aaf11cf13ed8cab414483f55e4c8 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 24 Feb 2021 00:06:11 +0200 Subject: [PATCH] Improve handling of errors noticed in provisioning API pings --- go.mod | 2 +- go.sum | 2 ++ provisioning.go | 6 ++++++ user.go | 9 ++++++++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 842058f..43a5229 100644 --- a/go.mod +++ b/go.mod @@ -16,4 +16,4 @@ require ( maunium.net/go/mautrix v0.8.3 ) -replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223153547-62454c3af2c8 +replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223220427-1fc7fa00f13b diff --git a/go.sum b/go.sum index 56d6ed0..36a692d 100644 --- a/go.sum +++ b/go.sum @@ -297,6 +297,8 @@ github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223140805-2de4c4c5473f h1:7GPYeq github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223140805-2de4c4c5473f/go.mod h1:rwwuTh1bKqhgrRvOBAr8hDqtuz8Cc1Quqw/0BeXb+/E= github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223153547-62454c3af2c8 h1:WVZyCGqywD7ss+soR35mBPim3OWrgaJTjqQHEvhoJRg= github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223153547-62454c3af2c8/go.mod h1:rwwuTh1bKqhgrRvOBAr8hDqtuz8Cc1Quqw/0BeXb+/E= +github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223220427-1fc7fa00f13b h1:oAcxqLVVaT5nzeB7P/Orlp4h4e9WaphcBUSTJgK0fDo= +github.com/tulir/go-whatsapp v0.4.0-rc.1.0.20210223220427-1fc7fa00f13b/go.mod h1:rwwuTh1bKqhgrRvOBAr8hDqtuz8Cc1Quqw/0BeXb+/E= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= diff --git a/provisioning.go b/provisioning.go index 0768883..88199e5 100644 --- a/provisioning.go +++ b/provisioning.go @@ -268,6 +268,12 @@ func (prov *ProvisioningAPI) Ping(w http.ResponseWriter, r *http.Request) { user.log.Debugln("Pinging WhatsApp mobile due to /ping API request") err := user.Conn.AdminTest() var errStr string + if err == whatsapp.ErrPingFalse { + user.log.Debugln("Forwarding ping false error from provisioning API to HandleError") + go user.HandleError(err) + } else if errors.Is(err, whatsapp.ErrConnectionTimeout) { + user.Conn.CountTimeout() + } if err != nil { errStr = err.Error() } diff --git a/user.go b/user.go index 81c146d..9d598c3 100644 --- a/user.go +++ b/user.go @@ -284,7 +284,7 @@ func (user *User) DeleteConnection() { func (user *User) RestoreSession() bool { if user.Session != nil { user.Conn.SetSession(*user.Session) - ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) defer cancel() err := user.Conn.Restore(true, ctx) if err == whatsapp.ErrAlreadyLoggedIn { @@ -841,6 +841,10 @@ func (user *User) HandleError(err error) { user.ConnectionErrors++ go user.tryReconnect(fmt.Sprintf("Your WhatsApp connection failed: %v", failed.Err)) } else if err == whatsapp.ErrPingFalse { + disconnectErr := user.Conn.Disconnect() + if disconnectErr != nil { + user.log.Warnln("Failed to disconnect after failed ping:", disconnectErr) + } user.bridge.Metrics.TrackDisconnection(user.MXID) user.ConnectionErrors++ go user.tryReconnect(fmt.Sprintf("Your WhatsApp connection failed: %v", err)) @@ -902,6 +906,9 @@ func (user *User) tryReconnect(msg string) { user.sendMarkdownBridgeAlert("\u26a0 Failed to reconnect to WhatsApp: unpaired from phone. " + "To re-pair your phone, log in again.") return + } else if errors.Is(err, whatsapp.ErrAlreadyLoggedIn) { + user.log.Warnln("Reconnection said we're already logged in, not trying anymore") + return } else { user.log.Errorln("Error while trying to reconnect after disconnection:", err) }