Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
ad43a92b
Commit
ad43a92b
authored
Oct 13, 2019
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use PacketHandler.Key
parent
d89f8acc
Changes
16
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
215 additions
and
132 deletions
+215
-132
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt
+14
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt
...re/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt
+2
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/QQ.kt
...-core/src/commonMain/kotlin/net.mamoe.mirai/contact/QQ.kt
+2
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/internal/MessageDataInternal.kt
...n/net.mamoe.mirai/message/internal/MessageDataInternal.kt
+21
-22
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt
...nMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt
+5
-13
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt
....mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt
+21
-31
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/ActionPacketHandler.kt
...mirai/network/protocol/tim/handler/ActionPacketHandler.kt
+2
-0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/EventPacketHandler.kt
....mirai/network/protocol/tim/handler/EventPacketHandler.kt
+13
-0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/PacketHandler.kt
...mamoe.mirai/network/protocol/tim/handler/PacketHandler.kt
+9
-4
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/ServerEvent.kt
...et.mamoe.mirai/network/protocol/tim/packet/ServerEvent.kt
+80
-32
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/ClientSendFriendMessagePacket.kt
...otocol/tim/packet/action/ClientSendFriendMessagePacket.kt
+4
-4
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/ByteReadPacketUtil.kt
...onMain/kotlin/net.mamoe.mirai/utils/ByteReadPacketUtil.kt
+10
-10
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/DebugUtil.kt
.../src/commonMain/kotlin/net.mamoe.mirai/utils/DebugUtil.kt
+10
-3
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/SocketBridge.kt
...c/commonMain/kotlin/net.mamoe.mirai/utils/SocketBridge.kt
+3
-1
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/SocketBridgeJvm.kt
...c/jvmMain/kotlin/net/mamoe/mirai/utils/SocketBridgeJvm.kt
+12
-2
mirai-debug/src/main/java/HexComparator.kt
mirai-debug/src/main/java/HexComparator.kt
+7
-7
No files found.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/Bot.kt
View file @
ad43a92b
...
...
@@ -7,8 +7,10 @@ import net.mamoe.mirai.contact.Group
import
net.mamoe.mirai.contact.QQ
import
net.mamoe.mirai.network.BotNetworkHandler
import
net.mamoe.mirai.network.protocol.tim.TIMBotNetworkHandler
import
net.mamoe.mirai.network.protocol.tim.packet.login.LoginResult
import
net.mamoe.mirai.utils.BotAccount
import
net.mamoe.mirai.utils.ContactList
import
net.mamoe.mirai.utils.LoginConfiguration
import
net.mamoe.mirai.utils.MiraiLogger
/**
...
...
@@ -44,7 +46,7 @@ class Bot(val account: BotAccount, val logger: MiraiLogger) {
val
contacts
=
ContactSystem
()
va
l
network
:
BotNetworkHandler
<
*
>
=
TIMBotNetworkHandler
(
this
)
va
r
network
:
BotNetworkHandler
<
*
>
=
TIMBotNetworkHandler
(
this
)
init
{
instances
.
add
(
this
)
...
...
@@ -54,6 +56,17 @@ class Bot(val account: BotAccount, val logger: MiraiLogger) {
override
fun
toString
():
String
=
"Bot{id=$id,qq=${account.qqNumber}}"
/**
* [关闭][BotNetworkHandler.close]网络处理器, 取消所有运行在 [BotNetworkHandler.NetworkScope] 下的协程.
* 然后重新启动并尝试登录
*/
suspend
fun
reinitializeNetworkHandler
(
configuration
:
LoginConfiguration
):
LoginResult
{
logger
.
logPurple
(
"Reinitializing BotNetworkHandler"
)
network
.
close
()
network
=
TIMBotNetworkHandler
(
this
)
return
network
.
login
(
configuration
)
}
/**
* Bot 联系人管理.
*
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Group.kt
View file @
ad43a92b
...
...
@@ -3,6 +3,7 @@ package net.mamoe.mirai.contact
import
net.mamoe.mirai.Bot
import
net.mamoe.mirai.contact.Group.Companion.groupNumberToId
import
net.mamoe.mirai.message.MessageChain
import
net.mamoe.mirai.network.protocol.tim.handler.EventPacketHandler
import
net.mamoe.mirai.utils.ContactList
import
kotlin.jvm.JvmStatic
...
...
@@ -28,7 +29,7 @@ class Group(bot: Bot, number: Long) : Contact(bot, number) {
get
()
=
throw
UnsupportedOperationException
(
"Not yet supported"
)
override
suspend
fun
sendMessage
(
message
:
MessageChain
)
{
bot
.
network
.
event
.
sendGroupMessage
(
this
,
message
)
bot
.
network
[
EventPacketHandler
]
.
sendGroupMessage
(
this
,
message
)
}
override
suspend
fun
sendXMLMessage
(
message
:
String
)
{
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/QQ.kt
View file @
ad43a92b
...
...
@@ -4,6 +4,7 @@ import net.mamoe.mirai.Bot
import
net.mamoe.mirai.message.At
import
net.mamoe.mirai.message.Message
import
net.mamoe.mirai.message.MessageChain
import
net.mamoe.mirai.network.protocol.tim.handler.EventPacketHandler
/**
* QQ 账号.
...
...
@@ -19,7 +20,7 @@ import net.mamoe.mirai.message.MessageChain
*/
class
QQ
(
bot
:
Bot
,
number
:
Long
)
:
Contact
(
bot
,
number
)
{
override
suspend
fun
sendMessage
(
message
:
MessageChain
)
{
bot
.
network
.
event
.
sendFriendMessage
(
this
,
message
)
bot
.
network
[
EventPacketHandler
]
.
sendFriendMessage
(
this
,
message
)
}
override
suspend
fun
sendXMLMessage
(
message
:
String
)
{
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/internal/MessageDataInternal.kt
View file @
ad43a92b
...
...
@@ -6,7 +6,7 @@ import kotlinx.io.core.*
import
net.mamoe.mirai.message.*
import
net.mamoe.mirai.utils.*
internal
fun
ByteArray
.
parseMessageFace
():
Face
=
read
{
internal
fun
IoBuffer
.
parseMessageFace
():
Face
{
//00 01 AF 0B 00 08 00 01 00 04 52 CC F5 D0 FF 00 02 14 F0
//00 01 0C 0B 00 08 00 01 00 04 52 CC F5 D0 FF 00 02 14 4D
discardExact
(
1
)
...
...
@@ -14,30 +14,30 @@ internal fun ByteArray.parseMessageFace(): Face = read {
val
id1
=
FaceID
.
ofId
(
readLVNumber
().
toInt
().
toUByte
())
//可能这个是id, 也可能下面那个
discardExact
(
readByte
().
toLong
())
readLVNumber
()
//某id?
return
@
read
Face
(
id1
)
return
Face
(
id1
)
}
internal
fun
ByteArray
.
parsePlainText
():
PlainText
=
read
{
internal
fun
IoBuffer
.
parsePlainText
():
PlainText
{
discardExact
(
1
)
PlainText
(
readLVString
())
return
PlainText
(
readLVString
())
}
internal
fun
ByteArray
.
parseMessageImage0x06
():
Image
=
read
{
internal
fun
IoBuffer
.
parseMessageImage0x06
():
Image
{
discardExact
(
1
)
MiraiLogger
.
logDebug
(
"好友的图片"
)
MiraiLogger
.
logDebug
(
this
@
parseMessageImage0x06
.
toUHexString
())
this
.
debugPrint
(
"好友的图片"
)
//MiraiLogger.logDebug(this
.toUHexString())
val
filenameLength
=
readShort
()
val
suffix
=
readString
(
filenameLength
).
substringAfter
(
"."
)
discardExact
(
this
@
parseMessageImage0x06
.
size
-
37
-
1
-
filenameLength
-
2
)
discardExact
(
this
@
parseMessageImage0x06
.
readRemaining
-
37
-
1
-
filenameLength
-
2
)
val
imageId
=
readString
(
36
)
MiraiLogger
.
logDebug
(
imageId
)
discardExact
(
1
)
//0x41
return
@
read
Image
(
"{$imageId}.$suffix"
)
return
Image
(
"{$imageId}.$suffix"
)
}
internal
fun
ByteArray
.
parseMessageImage0x03
():
Image
=
read
{
internal
fun
IoBuffer
.
parseMessageImage0x03
():
Image
{
discardExact
(
1
)
return
@
read
Image
(
String
(
readLVByteArray
()))
return
Image
(
String
(
readLVByteArray
()))
/*
println(String(readLVByteArray()))
readTLVMap()
...
...
@@ -53,14 +53,14 @@ internal fun ByteArray.parseMessageImage0x03(): Image = read {
return Image(imageId)*/
}
internal
fun
Byte
Array
.
parseMessageChain
():
MessageChain
=
read
{
readMessageChain
()
internal
fun
Byte
ReadPacket
.
parseMessageChain
():
MessageChain
{
re
turn
re
adMessageChain
()
}
internal
fun
ByteReadPacket
.
readMessage
():
Message
?
{
val
messageType
=
this
.
readByte
().
toInt
()
val
sectionLength
=
this
.
readShort
().
toLong
()
//sectionLength: short
val
sectionData
=
this
.
read
Bytes
(
sectionLength
.
toInt
())
//use buffer instead
val
sectionData
=
this
.
read
IoBuffer
(
sectionLength
.
toInt
())
//use buffer instead
return
when
(
messageType
)
{
0
x01
->
sectionData
.
parsePlainText
()
0
x02
->
sectionData
.
parseMessageFace
()
...
...
@@ -68,7 +68,12 @@ internal fun ByteReadPacket.readMessage(): Message? {
0
x06
->
sectionData
.
parseMessageImage0x06
()
0
x19
->
{
//长文本
0
x19
->
{
//未知, 可能是长文本?
//bot手机自己跟自己发消息会出这个
//sectionData: 01 00 1C AA 02 19 08 00 88 01 00 9A 01 11 78 00 C8 01 00 F0 01 00 F8 01 00 90 02 00 C8 02 00
sectionData
.
readBytes
().
debugPrint
(
"sectionData"
)
return
PlainText
(
"[UNKNOWN(${this.readBytes().toUHexString()})]"
)
println
()
val
value
=
readLVByteArray
()
//todo 未知压缩算法
PlainText
(
String
(
value
))
...
...
@@ -87,7 +92,6 @@ internal fun ByteReadPacket.readMessage(): Message? {
}
0
x0E
->
{
//null
null
}
...
...
@@ -102,16 +106,11 @@ internal fun ByteReadPacket.readMessage(): Message? {
fun
ByteReadPacket
.
readMessageChain
():
MessageChain
{
val
chain
=
MessageChain
()
var
got
:
Message
?
=
null
do
{
if
(
got
!=
null
)
{
chain
.
concat
(
got
)
}
if
(
this
.
remaining
==
0L
)
{
return
chain
}
got
=
this
.
readMessage
()
}
while
(
got
!=
null
)
}
while
(
this
.
readMessage
().
takeIf
{
it
!=
null
}
?.
let
{
chain
.
concat
(
it
)
}
!=
null
)
return
chain
}
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotNetworkHandler.kt
View file @
ad43a92b
...
...
@@ -45,22 +45,14 @@ interface BotNetworkHandler<Socket : DataPacketSocket> : Closeable {
var
socket
:
Socket
/**
* 事件处理. 如发送好友消息, 接受群消息并触发事件
* 得到 [PacketHandler].
* `get(EventPacketHandler)` 返回 [EventPacketHandler]
* `get(ActionPacketHandler)` 返回 [ActionPacketHandler]
*/
val
event
:
EventPacketHandler
operator
fun
<
T
:
PacketHandler
>
get
(
key
:
PacketHandler
.
Key
<
T
>):
T
/**
* 动作处理. 如发送好友请求, 处理别人发来的好友请求等
*/
val
action
:
ActionPacketHandler
/**
* [PacketHandler] 列表
*/
val
packetHandlers
:
PacketHandlerList
/**
* 尝试登录. 将会依次尝试登录到可用的服务器. 在任一服务器登录完成后返回登录结果
* 依次尝试登录到可用的服务器. 在任一服务器登录完成后返回登录结果
*/
suspend
fun
login
(
configuration
:
LoginConfiguration
):
LoginResult
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt
View file @
ad43a92b
...
...
@@ -24,24 +24,19 @@ import net.mamoe.mirai.utils.*
*
* @see BotNetworkHandler
*/
internal
class
TIMBotNetworkHandler
(
private
val
bot
:
Bot
)
:
BotNetworkHandler
<
TIMBotNetworkHandler
.
BotSocket
>
{
internal
class
TIMBotNetworkHandler
(
private
val
bot
:
Bot
)
:
BotNetworkHandler
<
TIMBotNetworkHandler
.
BotSocket
>
,
PacketHandlerList
()
{
override
val
NetworkScope
:
CoroutineScope
=
CoroutineScope
(
Dispatchers
.
Default
)
override
lateinit
var
socket
:
BotSocket
override
lateinit
var
event
:
EventPacketHandler
override
lateinit
var
action
:
ActionPacketHandler
override
val
packetHandlers
:
PacketHandlerList
=
PacketHandlerList
()
internal
val
temporaryPacketHandlers
=
mutableListOf
<
TemporaryPacketHandler
<
*
>>()
private
var
heartbeatJob
:
Job
?
=
null
override
suspend
fun
addHandler
(
temporaryPacketHandler
:
TemporaryPacketHandler
<
*
>)
{
temporaryPacketHandlers
.
add
(
temporaryPacketHandler
)
temporaryPacketHandler
.
send
(
action
.
session
)
temporaryPacketHandler
.
send
(
this
[
ActionPacketHandler
]
.
session
)
}
override
suspend
fun
login
(
configuration
:
LoginConfiguration
):
LoginResult
{
...
...
@@ -68,13 +63,11 @@ internal class TIMBotNetworkHandler(private val bot: Bot) : BotNetworkHandler<TI
//private | internal
private
fun
onLoggedIn
(
sessionKey
:
ByteArray
)
{
require
(
packetHandlers
.
size
==
0
)
{
"Already logged in"
}
require
(
size
==
0
)
{
"Already logged in"
}
val
session
=
LoginSession
(
bot
,
sessionKey
,
socket
,
NetworkScope
)
event
=
EventPacketHandler
(
session
)
action
=
ActionPacketHandler
(
session
)
packetHandlers
.
add
(
event
.
asNode
(
))
packetHandlers
.
add
(
action
.
asNode
(
))
add
(
EventPacketHandler
(
session
).
asNode
(
EventPacketHandler
))
add
(
ActionPacketHandler
(
session
).
asNode
(
ActionPacketHandler
))
}
private
lateinit
var
sessionKey
:
ByteArray
...
...
@@ -89,7 +82,7 @@ internal class TIMBotNetworkHandler(private val bot: Bot) : BotNetworkHandler<TI
this
.
loginResult
.
cancel
(
CancellationException
(
"socket closed"
))
}
this
.
packetHandlers
.
forEach
{
this
.
forEach
{
it
.
instance
.
close
()
}
...
...
@@ -177,21 +170,14 @@ internal class TIMBotNetworkHandler(private val bot: Bot) : BotNetworkHandler<TI
packet
.
use
{
//coz removeIf is not inline
with
(
temporaryPacketHandlers
.
iterator
())
{
while
(
hasNext
())
{
if
(
next
().
onPacketReceived
(
action
.
session
,
packet
))
{
remove
()
}
}
};
temporaryPacketHandlers
.
removeIfInlined
{
it
.
onPacketReceived
(
this
@TIMBotNetworkHandler
[
ActionPacketHandler
].
session
,
packet
)
}
//For logDebug
{
val
name
=
packet
::
class
.
simpleName
if
(
name
!=
null
&&
!
name
.
endsWith
(
"Encrypted"
)
&&
!
name
.
endsWith
(
"Raw"
))
{
bot
.
cyan
(
"Packet received: $packet"
)
}
}()
val
name
=
packet
::
class
.
simpleName
if
(
name
!=
null
&&
!
name
.
endsWith
(
"Encrypted"
)
&&
!
name
.
endsWith
(
"Raw"
))
{
bot
.
cyan
(
"Packet received: $packet"
)
}
if
(
packet
is
ServerEventPacket
)
{
//no need to sync acknowledgement packets
...
...
@@ -205,7 +191,7 @@ internal class TIMBotNetworkHandler(private val bot: Bot) : BotNetworkHandler<TI
}
loginHandler
.
onPacketReceived
(
packet
)
packetHandlers
.
forEach
{
this
@TIMBotNetworkHandler
.
forEach
{
it
.
instance
.
onPacketReceived
(
packet
)
}
}
...
...
@@ -223,7 +209,11 @@ internal class TIMBotNetworkHandler(private val bot: Bot) : BotNetworkHandler<TI
try
{
build
.
readAvailable
(
buffer
)
channel
.
send
(
buffer
)
//JVM: withContext(IO)
}
catch
(
e
:
Exception
)
{
}
catch
(
e
:
SendPacketInternalException
)
{
bot
.
logger
.
logError
(
"Caught SendPacketInternalException: ${e.cause?.message}"
)
bot
.
reinitializeNetworkHandler
(
configuration
)
return
@
withContext
}
catch
(
e
:
Throwable
)
{
e
.
log
()
return
@
withContext
}
finally
{
...
...
@@ -379,7 +369,7 @@ internal class TIMBotNetworkHandler(private val bot: Bot) : BotNetworkHandler<TI
//登录成功后会收到大量上次的消息, 忽略掉 todo 优化
NetworkScope
.
launch
{
delay
(
3000
)
event
.
ignoreMessage
=
false
this
@TIMBotNetworkHandler
[
EventPacketHandler
]
.
ignoreMessage
=
false
}
onLoggedIn
(
sessionKey
)
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/ActionPacketHandler.kt
View file @
ad43a92b
...
...
@@ -23,6 +23,8 @@ import net.mamoe.mirai.utils.hexToBytes
* @author Him188moe
*/
class
ActionPacketHandler
(
session
:
LoginSession
)
:
PacketHandler
(
session
)
{
companion
object
Key
:
PacketHandler
.
Key
<
ActionPacketHandler
>
private
val
addFriendSessions
=
mutableListOf
<
AddFriendSession
>()
private
val
uploadImageSessions
=
mutableListOf
<
UploadImageSession
>()
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/EventPacketHandler.kt
View file @
ad43a92b
...
...
@@ -24,6 +24,9 @@ import net.mamoe.mirai.utils.MiraiLogger
*/
@Suppress
(
"EXPERIMENTAL_API_USAGE"
)
class
EventPacketHandler
(
session
:
LoginSession
)
:
PacketHandler
(
session
)
{
companion
object
Key
:
PacketHandler
.
Key
<
EventPacketHandler
>
internal
var
ignoreMessage
:
Boolean
=
true
override
suspend
fun
onPacketReceived
(
packet
:
ServerPacket
):
Unit
=
with
(
session
)
{
...
...
@@ -57,6 +60,16 @@ class EventPacketHandler(session: LoginSession) : PacketHandler(session) {
//TODO
}
is
ServerFriendTypingStartedPacket
->
{
MiraiLogger
.
logInfo
(
"${packet.qq.toLong()} 正在输入"
)
//TODO
}
is
ServerFriendTypingCanceledPacket
->
{
MiraiLogger
.
logInfo
(
"${packet.qq.toLong()} 取消了输入"
)
//TODO
}
else
->
{
//ignored
}
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/PacketHandler.kt
View file @
ad43a92b
...
...
@@ -12,6 +12,8 @@ abstract class PacketHandler(
)
{
abstract
suspend
fun
onPacketReceived
(
packet
:
ServerPacket
)
interface
Key
<
T
:
PacketHandler
>
open
fun
close
()
{
}
...
...
@@ -19,15 +21,18 @@ abstract class PacketHandler(
class
PacketHandlerNode
<
T
:
PacketHandler
>(
val
clazz
:
KClass
<
T
>,
val
instance
:
T
val
instance
:
T
,
val
key
:
PacketHandler
.
Key
<
T
>
)
fun
<
T
:
PacketHandler
>
T
.
asNode
():
PacketHandlerNode
<
T
>
{
fun
<
T
:
PacketHandler
>
T
.
asNode
(
key
:
PacketHandler
.
Key
<
T
>
):
PacketHandlerNode
<
T
>
{
@Suppress
(
"UNCHECKED_CAST"
)
return
PacketHandlerNode
(
this
::
class
as
KClass
<
T
>,
this
)
return
PacketHandlerNode
(
this
::
class
as
KClass
<
T
>,
this
,
key
)
}
class
PacketHandlerList
:
MutableList
<
PacketHandlerNode
<
*
>>
by
mutableListOf
()
{
open
class
PacketHandlerList
:
MutableList
<
PacketHandlerNode
<
*
>>
by
mutableListOf
()
{
@Suppress
(
"UNCHECKED_CAST"
)
operator
fun
<
T
:
PacketHandler
>
get
(
key
:
PacketHandler
.
Key
<
T
>):
T
=
this
.
first
{
it
.
key
===
key
}.
instance
as
T
operator
fun
<
T
:
PacketHandler
>
get
(
clazz
:
KClass
<
T
>):
T
{
this
.
forEach
{
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/ServerEvent.kt
View file @
ad43a92b
This diff is collapsed.
Click to expand it.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/ClientSendFriendMessagePacket.kt
View file @
ad43a92b
...
...
@@ -18,12 +18,12 @@ class ClientSendFriendMessagePacket(
private
val
message
:
MessageChain
)
:
ClientPacket
()
{
override
fun
encode
(
builder
:
BytePacketBuilder
)
=
with
(
builder
)
{
this
.
writeRandom
(
2
)
writeRandom
(
2
)
this
.
writeQQ
(
botQQ
)
this
.
writeHex
(
TIMProtocol
.
fixVer2
)
writeQQ
(
botQQ
)
writeHex
(
TIMProtocol
.
fixVer2
)
this
.
encryptAndWrite
(
sessionKey
)
{
encryptAndWrite
(
sessionKey
)
{
writeQQ
(
botQQ
)
writeQQ
(
targetQQ
)
writeHex
(
"00 00 00 08 00 01 00 04 00 00 00 00"
)
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/ByteReadPacketUtil.kt
View file @
ad43a92b
...
...
@@ -86,7 +86,7 @@ fun ByteReadPacket.parseServerPacket(size: Int): ServerPacket {//TODO 优化
"00 BA"
->
ServerCaptchaPacket
.
Encrypted
(
this
,
idHex
)
"00 CE"
,
"00 17"
->
ServerEventPacket
.
Raw
.
Encrypted
(
this
,
idHex
.
hexToBytes
()
)
"00 CE"
,
"00 17"
->
ServerEventPacket
.
Raw
.
Encrypted
(
this
)
"00 81"
->
ServerFieldOnlineStatusChangedPacket
.
Encrypted
(
this
)
...
...
@@ -102,7 +102,7 @@ fun ByteReadPacket.parseServerPacket(size: Int): ServerPacket {//TODO 优化
}.
setId
(
idHex
)
}
fun
ByteReadPacke
t
.
readIP
():
String
=
buildString
(
4
+
3
)
{
fun
Inpu
t
.
readIP
():
String
=
buildString
(
4
+
3
)
{
repeat
(
4
)
{
val
byte
=
readUByte
()
this
.
append
(
byte
.
toString
())
...
...
@@ -110,11 +110,11 @@ fun ByteReadPacket.readIP(): String = buildString(4 + 3) {
}
}
fun
ByteReadPacke
t
.
readLVString
():
String
=
String
(
this
.
readLVByteArray
())
fun
Inpu
t
.
readLVString
():
String
=
String
(
this
.
readLVByteArray
())
fun
ByteReadPacke
t
.
readLVByteArray
():
ByteArray
=
this
.
readBytes
(
this
.
readShort
().
toInt
())
fun
Inpu
t
.
readLVByteArray
():
ByteArray
=
this
.
readBytes
(
this
.
readShort
().
toInt
())
fun
ByteReadPacke
t
.
readTLVMap
(
expectingEOF
:
Boolean
=
false
):
Map
<
Int
,
ByteArray
>
{
fun
Inpu
t
.
readTLVMap
(
expectingEOF
:
Boolean
=
false
):
Map
<
Int
,
ByteArray
>
{
val
map
=
mutableMapOf
<
Int
,
ByteArray
>()
var
type
:
UByte
...
...
@@ -144,11 +144,11 @@ fun ByteReadPacket.readTLVMap(expectingEOF: Boolean = false): Map<Int, ByteArray
fun
Map
<
Int
,
ByteArray
>.
printTLVMap
(
name
:
String
)
=
debugPrintln
(
"TLVMap $name= "
+
this
.
mapValues
{
(
_
,
value
)
->
value
.
toUHexString
()
})
fun
ByteReadPacke
t
.
readString
(
length
:
Number
):
String
=
String
(
this
.
readBytes
(
length
.
toInt
()))
fun
Inpu
t
.
readString
(
length
:
Number
):
String
=
String
(
this
.
readBytes
(
length
.
toInt
()))
private
const
val
TRUE_BYTE_VALUE
:
Byte
=
1
fun
ByteReadPacke
t
.
readBoolean
():
Boolean
=
this
.
readByte
()
==
TRUE_BYTE_VALUE
fun
ByteReadPacke
t
.
readLVNumber
():
Number
{
fun
Inpu
t
.
readBoolean
():
Boolean
=
this
.
readByte
()
==
TRUE_BYTE_VALUE
fun
Inpu
t
.
readLVNumber
():
Number
{
return
when
(
this
.
readShort
().
toInt
())
{
1
->
this
.
readByte
()
2
->
this
.
readShort
()
...
...
@@ -161,7 +161,7 @@ fun ByteReadPacket.readLVNumber(): Number {
//添加@JvmSynthetic 导致 idea 无法检查这个文件的错误
//@JvmSynthetic
@Deprecated
(
"Low efficiency"
,
ReplaceWith
(
""
))
fun
ByteReadPacket
.
gotoWhere
(
matcher
:
UByteArray
):
ByteReadPacket
{
fun
<
I
:
Input
>
I
.
gotoWhere
(
matcher
:
UByteArray
):
I
{
return
this
.
gotoWhere
(
matcher
.
toByteArray
())
}
...
...
@@ -169,7 +169,7 @@ fun ByteReadPacket.gotoWhere(matcher: UByteArray): ByteReadPacket {
* 去往下一个含这些连续字节的位置
*/
@Deprecated
(
"Low efficiency"
,
ReplaceWith
(
""
))
fun
ByteReadPacket
.
gotoWhere
(
matcher
:
ByteArray
):
ByteReadPacket
{
fun
<
I
:
Input
>
I
.
gotoWhere
(
matcher
:
ByteArray
):
I
{
require
(
matcher
.
isNotEmpty
())
loop
@
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/DebugUtil.kt
View file @
ad43a92b
package
net.mamoe.mirai.utils
import
kotlinx.io.core.ByteReadPacket
import
kotlinx.io.core.Input
import
kotlinx.io.core.IoBuffer
import
kotlinx.io.core.readBytes
...
...
@@ -9,20 +10,26 @@ internal object DebugLogger : MiraiLogger by PlatformLogger("Packet Debug")
internal
fun
debugPrintln
(
any
:
Any
?)
=
DebugLogger
.
logPurple
(
any
)
@Deprecated
(
"Debugging Warning"
,
ReplaceWith
(
""
))
internal
fun
ByteArray
.
debugPrint
(
name
:
String
):
ByteArray
{
DebugLogger
.
logPurple
(
name
+
"="
+
this
.
toUHexString
())
return
this
}
@Deprecated
(
"
Debugging Warning
"
,
ReplaceWith
(
""
))
@Deprecated
(
"
Low Efficiency
"
,
ReplaceWith
(
""
))
internal
fun
IoBuffer
.
debugPrint
(
name
:
String
):
IoBuffer
{
val
readBytes
=
this
.
readBytes
()
DebugLogger
.
logPurple
(
name
+
"="
+
readBytes
.
toUHexString
())
return
readBytes
.
toIoBuffer
()
}
@Deprecated
(
"
Debugging Warning"
,
ReplaceWith
(
"
"
))
@Deprecated
(
"
Low Efficiency"
,
ReplaceWith
(
"discardExact(n)
"
))
internal
fun
Input
.
debugDiscardExact
(
n
:
Number
,
name
:
String
=
""
)
{
DebugLogger
.
logPurple
(
"Discarded($n) $name="
+
this
.
readBytes
(
n
.
toInt
()).
toUHexString
())
}
@Deprecated
(
"Low Efficiency"
,
ReplaceWith
(
""
))
internal
fun
ByteReadPacket
.
debugPrint
(
name
:
String
=
""
):
ByteReadPacket
{
val
bytes
=
this
.
readBytes
()
DebugLogger
.
logPurple
(
"ByteReadPacket $name="
+
bytes
.
toUHexString
())
return
bytes
.
toReadPacket
()
}
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/SocketBridge.kt
View file @
ad43a92b
...
...
@@ -11,4 +11,6 @@ expect class MiraiDatagramChannel(serverHost: String, serverPort: Short) : Close
val
isOpen
:
Boolean
}
expect
class
ClosedChannelException
:
IOException
\ No newline at end of file
expect
class
ClosedChannelException
:
IOException
expect
class
SendPacketInternalException
(
cause
:
Throwable
?)
:
Exception
\ No newline at end of file
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/SocketBridgeJvm.kt
View file @
ad43a92b
...
...
@@ -14,7 +14,15 @@ actual class MiraiDatagramChannel actual constructor(serverHost: String, serverP
private
val
channel
:
DatagramChannel
=
DatagramChannel
.
open
().
connect
(
serverAddress
)
actual
suspend
fun
read
(
buffer
:
IoBuffer
)
=
withContext
(
Dispatchers
.
IO
)
{
(
channel
as
ReadableByteChannel
).
read
(
buffer
)
}
actual
suspend
fun
send
(
buffer
:
IoBuffer
)
=
withContext
(
Dispatchers
.
IO
)
{
buffer
.
readDirect
{
channel
.
send
(
it
,
serverAddress
)
}
}
actual
suspend
fun
send
(
buffer
:
IoBuffer
)
=
withContext
(
Dispatchers
.
IO
)
{
buffer
.
readDirect
{
try
{
channel
.
send
(
it
,
serverAddress
)
}
catch
(
e
:
Exception
)
{
throw
SendPacketInternalException
(
e
)
}
}
}
override
fun
close
()
{
channel
.
close
()
...
...
@@ -23,4 +31,6 @@ actual class MiraiDatagramChannel actual constructor(serverHost: String, serverP
actual
val
isOpen
:
Boolean
get
()
=
channel
.
isOpen
}
actual
typealias
ClosedChannelException
=
java
.
nio
.
channels
.
ClosedChannelException
\ No newline at end of file
actual
typealias
ClosedChannelException
=
java
.
nio
.
channels
.
ClosedChannelException
actual
class
SendPacketInternalException
actual
constructor
(
cause
:
Throwable
?)
:
Exception
(
cause
)
\ No newline at end of file
mirai-debug/src/main/java/HexComparator.kt
View file @
ad43a92b
...
...
@@ -20,14 +20,10 @@ import kotlin.math.max
object
HexComparator
{
private
val
RED
=
"\u001b[31m"
private
val
GREEN
=
"\u001b[33m"
private
val
UNKNOWN
=
"\u001b[30m"
private
val
BLUE
=
"\u001b[34m"
private
val
clipboardString
:
String
?
get
()
{
val
trans
=
Toolkit
.
getDefaultToolkit
().
systemClipboard
.
getContents
(
null
)
...
...
@@ -44,7 +40,6 @@ object HexComparator {
}
class
ConstMatcher
constructor
(
hex
:
String
)
{
private
val
matches
=
LinkedList
<
Match
>()
object
TestConsts
{
...
...
@@ -59,6 +54,7 @@ object HexComparator {
val
_Him188moe_
=
"Him188moe"
.
toByteArray
().
toUHexString
()
val
发图片 = "发图片".
toByteArray
().
toUHexString
()
val
群 = "群".
toByteArray
().
toUHexString
()
val
你好 = "你好".
toByteArray
().
toUHexString
()
val
SINGLE_PLAIN_MESSAGE_HEAD
=
"00 00 01 00 09 01"
...
...
@@ -69,7 +65,8 @@ object HexComparator {
@Suppress
(
"SpellCheckingInspection"
)
object
PacketIds
{
val
heartbeat
=
"00 58"
val
friendmsg
=
"00 CD"
val
friendmsgsend
=
"00 CD"
val
friendmsgevent
=
"00 CE"
}
init
{
...
...
@@ -104,7 +101,6 @@ object HexComparator {
}
}
private
fun
match
(
hex
:
String
,
field
:
Field
):
List
<
IntRange
>
{
val
constValue
:
String
try
{
...
...
@@ -293,6 +289,10 @@ object HexComparator {
}
fun
main
()
{
// println(HexComparator.colorize("00 00 00 25 00 08 00 02 00 01 00 09 00 06 00 01 00 00 00 01 00 0A 00 04 01 00 00 00 00 01 00 04 00 00 00 00 00 03 00 01 01 38 03 3E 03 3F A2 76 E4 B8 DD E7 86 74 F2 64 55 AD 9A EB 2F B9 DF F1 7F 8C 28 00 0B 78 14 5D A2 F5 CB 01 1D 00 00 00 00 01 00 00 00 01 4D 53 47 00 00 00 00 00 5D A2 F5 CA 9D 26 CB 5E 00 00 00 00 0C 00 86 22 00 0C E5 BE AE E8 BD AF E9 9B 85 E9 BB 91 00 00 01 00 09 01 00 06 E4 BD A0 E5 A5 BD 0E 00 07 01 00 04 00 00 00 09 19 00 18 01 00 15 AA 02 12 9A 01 0F 80 01 01 C8 01 00 F0 01 00 F8 01 00 90 02 00"))
val
scanner
=
Scanner
(
System
.
`in`
)
while
(
true
)
{
println
(
"Hex1: "
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment