-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathcustompuppet.go
156 lines (142 loc) · 4.61 KB
/
custompuppet.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
// mautrix-imessage - A Matrix-iMessage puppeting bridge.
// Copyright (C) 2022 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"
"crypto/sha512"
"encoding/hex"
"errors"
"fmt"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/appservice"
"maunium.net/go/mautrix/bridge"
"maunium.net/go/mautrix/id"
)
var (
ErrMismatchingMXID = errors.New("whoami result does not match custom mxid")
)
var _ bridge.DoublePuppet = (*User)(nil)
func (user *User) SwitchCustomMXID(accessToken string, mxid id.UserID) error {
if mxid != user.MXID {
return errors.New("mismatching mxid")
}
user.AccessToken = accessToken
return user.startCustomMXID()
}
func (user *User) CustomIntent() *appservice.IntentAPI {
return user.DoublePuppetIntent
}
func (user *User) initDoublePuppet() {
var err error
if len(user.AccessToken) > 0 {
err = user.startCustomMXID()
if errors.Is(err, mautrix.MUnknownToken) && len(user.bridge.Config.Bridge.LoginSharedSecret) > 0 {
user.log.Debugln("Unknown token while starting custom puppet, trying to relogin with shared secret")
err = user.loginWithSharedSecret()
if err == nil {
err = user.startCustomMXID()
}
}
} else {
err = user.loginWithSharedSecret()
if err == nil {
err = user.startCustomMXID()
}
}
if err != nil {
user.log.Warnln("Failed to switch to auto-logined custom puppet:", err)
} else {
user.log.Infoln("Successfully automatically enabled custom puppet")
}
}
func (user *User) loginWithSharedSecret() error {
user.log.Debugfln("Logging in with shared secret")
loginSecret := user.bridge.Config.Bridge.LoginSharedSecret
client, err := user.bridge.AS.NewExternalMautrixClient(user.MXID, "", user.bridge.Config.Bridge.DoublePuppetServerURL)
if err != nil {
return err
}
req := mautrix.ReqLogin{
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: string(user.MXID)},
DeviceID: id.DeviceID(user.bridge.Config.IMessage.BridgeName()),
InitialDeviceDisplayName: user.bridge.Config.IMessage.BridgeName(),
}
if loginSecret == "appservice" {
client.AccessToken = user.bridge.AS.Registration.AppToken
req.Type = mautrix.AuthTypeAppservice
} else {
mac := hmac.New(sha512.New, []byte(loginSecret))
mac.Write([]byte(user.MXID))
req.Password = hex.EncodeToString(mac.Sum(nil))
req.Type = mautrix.AuthTypePassword
}
resp, err := client.Login(&req)
if err != nil {
return fmt.Errorf("failed to log in with shared secret: %w", err)
}
user.AccessToken = resp.AccessToken
user.Update()
return nil
}
func (user *User) newDoublePuppetIntent() (*appservice.IntentAPI, error) {
client, err := user.bridge.AS.NewExternalMautrixClient(user.MXID, user.AccessToken, user.bridge.Config.Bridge.DoublePuppetServerURL)
if err != nil {
return nil, err
}
ia := user.bridge.AS.NewIntentAPI("custom")
ia.Client = client
ia.Localpart, _, _ = user.MXID.Parse()
ia.UserID = user.MXID
ia.IsCustomPuppet = true
return ia, nil
}
func (user *User) clearCustomMXID() {
user.AccessToken = ""
user.NextBatch = ""
user.DoublePuppetIntent = nil
}
func (user *User) startCustomMXID() error {
if len(user.AccessToken) == 0 {
user.clearCustomMXID()
return nil
}
intent, err := user.newDoublePuppetIntent()
if err != nil {
user.clearCustomMXID()
return fmt.Errorf("failed to create double puppet intent: %w", err)
}
resp, err := intent.Whoami()
if err != nil {
user.clearCustomMXID()
return fmt.Errorf("failed to ensure double puppet token is valid: %w", err)
}
if resp.UserID != user.MXID {
user.clearCustomMXID()
return ErrMismatchingMXID
}
user.DoublePuppetIntent = intent
return nil
}
func (user *User) tryRelogin(cause error, action string) bool {
user.log.Debugfln("Trying to relogin after '%v' while %s", cause, action)
err := user.loginWithSharedSecret()
if err != nil {
user.log.Errorfln("Failed to relogin after '%v' while %s: %v", cause, action, err)
return false
}
user.log.Infofln("Successfully relogined after '%v' while %s", cause, action)
return true
}