Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加握手报文协议增加注解,增加代码的易读性。 #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Handler/Frontend/FrontendAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@ public function getHandshakePacket(int $server_id)
$this->seed = array_merge($rand1, $rand2);
$hs = new HandshakePacket();
$hs->packetId = 0;
// 以下根据握手报文
// 协议版本号
$hs->protocolVersion = Versions::PROTOCOL_VERSION;
// 服务器版本号信息
$hs->serverVersion = Versions::SERVER_VERSION;
// 服务器线程
$hs->threadId = $server_id;
// 随机数
$hs->seed = $rand1;
// 填充值,服务器权能标识,
$hs->serverCapabilities = $this->getServerCapabilities();
// 字符编码
$hs->serverCharsetIndex = (CharsetUtil::getIndex(CONFIG['server']['charset'] ?? 'utf8mb4') & 0xff);
// 服务器状态
$hs->serverStatus = 2;
// 服务器权能标识+填充值
$hs->restOfScrambleBuff = $rand2;

return getString($hs->write());
Expand Down
12 changes: 12 additions & 0 deletions src/MysqlPacket/HandshakePacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,34 @@ public function write()
{
// default init 256,so it can avoid buff extract
$buffer = [];
// 写入消息头长度
BufferUtil::writeUB3($buffer, $this->calcPacketSize());
// 写入序号 -- 消息头的
$buffer[] = $this->packetId;
// 写入协议版本号
$buffer[] = $this->protocolVersion;
// 写入服务器版本信息
BufferUtil::writeWithNull($buffer, getBytes($this->serverVersion));
// 写入服务器线程ID
BufferUtil::writeUB4($buffer, $this->threadId);
// 挑战随机数 9个字节 包含一个填充值
BufferUtil::writeWithNull($buffer, $this->seed);
// 服务器权能标识
BufferUtil::writeUB2($buffer, $this->serverCapabilities);
// 1字节 字符编码
$buffer[] = $this->serverCharsetIndex;
BufferUtil::writeUB2($buffer, $this->serverStatus);
if ($this ->serverCapabilities & Capabilities::CLIENT_PLUGIN_AUTH) {
// 服务器权能标志 16位
BufferUtil::writeUB2($buffer, $this->serverCapabilities);
// 挑战长度+填充值+挑战随机数
$buffer[] = max(13, count($this->seed) + count($this->restOfScrambleBuff) + 1);
$buffer = array_merge($buffer, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
} else {
// 10字节填充数
$buffer = array_merge($buffer, self::$FILLER_13);
}
// +12字节挑战随机数
if ($this ->serverCapabilities & Capabilities::CLIENT_SECURE_CONNECTION) {
BufferUtil::writeWithNull($buffer, $this->restOfScrambleBuff);
}
Expand Down