|
| 1 | +package net.lz1998.mirai.ext |
| 2 | + |
| 3 | +import kotlinx.io.core.toByteArray |
| 4 | +import kotlinx.serialization.Serializable |
| 5 | +import kotlinx.serialization.json.Json |
| 6 | +import net.mamoe.mirai.utils.* |
| 7 | +import java.io.File |
| 8 | + |
| 9 | + |
| 10 | +import kotlin.jvm.JvmSynthetic |
| 11 | +import kotlin.random.Random |
| 12 | +import kotlin.random.nextInt |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * 加载一个设备信息. 若文件不存在或为空则随机并创建一个设备信息保存. |
| 17 | + */ |
| 18 | +public fun File.loadAsMyDeviceInfo(json: Json): MyDeviceInfo { |
| 19 | + if (!this.exists() || this.length() == 0L) { |
| 20 | + return MyDeviceInfo.random().also { |
| 21 | + this.writeText(json.encodeToString(MyDeviceInfo.serializer(), it)) |
| 22 | + } |
| 23 | + } |
| 24 | + return json.decodeFromString(MyDeviceInfo.serializer(), this.readText()) |
| 25 | +} |
| 26 | + |
| 27 | +@Serializable |
| 28 | +public class MyDeviceInfo( |
| 29 | + public val display: String, |
| 30 | + public val product: String, |
| 31 | + public val device: String, |
| 32 | + public val board: String, |
| 33 | + public val brand: String, |
| 34 | + public val model: String, |
| 35 | + public val bootloader: String, |
| 36 | + public val fingerprint: String, |
| 37 | + public val bootId: String, |
| 38 | + public val procVersion: String, |
| 39 | + public val baseBand: String, |
| 40 | + public val version: Version, |
| 41 | + public val simInfo: String, |
| 42 | + public val osType: String, |
| 43 | + public val macAddress: String, |
| 44 | + public val wifiBSSID: String, |
| 45 | + public val wifiSSID: String, |
| 46 | + public val imsiMd5: ByteArray, |
| 47 | + public val imei: String, |
| 48 | + public val apn: String, |
| 49 | + public val protocol: BotConfiguration.MiraiProtocol |
| 50 | +) { |
| 51 | + public val androidId: String get() = display |
| 52 | + public val ipAddress: ByteArray get() = byteArrayOf(192.toByte(), 168.toByte(), 1, 123) |
| 53 | + |
| 54 | + @Serializable |
| 55 | + public class Version( |
| 56 | + public val incremental: String = "5891938", |
| 57 | + public val release: String = "10", |
| 58 | + public val codename: String = "REL", |
| 59 | + public val sdk: Int = 29 |
| 60 | + ) |
| 61 | + |
| 62 | + public companion object { |
| 63 | + @JvmStatic |
| 64 | + public fun random(): MyDeviceInfo { |
| 65 | + return MyDeviceInfo( |
| 66 | + display = "SMC.${getRandomString(6, '0'..'9')}.001", |
| 67 | + product = "smc", |
| 68 | + device = "smc", |
| 69 | + board = "smc", |
| 70 | + brand = "pbbot", |
| 71 | + model = "smc", |
| 72 | + bootloader = "unknown", |
| 73 | + fingerprint = "pbbot/smc/smc:10/SMC.200122.001/${getRandomIntString(7)}:user/release-keys", |
| 74 | + bootId = generateUUID(getRandomByteArray(16).md5()), |
| 75 | + procVersion = "Linux version 3.0.31-${getRandomString(8)} ([email protected])", |
| 76 | + baseBand = "", |
| 77 | + version = Version(), |
| 78 | + simInfo = "T-Mobile", |
| 79 | + osType = "android", |
| 80 | + macAddress = "02:00:00:00:00:00", |
| 81 | + wifiBSSID = "02:00:00:00:00:00", |
| 82 | + wifiSSID = "KFC FREE WIFI", |
| 83 | + imsiMd5 = getRandomByteArray(16).md5(), |
| 84 | + imei = getRandomIntString(15), |
| 85 | + apn = "wifi", |
| 86 | + protocol = BotConfiguration.MiraiProtocol.ANDROID_PAD |
| 87 | + ) |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +public fun MyDeviceInfo.generateDeviceInfoData(): DeviceInfo { |
| 93 | + return DeviceInfo( |
| 94 | + display = display.toByteArray(), |
| 95 | + product = product.toByteArray(), |
| 96 | + device = device.toByteArray(), |
| 97 | + board = board.toByteArray(), |
| 98 | + brand = brand.toByteArray(), |
| 99 | + model = model.toByteArray(), |
| 100 | + bootloader = bootloader.toByteArray(), |
| 101 | + fingerprint = fingerprint.toByteArray(), |
| 102 | + bootId = bootId.toByteArray(), |
| 103 | + procVersion = procVersion.toByteArray(), |
| 104 | + baseBand = baseBand.toByteArray(), |
| 105 | + version = DeviceInfo.Version( |
| 106 | + incremental = version.incremental.toByteArray(), |
| 107 | + release = version.release.toByteArray(), |
| 108 | + codename = version.codename.toByteArray(), |
| 109 | + sdk = version.sdk |
| 110 | + ), |
| 111 | + simInfo = simInfo.toByteArray(), |
| 112 | + osType = osType.toByteArray(), |
| 113 | + macAddress = macAddress.toByteArray(), |
| 114 | + wifiBSSID = wifiBSSID.toByteArray(), |
| 115 | + wifiSSID = wifiSSID.toByteArray(), |
| 116 | + imsiMd5 = imsiMd5, |
| 117 | + imei = imei, |
| 118 | + apn = apn.toByteArray() |
| 119 | + ) |
| 120 | + |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +/** |
| 125 | + * 生成长度为 [length], 元素为随机 `0..255` 的 [ByteArray] |
| 126 | + */ |
| 127 | +@JvmSynthetic |
| 128 | +internal fun getRandomByteArray(length: Int): ByteArray = ByteArray(length) { Random.nextInt(0..255).toByte() } |
| 129 | + |
| 130 | +/** |
| 131 | + * 随机生成长度为 [length] 的 [String]. |
| 132 | + */ |
| 133 | +@JvmSynthetic |
| 134 | +internal fun getRandomString(length: Int): String = |
| 135 | + getRandomString(length, *defaultRanges) |
| 136 | + |
| 137 | +private val defaultRanges: Array<CharRange> = arrayOf('a'..'z', 'A'..'Z', '0'..'9') |
| 138 | +private val intCharRanges: Array<CharRange> = arrayOf('0'..'9') |
| 139 | + |
| 140 | +/** |
| 141 | + * 根据所给 [charRange] 随机生成长度为 [length] 的 [String]. |
| 142 | + */ |
| 143 | +@JvmSynthetic |
| 144 | +internal fun getRandomString(length: Int, charRange: CharRange): String = |
| 145 | + CharArray(length) { charRange.random() }.concatToString() |
| 146 | + |
| 147 | +/** |
| 148 | + * 根据所给 [charRanges] 随机生成长度为 [length] 的 [String]. |
| 149 | + */ |
| 150 | +@JvmSynthetic |
| 151 | +internal fun getRandomString(length: Int, vararg charRanges: CharRange): String = |
| 152 | + CharArray(length) { charRanges[Random.Default.nextInt(0..charRanges.lastIndex)].random() }.concatToString() |
| 153 | + |
| 154 | + |
| 155 | +@JvmSynthetic |
| 156 | +internal fun getRandomIntString(length: Int): String = |
| 157 | + getRandomString(length, *intCharRanges) |
0 commit comments