Skip to content

Commit 14dba8a

Browse files
committed
json device info
1 parent 1d079eb commit 14dba8a

File tree

3 files changed

+178
-6
lines changed

3 files changed

+178
-6
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ dependencies {
5858
implementation("net.mamoe:mirai-core-api-jvm:2.0-M2") {
5959
exclude("net.mamoe", "mirai-core-utils")
6060
}
61+
implementation("org.jetbrains.kotlinx:kotlinx-serialization-protobuf-jvm:1.0.1")
6162
implementation("net.mamoe:mirai-core-utils-jvm:2.0-M2")
6263
implementation("com.squareup.okhttp3:okhttp:4.8.0")
6364
// implementation("com.google.protobuf:protobuf-javalite:3.8.0")

src/main/kotlin/net/lz1998/mirai/entity/WebSocketBotClient.kt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
package net.lz1998.mirai.entity
22

33
import kotlinx.coroutines.*
4+
import kotlinx.serialization.json.Json
45
import net.lz1998.mirai.alias.BFrame
56
import net.lz1998.mirai.alias.BFrameType
67
import net.lz1998.mirai.ext.*
78
import net.lz1998.mirai.service.MyLoginSolver
89
import net.lz1998.mirai.utils.*
910
import net.mamoe.mirai.Bot
1011
import net.mamoe.mirai.BotFactory
11-
import net.mamoe.mirai.alsoLogin
1212
import net.mamoe.mirai.event.events.BotEvent
1313
import net.mamoe.mirai.event.events.BotInvitedJoinGroupRequestEvent
1414
import net.mamoe.mirai.event.events.MemberJoinRequestEvent
1515
import net.mamoe.mirai.event.events.NewFriendRequestEvent
1616
import okhttp3.*
1717
import okio.ByteString
1818
import okio.ByteString.Companion.toByteString
19+
import java.io.File
1920
import java.util.concurrent.TimeUnit
2021

22+
var json: Json = runCatching {
23+
Json {
24+
isLenient = true
25+
ignoreUnknownKeys = true
26+
prettyPrint = true
27+
}
28+
}.getOrElse { Json {} }
29+
2130
class WebsocketBotClient(override var botId: Long, override var password: String, wsUrl: String) : RemoteBot {
2231
override lateinit var bot: Bot
2332

@@ -107,13 +116,15 @@ class WebsocketBotClient(override var botId: Long, override var password: String
107116

108117
override suspend fun initBot() {
109118
wsClient = httpClient.newWebSocket(wsRequest, wsListener)
119+
120+
val myDeviceInfo = File("device/bot-${botId}.json").loadAsMyDeviceInfo(json)
110121
bot = BotFactory.newBot(botId, password) {
111-
// fileStrBasedDeviceInfo("device/${botId}.json")
112-
fileBasedDeviceInfo("device/bot-${botId}.json")
113-
// protocol=BotConfiguration.MiraiProtocol.ANDROID_WATCH
122+
protocol = myDeviceInfo.protocol
123+
deviceInfo = { myDeviceInfo.generateDeviceInfoData() }
114124
loginSolver = MyLoginSolver
115-
// noNetworkLog()
116-
}.alsoLogin()
125+
}
126+
bot.logger.info("DeviceInfo: ${json.encodeToString(MyDeviceInfo.serializer(), myDeviceInfo)}")
127+
117128
bot.eventChannel.subscribeAlways<BotEvent> {
118129
onBotEvent(this)
119130
}
@@ -131,6 +142,9 @@ class WebsocketBotClient(override var botId: Long, override var password: String
131142
bot.eventChannel.subscribeAlways<NewFriendRequestEvent> {
132143
bot.friendRequestLru.put(it.eventId, it)
133144
}
145+
GlobalScope.launch {
146+
bot.login()
147+
}
134148
}
135149

136150
override suspend fun login() {
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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

Comments
 (0)