Skip to content

Commit 32e2eed

Browse files
committed
链接不上,还是连不上
1 parent 9b16b4f commit 32e2eed

7 files changed

Lines changed: 384 additions & 15 deletions

File tree

app/Listener/ApplicationLifecycleListener.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ public function process(object $event): void
4242
'event' => 'BeforeWorkerStart',
4343
'workerNum' => $event->serverSetting['worker_num'] ?? 'unknown',
4444
]);
45+
46+
// 测试connection logger
47+
try {
48+
$connectionLogger = $this->container->get(\Hyperf\Logger\LoggerFactory::class)->get('connection');
49+
$connectionLogger->info('BeforeWorkerStart阶段connection logger测试', [
50+
'workerNum' => $event->serverSetting['worker_num'] ?? 'unknown',
51+
'pid' => getmypid(),
52+
]);
53+
} catch (\Throwable $e) {
54+
$this->logger->error('获取connection logger失败', [
55+
'error' => $e->getMessage(),
56+
'file' => $e->getFile(),
57+
'line' => $e->getLine(),
58+
]);
59+
}
4560
}
4661
}
4762
}

app/Listener/ServerStartListener.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,36 @@ public function process(object $event): void
5454
'worker_type' => $workerType,
5555
'event' => 'OnWorkerStart',
5656
]);
57+
58+
// 测试connection logger
59+
try {
60+
$connectionLogger = $this->container->get(\Hyperf\Logger\LoggerFactory::class)->get('connection');
61+
$connectionLogger->info('Worker进程中connection logger测试', [
62+
'worker_id' => $workerId,
63+
'worker_type' => $workerType,
64+
'pid' => getmypid(),
65+
]);
66+
} catch (\Throwable $e) {
67+
$this->logger->error('获取connection logger失败', [
68+
'error' => $e->getMessage(),
69+
'file' => $e->getFile(),
70+
'line' => $e->getLine(),
71+
]);
72+
}
73+
74+
// 测试ProxyService
75+
try {
76+
$proxyService = $this->container->get(\App\Service\ProxyService::class);
77+
$this->logger->info('ProxyService实例获取成功', [
78+
'worker_id' => $workerId,
79+
]);
80+
} catch (\Throwable $e) {
81+
$this->logger->error('获取ProxyService失败', [
82+
'error' => $e->getMessage(),
83+
'file' => $e->getFile(),
84+
'line' => $e->getLine(),
85+
]);
86+
}
5787
}
5888
}
5989
}

app/Protocol/MySql/Auth.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,33 @@ public static function parseHandshakeResponse(Packet $packet): array
3838
{
3939
$payload = $packet->getPayload();
4040
$offset = 0;
41+
$payloadLength = strlen($payload);
4142

4243
// Capability Flags (4 bytes)
44+
if ($offset + 4 > $payloadLength) {
45+
throw new \RuntimeException('解析握手响应失败:数据包长度不足,无法读取 Capability Flags');
46+
}
4347
$capabilities = unpack('V', substr($payload, $offset, 4))[1];
4448
$offset += 4;
4549

4650
// Max Packet Size (4 bytes)
51+
if ($offset + 4 > $payloadLength) {
52+
throw new \RuntimeException('解析握手响应失败:数据包长度不足,无法读取 Max Packet Size');
53+
}
4754
$maxPacketSize = unpack('V', substr($payload, $offset, 4))[1];
4855
$offset += 4;
4956

5057
// Character Set (1 byte)
58+
if ($offset + 1 > $payloadLength) {
59+
throw new \RuntimeException('解析握手响应失败:数据包长度不足,无法读取 Character Set');
60+
}
5161
$charset = ord($payload[$offset]);
5262
$offset += 1;
5363

5464
// Reserved (23 bytes)
65+
if ($offset + 23 > $payloadLength) {
66+
throw new \RuntimeException('解析握手响应失败:数据包长度不足,无法读取 Reserved');
67+
}
5568
$offset += 23;
5669

5770
// Username (NUL terminated string)
@@ -63,14 +76,21 @@ public static function parseHandshakeResponse(Packet $packet): array
6376
$offset++; // skip NUL
6477

6578
// Auth Response (length encoded integer)
79+
if ($offset + 1 > $payloadLength) {
80+
throw new \RuntimeException('解析握手响应失败:数据包长度不足,无法读取 Auth Response 长度');
81+
}
6682
$authResponseLength = ord($payload[$offset]);
6783
$offset += 1;
84+
85+
if ($offset + $authResponseLength > $payloadLength) {
86+
throw new \RuntimeException('解析握手响应失败:数据包长度不足,无法读取完整的 Auth Response');
87+
}
6888
$authResponse = substr($payload, $offset, $authResponseLength);
6989
$offset += $authResponseLength;
7090

7191
// Database (NUL terminated string) - only if CLIENT_CONNECT_WITH_DB
7292
$database = '';
73-
if ($capabilities & 0x00000008 && $offset < strlen($payload)) {
93+
if ($capabilities & 0x00000008 && $offset < $payloadLength) {
7494
while (isset($payload[$offset]) && $payload[$offset] !== "\x00") {
7595
$database .= $payload[$offset];
7696
$offset++;
@@ -80,7 +100,7 @@ public static function parseHandshakeResponse(Packet $packet): array
80100

81101
// Auth Plugin Name (NUL terminated string) - only if CLIENT_PLUGIN_AUTH
82102
$authPluginName = '';
83-
if ($capabilities & 0x00080000 && $offset < strlen($payload)) {
103+
if ($capabilities & 0x00080000 && $offset < $payloadLength) {
84104
while (isset($payload[$offset]) && $payload[$offset] !== "\x00") {
85105
$authPluginName .= $payload[$offset];
86106
$offset++;

app/Protocol/MySql/Parser.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,27 @@ public static function parsePackets(string $data): array
1212
{
1313
$packets = [];
1414
$offset = 0;
15+
$totalLength = strlen($data);
1516

16-
while ($offset < strlen($data)) {
17-
if ($offset + 4 > strlen($data)) {
17+
while ($offset < $totalLength) {
18+
// 检查是否有足够的字节来读取包头
19+
if ($offset + 4 > $totalLength) {
1820
break;
1921
}
2022

2123
$length = unpack('V', substr($data, $offset, 3) . "\x00")[1];
2224
$sequenceId = ord($data[$offset + 3]);
25+
26+
// 验证包长度是否合理
27+
if ($length < 0 || $length > 0xffffff) {
28+
throw new \RuntimeException("无效的包长度: {$length}");
29+
}
30+
31+
// 检查是否有足够的字节来读取payload
32+
if ($offset + 4 + $length > $totalLength) {
33+
break;
34+
}
35+
2336
$payload = substr($data, $offset + 4, $length);
2437

2538
$packets[] = new Packet($length, $sequenceId, $payload);

0 commit comments

Comments
 (0)