Send a valid request when doing hacky phone pings

This commit is contained in:
Tulir Asokan 2022-05-16 11:22:41 +03:00
parent 0ed0f61ced
commit 3ee7c8527a
4 changed files with 26 additions and 6 deletions

View file

@ -165,3 +165,9 @@ func (user *User) Update() {
user.log.Warnfln("Failed to update %s: %v", user.MXID, err) user.log.Warnfln("Failed to update %s: %v", user.MXID, err)
} }
} }
func (user *User) GetLastAppStateKeyID() ([]byte, error) {
var keyID []byte
err := user.db.QueryRow("SELECT key_id FROM whatsmeow_app_state_sync_keys ORDER BY timestamp DESC LIMIT 1").Scan(&keyID)
return keyID, err
}

2
go.mod
View file

@ -10,7 +10,7 @@ require (
github.com/prometheus/client_golang v1.12.2-0.20220514081015-5d584e2717ef github.com/prometheus/client_golang v1.12.2-0.20220514081015-5d584e2717ef
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/tidwall/gjson v1.14.1 github.com/tidwall/gjson v1.14.1
go.mau.fi/whatsmeow v0.0.0-20220515110043-a0a87653cff0 go.mau.fi/whatsmeow v0.0.0-20220516080154-a168fde5a9de
golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9 golang.org/x/image v0.0.0-20220413100746-70e8d0d3baa9
golang.org/x/net v0.0.0-20220513224357-95641704303c golang.org/x/net v0.0.0-20220513224357-95641704303c
google.golang.org/protobuf v1.28.0 google.golang.org/protobuf v1.28.0

4
go.sum
View file

@ -62,8 +62,8 @@ github.com/yuin/goldmark v1.4.12 h1:6hffw6vALvEDqJ19dOJvJKOoAOKe4NDaTqvd2sktGN0=
github.com/yuin/goldmark v1.4.12/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.4.12/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mau.fi/libsignal v0.0.0-20220425070825-c40c839ee6a0 h1:3IQF2bgAyibdo77hTejwuJe4jlypj9QaE4xCQuxrThM= go.mau.fi/libsignal v0.0.0-20220425070825-c40c839ee6a0 h1:3IQF2bgAyibdo77hTejwuJe4jlypj9QaE4xCQuxrThM=
go.mau.fi/libsignal v0.0.0-20220425070825-c40c839ee6a0/go.mod h1:kBOXTvYyDG/q1Ihgvd4J6WenGPh7wtEGvPKF6vmf5ak= go.mau.fi/libsignal v0.0.0-20220425070825-c40c839ee6a0/go.mod h1:kBOXTvYyDG/q1Ihgvd4J6WenGPh7wtEGvPKF6vmf5ak=
go.mau.fi/whatsmeow v0.0.0-20220515110043-a0a87653cff0 h1:VtmnDmyjGRaKP6i8cjvn8rHq+qolMdToXNQMEEtheM0= go.mau.fi/whatsmeow v0.0.0-20220516080154-a168fde5a9de h1:656AnW7jfFOU42e3py0beh5rj5/Ikg7sxmyQBLiSJGA=
go.mau.fi/whatsmeow v0.0.0-20220515110043-a0a87653cff0/go.mod h1:iUBgOLNaqShLrR17u0kIiRptIGFH+nbT1tRhaWBEX/c= go.mau.fi/whatsmeow v0.0.0-20220516080154-a168fde5a9de/go.mod h1:iUBgOLNaqShLrR17u0kIiRptIGFH+nbT1tRhaWBEX/c=
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9 h1:NUzdAbFtCJSXU20AOXgeqaUwg8Ypg4MPYmL+d+rsB5c= golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9 h1:NUzdAbFtCJSXU20AOXgeqaUwg8Ypg4MPYmL+d+rsB5c=
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=

20
user.go
View file

@ -506,15 +506,29 @@ const PhoneDisconnectPingTime = 10 * 24 * time.Hour
const PhoneMinPingInterval = 24 * time.Hour const PhoneMinPingInterval = 24 * time.Hour
func (user *User) sendHackyPhonePing() { func (user *User) sendHackyPhonePing() {
msgID := whatsmeow.GenerateMessageID()
user.PhoneLastPinged = time.Now() user.PhoneLastPinged = time.Now()
msgID := whatsmeow.GenerateMessageID()
keyIDs := make([]*waProto.AppStateSyncKeyId, 0, 1)
lastKeyID, err := user.GetLastAppStateKeyID()
if lastKeyID != nil {
keyIDs = append(keyIDs, &waProto.AppStateSyncKeyId{
KeyId: lastKeyID,
})
} else {
user.log.Warnfln("Failed to get last app state key ID to send hacky phone ping: %v - sending empty request", err)
}
ts, err := user.Client.SendMessage(user.JID.ToNonAD(), msgID, &waProto.Message{ ts, err := user.Client.SendMessage(user.JID.ToNonAD(), msgID, &waProto.Message{
ProtocolMessage: &waProto.ProtocolMessage{}, ProtocolMessage: &waProto.ProtocolMessage{
Type: waProto.ProtocolMessage_APP_STATE_SYNC_KEY_REQUEST.Enum(),
AppStateSyncKeyRequest: &waProto.AppStateSyncKeyRequest{
KeyIds: keyIDs,
},
},
}) })
if err != nil { if err != nil {
user.log.Warnfln("Failed to send hacky phone ping: %v", err) user.log.Warnfln("Failed to send hacky phone ping: %v", err)
} else { } else {
user.log.Debugfln("Sent hacky phone ping %s/%s because phone has been offline for >10 days", msgID, ts) user.log.Debugfln("Sent hacky phone ping %s/%s because phone has been offline for >10 days", msgID, ts.Unix())
user.PhoneLastPinged = ts user.PhoneLastPinged = ts
user.Update() user.Update()
} }