Files
webman-buildadmin/scripts/smoke_ws_auth_helper.php
2026-05-27 10:28:39 +08:00

60 lines
3.2 KiB
PHP

<?php
declare(strict_types=1);
/**
* 本地烟雾测试:验证 GameWebSocketAuthHelper / Registry / Dispatcher 关键路径。
* 不依赖 webman 进程,可直接 `php scripts/smoke_ws_auth_helper.php` 跑。
*/
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../support/bootstrap.php';
use app\common\service\GameWebSocketAuthHelper;
use app\common\service\GameWebSocketSubscriptionRegistry;
echo "[1] parseQueryString\n";
$q = GameWebSocketAuthHelper::parseQueryString('?auth_token=AAA&user_token=BBB&device_id=ios&extra=1');
echo json_encode($q, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), PHP_EOL, PHP_EOL;
echo "[2] authorize: missing auth-token (mobile)\n";
$r = GameWebSocketAuthHelper::authorize(['user_token' => 'whatever']);
echo json_encode($r, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), PHP_EOL, PHP_EOL;
echo "[3] authorize: invalid auth-token\n";
$r = GameWebSocketAuthHelper::authorize(['auth_token' => 'not_existing_token', 'user_token' => 'not_existing_user_token']);
echo json_encode($r, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), PHP_EOL, PHP_EOL;
echo "[4] authorize: invalid admin_ws_token bypass\n";
$r = GameWebSocketAuthHelper::authorize(['admin_ws_token' => 'no_such_admin_token_xxxxxx']);
echo json_encode($r, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), PHP_EOL, PHP_EOL;
echo "[5] issue + validate admin_ws_token\n";
$issued = GameWebSocketAuthHelper::issueAdminWsToken(99, 60);
echo 'issued: ', json_encode($issued, JSON_UNESCAPED_UNICODE), PHP_EOL;
if (($issued['token'] ?? '') !== '') {
$r = GameWebSocketAuthHelper::authorize(['admin_ws_token' => $issued['token']]);
echo 'authorize ok with issued token: ', json_encode($r, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT), PHP_EOL;
} else {
echo "WARN: issueAdminWsToken returned empty (Redis 不可用?)\n";
}
echo PHP_EOL;
echo "[6] SubscriptionRegistry\n";
GameWebSocketSubscriptionRegistry::reset();
GameWebSocketSubscriptionRegistry::registerConnection(10, 14, '127.0.0.1');
GameWebSocketSubscriptionRegistry::registerConnection(11, 0, '127.0.0.1');
$t1 = GameWebSocketSubscriptionRegistry::replaceSubscriptions(10, ['bet.win', 'period.opened', 'period.opened', '', ' ', 'bet.win']);
$t2 = GameWebSocketSubscriptionRegistry::replaceSubscriptions(11, ['admin.live.snapshot', 'period.opened']);
echo 'cid=10 topics=', json_encode($t1), PHP_EOL;
echo 'cid=11 topics=', json_encode($t2), PHP_EOL;
echo 'period.opened subscribers=', json_encode(GameWebSocketSubscriptionRegistry::connectionsForTopic('period.opened')), PHP_EOL;
echo 'bet.win subscribers=', json_encode(GameWebSocketSubscriptionRegistry::connectionsForTopic('bet.win')), PHP_EOL;
echo 'admin.live.snapshot subscribers=', json_encode(GameWebSocketSubscriptionRegistry::connectionsForTopic('admin.live.snapshot')), PHP_EOL;
GameWebSocketSubscriptionRegistry::unregisterConnection(10);
echo 'after unregister(10), bet.win subscribers=', json_encode(GameWebSocketSubscriptionRegistry::connectionsForTopic('bet.win')), PHP_EOL;
echo 'after unregister(10), period.opened subscribers=', json_encode(GameWebSocketSubscriptionRegistry::connectionsForTopic('period.opened')), PHP_EOL;
echo 'remaining connection count=', GameWebSocketSubscriptionRegistry::connectionCount(), PHP_EOL;
echo "\nALL DONE\n";