-
Notifications
You must be signed in to change notification settings - Fork 139
/
ReportService.kt
157 lines (144 loc) · 5.96 KB
/
ReportService.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package tech.mihoyo.mirai.web.http
import io.ktor.client.HttpClient
import io.ktor.client.request.headers
import io.ktor.client.request.request
import io.ktor.client.request.url
import io.ktor.http.ContentType
import io.ktor.http.HttpMethod
import io.ktor.http.content.TextContent
import javafx.application.Application.launch
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.serialization.UnstableDefault
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonException
import net.mamoe.mirai.console.plugins.PluginBase
import net.mamoe.mirai.event.Listener
import net.mamoe.mirai.event.events.BotEvent
import net.mamoe.mirai.event.events.BotOnlineEvent
import net.mamoe.mirai.event.subscribeAlways
import tech.mihoyo.mirai.MiraiApi
import tech.mihoyo.mirai.data.common.CQIgnoreEventDTO
import tech.mihoyo.mirai.data.common.CQMetaEventDTO
import tech.mihoyo.mirai.data.common.toCQDTO
import tech.mihoyo.mirai.util.logger
import tech.mihoyo.mirai.util.toJson
import tech.mihoyo.mirai.web.HttpApiService
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
class ReportService(
/**
* 插件对象
*/
override val console: PluginBase
) : HttpApiService {
private val http = HttpClient()
private val configByBots: MutableMap<Long, ReportServiceConfig> = mutableMapOf()
private val sha1UtilByBot: MutableMap<Long, Mac> = mutableMapOf()
private var subscription: Listener<BotEvent>? = null
override fun onLoad() {
}
override fun onEnable() {
subscription = console.subscribeAlways {
val mirai = MiraiApi(bot)
when (this) {
is BotOnlineEvent -> {
if (!configByBots.containsKey(bot.id)) {
val config = ReportServiceConfig(console.loadConfig("setting.yml"), bot)
configByBots[bot.id] = config
if (config.postUrl != "") {
if (config.secret != "") {
val mac = Mac.getInstance("HmacSHA1")
val secret = SecretKeySpec(config.secret.toByteArray(), "HmacSHA1")
mac.init(secret)
sha1UtilByBot[bot.id] = mac
}
this.toCQDTO(isRawMessage = config.postMessageFormat == "string")
.takeIf { it !is CQIgnoreEventDTO }?.apply {
val eventDTO = this
val jsonToSend = this.toJson()
GlobalScope.launch(Dispatchers.IO) {
report(
mirai,
config.postUrl,
bot.id,
jsonToSend,
config.secret,
eventDTO !is CQMetaEventDTO
)
}
}
}
}
}
else -> {
val event = this
configByBots[bot.id]?.apply {
if (this.postUrl != "") {
val config = this
event.toCQDTO(isRawMessage = config.postMessageFormat == "string")
.takeIf { it !is CQIgnoreEventDTO }?.apply {
val eventDTO = this
val jsonToSend = this.toJson()
GlobalScope.launch(Dispatchers.IO) {
report(
mirai,
config.postUrl,
bot.id,
jsonToSend,
config.secret,
eventDTO !is CQMetaEventDTO
)
}
}
}
}
}
}
}
}
@OptIn(UnstableDefault::class)
private suspend fun report(
miraiApi: MiraiApi,
url: String,
botId: Long,
json: String,
secret: String,
shouldHandleOperation: Boolean
) {
val res = http.request<String?> {
url(url)
headers {
append("User-Agent", "MiraiHttp/0.1.0")
append("X-Self-ID", botId.toString())
secret.takeIf { it != "" }?.apply {
append("X-Signature", getSha1Hash(botId, json))
}
}
method = HttpMethod.Post
body = TextContent(json, ContentType.Application.Json)
}
logger.debug("收到上报响应 $res")
if (shouldHandleOperation && res != null && res != "") {
try {
val respJson = Json.parseJson(res).jsonObject
val sentJson = Json.parseJson(json).jsonObject
val params = hashMapOf("context" to sentJson, "operation" to respJson)
miraiApi.cqHandleQuickOperation(params)
} catch (e: JsonException) {
logger.error("解析HTTP上报返回数据成json失败")
}
}
}
private fun getSha1Hash(botId: Long, content: String): String {
sha1UtilByBot[botId]?.apply {
return this.doFinal(content.toByteArray()).fold("", { str, it -> str + "%02x".format(it) })
}
return ""
}
override fun onDisable() {
subscription?.complete()
}
}