forked from mautrix/signal
-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
497 lines (435 loc) · 15 KB
/
main.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// mautrix-signal - A Matrix-signal puppeting bridge.
// Copyright (C) 2023 Scott Weber
//
// 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 (
"context"
_ "embed"
"fmt"
"os"
"sync"
"time"
"github.com/google/uuid"
"github.com/rs/zerolog"
"go.mau.fi/util/configupgrade"
"go.mau.fi/util/dbutil"
"github.com/element-hq/mautrix-go"
"github.com/element-hq/mautrix-go/bridge"
"github.com/element-hq/mautrix-go/bridge/bridgeconfig"
"github.com/element-hq/mautrix-go/bridge/commands"
"github.com/element-hq/mautrix-go/event"
"github.com/element-hq/mautrix-go/format"
"github.com/element-hq/mautrix-go/id"
"github.com/element-hq/mautrix-signal/config"
"github.com/element-hq/mautrix-signal/database"
"github.com/element-hq/mautrix-signal/msgconv/matrixfmt"
"github.com/element-hq/mautrix-signal/msgconv/signalfmt"
"github.com/element-hq/mautrix-signal/pkg/signalmeow"
"github.com/element-hq/mautrix-signal/pkg/signalmeow/store"
)
const activeUserMetricsIntervalSec = 60
//go:embed example-config.yaml
var ExampleConfig string
// Information to find out exactly which commit the bridge was built from.
// These are filled at build time with the -X linker flag.
var (
Tag = "unknown"
Commit = "unknown"
BuildTime = "unknown"
)
type SignalBridge struct {
bridge.Bridge
Config *config.Config
DB *database.Database
Metrics *MetricsHandler
MeowStore *store.Container
provisioning *ProvisioningAPI
puppetActivity *PuppetActivity
activePuppetMetricLoopDone chan bool
activePuppetMetricRequest chan bool
lastBlockingNotification int64
usersByMXID map[id.UserID]*User
usersBySignalID map[uuid.UUID]*User
usersLock sync.Mutex
managementRooms map[id.RoomID]*User
managementRoomsLock sync.Mutex
portalsByMXID map[id.RoomID]*Portal
portalsByID map[database.PortalKey]*Portal
portalsLock sync.Mutex
puppets map[uuid.UUID]*Puppet
puppetsByCustomMXID map[id.UserID]*Puppet
puppetsLock sync.Mutex
disappearingMessagesManager *DisappearingMessagesManager
}
var _ bridge.ChildOverride = (*SignalBridge)(nil)
func (br *SignalBridge) GetExampleConfig() string {
return ExampleConfig
}
func (br *SignalBridge) GetConfigPtr() interface{} {
br.Config = &config.Config{
BaseConfig: &br.Bridge.Config,
}
br.Config.BaseConfig.Bridge = &br.Config.Bridge
return br.Config
}
func (br *SignalBridge) Init() {
br.CommandProcessor = commands.NewProcessor(&br.Bridge)
br.RegisterCommands()
signalmeow.SetLogger(br.ZLog.With().Str("component", "signalmeow").Logger())
br.DB = database.New(br.Bridge.DB)
br.MeowStore = store.NewStore(br.Bridge.DB, dbutil.ZeroLogger(br.ZLog.With().Str("db_section", "signalmeow").Logger()))
ss := br.Config.Bridge.Provisioning.SharedSecret
if len(ss) > 0 && ss != "disable" {
br.provisioning = &ProvisioningAPI{bridge: br, log: br.ZLog.With().Str("component", "provisioning").Logger()}
}
br.disappearingMessagesManager = &DisappearingMessagesManager{
DB: br.DB,
Log: br.ZLog.With().Str("component", "disappearing messages").Logger(),
Bridge: br,
}
br.Metrics = NewMetricsHandler(br.Config.Metrics.Listen, br.ZLog.With().Str("component", "metrics").Logger(), br.DB, br.puppetActivity)
br.MatrixHandler.TrackEventDuration = br.Metrics.TrackMatrixEvent
signalFormatParams = &signalfmt.FormatParams{
GetUserInfo: func(ctx context.Context, u uuid.UUID) signalfmt.UserInfo {
puppet := br.GetPuppetBySignalID(u)
if puppet == nil {
return signalfmt.UserInfo{}
}
user := br.GetUserBySignalID(u)
if user != nil {
return signalfmt.UserInfo{
MXID: user.MXID,
Name: puppet.Name,
}
}
return signalfmt.UserInfo{
MXID: puppet.MXID,
Name: puppet.Name,
}
},
}
matrixFormatParams = &matrixfmt.HTMLParser{
GetUUIDFromMXID: func(ctx context.Context, userID id.UserID) uuid.UUID {
parsed, ok := br.ParsePuppetMXID(userID)
if ok {
return parsed
}
user := br.GetUserByMXIDIfExists(userID)
if user != nil && user.SignalID != uuid.Nil {
return user.SignalID
}
return uuid.Nil
},
}
}
func (br *SignalBridge) logLostPortals(ctx context.Context) {
exists, err := br.DB.TableExists(ctx, "lost_portals")
if err != nil {
br.ZLog.Err(err).Msg("Failed to check if lost_portals table exists")
} else if !exists {
return
}
lostPortals, err := br.DB.LostPortal.GetAll(ctx)
if err != nil {
br.ZLog.Err(err).Msg("Failed to get lost portals")
return
} else if len(lostPortals) == 0 {
return
}
lostCountByReceiver := make(map[string]int)
for _, lost := range lostPortals {
lostCountByReceiver[lost.Receiver]++
}
br.ZLog.Warn().
Any("count_by_receiver", lostCountByReceiver).
Msg("Some portals were discarded due to the receiver not being logged into the bridge anymore. " +
"Use `!signal cleanup-lost-portals` to remove them from the database. " +
"Alternatively, you can re-insert the data into the portal table with the appropriate receiver column to restore the portals.")
}
func (br *SignalBridge) Start() {
go br.logLostPortals(context.TODO())
err := br.MeowStore.Upgrade(context.TODO())
if err != nil {
br.ZLog.Fatal().Err(err).Msg("Failed to upgrade signalmeow database")
os.Exit(15)
}
if br.provisioning != nil {
br.ZLog.Debug().Msg("Initializing provisioning API")
br.provisioning.Init()
}
go br.StartUsers()
br.updateActivePuppetMetricNow()
go br.loopActivePuppetMetric()
if br.Config.Metrics.Enabled {
go br.Metrics.Start()
}
go br.disappearingMessagesManager.StartDisappearingLoop(context.TODO())
}
func (br *SignalBridge) Stop() {
br.Metrics.Stop()
for _, user := range br.usersByMXID {
br.ZLog.Debug().Stringer("user_id", user.MXID).Msg("Disconnecting user")
err := user.Disconnect()
if err != nil {
br.ZLog.Debug().Err(err).
Str("action", "stop").
Stringer("user_id", user.MXID).
Msg("Error on disconnect")
}
}
close(br.activePuppetMetricLoopDone)
}
func (br *SignalBridge) GetIPortal(mxid id.RoomID) bridge.Portal {
p := br.GetPortalByMXID(mxid)
if p == nil {
return nil
}
return p
}
func (br *SignalBridge) GetIUser(mxid id.UserID, create bool) bridge.User {
p := br.GetUserByMXID(mxid)
if p == nil {
return nil
}
return p
}
func (br *SignalBridge) IsGhost(mxid id.UserID) bool {
_, isGhost := br.ParsePuppetMXID(mxid)
return isGhost
}
func (br *SignalBridge) GetIGhost(mxid id.UserID) bridge.Ghost {
p := br.GetPuppetByMXID(mxid)
if p == nil {
return nil
}
return p
}
func (br *SignalBridge) CreatePrivatePortal(roomID id.RoomID, brInviter bridge.User, brGhost bridge.Ghost) {
inviter := brInviter.(*User)
puppet := brGhost.(*Puppet)
log := br.ZLog.With().
Str("action", "create private portal").
Stringer("target_room_id", roomID).
Stringer("inviter_mxid", brInviter.GetMXID()).
Stringer("invitee_uuid", puppet.SignalID).
Logger()
log.Debug().Msg("Creating private chat portal")
key := database.NewPortalKey(puppet.SignalID.String(), inviter.SignalID)
portal := br.GetPortalByChatID(key)
ctx := log.WithContext(context.TODO())
if len(portal.MXID) == 0 {
br.createPrivatePortalFromInvite(ctx, roomID, inviter, puppet, portal)
return
}
log.Debug().
Stringer("existing_room_id", portal.MXID).
Msg("Existing private chat portal found, trying to invite user")
ok := portal.ensureUserInvited(ctx, inviter)
if !ok {
log.Warn().Msg("Failed to invite user to existing private chat portal. Redirecting portal to new room")
br.createPrivatePortalFromInvite(ctx, roomID, inviter, puppet, portal)
return
}
intent := puppet.DefaultIntent()
errorMessage := fmt.Sprintf("You already have a private chat portal with me at [%[1]s](https://matrix.to/#/%[1]s)", portal.MXID)
errorContent := format.RenderMarkdown(errorMessage, true, false)
_, _ = intent.SendMessageEvent(ctx, roomID, event.EventMessage, errorContent)
log.Debug().Msg("Leaving ghost from private chat room after accepting invite because we already have a chat with the user")
_, _ = intent.LeaveRoom(ctx, roomID)
}
func (br *SignalBridge) createPrivatePortalFromInvite(ctx context.Context, roomID id.RoomID, inviter *User, puppet *Puppet, portal *Portal) {
log := zerolog.Ctx(ctx)
log.Debug().Msg("Creating private portal from invite")
// Check if room is already encrypted
var existingEncryption event.EncryptionEventContent
var encryptionEnabled bool
err := portal.MainIntent().StateEvent(ctx, roomID, event.StateEncryption, "", &existingEncryption)
if err != nil {
log.Err(err).Msg("Failed to check if encryption is enabled in private chat room")
} else {
encryptionEnabled = existingEncryption.Algorithm == id.AlgorithmMegolmV1
}
portal.MXID = roomID
br.portalsLock.Lock()
br.portalsByMXID[portal.MXID] = portal
br.portalsLock.Unlock()
intent := puppet.DefaultIntent()
if br.Config.Bridge.Encryption.Default || encryptionEnabled {
log.Debug().Msg("Adding bridge bot to new private chat portal as encryption is enabled")
_, err = intent.InviteUser(ctx, roomID, &mautrix.ReqInviteUser{UserID: br.Bot.UserID})
if err != nil {
log.Err(err).Msg("Failed to invite bridge bot to enable e2be")
}
err = br.Bot.EnsureJoined(ctx, roomID)
if err != nil {
log.Err(err).Msg("Failed to join as bridge bot to enable e2be")
}
if !encryptionEnabled {
_, err = intent.SendStateEvent(ctx, roomID, event.StateEncryption, "", portal.getEncryptionEventContent())
if err != nil {
log.Err(err).Msg("Failed to enable e2be")
}
}
br.AS.StateStore.SetMembership(ctx, roomID, inviter.MXID, event.MembershipJoin)
br.AS.StateStore.SetMembership(ctx, roomID, puppet.MXID, event.MembershipJoin)
br.AS.StateStore.SetMembership(ctx, roomID, br.Bot.UserID, event.MembershipJoin)
portal.Encrypted = true
}
portal.UpdateDMInfo(ctx, true)
_, _ = intent.SendNotice(ctx, roomID, "Private chat portal created")
log.Info().Msg("Created private chat portal after invite")
}
func (br *SignalBridge) UpdateActivePuppetMetric() {
defer func() {
if recover() != nil {
br.ZLog.Warn().Msg("Attempted to update active puppet metrics after bridge has stopped")
}
}()
br.activePuppetMetricRequest <- true
}
func (br *SignalBridge) updateActivePuppetMetricNow() {
br.ZLog.Debug().Msg("Updating active puppet count")
br.updateActivePuppetMetric(br.ZLog)
}
func (br *SignalBridge) updateActivePuppetMetric(log *zerolog.Logger) {
activeUsers, err := br.DB.Puppet.GetRecentlyActiveCount(
context.TODO(),
br.Config.Bridge.Limits.MinPuppetActivityDays,
br.Config.Bridge.Limits.PuppetInactivityDays,
)
if err != nil {
log.Warn().Msg("Failed to scan number of active puppets")
return
}
if br.Config.Bridge.Limits.BlockOnLimitReached {
blocked := br.Config.Bridge.Limits.MaxPuppetLimit < activeUsers
if br.puppetActivity.isBlocked != blocked {
br.puppetActivity.isBlocked = blocked
br.notifyBridgeBlocked(blocked)
}
}
log.Debug().Uint("active_puppet_count", activeUsers).Send()
br.puppetActivity.currentUserCount = activeUsers
if br.Config.Metrics.Enabled {
br.Metrics.updatePuppetActivity()
}
}
func (br *SignalBridge) loopActivePuppetMetric() {
log := br.ZLog.With().Str("module", "mau.active_puppet_metric").Logger()
ticker := time.Tick(activeUserMetricsIntervalSec * time.Second)
for {
select {
case <-ticker:
log.Info().Msg("Executing periodic active puppet metric check")
br.updateActivePuppetMetric(&log)
case <-br.activePuppetMetricRequest:
br.updateActivePuppetMetricNow()
case <-br.activePuppetMetricLoopDone:
close(br.activePuppetMetricRequest)
return
}
}
}
func (br *SignalBridge) notifyBridgeBlocked(isBlocked bool) {
var msg string
if isBlocked {
msg = br.Config.Bridge.Limits.BlockBeginsNotification
nextNotification := br.lastBlockingNotification + int64(br.Config.Bridge.Limits.BlockNotificationIntervalSeconds)
// We're only checking if the block is active, since the unblock notification will not be resent and we want it ASAP
if now := time.Now().Unix(); nextNotification > now {
return
} else {
br.lastBlockingNotification = now
}
} else {
msg = br.Config.Bridge.Limits.BlockEndsNotification
}
admins := make([]id.UserID, 0, len(br.Config.Bridge.Permissions))
for key, permissionLevel := range br.Config.Bridge.Permissions {
if permissionLevel == bridgeconfig.PermissionLevelAdmin {
// Only explicit MXIDs are notified, not wildcards or domains
if _, _, err := id.UserID(key).ParseAndValidate(); err == nil {
admins = append(admins, id.UserID(key))
}
}
}
if len(admins) == 0 {
br.ZLog.Debug().Msg("No bridge admins to notify about the bridge being blocked")
return
}
allAdmins := string(admins[0])
for _, adminMXID := range admins[1:] {
allAdmins += "," + string(adminMXID)
}
br.ZLog.Debug().Str("admins", allAdmins).Msg("Notifying bridge admins about bridge being blocked")
ctx := br.ZLog.WithContext(context.TODO())
for _, adminMXID := range admins {
admin := br.GetUserByMXID(adminMXID)
if admin == nil {
continue
}
roomID := admin.GetManagementRoomID()
if roomID == "" {
resp, err := br.Bot.CreateRoom(ctx, &mautrix.ReqCreateRoom{
Name: "Signal Bridge notice room",
IsDirect: true,
Invite: []id.UserID{adminMXID},
})
if err != nil {
br.ZLog.Warn().Err(err).Msg("Failed to create notice room")
continue
}
roomID = resp.RoomID
admin.SetManagementRoom(roomID)
}
// \u26a0 is a warning sign
br.Bot.SendNotice(ctx, roomID, "\u26a0 "+msg)
}
}
func main() {
br := &SignalBridge{
usersByMXID: make(map[id.UserID]*User),
usersBySignalID: make(map[uuid.UUID]*User),
managementRooms: make(map[id.RoomID]*User),
portalsByMXID: make(map[id.RoomID]*Portal),
portalsByID: make(map[database.PortalKey]*Portal),
puppets: make(map[uuid.UUID]*Puppet),
puppetsByCustomMXID: make(map[id.UserID]*Puppet),
puppetActivity: &PuppetActivity{
currentUserCount: 0,
isBlocked: false,
},
activePuppetMetricLoopDone: make(chan bool),
activePuppetMetricRequest: make(chan bool),
}
br.Bridge = bridge.Bridge{
Name: "mautrix-signal",
URL: "https://github.com/element-hq/mautrix-signal",
Description: "A Matrix-Signal puppeting bridge.",
Version: "0.6.1-mod-2",
ProtocolName: "Signal",
BeeperServiceName: "signal",
BeeperNetworkName: "signal",
CryptoPickleKey: "mautrix.bridge.e2ee",
ConfigUpgrader: &configupgrade.StructUpgrader{
SimpleUpgrader: configupgrade.SimpleUpgrader(config.DoUpgrade),
Blocks: config.SpacedBlocks,
Base: ExampleConfig,
},
Child: br,
}
br.InitVersion(Tag, Commit, BuildTime)
br.Main()
}