Commit ef98f22d authored by Him188moe's avatar Him188moe

Updated network

parent 4a319bf5
package net.mamoe.mirai;
import net.mamoe.mirai.utils.BotAccount;
import java.io.Closeable;
/**
* @author Him188moe
*/
public interface Bot extends Closeable {
BotAccount getAccount();
// TODO: 2019/9/13 add more
}
......@@ -17,6 +17,13 @@
</parent>
<dependencies>
<dependency>
<groupId>net.mamoe</groupId>
<artifactId>mirai-api</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
......
......@@ -3,7 +3,7 @@ package net.mamoe.mirai;
import lombok.Getter;
import net.mamoe.mirai.contact.Group;
import net.mamoe.mirai.contact.QQ;
import net.mamoe.mirai.network.BotNetworkHandler;
import net.mamoe.mirai.network.BotNetworkHandlerImpl;
import net.mamoe.mirai.utils.BotAccount;
import net.mamoe.mirai.utils.ContactList;
import net.mamoe.mirai.utils.config.MiraiConfigSection;
......@@ -19,7 +19,7 @@ import java.util.concurrent.atomic.AtomicInteger;
* <br>
* {@link Bot} 由 3 个模块组成.
* {@linkplain ContactSystem 联系人管理}: 可通过 {@link Bot#contacts} 访问
* {@linkplain BotNetworkHandler 网络处理器}: 可通过 {@link Bot#network} 访问
* {@linkplain BotNetworkHandlerImpl 网络处理器}: 可通过 {@link Bot#network} 访问
* {@linkplain BotAccount 机器人账号信息}: 可通过 {@link Bot#account} 访问
* <br>
* 若你需要得到机器人的 QQ 账号, 请访问 {@link Bot#account}
......@@ -29,7 +29,7 @@ import java.util.concurrent.atomic.AtomicInteger;
* Bot that is the base of the whole program.
* It consists of
* a {@link ContactSystem}, which manage contacts such as {@link QQ} and {@link Group};
* a {@link BotNetworkHandler}, which manages the connection to the server;
* a {@link BotNetworkHandlerImpl}, which manages the connection to the server;
* a {@link BotAccount}, which stores the account information(e.g. qq number the bot)
* <br>
* To get all the QQ contacts, access {@link Bot#account}
......@@ -53,7 +53,7 @@ public final class Bot implements Closeable {
public final ContactSystem contacts = new ContactSystem();
public final BotNetworkHandler network;
public final BotNetworkHandlerImpl network;
@Override
public String toString() {
......@@ -118,7 +118,7 @@ public final class Bot implements Closeable {
Objects.requireNonNull(owners);
this.account = account;
this.owners = Collections.unmodifiableList(owners);
this.network = new BotNetworkHandler(this);
this.network = new BotNetworkHandlerImpl(this);
}
......
......@@ -26,7 +26,7 @@ class Group(bot: Bot, number: Long) : Contact(bot, number), Closeable {
val members = ContactList<QQ>()
override fun sendMessage(message: MessageChain) {
bot.network.messageHandler.sendGroupMessage(this, message)
bot.network.message.sendGroupMessage(this, message)
}
override fun sendXMLMessage(message: String) {
......
......@@ -19,7 +19,7 @@ import net.mamoe.mirai.message.defaults.MessageChain
*/
class QQ(bot: Bot, number: Long) : Contact(bot, number) {
override fun sendMessage(message: MessageChain) {
bot.network.messageHandler.sendFriendMessage(this, message)
bot.network.message.sendFriendMessage(this, message)
}
override fun sendXMLMessage(message: String) {
......
......@@ -5,7 +5,8 @@ import net.mamoe.mirai.network.handler.DataPacketSocket
import net.mamoe.mirai.utils.getGTK
/**
* 一次会话. 当登录完成后, 客户端会拿到 sessionKey. 此时建立 session, 开始处理消息等事务
* 登录会话. 当登录完成后, 客户端会拿到 sessionKey.
* 此时建立 session, 然后开始处理事务.
*
* @author Him188moe
*/
......
......@@ -12,9 +12,7 @@ import net.mamoe.mirai.network.packet.image.ServerTryUploadGroupImageSuccessPack
import net.mamoe.mirai.network.packet.login.ClientChangeOnlineStatusPacket
import net.mamoe.mirai.task.MiraiThreadPool
import net.mamoe.mirai.utils.ClientLoginStatus
import net.mamoe.mirai.utils.ImageNetworkUtils
import net.mamoe.mirai.utils.getGTK
import net.mamoe.mirai.utils.toUHexString
import java.awt.image.BufferedImage
import java.io.Closeable
import java.util.*
......@@ -26,8 +24,10 @@ import java.util.function.Supplier
/**
* 动作: 获取好友列表, 点赞, 踢人等.
* 处理动作事件, 承担动作任务.
*
* @author Him188moe
*/
class ActionHandler(session: LoginSession) : PacketHandler(session) {
class ActionPacketHandler(session: LoginSession) : PacketHandler(session) {
private val addFriendSessions = Collections.synchronizedCollection(mutableListOf<AddFriendSession>())
private val uploadImageSessions = Collections.synchronizedCollection(mutableListOf<UploadImageSession>())
......@@ -42,7 +42,7 @@ class ActionHandler(session: LoginSession) : PacketHandler(session) {
}
}
is ServerTryUploadGroupImageSuccessPacket -> {
ImageNetworkUtils.postImage(packet.uKey.toUHexString(), )
// ImageNetworkUtils.postImage(packet.uKey.toUHexString(), )
}
is ServerTryUploadGroupImageFailedPacket -> {
......@@ -51,6 +51,7 @@ class ActionHandler(session: LoginSession) : PacketHandler(session) {
is ServerTryUploadGroupImageResponsePacket.Encrypted -> session.socket.distributePacket(packet.decrypt(session.sessionKey))
is ServerAccountInfoResponsePacket.Encrypted -> session.socket.distributePacket(packet.decrypt(session.sessionKey))
is ServerAccountInfoResponsePacket -> {
}
......@@ -67,6 +68,9 @@ class ActionHandler(session: LoginSession) : PacketHandler(session) {
session.gtk = getGTK(session.sKey)
}
is ServerEventPacket.Raw.Encrypted -> session.socket.distributePacket(packet.decrypt(session.sessionKey))
is ServerEventPacket.Raw -> session.socket.distributePacket(packet.distribute())
else -> {
}
}
......@@ -82,7 +86,7 @@ class ActionHandler(session: LoginSession) : PacketHandler(session) {
fun addFriend(qqNumber: Long, message: Lazy<String> = lazyOf("")): CompletableFuture<AddFriendResult> {
val future = CompletableFuture<AddFriendResult>()
val session = AddFriendSession(qqNumber, future, message)
uploadImageSessions.add(session)
// uploadImageSessions.add(session)
session.sendAddRequest();
return future
}
......@@ -138,7 +142,7 @@ class ActionHandler(session: LoginSession) : PacketHandler(session) {
}
ServerCanAddFriendResponsePacket.State.REQUIRE_VERIFICATION -> {
session.socket.sendPacket(ClientAddFriendPacket(session.bot.account.qqNumber, qq, session.sessionKey))
// session.socket.sendPacket(ClientAddFriendPacket(session.bot.account.qqNumber, qq, session.sessionKey))
}
ServerCanAddFriendResponsePacket.State.NOT_REQUIRE_VERIFICATION -> {
......@@ -210,7 +214,7 @@ class ActionHandler(session: LoginSession) : PacketHandler(session) {
}
override fun close() {
uploadImageSessions.remove(this)
// uploadImageSessions.remove(this)
}
}
}
\ No newline at end of file
package net.mamoe.mirai.network.handler
import net.mamoe.mirai.network.BotNetworkHandlerImpl
import net.mamoe.mirai.network.packet.ClientPacket
import net.mamoe.mirai.network.packet.ServerPacket
import java.io.Closeable
/**
* 网络接口.
* 发包 / 处理包.
* 仅可通过 [BotNetworkHandlerImpl.socket] 得到实例.
*
* @author Him188moe
*/
interface DataPacketSocket : Closeable {
......
......@@ -8,13 +8,15 @@ import net.mamoe.mirai.utils.notice
/**
* Kind of [PacketHandler] that prints all packets received in the format of hex byte array.
*
* @author Him188moe
*/
sealed class DebugHandler(session: LoginSession) : PacketHandler(session) {
class DebugPacketHandler(session: LoginSession) : PacketHandler(session) {
@ExperimentalUnsignedTypes
override fun onPacketReceived(packet: ServerPacket) {
if (!packet.javaClass.name.endsWith("Encrypted") && !packet.javaClass.name.endsWith("Raw")) {
session.bot notice "Packet received: $packet"
session.bot.notice("Packet received: $packet")
}
if (packet is ServerEventPacket) {
......
......@@ -14,8 +14,11 @@ import net.mamoe.mirai.network.packet.action.ServerSendGroupMessageResponsePacke
/**
* 处理消息事件, 承担消息发送任务.
*
* @author Him188moe
*/
class MessageHandler(session: LoginSession) : PacketHandler(session) {
@Suppress("EXPERIMENTAL_API_USAGE")
class MessagePacketHandler(session: LoginSession) : PacketHandler(session) {
internal var ignoreMessage: Boolean = true
init {
......@@ -67,7 +70,6 @@ class MessageHandler(session: LoginSession) : PacketHandler(session) {
}
}
@ExperimentalUnsignedTypes
fun sendFriendMessage(qq: QQ, message: MessageChain) {
session.socket.sendPacket(ClientSendFriendMessagePacket(session.bot.account.qqNumber, qq.number, session.sessionKey, message))
}
......
......@@ -24,8 +24,8 @@ class PacketHandlerNode<T : PacketHandler>(
val instance: T
)
infix fun PacketHandler.to(handler: PacketHandler): PacketHandlerNode<PacketHandler> {
return PacketHandlerNode(handler.javaClass, handler)
fun PacketHandler.asNode(): PacketHandlerNode<PacketHandler> {
return PacketHandlerNode(this.javaClass, this)
}
class PacketHandlerList : LinkedList<PacketHandlerNode<*>>() {
......
......@@ -119,7 +119,7 @@ class ServerGroupMessageEventPacket(input: DataInputStream, packetId: ByteArray,
0x19 -> MessageType.ANONYMOUS
else -> {
MiraiLogger debug ("ServerGroupMessageEventPacket id=$id")
MiraiLogger.debug("ServerGroupMessageEventPacket id=$id")
MessageType.OTHER
}
}
......
......@@ -64,6 +64,7 @@ class ClientSessionRequestPacket(
/**
* @author Him188moe
*/
@PacketId("08 28 04 34")
class ServerSessionKeyResponsePacket(inputStream: DataInputStream, private val dataLength: Int) : ServerPacket(inputStream) {
lateinit var sessionKey: ByteArray
lateinit var tlv0105: ByteArray
......
package net.mamoe.mirai.network.packet
import net.mamoe.mirai.network.Protocol
import net.mamoe.mirai.utils.*
import net.mamoe.mirai.utils.TEA
import net.mamoe.mirai.utils.TestedSuccessfully
import net.mamoe.mirai.utils.hexToBytes
import java.io.DataInputStream
/**
......@@ -18,9 +20,6 @@ class ClientVerificationCodeTransmissionRequestPacket(
) : ClientPacket() {
@TestedSuccessfully
override fun encode() {
MiraiLogger debug "packetId=$packetId"
MiraiLogger debug "verificationSequence=$verificationSequence"
this.writeByte(packetId)//part of packet id
this.writeQQ(qq)
......@@ -90,34 +89,6 @@ class ClientVerificationCodeSubmitPacket(
}
}
@ExperimentalUnsignedTypes
fun main() {
val token0825 = "6E AF F9 2C 20 2B DE 21 B6 13 6F 26 43 F4 04 7B 1F 88 08 4E 8E BE E5 D1 3F E7 93 DE DD E0 6E 38 65 C7 C7 D3 20 7D AC 73 AD F9 85 F9 CC 2A 2C 26 C6 B1 5B FD 34 3F D4 F2".hexToBytes()
val verificationCode = "AAAA"
val verificationToken = "84 2D 1D 9D 07 04 34 80 17 9E 3F 58 02 20 9A 1C 22 D0 73 7D 8A 90 1B 2F F8 E6 79 A6 84 2F 98 F5 1E 66 3D 9A 24 59 18 34 42 BD 45 DA E1 22 2D BC 2D 36 80 86 AD 44 C2 94".hexToBytes()
//00 02 00 00 08 04 01 E0 00 00 04 53 00 00 00 01 00 00 15 85 01 00 38 6E AF F9 2C 20 2B DE 21 B6 13 6F 26 43 F4 04 7B 1F 88 08 4E 8E BE E5 D1 3F E7 93 DE DD E0 6E 38 65 C7 C7 D3 20 7D AC 73 AD F9 85 F9 CC 2A 2C 26 C6 B1 5B FD 34 3F D4 F2 01 03 00 19 02 6D 28 41 D2 A5 6F D2 FC 3E 2A 1F 03 75 DE 6E 28 8F A8 19 3E 5F 16 49 D3 14 00 05 00 00 00 00 00 04 58 51 4E 44 00 38 84 2D 1D 9D 07 04 34 80 17 9E 3F 58 02 20 9A 1C 22 D0 73 7D 8A 90 1B 2F F8 E6 79 A6 84 2F 98 F5 1E 66 3D 9A 24 59 18 34 42 BD 45 DA E1 22 2D BC 2D 36 80 86 AD 44 C2 94 00 10 69 20 D1 14 74 F5 B3 93 E4 D5 02 B3 71 1A CD 2A
ByteArrayDataOutputStream().let {
it.writeHex("00 02 00 00 08 04 01 E0")
it.writeHex(Protocol.constantData2)
it.writeHex("01 00 38")
it.write(token0825)
it.writeHex("01 03")
it.writeShort(25)
it.writeHex(Protocol.publicKey)
it.writeHex("14 00 05 00 00 00 00 00 04")
it.write(verificationCode.substring(0..3).toByteArray())
it.writeHex("00 38")
it.write(verificationToken)
it.writeHex("00 10")
it.writeHex(Protocol.key00BAFix)
println(it.toByteArray().toUHexString())
}
}
/**
* 刷新验证码
*/
......@@ -152,7 +123,7 @@ class ClientVerificationCodeRefreshPacket(
}
/**
* 验证码输入错误
* 验证码输入错误, 同时也会给一部分验证码
*/
@PacketId("00 BA 32")
class ServerVerificationCodeWrongPacket(input: DataInputStream, dataSize: Int, packetId: ByteArray) : ServerVerificationCodeTransmissionPacket(input, dataSize, packetId)
......@@ -163,7 +134,7 @@ class ServerVerificationCodeWrongPacket(input: DataInputStream, dataSize: Int, p
* @author Him188moe
*/
@PacketId("00 BA 31")
abstract class ServerVerificationCodeTransmissionPacket(input: DataInputStream, private val dataSize: Int, private val packetId: ByteArray) : ServerVerificationCodePacket(input) {
open class ServerVerificationCodeTransmissionPacket(input: DataInputStream, private val dataSize: Int, private val packetId: ByteArray) : ServerVerificationCodePacket(input) {
lateinit var captchaSectionN: ByteArray
lateinit var verificationToken: ByteArray//56bytes
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment