You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Command/XCat.php

113 lines
3.9 KiB
PHTML

<?php
namespace App\Command;
use App\Models\User;
use App\Services\TicketTelegramNotifier;
use App\Utils\Hash;
class XCat
{
public $argv;
public function __construct($argv)
{
$this->argv = $argv;
}
public function boot()
{
try {
$result = match ((string) ($this->argv[1] ?? 'help')) {
'createAdmin' => $this->createAdmin(),
'cloudInit' => CloudSetup::initialize(),
'cloudAgentInit' => CloudAgentSetup::initialize(),
'cloudExportConfig' => CloudSetup::exportEngineConfig(),
'cloudWorker' => CloudWorker::run($this->argv) === 0,
'cloudLifecycle' => CloudLifecycle::run($this->argv) === 0,
'setTelegram' => $this->setTelegram(),
'backup' => Job::backup(),
'backupJob' => Job::backupJob(),
'usdtScanJob' => Job::usdtScanJob($this->argv),
'siteAnalyticsJob' => Job::siteAnalyticsJob($this->argv),
'help' => $this->help(),
default => throw new \InvalidArgumentException('未知命令,请执行 php xcat help 查看保留命令。'),
};
return $result === false ? 1 : 0;
} catch (\Throwable $exception) {
fwrite(STDERR, $exception->getMessage() . PHP_EOL);
return 1;
}
}
public function createAdmin()
{
$email = trim(strtolower((string) ($this->argv[2] ?? '')));
if ($email === '') {
fwrite(STDOUT, '管理员邮箱:');
$email = trim(strtolower((string) fgets(STDIN)));
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL) || strlen($email) > 254) {
throw new \InvalidArgumentException('请输入有效的管理员邮箱。');
}
$name = trim((string) ($this->argv[3] ?? 'admin'));
if ($name === '' || mb_strlen($name) > 128) {
throw new \InvalidArgumentException('管理员名称须为 1 至 128 个字符。');
}
if (User::where('email', $email)->exists()) {
throw new \RuntimeException('邮箱已存在,未创建或修改任何账户。');
}
$password = self::generatePassword();
$user = new User();
$user->user_name = $name;
$user->email = $email;
$user->pass = Hash::passwordHash($password);
$user->enable = 1;
$user->is_admin = 1;
$user->reg_date = date('Y-m-d H:i:s');
$user->reg_ip = '';
$user->money = 0;
$user->ref_by = 0;
$user->invite_num = 0;
$user->theme = 'venix';
if (!$user->save()) {
throw new \RuntimeException('管理员创建失败。');
}
echo '管理员 ID:' . $user->id . PHP_EOL;
echo '邮箱:' . $email . PHP_EOL;
echo '初始密码(仅显示一次):' . $password . PHP_EOL;
echo '请保管密码并登录后修改;若使用 super_admin 白名单,请包含该管理员 ID。' . PHP_EOL;
return true;
}
private static function generatePassword()
{
return 'A!a1' . bin2hex(random_bytes(12));
}
public function setTelegram()
{
TicketTelegramNotifier::registerWebhook();
echo '管理员工单提醒机器人 Webhook 已注册。' . PHP_EOL;
return true;
}
private function help()
{
echo "Virt Cloud CLI\n"
. " php xcat createAdmin <email> [name]\n"
. " php xcat cloudInit\n"
. " php xcat cloudAgentInit\n"
. " php xcat cloudExportConfig\n"
. " php xcat cloudWorker [--once] [--policy-only]\n"
. " php xcat cloudLifecycle [--once]\n"
. " php xcat setTelegram\n"
. " php xcat backup\n"
. " php xcat backupJob\n"
. " php xcat usdtScanJob [wallet-limit]\n"
. " php xcat siteAnalyticsJob [days]\n";
return true;
}
}