-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
connecttest.go
330 lines (313 loc) · 10.3 KB
/
connecttest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// mautrix-imessage - A Matrix-iMessage puppeting bridge.
// Copyright (C) 2023 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"crypto/hmac"
cryptoRand "crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"math/rand"
"time"
"maunium.net/go/mautrix/crypto/utils"
"go.mau.fi/mautrix-imessage/database"
"go.mau.fi/mautrix-imessage/imessage"
)
func encryptTestPayload(keyStr string, payload map[string]any) (map[string]any, error) {
key, err := base64.RawStdEncoding.DecodeString(keyStr)
if err != nil {
return nil, fmt.Errorf("failed to decode key: %w", err)
}
aesKey, hmacKey := utils.DeriveKeysSHA256(key, "meow")
data, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("failed to marshal payload: %w", err)
}
iv := make([]byte, utils.AESCTRIVLength)
_, err = cryptoRand.Read(iv)
if err != nil {
return nil, fmt.Errorf("failed to generate iv: %w", err)
}
data = utils.XorA256CTR(data, aesKey, *(*[utils.AESCTRIVLength]byte)(iv))
h := hmac.New(sha256.New, hmacKey[:])
h.Write(data)
h.Write(iv)
return map[string]any{
"ciphertext": base64.RawStdEncoding.EncodeToString(data),
"checksum": base64.RawStdEncoding.EncodeToString(h.Sum(nil)),
"iv": base64.RawStdEncoding.EncodeToString(iv),
}, nil
}
func decryptTestPayload(keyStr string, encrypted map[string]any) (map[string]any, error) {
key, err := base64.RawStdEncoding.DecodeString(keyStr)
if err != nil {
return nil, fmt.Errorf("failed to decode key: %w", err)
}
var ciphertext, checksum, iv []byte
getKey := func(key string) ([]byte, error) {
val, ok := encrypted[key].(string)
if !ok {
return nil, fmt.Errorf("missing %s in encrypted payload", key)
}
decoded, err := base64.RawStdEncoding.DecodeString(val)
if err != nil {
return nil, fmt.Errorf("failed to decode %s: %w", key, err)
}
return decoded, nil
}
if ciphertext, err = getKey("ciphertext"); err != nil {
return nil, err
} else if checksum, err = getKey("checksum"); err != nil {
return nil, err
} else if iv, err = getKey("iv"); err != nil {
return nil, err
} else if len(iv) != utils.AESCTRIVLength {
return nil, fmt.Errorf("incorrect iv length %d", len(iv))
}
aesKey, hmacKey := utils.DeriveKeysSHA256(key, "meow")
h := hmac.New(sha256.New, hmacKey[:])
h.Write(ciphertext)
h.Write(iv)
if !hmac.Equal(h.Sum(nil), checksum) {
return nil, fmt.Errorf("mismatching hmac")
}
var payload map[string]any
err = json.Unmarshal(utils.XorA256CTR(ciphertext, aesKey, *(*[utils.AESCTRIVLength]byte)(iv)), &payload)
return payload, err
}
const startupTestKey = "com.beeper.startup_test"
const startupTestIDKey = "com.beeper.startup_test_id"
const startupTestResponseKey = "com.beeper.startup_test_response"
const hackyTestSegmentEvent = "iMC startup test"
const hackyTestLogAction = "hacky startup test"
func trackStartupTestError(erroredAt, randomID string) {
Segment.Track(hackyTestSegmentEvent, map[string]any{
"event": "fail",
"error": erroredAt,
"random_id": randomID,
})
}
func (br *IMBridge) hackyTestLoop() {
log := br.ZLog.With().
Str("identifier", br.Config.HackyStartupTest.Identifier).
Str("action", "hacky periodic test").
Logger()
log.Info().
Int("interval", br.Config.HackyStartupTest.PeriodicResolve).
Msg("Starting hacky periodic test loop")
for {
time.Sleep(time.Duration(br.Config.HackyStartupTest.PeriodicResolve) * time.Second)
log.Debug().Msg("Sending hacky periodic test")
resp, err := br.WebsocketHandler.StartChat(StartDMRequest{
Identifier: br.Config.HackyStartupTest.Identifier,
})
if err != nil {
log.Error().Err(err).Msg("Failed to resolve identifier")
} else {
log.Info().Interface("response", resp).Msg("Successfully resolved identifier")
}
}
}
func (br *IMBridge) hackyStartupTests(sleep, forceSend bool) {
if sleep {
time.Sleep(time.Duration(rand.Intn(120)+60) * time.Second)
}
br.DB.KV.Set(database.KVBridgeWasConnected, "true")
randomID := br.Bot.TxnID()
log := br.ZLog.With().
Str("identifier", br.Config.HackyStartupTest.Identifier).
Str("random_id", randomID).
Str("action", hackyTestLogAction).
Logger()
log.Info().Msg("Running hacky startup test")
actuallyStart := br.Config.HackyStartupTest.Message != "" && (br.Config.HackyStartupTest.SendOnStartup || forceSend)
resp, err := br.WebsocketHandler.StartChat(StartDMRequest{
Identifier: br.Config.HackyStartupTest.Identifier,
ActuallyStart: actuallyStart,
})
if err != nil {
log.Error().Err(err).Msg("Failed to resolve identifier")
trackStartupTestError("start chat", randomID)
return
}
log.Info().Interface("response", resp).Msg("Successfully resolved identifier")
if !actuallyStart {
return
}
portal := br.GetPortalByGUID(resp.GUID)
payload, err := encryptTestPayload(br.Config.HackyStartupTest.Key, map[string]any{
"segment_user_id": Segment.userID,
"user_id": br.user.MXID.String(),
"random_id": randomID,
})
if err != nil {
trackStartupTestError("encrypt payload", randomID)
log.Error().Err(err).Msg("Failed to encrypt ping payload")
return
}
metadata := map[string]any{
startupTestKey: payload,
startupTestIDKey: randomID,
}
sendResp, err := br.IM.SendMessage(portal.getTargetGUID("text message", "startup test", ""), br.Config.HackyStartupTest.Message, "", 0, nil, metadata)
if err != nil {
log.Error().Err(err).Msg("Failed to send message")
trackStartupTestError("send message", randomID)
return
}
br.pendingHackyTestGUID = sendResp.GUID
br.pendingHackyTestRandomID = randomID
log.Info().Str("msg_guid", sendResp.GUID).Msg("Successfully sent message")
Segment.Track(hackyTestSegmentEvent, map[string]any{
"event": "message sent",
"random_id": randomID,
"msg_guid": sendResp.GUID,
})
go func() {
time.Sleep(30 * time.Second)
if !br.hackyTestSuccess {
log.Info().Msg("Hacky test success flag not set, sending timeout event")
Segment.Track(hackyTestSegmentEvent, map[string]any{
"event": "timeout",
"random_id": randomID,
"msg_guid": sendResp.GUID,
})
}
}()
}
func (br *IMBridge) trackStartupTestPingStatus(msgStatus *imessage.SendMessageStatus) {
br.ZLog.Info().
Interface("status", msgStatus).
Str("random_id", br.pendingHackyTestRandomID).
Str("action", hackyTestLogAction).
Msg("Received message status for hacky test")
meta := map[string]any{
"msg_guid": msgStatus.GUID,
"random_id": br.pendingHackyTestRandomID,
}
switch msgStatus.Status {
case "delivered":
meta["event"] = "ping delivered"
case "sent":
meta["event"] = "ping sent"
case "failed":
meta["event"] = "ping failed"
meta["error"] = msgStatus.Message
default:
return
}
Segment.Track(hackyTestSegmentEvent, meta)
}
func (br *IMBridge) receiveStartupTestPing(msg *imessage.Message) {
unencryptedID, ok := msg.Metadata[startupTestIDKey].(string)
if !ok {
return
}
encryptedPayload, ok := msg.Metadata[startupTestKey].(map[string]any)
if !ok {
return
}
log := br.ZLog.With().
Str("action", hackyTestLogAction).
Str("random_id", unencryptedID).
Str("msg_guid", msg.GUID).
Str("chat_guid", msg.ChatGUID).
Str("sender_guid", msg.Sender.String()).
Logger()
log.Info().Msg("Received startup test ping")
payload, err := decryptTestPayload(br.Config.HackyStartupTest.Key, encryptedPayload)
if err != nil {
Segment.Track(hackyTestSegmentEvent, map[string]any{
"event": "fail",
"error": "ping decrypt",
"random_id": unencryptedID,
"msg_guid": msg.GUID,
})
return
}
userID, _ := payload["user_id"].(string)
segmentUserID, _ := payload["segment_user_id"].(string)
log = log.With().
Str("matrix_user_id", userID).
Str("segment_user_id", segmentUserID).
Logger()
log.Info().Msg("Decrypted startup test ping")
randomID, ok := payload["random_id"].(string)
if randomID != unencryptedID {
log.Warn().Str("encrypted_random_id", randomID).Msg("Mismatching random ID in encrypted payload")
Segment.TrackUser(hackyTestSegmentEvent, segmentUserID, map[string]any{
"event": "fail",
"error": "mismatching random ID",
"random_id": randomID,
"msg_guid": msg.GUID,
})
return
}
Segment.TrackUser(hackyTestSegmentEvent, segmentUserID, map[string]any{
"event": "ping received",
"random_id": unencryptedID,
"msg_guid": msg.GUID,
})
time.Sleep(2 * time.Second)
resp, err := br.IM.SendMessage(msg.ChatGUID, br.Config.HackyStartupTest.ResponseMessage, msg.GUID, 0, nil, map[string]any{
startupTestResponseKey: map[string]any{
"random_id": randomID,
},
})
if err != nil {
log.Error().Err(err).Msg("Failed to send pong")
Segment.TrackUser(hackyTestSegmentEvent, segmentUserID, map[string]any{
"event": "fail",
"error": "pong send",
"random_id": unencryptedID,
"msg_guid": msg.GUID,
})
return
}
log.Info().Str("pong_msg_guid", resp.GUID).Msg("Sent pong")
Segment.TrackUser(hackyTestSegmentEvent, segmentUserID, map[string]any{
"event": "pong sent",
"random_id": unencryptedID,
"msg_guid": msg.GUID,
"pong_guid": resp.GUID,
})
}
func (br *IMBridge) receiveStartupTestPong(resp map[string]any, msg *imessage.Message) {
randomID, _ := resp["random_id"].(string)
br.ZLog.Info().
Str("action", hackyTestLogAction).
Str("random_id", randomID).
Str("msg_guid", msg.GUID).
Str("reply_to_guid", msg.ReplyToGUID).
Msg("Startup test successful")
Segment.Track(hackyTestSegmentEvent, map[string]any{
"event": "success",
"random_id": randomID,
"pong_guid": msg.GUID,
"msg_guid": msg.ReplyToGUID,
})
if br.pendingHackyTestRandomID == randomID {
br.hackyTestSuccess = true
br.pendingHackyTestGUID = ""
br.pendingHackyTestRandomID = ""
} else {
br.ZLog.Info().
Str("pending_random_id", br.pendingHackyTestRandomID).
Str("pending_msg_guid", br.pendingHackyTestGUID).
Msg("Pending test ID isn't same as received ID")
}
}