1616package love.forte.simbot.component.mirai.configuration
1717
1818import cn.hutool.crypto.SecureUtil
19- import kotlinx.serialization.ExperimentalSerializationApi
2019import kotlinx.serialization.encodeToString
2120import kotlinx.serialization.json.Json
2221import love.forte.common.configuration.annotation.ConfigInject
@@ -52,144 +51,154 @@ public fun miraiBotLogger(botCode: Long, type: String? = null): Logger {
5251@Beans(" miraiConfiguration" )
5352@AsMiraiConfig
5453public class MiraiConfiguration {
55-
54+
5655 private companion object : TypedCompLogger (MiraiConfiguration ::class .java)
57-
56+
5857 /* *
5958 * mirai心跳周期. 过长会导致被服务器断开连接. 单位毫秒
6059 * @see BotConfiguration.heartbeatPeriodMillis
6160 */
6261 @field:ConfigInject
6362 var heartbeatPeriodMillis: Long = BotConfiguration .Default .heartbeatPeriodMillis
64-
63+
6564 /* *
6665 * 每次心跳时等待结果的时间.
6766 * 一旦心跳超时, 整个网络服务将会重启 (将消耗约 1s). 除正在进行的任务 (如图片上传) 会被中断外, 事件和插件均不受影响.
6867 * @see BotConfiguration.heartbeatTimeoutMillis
6968 */
7069 @field:ConfigInject
7170 var heartbeatTimeoutMillis: Long = BotConfiguration .Default .heartbeatTimeoutMillis
72-
71+
7372 /* *
7473 * mirai 心跳策略.
7574 */
7675 @field:ConfigInject
7776 var heartbeatStrategy: BotConfiguration .HeartbeatStrategy = BotConfiguration .Default .heartbeatStrategy
78-
79-
77+
78+
8079 /* * 最多尝试多少次重连 */
8180 @field:ConfigInject
8281 var reconnectionRetryTimes: Int = BotConfiguration .Default .reconnectionRetryTimes
83-
84-
82+
83+
8584 /* *
8685 * 使用协议类型。
8786 * 默认使用 [安卓手机协议][BotConfiguration.MiraiProtocol.ANDROID_PHONE]。
8887 */
8988 @field:ConfigInject
9089 var protocol: BotConfiguration .MiraiProtocol = BotConfiguration .Default .protocol
91-
90+
9291 /* * 关闭mirai的bot logger */
9392 @field:ConfigInject
9493 var noBotLog: Boolean = false
95-
94+
9695 /* * 关闭mirai网络日志 */
9796 @field:ConfigInject
9897 var noNetworkLog: Boolean = false
99-
98+
10099 /* * mirai bot log切换使用simbot的log(slf4j-api) */
101100 @field:ConfigInject
102101 var useSimbotBotLog: Boolean = true
103-
102+
104103 /* * mirai 网络log 切换使用simbot的log(slf4j-api) */
105104 @field:ConfigInject
106105 var useSimbotNetworkLog: Boolean = true
107-
106+
108107 /* * mirai配置自定义deviceInfoSeed的时候使用的随机种子。默认为1. */
109108 @field:ConfigInject
110109 var deviceInfoSeed: Long = 1L
111-
112-
110+
111+
113112 @field:ConfigInject
114- var deviceInfoFile: String? = " "
115-
113+ var deviceInfoFile: String? = " device.json"
114+
115+ /* *
116+ * 如果为true,则通过 [deviceInfoFile] 的值进行基于文件的随机设备信息配置。
117+ */
118+ @field:ConfigInject
119+ var deviceInfoFileBased: Boolean = true
120+
116121 /* *
117122 * 是否输出设备信息
118123 */
119124 @field:ConfigInject
120125 var deviceInfoOutput: Boolean = false
121-
126+
122127 /* *
123128 * @see BotConfiguration.highwayUploadCoroutineCount
124129 */
125130 @field:ConfigInject
126131 var highwayUploadCoroutineCount: Int = BotConfiguration .Default .highwayUploadCoroutineCount
127-
132+
128133 private val json = Json {
129134 isLenient = true
130135 ignoreUnknownKeys = true
131136 prettyPrint = true
132137 }
133-
138+
134139 /* *
135140 * mirai官方配置类获取函数,默认为其默认值
136141 * */
137142 // @set:Deprecated("use setPostBotConfigurationProcessor((code, conf) -> {...})")
138-
139- @OptIn(ExperimentalSerializationApi ::class )
143+
140144 val botConfiguration: (String ) -> BotConfiguration = {
141145 val conf = BotConfiguration ()
142-
143- deviceInfoFile.takeIf { it?.isNotBlank() == true }?.runCatching {
144- logger.info(" Try to use device info file: $this " )
145- val jsonReader = ResourceUtil .getResourceUtf8Reader(this )
146- val json = jsonReader.use { it.readText() }
147- conf.loadDeviceInfoJson(json)
148- }?.getOrElse { e ->
149- logger.error(" Load device Info json file: $deviceInfoFile failed. get device by simbot default." , e)
150- null
151- } ? : run {
152- conf.deviceInfo = {
153- val devInfo = simbotMiraiDeviceInfo(it.id, deviceInfoSeed)
154-
155- if (deviceInfoOutput) {
156- runCatching<Unit > {
157- val devInfoJson = json.encodeToString(devInfo)
158- val outFile = File (" simbot-devInfo.json" )
159- if (! outFile.exists()) {
160- outFile.apply {
161- parentFile?.mkdirs()
162- createNewFile()
146+
147+ if (deviceInfoFileBased) {
148+ conf.fileBasedDeviceInfo(deviceInfoFile?.takeIf { it.isNotEmpty() } ? : " device.json" )
149+ } else {
150+ deviceInfoFile.takeIf { it?.isNotBlank() == true }?.runCatching {
151+ logger.info(" Try to use device info file: $this " )
152+ val jsonReader = ResourceUtil .getResourceUtf8Reader(this )
153+ val json = jsonReader.use { it.readText() }
154+ conf.loadDeviceInfoJson(json)
155+ }?.getOrElse { e ->
156+ logger.error(" Load device info json file: $deviceInfoFile failed. get device via mirai random." , e)
157+ null
158+ } ? : run {
159+ // conf.fileBasedDeviceInfo(deviceInfoFile ?: "device.json")
160+ conf.deviceInfo = {
161+ // random.
162+ DeviceInfo .random().also { devInfo ->
163+ if (deviceInfoOutput) {
164+ logger.warn(" The configuration property 'deviceInfoOutput' is not recommended" )
165+ runCatching<Unit > {
166+ val devInfoJson = json.encodeToString(devInfo)
167+ val outFile = File (" simbot-devInfo.json" )
168+ if (! outFile.exists()) {
169+ outFile.apply {
170+ parentFile?.mkdirs()
171+ createNewFile()
172+ }
173+ }
174+ FileWriter (outFile).use { w ->
175+ w.write(devInfoJson)
176+ logger.info(" DevInfo write to ${outFile.canonicalPath} " )
177+ }
178+ }.getOrElse { e ->
179+ logger.error(" Write devInfo failed: {}" , e.localizedMessage)
180+ if (! logger.isDebugEnabled) {
181+ logger.error(" Enable debug log for more information." )
182+ }
183+ logger.debug(" Write devInfo failed." , e)
163184 }
164185 }
165- FileWriter (outFile).use { w ->
166- w.write(devInfoJson)
167- logger.info(" DevInfo write to ${outFile.canonicalPath} " )
168- }
169- }.getOrElse { e ->
170- logger.error(" Write devInfo failed: {}" , e.localizedMessage)
171- if (! logger.isDebugEnabled) {
172- logger.error(" Enable debug log for more information." )
173- }
174- logger.debug(" Write devInfo failed." , e)
175186 }
176187 }
177-
178-
179- devInfo
180188 }
181189 }
182-
183-
190+
191+
192+
184193 conf.heartbeatPeriodMillis = this .heartbeatPeriodMillis
185194 conf.heartbeatTimeoutMillis = this .heartbeatTimeoutMillis
186195 // conf.firstReconnectDelayMillis = this.firstReconnectDelayMillis
187196 // conf.reconnectPeriodMillis = this.reconnectPeriodMillis
188197 conf.reconnectionRetryTimes = this .reconnectionRetryTimes
189198 conf.protocol = this .protocol
190199 conf.highwayUploadCoroutineCount = highwayUploadCoroutineCount
191-
192-
200+
201+
193202 if (noBotLog) {
194203 conf.noBotLog()
195204 }
@@ -223,12 +232,12 @@ public class MiraiConfiguration {
223232 if (logger is MiraiLoggerWithSwitch ) logger else logger.withSwitch(true )
224233 }
225234 }
226-
235+
227236 conf
228237 }
229238}
230239
231-
240+ @Deprecated( " Unused " )
232241internal fun simbotMiraiDeviceInfo (c : Long , s : Long ): DeviceInfo {
233242 val r = Random (c * s)
234243 return DeviceInfo (
@@ -241,9 +250,11 @@ internal fun simbotMiraiDeviceInfo(c: Long, s: Long): DeviceInfo {
241250 bootloader = " unknown" .toByteArray(),
242251 // mamoe/mirai/mirai:10/MIRAI.200122.001/
243252 fingerprint = " mamoe/mirai/mirai:10/MIRAI.200122.001/${
244- getRandomString(7 ,
253+ getRandomString(
254+ 7 ,
245255 ' 0' .. ' 9' ,
246- r)
256+ r
257+ )
247258 } :user/release-keys" .toByteArray(),
248259 bootId = generateUUID(SecureUtil .md5().digest(getRandomByteArray(16 , r))).toByteArray(),
249260 procVersion = " Linux version 3.0.31-${getRandomString(8 , r)} (android-build@xxx.xxx.xxx.xxx.com)" .toByteArray(),
@@ -257,7 +268,7 @@ internal fun simbotMiraiDeviceInfo(c: Long, s: Long): DeviceInfo {
257268 imsiMd5 = SecureUtil .md5().digest(getRandomByteArray(16 , r)),
258269 imei = getRandomString(15 , ' 0' .. ' 9' , r),
259270 apn = " wifi" .toByteArray()
260-
271+
261272 )
262273}
263274
0 commit comments