From 17b42a369a9b720c1d420c1f244a0f690203d28a Mon Sep 17 00:00:00 2001 From: zhaoYangguang <1163765691@qq.com> Date: Thu, 26 Nov 2020 18:35:33 +0800 Subject: [PATCH] update crypto --- commands.go | 2 +- config/registration.go | 3 +- crypto.go | 39 ++++++++++++++++++- .../2020-10-28-crypto-store-cross-signing.go | 13 +++++++ database/upgrades/upgrades.go | 2 +- go.mod | 4 +- go.sum | 6 +++ 7 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 database/upgrades/2020-10-28-crypto-store-cross-signing.go diff --git a/commands.go b/commands.go index 88f0589..72e7b93 100644 --- a/commands.go +++ b/commands.go @@ -443,7 +443,7 @@ const cmdDisconnectHelp = `disconnect - Disconnect from WhatsApp (without loggin // ce.Reply("Successfully disconnected. Use the `reconnect` command to reconnect.") //} -const cmdPingHelp = `ping - Check your connection to WhatsApp.` +const cmdPingHelp = `ping - Check your connection to Skype.` func (handler *CommandHandler) CommandPing(ce *CommandEvent) { if ce.User.Session == nil || ce.User.Session.SkypeToken == "" { diff --git a/config/registration.go b/config/registration.go index 00922b7..fafd59e 100644 --- a/config/registration.go +++ b/config/registration.go @@ -58,7 +58,8 @@ func (config *Config) GetRegistration() (*appservice.Registration, error) { func (config *Config) copyToRegistration(registration *appservice.Registration) error { registration.ID = config.AppService.ID registration.URL = config.AppService.Address - registration.RateLimited = false + falseVal := false + registration.RateLimited = &falseVal registration.SenderLocalpart = config.AppService.Bot.Username userIDRegex, err := regexp.Compile(fmt.Sprintf("^@%s:%s$", diff --git a/crypto.go b/crypto.go index 194872a..709325e 100644 --- a/crypto.go +++ b/crypto.go @@ -82,7 +82,6 @@ func (helper *CryptoHelper) Init() error { helper.mach = crypto.NewOlmMachine(helper.client, logger, helper.store, stateStore) helper.mach.AllowKeyShare = helper.allowKeyShare - helper.client.Logger = logger.int.Sub("Bot") helper.client.Syncer = &cryptoSyncer{helper.mach} helper.client.Store = &cryptoClientStore{helper.store} @@ -114,6 +113,44 @@ func (helper *CryptoHelper) allowKeyShare(device *crypto.DeviceIdentity, info ev } func (helper *CryptoHelper) loginBot() (*mautrix.Client, error) { + deviceID := helper.store.FindDeviceID() + if len(deviceID) > 0 { + helper.log.Debugln("Found existing device ID for bot in database:", deviceID) + } + client, err := mautrix.NewClient(helper.bridge.AS.HomeserverURL, "", "") + if err != nil { + return nil, fmt.Errorf("failed to initialize client: %w", err) + } + client.Logger = helper.baseLog.Sub("Bot") + flows, err := client.GetLoginFlows() + if err != nil { + return nil, fmt.Errorf("failed to get supported login flows: %w", err) + } + if !flows.HasFlow(mautrix.AuthTypeAppservice) { + // TODO after synapse 1.22, turn this into an error + helper.log.Warnln("Encryption enabled in config, but homeserver does not advertise appservice login") + //return nil, fmt.Errorf("homeserver does not support appservice login") + } + // We set the API token to the AS token here to authenticate the appservice login + // It'll get overridden after the login + client.AccessToken = helper.bridge.AS.Registration.AppToken + resp, err := client.Login(&mautrix.ReqLogin{ + Type: mautrix.AuthTypeAppservice, + Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: string(helper.bridge.AS.BotMXID())}, + DeviceID: deviceID, + InitialDeviceDisplayName: "WhatsApp Bridge", + StoreCredentials: true, + }) + if err != nil { + return nil, fmt.Errorf("failed to log in as bridge bot: %w", err) + } + if len(deviceID) == 0 { + helper.store.DeviceID = resp.DeviceID + } + return client, nil +} + +func (helper *CryptoHelper) loginBotOld() (*mautrix.Client, error) { deviceID := helper.store.FindDeviceID() if len(deviceID) > 0 { helper.log.Debugln("Found existing device ID for bot in database:", deviceID) diff --git a/database/upgrades/2020-10-28-crypto-store-cross-signing.go b/database/upgrades/2020-10-28-crypto-store-cross-signing.go new file mode 100644 index 0000000..2fb89f0 --- /dev/null +++ b/database/upgrades/2020-10-28-crypto-store-cross-signing.go @@ -0,0 +1,13 @@ +package upgrades + +import ( + "database/sql" + + "maunium.net/go/mautrix/crypto/sql_store_upgrade" +) + +func init() { + upgrades[18] = upgrade{"Add cross-signing keys to crypto store", func(tx *sql.Tx, c context) error { + return sql_store_upgrade.Upgrades[3](tx, c.dialect.String()) + }} +} diff --git a/database/upgrades/upgrades.go b/database/upgrades/upgrades.go index f020d94..a870745 100644 --- a/database/upgrades/upgrades.go +++ b/database/upgrades/upgrades.go @@ -39,7 +39,7 @@ type upgrade struct { fn upgradeFunc } -const NumberOfUpgrades = 18 +const NumberOfUpgrades = 19 var upgrades [NumberOfUpgrades]upgrade diff --git a/go.mod b/go.mod index 6d043b9..b5e8f40 100644 --- a/go.mod +++ b/go.mod @@ -16,11 +16,11 @@ require ( gopkg.in/yaml.v2 v2.3.0 maunium.net/go/mauflag v1.0.0 maunium.net/go/maulogger/v2 v2.1.1 - maunium.net/go/mautrix v0.7.2 + maunium.net/go/mautrix v0.8.0-rc.4 ) replace github.com/Rhymen/go-whatsapp => github.com/tulir/go-whatsapp v0.2.8 -replace maunium.net/go/mautrix => github.com/pidongqianqian/mautrix-go v0.7.3-0.20201106154702-3c2230569f1d +replace maunium.net/go/mautrix => github.com/pidongqianqian/mautrix-go v0.8.0-rc.4.0.20201126070406-7b13ac473bcc replace github.com/kelaresg/go-skypeapi => /Users/yangguang/matrix/go-skypeapi-kelare diff --git a/go.sum b/go.sum index 0c69ab8..c099393 100644 --- a/go.sum +++ b/go.sum @@ -208,6 +208,10 @@ github.com/pidongqianqian/mautrix-go v0.7.3-0.20201106123139-e1c6c37e09d6 h1:RKx github.com/pidongqianqian/mautrix-go v0.7.3-0.20201106123139-e1c6c37e09d6/go.mod h1:TtVePxoEaw6+RZDKVajw66Yaj1lqLjH8l4FF3krsqWY= github.com/pidongqianqian/mautrix-go v0.7.3-0.20201106154702-3c2230569f1d h1:cw6XWBIvj8DaRO/rWzndd4q5Yzbei6Sgj/UUPHuCWv4= github.com/pidongqianqian/mautrix-go v0.7.3-0.20201106154702-3c2230569f1d/go.mod h1:Va/74MijqaS0DQ3aUqxmFO54/PMfr1LVsCOcGRHbYmo= +github.com/pidongqianqian/mautrix-go v0.7.3-0.20201126040927-dbde233d88f6 h1:A2W0H1dcKLw1f4z5hA5BET0mAn3gyhwyYGBhYN6Dz9g= +github.com/pidongqianqian/mautrix-go v0.7.3-0.20201126040927-dbde233d88f6/go.mod h1:TtVePxoEaw6+RZDKVajw66Yaj1lqLjH8l4FF3krsqWY= +github.com/pidongqianqian/mautrix-go v0.8.0-rc.4.0.20201126070406-7b13ac473bcc h1:wXc6kSKzzYrLgqJwaCgJvKZc4Qfq85/3TivSItUFuNk= +github.com/pidongqianqian/mautrix-go v0.8.0-rc.4.0.20201126070406-7b13ac473bcc/go.mod h1:TtVePxoEaw6+RZDKVajw66Yaj1lqLjH8l4FF3krsqWY= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -461,6 +465,8 @@ maunium.net/go/mauflag v1.0.0 h1:YiaRc0tEI3toYtJMRIfjP+jklH45uDHtT80nUamyD4M= maunium.net/go/mauflag v1.0.0/go.mod h1:nLivPOpTpHnpzEh8jEdSL9UqO9+/KBJFmNRlwKfkPeA= maunium.net/go/maulogger/v2 v2.1.1 h1:NAZNc6XUFJzgzfewCzVoGkxNAsblLCSSEdtDuIjP0XA= maunium.net/go/maulogger/v2 v2.1.1/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A= +maunium.net/go/mautrix v0.7.13 h1:qfnvLxvQafvLgHbdZF/+9qs9gyArYf8fUnzfQbjgQaU= +maunium.net/go/mautrix v0.7.13/go.mod h1:Jn0ijwXwMFvJFIN9IljirIVKpZQbZP/Dk7pdX2qDmXk= maunium.net/go/mautrix-whatsapp v0.1.4 h1:qlkb3eXcKm1QE6AjrAl9aKxokHlwj7BNr+aUQFXFmWE= maunium.net/go/mautrix-whatsapp v0.1.4/go.mod h1:yC5pjdUQckJzuMX5rrg2237kz/7zP+7qO1uckOODe5M= rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=