From 3772861e17ad956540e109258d29be15cc8bd294 Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 28 Jul 2021 10:57:39 +0800 Subject: [PATCH] Move `AccountSecrets` from package `context` to `components` --- .../kotlin/network/QQAndroidClient.kt | 2 +- .../components/AccountSecretsManager.kt | 92 ++++++++++++++-- .../kotlin/network/components/SsoProcessor.kt | 1 - .../kotlin/network/context/AccountSecrets.kt | 104 ------------------ .../framework/components/TestSsoProcessor.kt | 2 +- .../kotlin/network/framework/sessionUtils.kt | 4 +- 6 files changed, 89 insertions(+), 116 deletions(-) delete mode 100644 mirai-core/src/commonMain/kotlin/network/context/AccountSecrets.kt diff --git a/mirai-core/src/commonMain/kotlin/network/QQAndroidClient.kt b/mirai-core/src/commonMain/kotlin/network/QQAndroidClient.kt index ffd66d011..574eea322 100644 --- a/mirai-core/src/commonMain/kotlin/network/QQAndroidClient.kt +++ b/mirai-core/src/commonMain/kotlin/network/QQAndroidClient.kt @@ -19,8 +19,8 @@ import kotlinx.io.core.toByteArray import net.mamoe.mirai.data.OnlineStatus import net.mamoe.mirai.internal.BotAccount import net.mamoe.mirai.internal.QQAndroidBot +import net.mamoe.mirai.internal.network.components.AccountSecrets import net.mamoe.mirai.internal.network.components.SsoSession -import net.mamoe.mirai.internal.network.context.AccountSecrets import net.mamoe.mirai.internal.network.protocol.SyncingCacheList import net.mamoe.mirai.internal.network.protocol.data.jce.FileStoragePushFSSvcList import net.mamoe.mirai.internal.network.protocol.packet.Tlv diff --git a/mirai-core/src/commonMain/kotlin/network/components/AccountSecretsManager.kt b/mirai-core/src/commonMain/kotlin/network/components/AccountSecretsManager.kt index 6142411ff..0fa093424 100644 --- a/mirai-core/src/commonMain/kotlin/network/components/AccountSecretsManager.kt +++ b/mirai-core/src/commonMain/kotlin/network/components/AccountSecretsManager.kt @@ -9,20 +9,25 @@ package net.mamoe.mirai.internal.network.components +import kotlinx.io.core.toByteArray +import kotlinx.serialization.Serializable import net.mamoe.mirai.Bot import net.mamoe.mirai.internal.BotAccount +import net.mamoe.mirai.internal.network.LoginExtraData +import net.mamoe.mirai.internal.network.WLoginSigInfo import net.mamoe.mirai.internal.network.component.ComponentKey -import net.mamoe.mirai.internal.network.context.AccountSecrets -import net.mamoe.mirai.internal.network.context.AccountSecretsImpl +import net.mamoe.mirai.internal.network.getRandomByteArray +import net.mamoe.mirai.internal.network.protocol.packet.login.wtlogin.get_mpasswd import net.mamoe.mirai.internal.utils.actualCacheDir +import net.mamoe.mirai.internal.utils.crypto.ECDHInitialPublicKey import net.mamoe.mirai.internal.utils.crypto.TEA +import net.mamoe.mirai.internal.utils.crypto.defaultInitialPublicKey +import net.mamoe.mirai.internal.utils.io.ProtoBuf import net.mamoe.mirai.internal.utils.io.serialization.loadAs import net.mamoe.mirai.internal.utils.io.serialization.toByteArray -import net.mamoe.mirai.utils.BotConfiguration -import net.mamoe.mirai.utils.DeviceInfo -import net.mamoe.mirai.utils.MiraiLogger -import net.mamoe.mirai.utils.info +import net.mamoe.mirai.utils.* import java.io.File +import java.util.concurrent.CopyOnWriteArraySet /** * For a [Bot]. @@ -39,6 +44,79 @@ internal interface AccountSecretsManager { companion object : ComponentKey } +/** + * Secrets for authentication with server. (login) + */ +internal interface AccountSecrets { + var wLoginSigInfoField: WLoginSigInfo? + + val wLoginSigInfoInitialized get() = wLoginSigInfoField != null + var wLoginSigInfo: WLoginSigInfo + get() = wLoginSigInfoField ?: error("wLoginSigInfoField is not yet initialized") + set(value) { + wLoginSigInfoField = value + } + + /** + * t537 + */ + var loginExtraData: MutableSet + + var G: ByteArray // sigInfo[2] + var dpwd: ByteArray + var randSeed: ByteArray // t403 + + /** + * t108 时更新 + */ + var ksid: ByteArray + + var tgtgtKey: ByteArray + val randomKey: ByteArray + var ecdhInitialPublicKey: ECDHInitialPublicKey +} + + +@Suppress("ArrayInDataClass") // for `copy` +@Serializable +internal data class AccountSecretsImpl( + override var loginExtraData: MutableSet, + override var wLoginSigInfoField: WLoginSigInfo?, + override var G: ByteArray, + override var dpwd: ByteArray = get_mpasswd().toByteArray(), + override var randSeed: ByteArray, + override var ksid: ByteArray, + override var tgtgtKey: ByteArray, + override val randomKey: ByteArray, + override var ecdhInitialPublicKey: ECDHInitialPublicKey, +) : AccountSecrets, ProtoBuf + +internal fun AccountSecretsImpl( + other: AccountSecrets, +): AccountSecretsImpl = other.run { + AccountSecretsImpl( + loginExtraData, wLoginSigInfoField, G, dpwd, + randSeed, ksid, tgtgtKey, randomKey, ecdhInitialPublicKey + ) +} + +internal fun AccountSecretsImpl( + device: DeviceInfo, account: BotAccount, +): AccountSecretsImpl { + return AccountSecretsImpl( + loginExtraData = CopyOnWriteArraySet(), + wLoginSigInfoField = null, + G = device.guid, + dpwd = get_mpasswd().toByteArray(), + randSeed = EMPTY_BYTE_ARRAY, + ksid = EMPTY_BYTE_ARRAY, + tgtgtKey = (account.passwordMd5 + ByteArray(4) + account.id.toInt().toByteArray()).md5(), + randomKey = getRandomByteArray(16), + ecdhInitialPublicKey = defaultInitialPublicKey + ) +} + + internal fun AccountSecretsManager.getSecretsOrCreate(account: BotAccount, device: DeviceInfo): AccountSecrets { var secrets = getSecrets(account) if (secrets == null) { @@ -95,7 +173,7 @@ internal class FileCacheAccountSecretsManager( val loaded = kotlin.runCatching { TEA.decrypt(file.readBytes(), account.passwordMd5).loadAs(AccountSecretsImpl.serializer()) }.getOrElse { e -> - if (e.message == "Field 'ecdhInitialPublicKey' is required for type with serial name 'net.mamoe.mirai.internal.network.context.AccountSecretsImpl', but it was missing") { + if (e.message == "Field 'ecdhInitialPublicKey' is required for type with serial name 'net.mamoe.mirai.internal.network.components.AccountSecretsImpl', but it was missing") { logger.info { "Detected old account secrets, invalidating..." } } else { logger.error("Failed to load account secrets from local cache. Invalidating cache...", e) diff --git a/mirai-core/src/commonMain/kotlin/network/components/SsoProcessor.kt b/mirai-core/src/commonMain/kotlin/network/components/SsoProcessor.kt index c8ba9fc80..865820c1c 100644 --- a/mirai-core/src/commonMain/kotlin/network/components/SsoProcessor.kt +++ b/mirai-core/src/commonMain/kotlin/network/components/SsoProcessor.kt @@ -14,7 +14,6 @@ import net.mamoe.mirai.internal.network.Packet import net.mamoe.mirai.internal.network.QQAndroidClient import net.mamoe.mirai.internal.network.WLoginSigInfo import net.mamoe.mirai.internal.network.component.ComponentKey -import net.mamoe.mirai.internal.network.context.AccountSecretsImpl import net.mamoe.mirai.internal.network.handler.NetworkHandler import net.mamoe.mirai.internal.network.impl.netty.NettyNetworkHandler import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacketWithRespType diff --git a/mirai-core/src/commonMain/kotlin/network/context/AccountSecrets.kt b/mirai-core/src/commonMain/kotlin/network/context/AccountSecrets.kt deleted file mode 100644 index 99f9461d7..000000000 --- a/mirai-core/src/commonMain/kotlin/network/context/AccountSecrets.kt +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright 2019-2021 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -package net.mamoe.mirai.internal.network.context - -import kotlinx.io.core.toByteArray -import kotlinx.serialization.Serializable -import net.mamoe.mirai.internal.BotAccount -import net.mamoe.mirai.internal.network.LoginExtraData -import net.mamoe.mirai.internal.network.WLoginSigInfo -import net.mamoe.mirai.internal.network.getRandomByteArray -import net.mamoe.mirai.internal.network.protocol.packet.login.wtlogin.get_mpasswd -import net.mamoe.mirai.internal.utils.crypto.ECDHInitialPublicKey -import net.mamoe.mirai.internal.utils.crypto.defaultInitialPublicKey -import net.mamoe.mirai.internal.utils.io.ProtoBuf -import net.mamoe.mirai.utils.DeviceInfo -import net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY -import net.mamoe.mirai.utils.md5 -import net.mamoe.mirai.utils.toByteArray -import java.util.concurrent.CopyOnWriteArraySet - -/** - * Secrets for authentication with server. (login) - */ -internal interface AccountSecrets { - var wLoginSigInfoField: WLoginSigInfo? - - val wLoginSigInfoInitialized get() = wLoginSigInfoField != null - var wLoginSigInfo: WLoginSigInfo - get() = wLoginSigInfoField ?: error("wLoginSigInfoField is not yet initialized") - set(value) { - wLoginSigInfoField = value - } - - /** - * t537 - */ - var loginExtraData: MutableSet - - var G: ByteArray // sigInfo[2] - var dpwd: ByteArray - var randSeed: ByteArray // t403 - - /** - * t108 时更新 - */ - var ksid: ByteArray - - var tgtgtKey: ByteArray - val randomKey: ByteArray - var ecdhInitialPublicKey: ECDHInitialPublicKey -} - -@Suppress("ArrayInDataClass") // for `copy` -@Serializable -internal data class AccountSecretsImpl( - override var loginExtraData: MutableSet, - override var wLoginSigInfoField: WLoginSigInfo?, - override var G: ByteArray, - override var dpwd: ByteArray = get_mpasswd().toByteArray(), - override var randSeed: ByteArray, - override var ksid: ByteArray, - override var tgtgtKey: ByteArray, - override val randomKey: ByteArray, - override var ecdhInitialPublicKey: ECDHInitialPublicKey, -) : AccountSecrets, ProtoBuf - -internal fun AccountSecretsImpl( - other: AccountSecrets, -): AccountSecretsImpl = other.run { - AccountSecretsImpl( - loginExtraData, - wLoginSigInfoField, - G, - dpwd, - randSeed, - ksid, - tgtgtKey, - randomKey, - ecdhInitialPublicKey - ) -} - -internal fun AccountSecretsImpl( - device: DeviceInfo, account: BotAccount, -): AccountSecretsImpl { - return AccountSecretsImpl( - loginExtraData = CopyOnWriteArraySet(), - wLoginSigInfoField = null, - G = device.guid, - dpwd = get_mpasswd().toByteArray(), - randSeed = EMPTY_BYTE_ARRAY, - ksid = EMPTY_BYTE_ARRAY, - tgtgtKey = (account.passwordMd5 + ByteArray(4) + account.id.toInt().toByteArray()).md5(), - randomKey = getRandomByteArray(16), - ecdhInitialPublicKey = defaultInitialPublicKey - ) -} \ No newline at end of file diff --git a/mirai-core/src/commonTest/kotlin/network/framework/components/TestSsoProcessor.kt b/mirai-core/src/commonTest/kotlin/network/framework/components/TestSsoProcessor.kt index 43340a051..6ba0778ab 100644 --- a/mirai-core/src/commonTest/kotlin/network/framework/components/TestSsoProcessor.kt +++ b/mirai-core/src/commonTest/kotlin/network/framework/components/TestSsoProcessor.kt @@ -11,10 +11,10 @@ package net.mamoe.mirai.internal.network.framework.components import net.mamoe.mirai.internal.QQAndroidBot import net.mamoe.mirai.internal.network.QQAndroidClient +import net.mamoe.mirai.internal.network.components.AccountSecretsImpl import net.mamoe.mirai.internal.network.components.SsoProcessor import net.mamoe.mirai.internal.network.components.SsoSession import net.mamoe.mirai.internal.network.components.createDeviceInfo -import net.mamoe.mirai.internal.network.context.AccountSecretsImpl import net.mamoe.mirai.internal.network.handler.NetworkHandler import net.mamoe.mirai.internal.network.handler.logger import net.mamoe.mirai.internal.network.protocol.data.jce.SvcRespRegister diff --git a/mirai-core/src/commonTest/kotlin/network/framework/sessionUtils.kt b/mirai-core/src/commonTest/kotlin/network/framework/sessionUtils.kt index e9bcb7103..91c0d43ad 100644 --- a/mirai-core/src/commonTest/kotlin/network/framework/sessionUtils.kt +++ b/mirai-core/src/commonTest/kotlin/network/framework/sessionUtils.kt @@ -13,9 +13,9 @@ import net.mamoe.mirai.event.events.BotOnlineEvent import net.mamoe.mirai.internal.QQAndroidBot import net.mamoe.mirai.internal.network.QQAndroidClient import net.mamoe.mirai.internal.network.WLoginSigInfo +import net.mamoe.mirai.internal.network.components.AccountSecrets +import net.mamoe.mirai.internal.network.components.AccountSecretsImpl import net.mamoe.mirai.internal.network.components.SsoSession -import net.mamoe.mirai.internal.network.context.AccountSecrets -import net.mamoe.mirai.internal.network.context.AccountSecretsImpl import net.mamoe.mirai.internal.utils.io.serialization.loadAs import net.mamoe.mirai.internal.utils.io.serialization.toByteArray import net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY