Tool.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Command;
  4. use App\Models\Config;
  5. use App\Models\Link;
  6. use App\Models\Node;
  7. use App\Models\User as ModelsUser;
  8. use App\Services\MFA;
  9. use App\Utils\Hash;
  10. use App\Utils\Tools;
  11. use Exception;
  12. use Ramsey\Uuid\Uuid;
  13. use Telegram\Bot\Api;
  14. use Telegram\Bot\Exceptions\TelegramSDKException;
  15. use function count;
  16. use function date;
  17. use function fgets;
  18. use function file_get_contents;
  19. use function file_put_contents;
  20. use function fwrite;
  21. use function in_array;
  22. use function json_decode;
  23. use function json_encode;
  24. use function method_exists;
  25. use function strtolower;
  26. use function trim;
  27. use const JSON_PRETTY_PRINT;
  28. use const JSON_UNESCAPED_UNICODE;
  29. use const PHP_EOL;
  30. use const STDIN;
  31. use const STDOUT;
  32. final class Tool extends Command
  33. {
  34. public string $description = <<<EOL
  35. ├─=: php xcat Tool [选项]
  36. │ ├─ setTelegram - 设置 Telegram 机器人
  37. │ ├─ resetSetting - 使用默认值覆盖数据库配置
  38. │ ├─ exportSetting - 导出数据库配置
  39. │ ├─ importSetting - 导入数据库配置
  40. │ ├─ resetNodePassword - 重置所有节点通讯密钥
  41. │ ├─ resetNodeBandwidth - 重置所有节点流量
  42. │ ├─ resetPort - 重置所有用户端口
  43. │ ├─ resetBandwidth - 重置所有用户流量
  44. │ ├─ resetPassword - 重置所有用户登录密码
  45. │ ├─ resetPasswd - 重置所有用户连接密码
  46. │ ├─ clearSubToken - 清除用户 Sub Token
  47. │ ├─ generateUUID - 为所有用户生成新的 UUID
  48. │ ├─ generateGa - 为所有用户生成新的 Ga Secret
  49. │ ├─ generateApiToken - 为所有用户生成新的 API Token
  50. │ ├─ setTheme - 为所有用户设置新的主题
  51. │ ├─ createAdmin - 创建管理员帐号
  52. EOL;
  53. public function boot(): void
  54. {
  55. if (count($this->argv) === 2) {
  56. echo $this->description;
  57. } else {
  58. $methodName = $this->argv[2];
  59. if (method_exists($this, $methodName)) {
  60. $this->$methodName();
  61. } else {
  62. echo '方法不存在' . PHP_EOL;
  63. }
  64. }
  65. }
  66. /**
  67. * @throws TelegramSDKException
  68. */
  69. public function setTelegram(): void
  70. {
  71. $WebhookUrl = $_ENV['baseUrl'] . '/callback/telegram?token=' . Config::obtain('telegram_request_token');
  72. $telegram = new Api(Config::obtain('telegram_token'));
  73. $telegram->removeWebhook();
  74. if ($telegram->setWebhook(['url' => $WebhookUrl])) {
  75. echo 'Bot @' . $telegram->getMe()->getUsername() . ' 设置成功!' . PHP_EOL;
  76. } else {
  77. echo '设置失败!' . PHP_EOL;
  78. }
  79. }
  80. public function resetSetting(): void
  81. {
  82. $settings = Config::all();
  83. foreach ($settings as $setting) {
  84. $setting->value = $setting->default;
  85. $setting->save();
  86. }
  87. echo '已使用默认值覆盖所有数据库设置' . PHP_EOL;
  88. }
  89. public function exportSetting(): void
  90. {
  91. $settings = Config::all();
  92. foreach ($settings as $setting) {
  93. // 因为主键自增所以即便设置为 null 也会在导入时自动分配 id
  94. // 同时避免多位开发者 pull request 时 settings.json 文件 id 重复所可能导致的冲突
  95. $setting->id = null;
  96. // 避免开发者调试配置泄露
  97. $setting->value = $setting->default;
  98. }
  99. $json_settings = json_encode($settings, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  100. file_put_contents('./config/settings.json', $json_settings);
  101. echo '已导出所有数据库设置' . PHP_EOL;
  102. }
  103. public function importSetting(): void
  104. {
  105. $json_settings = file_get_contents('./config/settings.json');
  106. $settings = json_decode($json_settings, true);
  107. $config = [];
  108. $add_counter = 0;
  109. $update_counter = 0;
  110. $del_counter = 0;
  111. // 检查新增
  112. foreach ($settings as $item) {
  113. $config[] = $item['item'];
  114. $item_name = $item['item'];
  115. $query = (new Config())->where('item', $item['item'])->first();
  116. if ($query === null) {
  117. $new_item = new Config();
  118. $new_item->id = null;
  119. $new_item->item = $item['item'];
  120. $new_item->value = $item['value'];
  121. $new_item->class = $item['class'];
  122. $new_item->is_public = $item['is_public'];
  123. $new_item->type = $item['type'];
  124. $new_item->default = $item['default'];
  125. $new_item->mark = $item['mark'];
  126. $new_item->save();
  127. echo '添加新数据库设置:' . $item_name . PHP_EOL;
  128. $add_counter += 1;
  129. continue;
  130. }
  131. if ($query->class !== $item['class']) {
  132. $query->class = $item['class'];
  133. $query->save();
  134. echo '更新数据库设置:' . $item_name . PHP_EOL;
  135. $update_counter += 1;
  136. }
  137. }
  138. // 检查移除
  139. $db_settings = Config::all();
  140. foreach ($db_settings as $db_setting) {
  141. if (! in_array($db_setting->item, $config)) {
  142. $db_setting->delete();
  143. $del_counter += 1;
  144. }
  145. }
  146. if ($add_counter !== 0) {
  147. echo '添加了 ' . $add_counter . ' 项新数据库设置' . PHP_EOL;
  148. }
  149. if ($update_counter !== 0) {
  150. echo '更新了 ' . $update_counter . ' 项数据库设置' . PHP_EOL;
  151. }
  152. if ($del_counter !== 0) {
  153. echo '移除了 ' . $del_counter . ' 项数据库设置' . PHP_EOL;
  154. }
  155. }
  156. public function resetNodePassword(): void
  157. {
  158. $nodes = Node::all();
  159. foreach ($nodes as $node) {
  160. $node->password = Tools::genRandomChar(32);
  161. $node->save();
  162. }
  163. echo '已重置所有节点密码' . PHP_EOL;
  164. }
  165. public function resetNodeBandwidth(): void
  166. {
  167. $nodes = Node::all();
  168. foreach ($nodes as $node) {
  169. $node->node_bandwidth = 0;
  170. $node->save();
  171. }
  172. echo '已重置所有节点流量' . PHP_EOL;
  173. }
  174. /**
  175. * 重置所有用户端口
  176. */
  177. public function resetPort(): void
  178. {
  179. $users = ModelsUser::all();
  180. if (count($users) === 0 || count($users) >= 65535) {
  181. echo '无效的用户数量' . PHP_EOL;
  182. return;
  183. }
  184. (new ModelsUser())->update([
  185. 'port' => 0,
  186. ]);
  187. foreach ($users as $user) {
  188. $user->port = Tools::getSsPort();
  189. $user->save();
  190. }
  191. echo '已重置所有用户端口' . PHP_EOL;
  192. }
  193. /**
  194. * 重置所有用户流量
  195. */
  196. public function resetBandwidth(): void
  197. {
  198. (new ModelsUser())->where('is_banned', 0)->update([
  199. 'd' => 0,
  200. 'u' => 0,
  201. 'transfer_today' => 0,
  202. ]);
  203. echo '已重置所有用户流量' . PHP_EOL;
  204. }
  205. /**
  206. * 重置所有用户登录密码
  207. */
  208. public function resetPassword(): void
  209. {
  210. $users = ModelsUser::all();
  211. foreach ($users as $user) {
  212. $user->pass = Hash::passwordHash(Tools::genRandomChar(32));
  213. $user->save();
  214. }
  215. echo '已重置所有用户登录密码' . PHP_EOL;
  216. }
  217. /**
  218. * 重置所有用户连接密码
  219. */
  220. public function resetPasswd(): void
  221. {
  222. $users = ModelsUser::all();
  223. foreach ($users as $user) {
  224. $user->passwd = Tools::genRandomChar(16);
  225. $user->save();
  226. }
  227. echo '已重置所有用户连接密码' . PHP_EOL;
  228. }
  229. /**
  230. * 清除用户 Sub Token
  231. */
  232. public function clearSubToken(): void
  233. {
  234. Link::query()->truncate();
  235. echo '已清除所有用户 Sub Token' . PHP_EOL;
  236. }
  237. /**
  238. * 为所有用户生成新的 UUID
  239. */
  240. public function generateUUID(): void
  241. {
  242. $users = ModelsUser::all();
  243. foreach ($users as $user) {
  244. $user->uuid = Uuid::uuid4();
  245. $user->save();
  246. }
  247. echo '已为所有用户生成新的 UUID' . PHP_EOL;
  248. }
  249. /**
  250. * 二次验证
  251. */
  252. public function generateGa(): void
  253. {
  254. $users = ModelsUser::all();
  255. foreach ($users as $user) {
  256. try {
  257. $user->ga_token = MFA::generateGaToken();
  258. $user->save();
  259. } catch (Exception $e) {
  260. echo $e->getMessage();
  261. }
  262. }
  263. echo '已为所有用户生成新的 Ga Secret' . PHP_EOL;
  264. }
  265. /**
  266. * 为所有用户生成新的 Api Token
  267. */
  268. public function generateApiToken(): void
  269. {
  270. $users = ModelsUser::all();
  271. foreach ($users as $user) {
  272. $user->generateApiToken();
  273. }
  274. echo '已为所有用户生成新的 Api Token' . PHP_EOL;
  275. }
  276. /**
  277. * 为所有用户设置新的主题
  278. */
  279. public function setTheme(): void
  280. {
  281. fwrite(STDOUT, '请输入要设置的主题名称: ');
  282. $theme = trim(fgets(STDIN));
  283. $users = ModelsUser::all();
  284. foreach ($users as $user) {
  285. $user->theme = $theme;
  286. $user->save();
  287. }
  288. echo '已为所有用户设置新的主题: ' . $theme . PHP_EOL;
  289. }
  290. /**
  291. * 创建 Admin 账户
  292. *
  293. * @throws Exception
  294. */
  295. public function createAdmin(): void
  296. {
  297. $y = '';
  298. $email = '';
  299. $passwd = '';
  300. if (count($this->argv) === 3) {
  301. // ask for input
  302. echo '(1/3) 请输入管理员邮箱:' . PHP_EOL;
  303. // get input
  304. $email = trim(fgets(STDIN));
  305. // write input back
  306. echo '(2/3) 请输入管理员账户密码:' . PHP_EOL;
  307. $passwd = trim(fgets(STDIN));
  308. echo '(3/3) 按 Y 或 y 确认创建:';
  309. $y = trim(fgets(STDIN));
  310. } elseif (count($this->argv) === 5) {
  311. [,,, $email, $passwd] = $this->argv;
  312. $y = 'y';
  313. }
  314. if (strtolower($y) === 'y') {
  315. // do reg user
  316. $user = new ModelsUser();
  317. $user->user_name = 'Admin';
  318. $user->email = $email;
  319. $user->remark = '';
  320. $user->pass = Hash::passwordHash($passwd);
  321. $user->passwd = Tools::genRandomChar(16);
  322. $user->uuid = Uuid::uuid4();
  323. $user->api_token = Uuid::uuid4();
  324. $user->port = Tools::getSsPort();
  325. $user->u = 0;
  326. $user->d = 0;
  327. $user->transfer_enable = 0;
  328. $user->ref_by = 0;
  329. $user->is_admin = 1;
  330. $user->reg_date = date('Y-m-d H:i:s');
  331. $user->money = 0;
  332. $user->im_type = 0;
  333. $user->im_value = '';
  334. $user->class = 0;
  335. $user->node_iplimit = 0;
  336. $user->node_speedlimit = 0;
  337. $user->theme = $_ENV['theme'];
  338. $user->locale = $_ENV['locale'];
  339. $user->ga_token = MFA::generateGaToken();
  340. $user->ga_enable = 0;
  341. if ($user->save()) {
  342. echo '创建成功,请在主页登录' . PHP_EOL;
  343. } else {
  344. echo '创建失败,请检查数据库配置' . PHP_EOL;
  345. }
  346. } else {
  347. echo '已取消创建' . PHP_EOL;
  348. }
  349. }
  350. }