Skip to content

Commit 3e6d364

Browse files
committed
优化 ProxyAuthenticator 和 BackendExecutor 的日志记录,增强调试信息;更新 MySQLHandshake 方法以支持外部 authPluginData 参数;调整 MySQLProxyService 以确保握手包生成的一致性。
1 parent 49a1416 commit 3e6d364

4 files changed

Lines changed: 58 additions & 13 deletions

File tree

app/Proxy/Auth/ProxyAuthenticator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ public function authenticate(
7373
// 验证密码
7474
$isValid = $this->verifyPassword($authResponse, $account['password'], $authPluginData);
7575

76-
$this->logger->info('代理账号验证结果,密码错误', [
76+
$this->logger->info('代理账号验证结果', [
7777
'username' => $username,
7878
'database' => $database,
79-
'is_valid' => $isValid,
79+
'是否成功' => $isValid,
8080
]);
8181

8282
return $isValid;
@@ -122,6 +122,10 @@ private function verifyPassword(string $clientAuthResponse, string $storedPasswo
122122
$isValid = hash_equals($expectedResponse, $clientAuthResponse);
123123

124124
$this->logger->debug('密码验证详情', [
125+
// 以 hex 形式记录二进制内容,便于对比与调试(生产请谨慎)
126+
'auth_plugin_data_hex' => bin2hex($authPluginData),
127+
'client_response_hex' => bin2hex($clientAuthResponse),
128+
'expected_response_hex' => bin2hex($expectedResponse),
125129
'client_response_length' => mb_strlen($clientAuthResponse),
126130
'expected_response_length' => mb_strlen($expectedResponse),
127131
'responses_match' => $isValid,

app/Proxy/Executor/BackendExecutor.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace App\Proxy\Executor;
66

7-
use Hyperf\DB\DB;
7+
use Hyperf\DbConnection\Db;
88
use App\Protocol\MySql\Packet;
99
use Hyperf\Logger\LoggerFactory;
1010
use Psr\Log\LoggerInterface;
@@ -40,11 +40,19 @@ public function execute(string $sql): array
4040

4141
try {
4242
// 使用 Hyperf 数据库连接执行 SQL
43+
/** @var \Hyperf\DbConnection\Connection $connection */
4344
$connection = DB::connection('backend_mysql');
4445

4546
// 对于 SELECT 查询,使用 select 方法
46-
if (stripos(trim($sql), 'select') === 0) {
47+
// 支持前置注释(例如 MySQL Connector/J 会在前面带注释),所以使用正则去除注释后判断
48+
if (preg_match('/^\s*(?:\/\*[\s\S]*?\*\/\s*)*(select)\b/i', $sql) === 1) {
4749
$result = $connection->select($sql);
50+
// 某些 PDO 驱动或数据库层返回对象(stdClass),但下游期望关联数组
51+
if (!empty($result)) {
52+
$result = array_map(function ($row) {
53+
return is_array($row) ? $row : (array) $row;
54+
}, $result);
55+
}
4856

4957
$elapsedMs = (int) ((microtime(true) - $startTime) * 1000);
5058

@@ -58,7 +66,22 @@ public function execute(string $sql): array
5866
} else {
5967
// 对于其他查询(如 INSERT, UPDATE, DELETE),使用 statement 方法
6068
$affectedRows = $connection->statement($sql);
61-
$lastInsertId = $connection->getPdo()->lastInsertId();
69+
// 某些驱动 statement() 返回 bool 而非受影响行数,统一转换为 int
70+
if (is_bool($affectedRows)) {
71+
$affectedRows = $affectedRows ? 1 : 0;
72+
} else {
73+
$affectedRows = (int) $affectedRows;
74+
}
75+
// 尝试通过可用方法获取最后插入 ID
76+
$lastInsertId = 0;
77+
if (method_exists($connection, 'getPdo')) {
78+
$pdo = $connection->getPdo();
79+
if ($pdo instanceof \PDO) {
80+
$lastInsertId = (int) $pdo->lastInsertId();
81+
}
82+
} elseif (method_exists($connection, 'lastInsertId')) {
83+
$lastInsertId = (int) $connection->lastInsertId();
84+
}
6285

6386
$elapsedMs = (int) ((microtime(true) - $startTime) * 1000);
6487

@@ -236,14 +259,25 @@ public function getPoolStats(): array
236259
{
237260
// 使用 Hyperf 的连接池统计
238261
try {
262+
/** @var \Hyperf\DbConnection\Connection $connection */
239263
$connection = DB::connection('backend_mysql');
240-
$pool = $connection->getPool();
264+
// 使用动态检测以兼容不同 DB 实现
265+
if (method_exists($connection, 'getPool')) {
266+
$pool = $connection->getPool();
267+
return [
268+
'pool_type' => 'hyperf',
269+
'pool_name' => 'backend_mysql',
270+
'connections_count' => $pool->getConnectionsCount(),
271+
'idle_connections_count' => $pool->getIdleConnectionsCount(),
272+
];
273+
}
241274

275+
// 如果没有 pool 支持,则返回基础信息
242276
return [
243277
'pool_type' => 'hyperf',
244278
'pool_name' => 'backend_mysql',
245-
'connections_count' => $pool->getConnectionsCount(),
246-
'idle_connections_count' => $pool->getIdleConnectionsCount(),
279+
'connections_count' => null,
280+
'idle_connections_count' => null,
247281
];
248282
} catch (\Throwable $e) {
249283
return [

app/Proxy/Protocol/MySQLHandshake.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,16 @@ public function __construct(LoggerFactory $loggerFactory)
3030

3131
/**
3232
* 创建服务器握手包
33+
*
34+
* @param int $threadId 线程/连接 id
35+
* @param string|null $authPluginData 如果传入则使用该 auth 插件数据(保证握手与验证使用相同的 salt)
3336
*/
34-
public function createServerHandshake(int $threadId): Packet
37+
public function createServerHandshake(int $threadId, ?string $authPluginData = null): Packet
3538
{
36-
// 生成随机认证插件数据
37-
$authPluginData = $this->generateAuthPluginData(20);
39+
// 使用外部传入的 authPluginData(如果有),否则生成新的
40+
if ($authPluginData === null) {
41+
$authPluginData = $this->generateAuthPluginData(20);
42+
}
3843
$authPluginData1 = substr($authPluginData, 0, 8);
3944
$authPluginDataLen = strlen($authPluginData);
4045
$authPluginData2 = substr($authPluginData, 8);
@@ -59,6 +64,8 @@ public function createServerHandshake(int $threadId): Packet
5964
'capabilities' => sprintf('0x%08x', self::CAPABILITIES),
6065
'charset' => self::CHARSET,
6166
'auth_plugin_data_length' => $authPluginDataLen,
67+
// 为调试方便以 hex 形式记录 auth 插件数据(注意生产环境隐私)
68+
'auth_plugin_data_hex' => bin2hex($authPluginData),
6269
]);
6370

6471
return Packet::create(0, $payload);

app/Proxy/Service/MySQLProxyService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ public function handleConnect(ConnectionContext $context): Packet
4848
'remote_port' => $context->getClientPort(),
4949
]);
5050

51-
// 生成并返回服务器握手包
52-
return $this->handshake->createServerHandshake($context->getThreadId());
51+
// 生成并返回服务器握手包,使用 context 中保存的 authPluginData(确保发送给客户端的 salt 与后续验证一致)
52+
return $this->handshake->createServerHandshake($context->getThreadId(), $context->getAuthPluginData());
5353
}
5454

5555
/**

0 commit comments

Comments
 (0)