|
@@ -21,6 +21,7 @@ import (
|
|
"crypto/sha512"
|
|
"crypto/sha512"
|
|
"encoding/hex"
|
|
"encoding/hex"
|
|
"errors"
|
|
"errors"
|
|
|
|
+ "fmt"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
"go.mau.fi/whatsmeow/types"
|
|
"go.mau.fi/whatsmeow/types"
|
|
@@ -64,10 +65,15 @@ func (puppet *Puppet) SwitchCustomMXID(accessToken string, mxid id.UserID) error
|
|
}
|
|
}
|
|
|
|
|
|
func (puppet *Puppet) loginWithSharedSecret(mxid id.UserID) (string, error) {
|
|
func (puppet *Puppet) loginWithSharedSecret(mxid id.UserID) (string, error) {
|
|
|
|
+ _, homeserver, _ := mxid.Parse()
|
|
puppet.log.Debugfln("Logging into %s with shared secret", mxid)
|
|
puppet.log.Debugfln("Logging into %s with shared secret", mxid)
|
|
- mac := hmac.New(sha512.New, []byte(puppet.bridge.Config.Bridge.LoginSharedSecret))
|
|
|
|
|
|
+ mac := hmac.New(sha512.New, []byte(puppet.bridge.Config.Bridge.LoginSharedSecretMap[homeserver]))
|
|
mac.Write([]byte(mxid))
|
|
mac.Write([]byte(mxid))
|
|
- resp, err := puppet.bridge.AS.BotClient().Login(&mautrix.ReqLogin{
|
|
|
|
|
|
+ client, err := puppet.bridge.newDoublePuppetClient(mxid, "")
|
|
|
|
+ if err != nil {
|
|
|
|
+ return "", fmt.Errorf("failed to create mautrix client to log in: %v", err)
|
|
|
|
+ }
|
|
|
|
+ resp, err := client.Login(&mautrix.ReqLogin{
|
|
Type: mautrix.AuthTypePassword,
|
|
Type: mautrix.AuthTypePassword,
|
|
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: string(mxid)},
|
|
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: string(mxid)},
|
|
Password: hex.EncodeToString(mac.Sum(nil)),
|
|
Password: hex.EncodeToString(mac.Sum(nil)),
|
|
@@ -80,26 +86,44 @@ func (puppet *Puppet) loginWithSharedSecret(mxid id.UserID) (string, error) {
|
|
return resp.AccessToken, nil
|
|
return resp.AccessToken, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (puppet *Puppet) newCustomIntent() (*appservice.IntentAPI, error) {
|
|
|
|
- if len(puppet.CustomMXID) == 0 {
|
|
|
|
- return nil, ErrNoCustomMXID
|
|
|
|
- }
|
|
|
|
- _, homeserver, err := puppet.CustomMXID.Parse()
|
|
|
|
|
|
+func (bridge *Bridge) newDoublePuppetClient(mxid id.UserID, accessToken string) (*mautrix.Client, error) {
|
|
|
|
+ _, homeserver, err := mxid.Parse()
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
- homeserverUrl, found := puppet.bridge.Config.Bridge.DoublePuppetServerMap[homeserver]
|
|
|
|
|
|
+ homeserverURL, found := bridge.Config.Bridge.DoublePuppetServerMap[homeserver]
|
|
if !found {
|
|
if !found {
|
|
- puppet.log.Debugfln("Homeserver not found in double puppet server map. Using local homeserver")
|
|
|
|
- homeserverUrl = puppet.bridge.AS.HomeserverURL
|
|
|
|
|
|
+ if homeserver == bridge.AS.HomeserverDomain {
|
|
|
|
+ homeserverURL = bridge.AS.HomeserverURL
|
|
|
|
+ } else if bridge.Config.Bridge.DoublePuppetAllowDiscovery {
|
|
|
|
+ resp, err := mautrix.DiscoverClientAPI(homeserver)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, fmt.Errorf("failed to find homeserver URL for %s: %v", homeserver, err)
|
|
|
|
+ }
|
|
|
|
+ homeserverURL = resp.Homeserver.BaseURL
|
|
|
|
+ bridge.Log.Debugfln("Discovered URL %s for %s to enable double puppeting for %s", homeserverURL, homeserver, mxid)
|
|
|
|
+ } else {
|
|
|
|
+ return nil, fmt.Errorf("double puppeting from %s is not allowed", homeserver)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ client, err := mautrix.NewClient(homeserverURL, mxid, accessToken)
|
|
|
|
+ if err != nil {
|
|
|
|
+ return nil, err
|
|
|
|
+ }
|
|
|
|
+ client.Logger = bridge.AS.Log.Sub(mxid.String())
|
|
|
|
+ client.Client = bridge.AS.HTTPClient
|
|
|
|
+ client.DefaultHTTPRetries = bridge.AS.DefaultHTTPRetries
|
|
|
|
+ return client, nil
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (puppet *Puppet) newCustomIntent() (*appservice.IntentAPI, error) {
|
|
|
|
+ if len(puppet.CustomMXID) == 0 {
|
|
|
|
+ return nil, ErrNoCustomMXID
|
|
}
|
|
}
|
|
- client, err := mautrix.NewClient(homeserverUrl, puppet.CustomMXID, puppet.AccessToken)
|
|
|
|
|
|
+ client, err := puppet.bridge.newDoublePuppetClient(puppet.CustomMXID, puppet.AccessToken)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
- client.Logger = puppet.bridge.AS.Log.Sub(string(puppet.CustomMXID))
|
|
|
|
- client.Client = puppet.bridge.AS.HTTPClient
|
|
|
|
- client.DefaultHTTPRetries = puppet.bridge.AS.DefaultHTTPRetries
|
|
|
|
client.Syncer = puppet
|
|
client.Syncer = puppet
|
|
client.Store = puppet
|
|
client.Store = puppet
|
|
|
|
|
|
@@ -264,7 +288,7 @@ func (puppet *Puppet) handleTypingEvent(portal *Portal, evt *event.Event) {
|
|
}
|
|
}
|
|
|
|
|
|
func (puppet *Puppet) tryRelogin(cause error, action string) bool {
|
|
func (puppet *Puppet) tryRelogin(cause error, action string) bool {
|
|
- if !puppet.bridge.Config.CanDoublePuppet(puppet.CustomMXID) {
|
|
|
|
|
|
+ if !puppet.bridge.Config.CanAutoDoublePuppet(puppet.CustomMXID) {
|
|
return false
|
|
return false
|
|
}
|
|
}
|
|
puppet.log.Debugfln("Trying to relogin after '%v' while %s", cause, action)
|
|
puppet.log.Debugfln("Trying to relogin after '%v' while %s", cause, action)
|