Add option to reconnect aggressively
This commit is contained in:
parent
f57735901f
commit
0e3f211118
2 changed files with 54 additions and 1 deletions
27
commands.go
27
commands.go
|
@ -42,6 +42,7 @@ func (br *GMBridge) RegisterCommands() {
|
|||
cmdLogout,
|
||||
cmdReconnect,
|
||||
cmdDisconnect,
|
||||
cmdSetActive,
|
||||
cmdPing,
|
||||
cmdDeletePortal,
|
||||
cmdDeleteAllPortals,
|
||||
|
@ -246,6 +247,28 @@ func fnDisconnect(ce *WrappedCommandEvent) {
|
|||
ce.User.BridgeState.Send(status.BridgeState{StateEvent: status.StateBadCredentials, Error: GMNotConnected})
|
||||
}
|
||||
|
||||
var cmdSetActive = &commands.FullHandler{
|
||||
Func: wrapCommand(fnSetActive),
|
||||
Name: "set-active",
|
||||
Help: commands.HelpMeta{
|
||||
Section: HelpSectionConnectionManagement,
|
||||
Description: "Set the bridge as the active browser (if you opened Google Messages in a real browser)",
|
||||
},
|
||||
}
|
||||
|
||||
func fnSetActive(ce *WrappedCommandEvent) {
|
||||
if ce.User.Client == nil {
|
||||
ce.Reply("You don't have a Google Messages connection.")
|
||||
return
|
||||
}
|
||||
err := ce.User.Client.SetActiveSession()
|
||||
if err != nil {
|
||||
ce.Reply("Failed to set active session: %v", err)
|
||||
} else {
|
||||
ce.Reply("Set bridge as active session")
|
||||
}
|
||||
}
|
||||
|
||||
var cmdPing = &commands.FullHandler{
|
||||
Func: wrapCommand(fnPing),
|
||||
Name: "ping",
|
||||
|
@ -264,8 +287,10 @@ func fnPing(ce *WrappedCommandEvent) {
|
|||
}
|
||||
} else if ce.User.Client == nil || !ce.User.Client.IsConnected() {
|
||||
ce.Reply("You're logged in as %s, but you don't have a Google Messages connection.", ce.User.PhoneID)
|
||||
} else if ce.User.browserInactiveType == "" {
|
||||
ce.Reply("Logged in as %s and active as primary browser", ce.User.PhoneID)
|
||||
} else {
|
||||
ce.Reply("Logged in as %s, connection to Google Messages may be OK", ce.User.PhoneID)
|
||||
ce.Reply("Logged in as %s, but not active, use `set-active` to reconnect", ce.User.PhoneID)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
28
user.go
28
user.go
|
@ -646,16 +646,37 @@ func (user *User) HandleEvent(event interface{}) {
|
|||
}
|
||||
}
|
||||
|
||||
func (user *User) aggressiveSetActive() {
|
||||
sleepTimes := []int{2, 5, 10, 30}
|
||||
for i := 0; i < 4; i++ {
|
||||
sleep := time.Duration(sleepTimes[i]) * time.Second
|
||||
user.zlog.Info().
|
||||
Int("sleep_seconds", int(sleep.Seconds())).
|
||||
Msg("Aggressively reactivating after sleep")
|
||||
time.Sleep(sleep)
|
||||
err := user.Client.SetActiveSession()
|
||||
if err != nil {
|
||||
user.zlog.Warn().Err(err).Msg("Failed to set self as active session")
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (user *User) handleUserAlert(v *gmproto.UserAlertEvent) {
|
||||
user.zlog.Debug().Any("data", v).Msg("Got user alert event")
|
||||
becameInactive := false
|
||||
switch v.GetAlertType() {
|
||||
case gmproto.AlertType_BROWSER_INACTIVE:
|
||||
// TODO aggressively reactivate if configured to do so
|
||||
user.browserInactiveType = GMBrowserInactive
|
||||
becameInactive = true
|
||||
case gmproto.AlertType_BROWSER_INACTIVE_FROM_TIMEOUT:
|
||||
user.browserInactiveType = GMBrowserInactiveTimeout
|
||||
becameInactive = true
|
||||
case gmproto.AlertType_BROWSER_INACTIVE_FROM_INACTIVITY:
|
||||
user.browserInactiveType = GMBrowserInactiveInactivity
|
||||
becameInactive = true
|
||||
case gmproto.AlertType_MOBILE_DATA_CONNECTION:
|
||||
user.mobileData = true
|
||||
case gmproto.AlertType_MOBILE_WIFI_CONNECTION:
|
||||
|
@ -667,6 +688,13 @@ func (user *User) handleUserAlert(v *gmproto.UserAlertEvent) {
|
|||
default:
|
||||
return
|
||||
}
|
||||
if becameInactive {
|
||||
if user.bridge.Config.GoogleMessages.AggressiveReconnect {
|
||||
go user.aggressiveSetActive()
|
||||
} else {
|
||||
user.sendMarkdownBridgeAlert("Google Messages was opened in another browser. Use `set-active` to reconnect the bridge.")
|
||||
}
|
||||
}
|
||||
user.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue