Move DeviceInfo

This commit is contained in:
Him188 2020-02-09 23:20:29 +08:00
parent 23adcf6d4a
commit e116224180
5 changed files with 38 additions and 36 deletions

View File

@ -7,22 +7,20 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.qqandroid.utils package net.mamoe.mirai.utils
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.net.wifi.WifiManager import android.net.wifi.WifiManager
import android.os.Build import android.os.Build
import android.telephony.TelephonyManager import android.telephony.TelephonyManager
import kotlinx.io.core.toByteArray import kotlinx.io.core.toByteArray
import net.mamoe.mirai.utils.Context
import net.mamoe.mirai.utils.localIpAddress
import net.mamoe.mirai.utils.md5
import java.io.File import java.io.File
/** /**
* Delegated by [Build] * 部分引用指向 [Build].
* 部分需要权限, 若无权限则会使用默认值.
*/ */
actual class SystemDeviceInfo actual constructor(context: Context) : DeviceInfo(context) { actual open class SystemDeviceInfo actual constructor(context: Context) : DeviceInfo(context) {
override val display: ByteArray get() = Build.DISPLAY.toByteArray() override val display: ByteArray get() = Build.DISPLAY.toByteArray()
override val product: ByteArray get() = Build.PRODUCT.toByteArray() override val product: ByteArray get() = Build.PRODUCT.toByteArray()
override val device: ByteArray get() = Build.DEVICE.toByteArray() override val device: ByteArray get() = Build.DEVICE.toByteArray()

View File

@ -39,6 +39,10 @@ class BotConfiguration {
* 日志记录器 * 日志记录器
*/ */
var logger: MiraiLogger? = null var logger: MiraiLogger? = null
/**
* 设备信息覆盖
*/
var deviceInfo: ((Context) -> DeviceInfo)? = null
/** /**
* [CoroutineContext] * [CoroutineContext]

View File

@ -7,17 +7,17 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.qqandroid.utils package net.mamoe.mirai.utils
import kotlinx.serialization.SerialId import kotlinx.serialization.SerialId
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import net.mamoe.mirai.qqandroid.io.serialization.ProtoBufWithNullableSupport import kotlinx.serialization.protobuf.ProtoBuf
import net.mamoe.mirai.utils.Context
import net.mamoe.mirai.utils.cryptor.contentToString import net.mamoe.mirai.utils.cryptor.contentToString
import net.mamoe.mirai.utils.getValue
import net.mamoe.mirai.utils.unsafeWeakRef
abstract class DeviceInfo( /**
* 设备信息. 可通过继承 [SystemDeviceInfo] 来在默认的基础上修改
*/
abstract class DeviceInfo internal constructor(
context: Context context: Context
) { ) {
val context: Context by context.unsafeWeakRef() val context: Context by context.unsafeWeakRef()
@ -72,7 +72,7 @@ abstract class DeviceInfo(
@SerialId(9) val innerVersion: ByteArray @SerialId(9) val innerVersion: ByteArray
) )
return ProtoBufWithNullableSupport.dump( return ProtoBuf.dump(
DevInfo.serializer(), DevInfo( DevInfo.serializer(), DevInfo(
bootloader, bootloader,
procVersion, procVersion,
@ -87,10 +87,6 @@ abstract class DeviceInfo(
) )
} }
override fun toString(): String { // net.mamoe.mirai.utils.cryptor.ProtoKt.contentToString
return "DeviceInfo(display=${display.contentToString()}, product=${product.contentToString()}, device=${device.contentToString()}, board=${board.contentToString()}, brand=${brand.contentToString()}, model=${model.contentToString()}, bootloader=${bootloader.contentToString()}, fingerprint=${fingerprint.contentToString()}, bootId=${bootId.contentToString()}, procVersion=${procVersion.contentToString()}, baseBand=${baseBand.contentToString()}, version=$version, simInfo=${simInfo.contentToString()}, osType=${osType.contentToString()}, macAddress=${macAddress.contentToString()}, wifiBSSID=${wifiBSSID?.contentToString()}, wifiSSID=${wifiSSID?.contentToString()}, imsiMd5=${imsiMd5.contentToString()}, imei='$imei', ipAddress='$ipAddress', androidId=${androidId.contentToString()}, apn=${apn.contentToString()})"
}
interface Version { interface Version {
val incremental: ByteArray val incremental: ByteArray
val release: ByteArray val release: ByteArray
@ -99,6 +95,11 @@ abstract class DeviceInfo(
} }
} }
/**
* Defaults "%4;7t>;28<fc.5*6".toByteArray()
*/
fun generateGuid(androidId: ByteArray, macAddress: ByteArray): ByteArray = md5(androidId + macAddress)
/* /*
fun DeviceInfo.toOidb0x769DeviceInfo() : Oidb0x769.DeviceInfo = Oidb0x769.DeviceInfo( fun DeviceInfo.toOidb0x769DeviceInfo() : Oidb0x769.DeviceInfo = Oidb0x769.DeviceInfo(
brand = brand.encodeToString(), brand = brand.encodeToString(),

View File

@ -7,11 +7,15 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.qqandroid.utils package net.mamoe.mirai.utils
import net.mamoe.mirai.utils.Context import net.mamoe.mirai.utils.Context
import net.mamoe.mirai.utils.DeviceInfo
/** /**
* System default values * 通过本机信息来获取设备信息.
*
* Android: 获取手机信息, QQ 官方相同.
* JVM: 部分为常量, 部分为随机
*/ */
expect class SystemDeviceInfo(context: Context) : DeviceInfo open expect class SystemDeviceInfo(context: Context) : DeviceInfo

View File

@ -7,13 +7,14 @@
* https://github.com/mamoe/mirai/blob/master/LICENSE * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.qqandroid.utils package net.mamoe.mirai.utils
import kotlinx.io.core.toByteArray import kotlinx.io.core.toByteArray
import net.mamoe.mirai.utils.Context import net.mamoe.mirai.utils.io.getRandomByteArray
import net.mamoe.mirai.utils.localIpAddress import net.mamoe.mirai.utils.io.getRandomString
actual class SystemDeviceInfo actual constructor(context: Context) : DeviceInfo(context) { @UseExperimental(ExperimentalUnsignedTypes::class)
actual open class SystemDeviceInfo actual constructor(context: Context) : DeviceInfo(context) {
override val display: ByteArray get() = "MIRAI.200122.001".toByteArray() override val display: ByteArray get() = "MIRAI.200122.001".toByteArray()
override val product: ByteArray get() = "mirai".toByteArray() override val product: ByteArray get() = "mirai".toByteArray()
override val device: ByteArray get() = "mirai".toByteArray() override val device: ByteArray get() = "mirai".toByteArray()
@ -22,24 +23,18 @@ actual class SystemDeviceInfo actual constructor(context: Context) : DeviceInfo(
override val model: ByteArray get() = "mirai".toByteArray() override val model: ByteArray get() = "mirai".toByteArray()
override val bootloader: ByteArray get() = "unknown".toByteArray() override val bootloader: ByteArray get() = "unknown".toByteArray()
override val fingerprint: ByteArray get() = "mamoe/mirai/mirai:10/MIRAI.200122.001/5891938:user/release-keys".toByteArray() override val fingerprint: ByteArray get() = "mamoe/mirai/mirai:10/MIRAI.200122.001/5891938:user/release-keys".toByteArray()
override val bootId: ByteArray get() = "5974cb66-bb69-4e82-a436-836b98ebd88c".toByteArray() override val bootId: ByteArray = ExternalImage.generateUUID(md5(getRandomByteArray(16))).toByteArray()
override val procVersion: ByteArray get() = "Linux version 3.0.31-g6fb96c9 (android-build@xxx.xxx.xxx.xxx.com)".toByteArray() override val procVersion: ByteArray get() = "Linux version 3.0.31-g6fb96c9 (android-build@xxx.xxx.xxx.xxx.com)".toByteArray()
override val baseBand: ByteArray get() = byteArrayOf() override val baseBand: ByteArray get() = byteArrayOf()
override val version: DeviceInfo.Version get() = Version override val version: DeviceInfo.Version get() = Version
override val simInfo: ByteArray get() = "T-Mobile".toByteArray() override val simInfo: ByteArray get() = "T-Mobile".toByteArray()
override val osType: ByteArray get() = "android".toByteArray() override val osType: ByteArray get() = "android".toByteArray()
override val macAddress: ByteArray get() = "02:00:00:00:00:00".toByteArray() override val macAddress: ByteArray get() = "02:00:00:00:00:00".toByteArray()
override val wifiBSSID: ByteArray? override val wifiBSSID: ByteArray? get() = "02:00:00:00:00:00".toByteArray()
get() = "02:00:00:00:00:00".toByteArray() override val wifiSSID: ByteArray? get() = "<unknown ssid>".toByteArray()
override val wifiSSID: ByteArray? override val imsiMd5: ByteArray get() = md5(getRandomByteArray(16))
get() = "<unknown ssid>".toByteArray() override val imei: String get() = getRandomString(15, '0'..'9')
@UseExperimental(ExperimentalUnsignedTypes::class) override val ipAddress: ByteArray get() = localIpAddress().split(".").map { it.toUByte().toByte() }.takeIf { it.size == 4 }?.toByteArray() ?: byteArrayOf()
override val imsiMd5: ByteArray
get() = ubyteArrayOf(0xD4u, 0x1Du, 0x8Cu, 0xD9u, 0x8Fu, 0x00u, 0xB2u, 0x04u, 0xE9u, 0x80u, 0x09u, 0x98u, 0xECu, 0xF8u, 0x42u, 0x7Eu).toByteArray()
override val imei: String get() = "858414524711993"
@UseExperimental(ExperimentalUnsignedTypes::class)
override val ipAddress: ByteArray
get() = localIpAddress().split(".").map { it.toUByte().toByte() }.takeIf { it.size == 4 }?.toByteArray() ?: byteArrayOf()
override val androidId: ByteArray get() = display override val androidId: ByteArray get() = display
override val apn: ByteArray get() = "wifi".toByteArray() override val apn: ByteArray get() = "wifi".toByteArray()