From 60506593e5a15f75982411444fe4a1a2b84713c2 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 6 Sep 2022 15:38:47 -0400 Subject: [PATCH] Handle StreamReplaced errors --- config/bridge.go | 3 ++- config/upgrade.go | 1 + example-config.yaml | 3 +++ go.mod | 2 +- go.sum | 4 ++-- user.go | 9 +++++++++ 6 files changed, 18 insertions(+), 4 deletions(-) diff --git a/config/bridge.go b/config/bridge.go index 758bed3..5828ee7 100644 --- a/config/bridge.go +++ b/config/bridge.go @@ -121,7 +121,8 @@ type BridgeConfig struct { DisableStatusBroadcastSend bool `yaml:"disable_status_broadcast_send"` DisappearingMessagesInGroups bool `yaml:"disappearing_messages_in_groups"` - DisableBridgeAlerts bool `yaml:"disable_bridge_alerts"` + DisableBridgeAlerts bool `yaml:"disable_bridge_alerts"` + CrashOnStreamReplaced bool `yaml:"crash_on_stream_replaced"` CommandPrefix string `yaml:"command_prefix"` diff --git a/config/upgrade.go b/config/upgrade.go index 77a7891..562c8a1 100644 --- a/config/upgrade.go +++ b/config/upgrade.go @@ -89,6 +89,7 @@ func DoUpgrade(helper *up.Helper) { helper.Copy(up.Bool, "bridge", "federate_rooms") helper.Copy(up.Bool, "bridge", "disappearing_messages_in_groups") helper.Copy(up.Bool, "bridge", "disable_bridge_alerts") + helper.Copy(up.Bool, "bridge", "crash_on_stream_replaced") helper.Copy(up.Bool, "bridge", "url_previews") helper.Copy(up.Bool, "bridge", "caption_in_message") helper.Copy(up.Str|up.Null, "bridge", "message_handling_timeout", "error_after") diff --git a/example-config.yaml b/example-config.yaml index eef9649..864b8c9 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -274,6 +274,9 @@ bridge: # Should the bridge never send alerts to the bridge management room? # These are mostly things like the user being logged out. disable_bridge_alerts: false + # Should the bridge stop if the WhatsApp server says another user connected with the same session? + # This is only safe on single-user bridges. + crash_on_stream_replaced: false # Should the bridge detect URLs in outgoing messages, ask the homeserver to generate a preview, # and send it to WhatsApp? URL previews can always be sent using the `com.beeper.linkpreviews` # key in the event content even if this is disabled. diff --git a/go.mod b/go.mod index f6f47e6..c27d860 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( golang.org/x/net v0.0.0-20220812174116-3211cb980234 google.golang.org/protobuf v1.28.1 maunium.net/go/maulogger/v2 v2.3.2 - maunium.net/go/mautrix v0.12.1-0.20220825085831-88c1e0dc4593 + maunium.net/go/mautrix v0.12.1-0.20220906193443-f700a94f80b7 ) require ( diff --git a/go.sum b/go.sum index 0382dda..addb142 100644 --- a/go.sum +++ b/go.sum @@ -100,5 +100,5 @@ maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M= maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA= maunium.net/go/maulogger/v2 v2.3.2 h1:1XmIYmMd3PoQfp9J+PaHhpt80zpfmMqaShzUTC7FwY0= maunium.net/go/maulogger/v2 v2.3.2/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A= -maunium.net/go/mautrix v0.12.1-0.20220825085831-88c1e0dc4593 h1:HsTKCkpyDnJg0rOjzynChCAgK9NrICXEqMRaRiz43fI= -maunium.net/go/mautrix v0.12.1-0.20220825085831-88c1e0dc4593/go.mod h1:/jxQFIipObSsjZPH6o3xyUi8uoULz3Hfr/8p9loqpYE= +maunium.net/go/mautrix v0.12.1-0.20220906193443-f700a94f80b7 h1:7HXfRjWHoZ9ISo9K19yR1j8WSrbn1q2Sd0eqdAQXxVg= +maunium.net/go/mautrix v0.12.1-0.20220906193443-f700a94f80b7/go.mod h1:/jxQFIipObSsjZPH6o3xyUi8uoULz3Hfr/8p9loqpYE= diff --git a/user.go b/user.go index 998e101..14d521b 100644 --- a/user.go +++ b/user.go @@ -815,6 +815,15 @@ func (user *User) HandleEvent(event interface{}) { } go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Message: message}) user.bridge.Metrics.TrackConnectionState(user.JID, false) + case *events.StreamReplaced: + if user.bridge.Config.Bridge.CrashOnStreamReplaced { + user.log.Infofln("Stopping bridge due to StreamReplaced event") + user.bridge.ManualStop(60) + } else { + go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Message: "Stream replaced"}) + user.bridge.Metrics.TrackConnectionState(user.JID, false) + user.sendMarkdownBridgeAlert("The bridge was started in another location. Use `reconnect` to reconnect this one.") + } case *events.ConnectFailure: go user.BridgeState.Send(status.BridgeState{StateEvent: status.StateUnknownError, Message: fmt.Sprintf("Unknown connection failure: %s", v.Reason)}) user.bridge.Metrics.TrackConnectionState(user.JID, false)