Skip to content

Commit 56d6022

Browse files
committed
增加 proxy例子 移除不必要的文件
1 parent c055f24 commit 56d6022

File tree

6 files changed

+67
-171
lines changed

6 files changed

+67
-171
lines changed

examples/proxy.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Workbunny\MysqlProtocol\Utils\Binary;
6+
use Workbunny\MysqlProtocol\Utils\Packet;
7+
use Workerman\Connection\TcpConnection;
8+
9+
require_once __DIR__ . '/../vendor/autoload.php';
10+
11+
$server = new \Workerman\Worker("MySQL://0.0.0.0:8844");
12+
$server->name = 'workbunny-mysql-server';
13+
$server->count = 2;
14+
$server->onConnect = function (TcpConnection $source) {
15+
// 创建与MySQL-server的连接
16+
$target = new \Workerman\Connection\AsyncTcpConnection("MySQL://host.docker.internal:3306");
17+
// 管道传输
18+
$target->pipe($source);
19+
$target->onMessage = function (TcpConnection $target, Binary $data) use ($source) {
20+
// 客户端的来源信息
21+
dump('-------------------------------------------------------------------------------------------------------------------');
22+
dump('Server', $data->dump(), Packet::parser(null, $data));
23+
dump('Packet-Class: ' . Packet::getPacketClass($data));
24+
dump('-------------------------------------------------------------------------------------------------------------------');
25+
$source->send($data);
26+
};
27+
28+
$source->pipe($target);
29+
$source->onMessage = function (TcpConnection $source, Binary $data) use ($target) {
30+
dump('-------------------------------------------------------------------------------------------------------------------');
31+
dump('Client', $data->dump(), Packet::parser(null, $data));
32+
dump('-------------------------------------------------------------------------------------------------------------------');
33+
$target->send($data);
34+
};
35+
36+
$target->connect();
37+
};
38+
39+
\Workerman\Worker::runAll();

src/Packets/AuthMoreDataRequest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class AuthMoreDataRequest implements PacketInterface
2929
* @param Binary $binary
3030
* @return array 包含:
3131
* - flag: int,标志字节
32-
* - extra_data: string,附加数据(可能为空)
32+
* - extra_data: array,附加数据字节组
3333
*/
3434
public static function unpack(Binary $binary): array
3535
{
@@ -41,9 +41,9 @@ public static function unpack(Binary $binary): array
4141
}
4242
// 如果后续有附加数据,则读取之
4343
$remainingLength = $binary->length() - $binary->getReadCursor();
44-
$extraData = '';
44+
$extraData = [];
4545
if ($remainingLength > 0) {
46-
$extraData = Binary::BytesToString($binary->readBytes($remainingLength));
46+
$extraData = $binary->readBytes($remainingLength);
4747
}
4848

4949
return [
@@ -60,7 +60,7 @@ public static function unpack(Binary $binary): array
6060
* - flag: int,标志字节(默认 0x01 表示请求全认证)
6161
*
6262
* 可选键:
63-
* - extra_data: string,附加数据
63+
* - extra_data: array,附加数据
6464
*
6565
* @param array $data
6666
* @return Binary
@@ -69,10 +69,10 @@ public static function pack(array $data): Binary
6969
{
7070
$packetId = $data['packet_id'] ?? 0;
7171
return Packet::binary(function (Binary $binary) use ($data) {
72-
$extraData = $data['extra_data'] ?? null;
72+
$extraData = $data['extra_data'] ?? [];
7373
$binary->writeByte(self::PACKET_FLAG);
7474
if ($extraData) {
75-
$binary->writeBytes(Binary::StringToBytes($extraData));
75+
$binary->writeBytes($extraData);
7676
}
7777
}, $packetId);
7878
}

src/Plugins/AbstractPlugin.php

Lines changed: 0 additions & 90 deletions
This file was deleted.

src/Plugins/CachingSha2PasswordAuthPlugin.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/Plugins/MySQLNativePasswordAuthPlugin.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/Utils/Packet.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Workbunny\MysqlProtocol\Constants\ExceptionCode;
99
use Workbunny\MysqlProtocol\Exceptions\Exception;
1010
use Workbunny\MysqlProtocol\Exceptions\PacketException;
11+
use Workbunny\MysqlProtocol\Packets\AuthMoreDataRequest;
12+
use Workbunny\MysqlProtocol\Packets\AuthSwitchRequest;
1113
use Workbunny\MysqlProtocol\Packets\EOF;
1214
use Workbunny\MysqlProtocol\Packets\Error;
1315
use Workbunny\MysqlProtocol\Packets\OK;
@@ -63,19 +65,33 @@ public static function parser(?Closure $closure, Binary $binary): array
6365
* 根据包头获取包类型
6466
*
6567
* @param Binary $binary
68+
* @param Closure|null $closure = function(Binary $binary): class-string<PacketInterface>|null {}
6669
* @return class-string<PacketInterface>|null
6770
*/
68-
public static function getPacketClass(Binary $binary): ?string
71+
public static function getPacketClass(Binary $binary, ?Closure $closure = null): ?string
6972
{
73+
// 获取当前指针
7074
$readCursor = $binary->getReadCursor();
75+
// 重置读指针
76+
$binary->setReadCursor(0);
77+
// 包长
78+
$packetLength = $binary->readUB(Binary::UB3);
79+
// 都指针下移包身
7180
$binary->setReadCursor(4);
81+
// 读取flag
7282
$header = $binary->readByte();
83+
// 恢复指针
7384
$binary->setReadCursor($readCursor);
74-
return match ($header) {
75-
Ok::PACKET_FLAG => OK::class,
76-
Error::PACKET_FLAG => Error::class,
77-
EOF::PACKET_FLAG => EOF::class,
78-
default => null,
85+
return match (true) {
86+
$header === Ok::PACKET_FLAG => OK::class,
87+
$header === Error::PACKET_FLAG => Error::class,
88+
// EOF 包体长度小于9字节,且flag为0xFE
89+
($packetLength < 9) and $header === EOF::PACKET_FLAG => EOF::class,
90+
// AuthSwitchRequest 包体长度通常大于9字节,且flag为0xFE
91+
$header === AuthSwitchRequest::PACKET_FLAG => AuthSwitchRequest::class,
92+
$header === AuthMoreDataRequest::PACKET_FLAG => AuthMoreDataRequest::class,
93+
// 如果传入自定义解析函数,则使用
94+
default => $closure ? $closure($binary) : null,
7995
};
8096
}
8197

0 commit comments

Comments
 (0)