Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from simple-robot/upgrade_mirai
Browse files Browse the repository at this point in the history
升级Mirai版本并调整默认的设备信息配置形式
ForteScarlet authored Oct 30, 2022
2 parents 9e9b1bb + 459286f commit d3c0123
Showing 28 changed files with 113 additions and 96 deletions.
2 changes: 1 addition & 1 deletion component/component-ding/pom.xml
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
<parent>
<artifactId>component-parent</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion component/component-kaiheila-parent/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>component-parent</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>

2 changes: 1 addition & 1 deletion component/component-lovelycat-httpapi/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>component-parent</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
4 changes: 2 additions & 2 deletions component/component-mirai/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<groupId>love.forte.simple-robot</groupId>
<artifactId>component-parent</artifactId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -22,7 +22,7 @@
<url>https://github.com/ForteScarlet/simpler-robot/tree/dev/component/component-mirai</url>

<properties>
<mirai.version>2.11.1</mirai.version>
<mirai.version>2.13.0-RC2</mirai.version>
</properties>


Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@
package love.forte.simbot.component.mirai.configuration

import cn.hutool.crypto.SecureUtil
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import love.forte.common.configuration.annotation.ConfigInject
@@ -52,144 +51,154 @@ public fun miraiBotLogger(botCode: Long, type: String? = null): Logger {
@Beans("miraiConfiguration")
@AsMiraiConfig
public class MiraiConfiguration {

private companion object : TypedCompLogger(MiraiConfiguration::class.java)

/**
* mirai心跳周期. 过长会导致被服务器断开连接. 单位毫秒
* @see BotConfiguration.heartbeatPeriodMillis
*/
@field:ConfigInject
var heartbeatPeriodMillis: Long = BotConfiguration.Default.heartbeatPeriodMillis

/**
* 每次心跳时等待结果的时间.
* 一旦心跳超时, 整个网络服务将会重启 (将消耗约 1s). 除正在进行的任务 (如图片上传) 会被中断外, 事件和插件均不受影响.
* @see BotConfiguration.heartbeatTimeoutMillis
*/
@field:ConfigInject
var heartbeatTimeoutMillis: Long = BotConfiguration.Default.heartbeatTimeoutMillis

/**
* mirai 心跳策略.
*/
@field:ConfigInject
var heartbeatStrategy: BotConfiguration.HeartbeatStrategy = BotConfiguration.Default.heartbeatStrategy


/** 最多尝试多少次重连 */
@field:ConfigInject
var reconnectionRetryTimes: Int = BotConfiguration.Default.reconnectionRetryTimes


/**
* 使用协议类型。
* 默认使用 [安卓手机协议][BotConfiguration.MiraiProtocol.ANDROID_PHONE]。
*/
@field:ConfigInject
var protocol: BotConfiguration.MiraiProtocol = BotConfiguration.Default.protocol

/** 关闭mirai的bot logger */
@field:ConfigInject
var noBotLog: Boolean = false

/** 关闭mirai网络日志 */
@field:ConfigInject
var noNetworkLog: Boolean = false

/** mirai bot log切换使用simbot的log(slf4j-api) */
@field:ConfigInject
var useSimbotBotLog: Boolean = true

/** mirai 网络log 切换使用simbot的log(slf4j-api) */
@field:ConfigInject
var useSimbotNetworkLog: Boolean = true

/** mirai配置自定义deviceInfoSeed的时候使用的随机种子。默认为1. */
@field:ConfigInject
var deviceInfoSeed: Long = 1L


@field:ConfigInject
var deviceInfoFile: String? = ""

var deviceInfoFile: String? = "device.json"

/**
* 如果为true,则通过 [deviceInfoFile] 的值进行基于文件的随机设备信息配置。
*/
@field:ConfigInject
var deviceInfoFileBased: Boolean = true

/**
* 是否输出设备信息
*/
@field:ConfigInject
var deviceInfoOutput: Boolean = false

/**
* @see BotConfiguration.highwayUploadCoroutineCount
*/
@field:ConfigInject
var highwayUploadCoroutineCount: Int = BotConfiguration.Default.highwayUploadCoroutineCount

private val json = Json {
isLenient = true
ignoreUnknownKeys = true
prettyPrint = true
}

/**
* mirai官方配置类获取函数,默认为其默认值
* */
// @set:Deprecated("use setPostBotConfigurationProcessor((code, conf) -> {...})")

@OptIn(ExperimentalSerializationApi::class)

val botConfiguration: (String) -> BotConfiguration = {
val conf = BotConfiguration()

deviceInfoFile.takeIf { it?.isNotBlank() == true }?.runCatching {
logger.info("Try to use device info file: $this")
val jsonReader = ResourceUtil.getResourceUtf8Reader(this)
val json = jsonReader.use { it.readText() }
conf.loadDeviceInfoJson(json)
}?.getOrElse { e ->
logger.error("Load device Info json file: $deviceInfoFile failed. get device by simbot default.", e)
null
} ?: run {
conf.deviceInfo = {
val devInfo = simbotMiraiDeviceInfo(it.id, deviceInfoSeed)

if (deviceInfoOutput) {
runCatching<Unit> {
val devInfoJson = json.encodeToString(devInfo)
val outFile = File("simbot-devInfo.json")
if (!outFile.exists()) {
outFile.apply {
parentFile?.mkdirs()
createNewFile()

if (deviceInfoFileBased) {
conf.fileBasedDeviceInfo(deviceInfoFile?.takeIf { it.isNotEmpty() } ?: "device.json")
} else {
deviceInfoFile.takeIf { it?.isNotBlank() == true }?.runCatching {
logger.info("Try to use device info file: $this")
val jsonReader = ResourceUtil.getResourceUtf8Reader(this)
val json = jsonReader.use { it.readText() }
conf.loadDeviceInfoJson(json)
}?.getOrElse { e ->
logger.error("Load device info json file: $deviceInfoFile failed. get device via mirai random.", e)
null
} ?: run {
// conf.fileBasedDeviceInfo(deviceInfoFile ?: "device.json")
conf.deviceInfo = {
// random.
DeviceInfo.random().also { devInfo ->
if (deviceInfoOutput) {
logger.warn("The configuration property 'deviceInfoOutput' is not recommended")
runCatching<Unit> {
val devInfoJson = json.encodeToString(devInfo)
val outFile = File("simbot-devInfo.json")
if (!outFile.exists()) {
outFile.apply {
parentFile?.mkdirs()
createNewFile()
}
}
FileWriter(outFile).use { w ->
w.write(devInfoJson)
logger.info("DevInfo write to ${outFile.canonicalPath}")
}
}.getOrElse { e ->
logger.error("Write devInfo failed: {}", e.localizedMessage)
if (!logger.isDebugEnabled) {
logger.error("Enable debug log for more information.")
}
logger.debug("Write devInfo failed.", e)
}
}
FileWriter(outFile).use { w ->
w.write(devInfoJson)
logger.info("DevInfo write to ${outFile.canonicalPath}")
}
}.getOrElse { e ->
logger.error("Write devInfo failed: {}", e.localizedMessage)
if (!logger.isDebugEnabled) {
logger.error("Enable debug log for more information.")
}
logger.debug("Write devInfo failed.", e)
}
}


devInfo
}
}





conf.heartbeatPeriodMillis = this.heartbeatPeriodMillis
conf.heartbeatTimeoutMillis = this.heartbeatTimeoutMillis
// conf.firstReconnectDelayMillis = this.firstReconnectDelayMillis
// conf.reconnectPeriodMillis = this.reconnectPeriodMillis
conf.reconnectionRetryTimes = this.reconnectionRetryTimes
conf.protocol = this.protocol
conf.highwayUploadCoroutineCount = highwayUploadCoroutineCount


if (noBotLog) {
conf.noBotLog()
}
@@ -223,12 +232,12 @@ public class MiraiConfiguration {
if (logger is MiraiLoggerWithSwitch) logger else logger.withSwitch(true)
}
}

conf
}
}


@Deprecated("Unused")
internal fun simbotMiraiDeviceInfo(c: Long, s: Long): DeviceInfo {
val r = Random(c * s)
return DeviceInfo(
@@ -241,9 +250,11 @@ internal fun simbotMiraiDeviceInfo(c: Long, s: Long): DeviceInfo {
bootloader = "unknown".toByteArray(),
// mamoe/mirai/mirai:10/MIRAI.200122.001/
fingerprint = "mamoe/mirai/mirai:10/MIRAI.200122.001/${
getRandomString(7,
getRandomString(
7,
'0'..'9',
r)
r
)
}:user/release-keys".toByteArray(),
bootId = generateUUID(SecureUtil.md5().digest(getRandomByteArray(16, r))).toByteArray(),
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 {
imsiMd5 = SecureUtil.md5().digest(getRandomByteArray(16, r)),
imei = getRandomString(15, '0'..'9', r),
apn = "wifi".toByteArray()

)
}

2 changes: 1 addition & 1 deletion component/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>project</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion core-api/api/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>project</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion core-api/core/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>project</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion http/client-core/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>http-parent</artifactId>
<groupId>love.forte.simple-robot.http</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion http/client-ktor/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>http-parent</artifactId>
<groupId>love.forte.simple-robot.http</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion http/client-restTemplate/pom.xml
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
<parent>
<artifactId>http-parent</artifactId>
<groupId>love.forte.simple-robot.http</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion http/pom.xml
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
<parent>
<artifactId>project</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
4 changes: 2 additions & 2 deletions parent/pom.xml
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@

<groupId>love.forte.simple-robot</groupId>
<artifactId>parent</artifactId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<packaging>pom</packaging>

<modules>
@@ -23,7 +23,7 @@
<description>simple-robot是一个通用bot开发框架,以同一种灵活的标准来编写不同平台的bot应用。而simpler-robot便是simple-robot 2.x版本命名。</description>

<properties>
<simbot.version>2.4.1<!--v--></simbot.version>
<simbot.version>2.5.0<!--v--></simbot.version>
<simbot.khl.version>0.0.1-ALPHA.1<!--khlv--></simbot.khl.version>

<maven.compiler.source>8</maven.compiler.source>
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
<parent>
<groupId>love.forte.simple-robot</groupId>
<artifactId>parent</artifactId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>./parent/pom.xml</relativePath>
</parent>

2 changes: 1 addition & 1 deletion serialization/json-core/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>serialization-parent</artifactId>
<groupId>love.forte.simple-robot.serialization</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion serialization/json-fastjson/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>serialization-parent</artifactId>
<groupId>love.forte.simple-robot.serialization</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion serialization/json-jackson/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>serialization-parent</artifactId>
<groupId>love.forte.simple-robot.serialization</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion serialization/json-moshi/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>serialization-parent</artifactId>
<groupId>love.forte.simple-robot.serialization</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion serialization/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>project</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion simbot-x/plugin-core/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>project</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
<parent>
<artifactId>spring-boot-starter</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>spring-boot-starter</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@

/**
* simbot配置文件对应实例。实际上没有被使用到,仅用作提供springboot的配置文件快捷提醒。
*
* <p>
* 真正使用到的配置类为 {@link love.forte.simbot.component.mirai.configuration.MiraiConfiguration}
*
* @author <a href="https://github.com/ForteScarlet"> ForteScarlet </a>
@@ -93,5 +93,11 @@ private static BotConfiguration getDef() {
/**
* mirai设备信息文件路径。
*/
private String deviceInfoFile = "";
private String deviceInfoFile = "device.json";

/**
* 如果为true,则通过 {@link #deviceInfoFile} 的值进行基于文件的随机设备信息配置。
* 默认为true
*/
private boolean deviceInfoFileBased = true;
}
2 changes: 1 addition & 1 deletion spring-boot-starter/core-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>spring-boot-starter</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>project</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
2 changes: 1 addition & 1 deletion time-task/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>project</artifactId>
<groupId>love.forte.simple-robot</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
</parent>
<modelVersion>4.0.0</modelVersion>

2 changes: 1 addition & 1 deletion time-task/time-task-core/pom.xml
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
<parent>
<artifactId>time-task-parent</artifactId>
<groupId>love.forte.simple-robot.time-task</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
</parent>
<modelVersion>4.0.0</modelVersion>

2 changes: 1 addition & 1 deletion time-task/time-task-quartz/pom.xml
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
<parent>
<artifactId>time-task-parent</artifactId>
<groupId>love.forte.simple-robot.time-task</groupId>
<version>2.4.1<!--v--></version>
<version>2.5.0<!--v--></version>
</parent>
<modelVersion>4.0.0</modelVersion>

0 comments on commit d3c0123

Please sign in to comment.