1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-25 04:50:26 +08:00

add device info

This commit is contained in:
Him188 2023-05-08 12:06:29 +01:00
parent 5b2a45fba3
commit c6f8de9468
No known key found for this signature in database
GPG Key ID: BA439CDDCF652375
5 changed files with 191 additions and 249 deletions
mirai-core-api/src
commonMain/kotlin/utils
jvmBaseMain/kotlin/utils
nativeMain/kotlin/utils

View File

@ -23,9 +23,23 @@ import kotlinx.serialization.protobuf.ProtoNumber
import net.mamoe.mirai.utils.DeviceInfoManager.Version.Companion.trans
import kotlin.jvm.JvmInline
import kotlin.jvm.JvmStatic
import kotlin.jvm.JvmSynthetic
import kotlin.random.Random
public expect class DeviceInfo(
internal const val DeviceInfoConstructorDeprecationMessage =
"Constructor and serializer of DeviceInfo is deprecated and will be removed in the future." +
"This is because new properties can be added and it requires too much effort to maintain public stability." +
"Please use DeviceInfo.serializeToString and DeviceInfo.deserializeFromString instead."
/**
* 表示设备信息
*
* ## 自定义设备信息
*/
public expect class DeviceInfo
@Deprecated(DeviceInfoConstructorDeprecationMessage, level = DeprecationLevel.WARNING)
@DeprecatedSinceMirai(warningSince = "2.15") // planned internal
public constructor(
display: ByteArray,
product: ByteArray,
device: ByteArray,
@ -48,38 +62,6 @@ public expect class DeviceInfo(
apn: ByteArray,
androidId: ByteArray,
) {
@Deprecated(
"This DeviceInfo constructor may randomize field `androidId` without your random instance. " +
"It is better to specify `androidId` explicitly.",
replaceWith = ReplaceWith(
"net.mamoe.mirai.utils.DeviceInfo(display, product, device, board, brand, model, " +
"bootloader, fingerprint, bootId, procVersion, baseBand, version, simInfo, osType, " +
"macAddress, wifiBSSID, wifiSSID, imsiMd5, imei, apn, androidId)"
)
)
public constructor(
display: ByteArray,
product: ByteArray,
device: ByteArray,
board: ByteArray,
brand: ByteArray,
model: ByteArray,
bootloader: ByteArray,
fingerprint: ByteArray,
bootId: ByteArray,
procVersion: ByteArray,
baseBand: ByteArray,
version: Version,
simInfo: ByteArray,
osType: ByteArray,
macAddress: ByteArray,
wifiBSSID: ByteArray,
wifiSSID: ByteArray,
imsiMd5: ByteArray,
imei: String,
apn: ByteArray,
)
public val display: ByteArray
public val product: ByteArray
public val device: ByteArray
@ -154,7 +136,27 @@ public expect class DeviceInfo(
@JvmStatic
public fun random(random: Random): DeviceInfo
@Deprecated(DeviceInfoConstructorDeprecationMessage, level = DeprecationLevel.WARNING)
@DeprecatedSinceMirai(warningSince = "2.15") // planned internal
public fun serializer(): KSerializer<DeviceInfo>
/**
* 将此 [DeviceInfo] 序列化为字符串. 序列化的字符串可以在以后通过 [DeviceInfo.deserializeFromString] 反序列化为 [DeviceInfo].
*
* 序列化的字符串有兼容性保证, 在旧版 mirai 序列化的字符串, 可以在新版 mirai 使用. 但新版 mirai 序列化的字符串不一定能在旧版使用.
*
* @since 2.15
*/
@JvmStatic
public fun serializeToString(deviceInfo: DeviceInfo): String
/**
* 将通过 [serializeToString] 序列化得到的字符串反序列化为 [DeviceInfo].
* 此函数兼容旧版 mirai 序列化的字符串.
* @since 2.15
*/
@JvmStatic
public fun deserializeFromString(string: String): DeviceInfo
}
/**
@ -169,7 +171,18 @@ public expect class DeviceInfo(
override fun hashCode(): Int
}
/**
* 将此 [DeviceInfo] 序列化为字符串. 序列化的字符串可以在以后通过 [DeviceInfo.deserializeFromString] 反序列化为 [DeviceInfo].
*
* 序列化的字符串有兼容性保证, 在旧版 mirai 序列化的字符串, 可以在新版 mirai 使用. 但新版 mirai 序列化的字符串不一定能在旧版使用.
*
* @since 2.15
*/
@JvmSynthetic
public fun DeviceInfo.serializeToString(): String = DeviceInfo.serializeToString(this)
internal object DeviceInfoCommonImpl {
@Suppress("DEPRECATION")
fun randomDeviceInfo(random: Random) = DeviceInfo(
display = "MIRAI.${getRandomString(6, '0'..'9', random)}.001".toByteArray(),
product = "mirai".toByteArray(),

View File

@ -1,171 +0,0 @@
/*
* Copyright 2019-2023 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/dev/LICENSE
*/
package net.mamoe.mirai.utils
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationException
import kotlinx.serialization.builtins.ByteArraySerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
import kotlinx.serialization.descriptors.element
import kotlinx.serialization.encoding.*
import kotlin.contracts.contract
@Suppress("DuplicatedCode")
@Deprecated(
message = "Dont use this serializer directly, it will be removed in future. " +
"ABI compatibility is not guaranteed.",
level = DeprecationLevel.WARNING
)
public object DeviceInfoDelegateSerializer : KSerializer<DeviceInfo> {
override val descriptor: SerialDescriptor =
buildClassSerialDescriptor(DeviceInfo::class.simpleName ?: "DeviceInfo") {
element<ByteArray>("display")
element<ByteArray>("product")
element<ByteArray>("device")
element<ByteArray>("board")
element<ByteArray>("brand")
element<ByteArray>("model")
element<ByteArray>("bootloader")
element<ByteArray>("fingerprint")
element<ByteArray>("bootId")
element<ByteArray>("procVersion")
element<ByteArray>("baseBand")
element("version", DeviceInfo.Version.serializer().descriptor)
element<ByteArray>("simInfo")
element<ByteArray>("osType")
element<ByteArray>("macAddress")
element<ByteArray>("wifiBSSID")
element<ByteArray>("wifiSSID")
element<ByteArray>("imsiMd5")
element<String>("imei")
element<ByteArray>("apn")
element<ByteArray>("androidId", isOptional = true)
}
override fun deserialize(decoder: Decoder): DeviceInfo {
return decoder.decodeStructure(descriptor) {
var display: ByteArray? = null
var product: ByteArray? = null
var device: ByteArray? = null
var board: ByteArray? = null
var brand: ByteArray? = null
var model: ByteArray? = null
var bootloader: ByteArray? = null
var fingerprint: ByteArray? = null
var bootId: ByteArray? = null
var procVersion: ByteArray? = null
var baseBand: ByteArray? = null
var version: DeviceInfo.Version? = null
var simInfo: ByteArray? = null
var osType: ByteArray? = null
var macAddress: ByteArray? = null
var wifiBSSID: ByteArray? = null
var wifiSSID: ByteArray? = null
var imsiMd5: ByteArray? = null
var imei: String? = null
var apn: ByteArray? = null
var androidId: ByteArray? = null
while (true) {
val index = decodeElementIndex(descriptor)
when (index) {
0 -> display = decodeSerializableElement(descriptor, index, ByteArraySerializer())
1 -> product = decodeSerializableElement(descriptor, index, ByteArraySerializer())
2 -> device = decodeSerializableElement(descriptor, index, ByteArraySerializer())
3 -> board = decodeSerializableElement(descriptor, index, ByteArraySerializer())
4 -> brand = decodeSerializableElement(descriptor, index, ByteArraySerializer())
5 -> model = decodeSerializableElement(descriptor, index, ByteArraySerializer())
6 -> bootloader = decodeSerializableElement(descriptor, index, ByteArraySerializer())
7 -> fingerprint = decodeSerializableElement(descriptor, index, ByteArraySerializer())
8 -> bootId = decodeSerializableElement(descriptor, index, ByteArraySerializer())
9 -> procVersion = decodeSerializableElement(descriptor, index, ByteArraySerializer())
10 -> baseBand = decodeSerializableElement(descriptor, index, ByteArraySerializer())
11 -> version = decodeSerializableElement(descriptor, index, DeviceInfo.Version.serializer())
12 -> simInfo = decodeSerializableElement(descriptor, index, ByteArraySerializer())
13 -> osType = decodeSerializableElement(descriptor, index, ByteArraySerializer())
14 -> macAddress = decodeSerializableElement(descriptor, index, ByteArraySerializer())
15 -> wifiBSSID = decodeSerializableElement(descriptor, index, ByteArraySerializer())
16 -> wifiSSID = decodeSerializableElement(descriptor, index, ByteArraySerializer())
17 -> imsiMd5 = decodeSerializableElement(descriptor, index, ByteArraySerializer())
18 -> imei = decodeStringElement(descriptor, index)
19 -> apn = decodeSerializableElement(descriptor, index, ByteArraySerializer())
20 -> androidId = decodeSerializableElement(descriptor, index, ByteArraySerializer())
}
if (index == CompositeDecoder.DECODE_DONE) break
}
display = assertNotNullOrDecodeFailed(display, "display")
product = assertNotNullOrDecodeFailed(product, "product")
device = assertNotNullOrDecodeFailed(device, "device")
board = assertNotNullOrDecodeFailed(board, "board")
brand = assertNotNullOrDecodeFailed(brand, "brand")
model = assertNotNullOrDecodeFailed(model, "model")
bootloader = assertNotNullOrDecodeFailed(bootloader, "bootloader")
fingerprint = assertNotNullOrDecodeFailed(fingerprint, "fingerprint")
bootId = assertNotNullOrDecodeFailed(bootId, "bootId")
procVersion = assertNotNullOrDecodeFailed(procVersion, "procVersion")
baseBand = assertNotNullOrDecodeFailed(baseBand, "baseBand")
version = assertNotNullOrDecodeFailed(version, "version")
simInfo = assertNotNullOrDecodeFailed(simInfo, "simInfo")
osType = assertNotNullOrDecodeFailed(osType, "osType")
macAddress = assertNotNullOrDecodeFailed(macAddress, "macAddress")
wifiBSSID = assertNotNullOrDecodeFailed(wifiBSSID, "wifiBSSID")
wifiSSID = assertNotNullOrDecodeFailed(wifiSSID, "wifiSSID")
imsiMd5 = assertNotNullOrDecodeFailed(imsiMd5, "imsiMd5")
imei = assertNotNullOrDecodeFailed(imei, "imei")
apn = assertNotNullOrDecodeFailed(apn, "apn")
return@decodeStructure DeviceInfo(
display, product, device, board, brand, model, bootloader,
fingerprint, bootId, procVersion, baseBand, version, simInfo,
osType, macAddress, wifiBSSID, wifiSSID, imsiMd5, imei, apn,
androidId ?: display
)
}
}
override fun serialize(encoder: Encoder, value: DeviceInfo) {
encoder.encodeStructure(descriptor) {
encodeSerializableElement(descriptor, 0, ByteArraySerializer(), value.display)
encodeSerializableElement(descriptor, 1, ByteArraySerializer(), value.product)
encodeSerializableElement(descriptor, 2, ByteArraySerializer(), value.device)
encodeSerializableElement(descriptor, 3, ByteArraySerializer(), value.board)
encodeSerializableElement(descriptor, 4, ByteArraySerializer(), value.brand)
encodeSerializableElement(descriptor, 5, ByteArraySerializer(), value.model)
encodeSerializableElement(descriptor, 6, ByteArraySerializer(), value.bootloader)
encodeSerializableElement(descriptor, 7, ByteArraySerializer(), value.fingerprint)
encodeSerializableElement(descriptor, 8, ByteArraySerializer(), value.bootId)
encodeSerializableElement(descriptor, 9, ByteArraySerializer(), value.procVersion)
encodeSerializableElement(descriptor, 10, ByteArraySerializer(), value.baseBand)
encodeSerializableElement(descriptor, 11, DeviceInfo.Version.serializer(), value.version)
encodeSerializableElement(descriptor, 12, ByteArraySerializer(), value.simInfo)
encodeSerializableElement(descriptor, 13, ByteArraySerializer(), value.osType)
encodeSerializableElement(descriptor, 14, ByteArraySerializer(), value.macAddress)
encodeSerializableElement(descriptor, 15, ByteArraySerializer(), value.wifiBSSID)
encodeSerializableElement(descriptor, 16, ByteArraySerializer(), value.wifiSSID)
encodeSerializableElement(descriptor, 17, ByteArraySerializer(), value.imsiMd5)
encodeSerializableElement(descriptor, 18, String.serializer(), value.imei)
encodeSerializableElement(descriptor, 19, ByteArraySerializer(), value.apn)
encodeSerializableElement(descriptor, 20, ByteArraySerializer(), value.androidId)
}
}
private fun <T : Any> assertNotNullOrDecodeFailed(value: T?, fieldName: String): T {
contract {
returns() implies (value != null)
}
return value ?: throw SerializationException("Failed to deserialize DeviceInfo: missing field \"$fieldName\".")
}
}

View File

@ -0,0 +1,102 @@
/*
* Copyright 2019-2023 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/dev/LICENSE
*/
package net.mamoe.mirai.utils
import io.ktor.utils.io.core.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
@Serializable
internal class DeviceInfoV1Legacy(
val product: ByteArray,
val display: ByteArray,
val device: ByteArray,
val board: ByteArray,
val brand: ByteArray,
val model: ByteArray,
val bootloader: ByteArray,
val fingerprint: ByteArray,
val bootId: ByteArray,
val procVersion: ByteArray,
val baseBand: ByteArray,
val version: DeviceInfoV1LegacyVersion,
val simInfo: ByteArray,
val osType: ByteArray,
val macAddress: ByteArray,
val wifiBSSID: ByteArray,
val wifiSSID: ByteArray,
val imsiMd5: ByteArray,
val imei: String,
val apn: ByteArray,
val androidId: ByteArray? = null
)
@Serializable
internal class DeviceInfoV1LegacyVersion(
val incremental: ByteArray = "5891938".toByteArray(),
val release: ByteArray = "10".toByteArray(),
val codename: ByteArray = "REL".toByteArray(),
val sdk: Int = 29
)
internal object DeviceInfoV1LegacySerializer : KSerializer<DeviceInfo> by DeviceInfoV1Legacy.serializer().map(
DeviceInfoV1Legacy.serializer().descriptor.copy("DeviceInfo"),
deserialize = {
@Suppress("DEPRECATION")
DeviceInfo(
display,
product,
device,
board,
brand,
model,
bootloader,
fingerprint,
bootId,
procVersion,
baseBand,
DeviceInfo.Version(version.incremental, version.release, version.codename, version.sdk),
simInfo,
osType,
macAddress,
wifiBSSID,
wifiSSID,
imsiMd5,
imei,
apn,
androidId = display
)
},
serialize = {
DeviceInfoV1Legacy(
display,
product,
device,
board,
brand,
model,
bootloader,
fingerprint,
bootId,
procVersion,
baseBand,
DeviceInfoV1LegacyVersion(version.incremental, version.release, version.codename, version.sdk),
simInfo,
osType,
macAddress,
wifiBSSID,
wifiSSID,
imsiMd5,
imei,
apn
)
}
)

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
* Copyright 2019-2023 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.
@ -16,7 +16,7 @@ import kotlinx.serialization.json.Json
import java.io.File
import kotlin.random.Random
@Serializable(DeviceInfoDelegateSerializer::class)
@Serializable(DeviceInfoV1LegacySerializer::class)
public actual class DeviceInfo public actual constructor(
public actual val display: ByteArray,
public actual val product: ByteArray,
@ -47,9 +47,11 @@ public actual class DeviceInfo public actual constructor(
"net.mamoe.mirai.utils.DeviceInfo(display, product, device, board, brand, model, " +
"bootloader, fingerprint, bootId, procVersion, baseBand, version, simInfo, osType, " +
"macAddress, wifiBSSID, wifiSSID, imsiMd5, imei, apn, androidId)"
)
),
level = DeprecationLevel.WARNING
)
public actual constructor(
@DeprecatedSinceMirai(warningSince = "2.15")
public constructor(
display: ByteArray,
product: ByteArray,
device: ByteArray,
@ -160,6 +162,24 @@ public actual class DeviceInfo public actual constructor(
public actual fun random(random: Random): DeviceInfo {
return DeviceInfoCommonImpl.randomDeviceInfo(random)
}
/**
* 将此 [DeviceInfo] 序列化为字符串. 序列化的字符串可以在以后通过 [DeviceInfo.deserializeFromString] 反序列化为 [DeviceInfo].
*
* 序列化的字符串有兼容性保证, 在旧版 mirai 序列化的字符串, 可以在新版 mirai 使用. 但新版 mirai 序列化的字符串不一定能在旧版使用.
*
* @since 2.15
*/
@JvmStatic
public actual fun serializeToString(deviceInfo: DeviceInfo): String = DeviceInfoManager.serialize(deviceInfo)
/**
* 将通过 [serializeToString] 序列化得到的字符串反序列化为 [DeviceInfo].
* 此函数兼容旧版 mirai 序列化的字符串.
* @since 2.15
*/
@JvmStatic
public actual fun deserializeFromString(string: String): DeviceInfo = DeviceInfoManager.deserialize(string)
}
/**
@ -180,5 +200,5 @@ public actual class DeviceInfo public actual constructor(
@Suppress("ClassName")
@Deprecated("For binary compatibility", level = DeprecationLevel.HIDDEN)
public object `$serializer` : KSerializer<DeviceInfo> by DeviceInfoDelegateSerializer
public object `$serializer` : KSerializer<DeviceInfo> by DeviceInfoV1LegacySerializer
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
* Copyright 2019-2023 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.
@ -13,7 +13,7 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlin.random.Random
@Serializable(DeviceInfoDelegateSerializer::class)
@Serializable(DeviceInfoV1LegacySerializer::class)
public actual class DeviceInfo public actual constructor(
public actual val display: ByteArray,
public actual val product: ByteArray,
@ -37,44 +37,6 @@ public actual class DeviceInfo public actual constructor(
public actual val apn: ByteArray,
public actual val androidId: ByteArray,
) {
@Deprecated(
"This DeviceInfo constructor may randomize field `androidId` without your random instance. " +
"It is better to specify `android` id explicitly.",
replaceWith = ReplaceWith(
"net.mamoe.mirai.utils.DeviceInfo(display, product, device, board, brand, model, " +
"bootloader, fingerprint, bootId, procVersion, baseBand, version, simInfo, osType, " +
"macAddress, wifiBSSID, wifiSSID, imsiMd5, imei, apn, androidId)"
)
)
public actual constructor(
display: ByteArray,
product: ByteArray,
device: ByteArray,
board: ByteArray,
brand: ByteArray,
model: ByteArray,
bootloader: ByteArray,
fingerprint: ByteArray,
bootId: ByteArray,
procVersion: ByteArray,
baseBand: ByteArray,
version: Version,
simInfo: ByteArray,
osType: ByteArray,
macAddress: ByteArray,
wifiBSSID: ByteArray,
wifiSSID: ByteArray,
imsiMd5: ByteArray,
imei: String,
apn: ByteArray
) : this(
display, product, device, board, brand, model, bootloader,
fingerprint, bootId, procVersion, baseBand, version, simInfo,
osType, macAddress, wifiBSSID, wifiSSID, imsiMd5, imei, apn,
androidId = display
)
public actual val ipAddress: ByteArray get() = byteArrayOf(192.toByte(), 168.toByte(), 1, 123)
init {
@ -137,6 +99,22 @@ public actual class DeviceInfo public actual constructor(
public actual fun random(random: Random): DeviceInfo {
return DeviceInfoCommonImpl.randomDeviceInfo(random)
}
/**
* 将此 [DeviceInfo] 序列化为字符串. 序列化的字符串可以在以后通过 [DeviceInfo.deserializeFromString] 反序列化为 [DeviceInfo].
*
* 序列化的字符串有兼容性保证, 在旧版 mirai 序列化的字符串, 可以在新版 mirai 使用. 但新版 mirai 序列化的字符串不一定能在旧版使用.
*
* @since 2.15
*/
public actual fun serializeToString(deviceInfo: DeviceInfo): String = DeviceInfoManager.serialize(deviceInfo)
/**
* 将通过 [serializeToString] 序列化得到的字符串反序列化为 [DeviceInfo].
* 此函数兼容旧版 mirai 序列化的字符串.
* @since 2.15
*/
public actual fun deserializeFromString(string: String): DeviceInfo = DeviceInfoManager.deserialize(string)
}
/**