Tool.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 danielsreichenbach\GeoIP2Update\Client;
  12. use Exception;
  13. use Ramsey\Uuid\Uuid;
  14. use Telegram\Bot\Api;
  15. use Telegram\Bot\Exceptions\TelegramSDKException;
  16. use function count;
  17. use function date;
  18. use function fgets;
  19. use function file_get_contents;
  20. use function fwrite;
  21. use function in_array;
  22. use function json_decode;
  23. use function method_exists;
  24. use function strtolower;
  25. use function trim;
  26. use const BASE_PATH;
  27. use const PHP_EOL;
  28. use const STDIN;
  29. use const STDOUT;
  30. final class Tool extends Command
  31. {
  32. public string $description = <<<EOL
  33. ├─=: php xcat Tool [选项]
  34. │ ├─ setTelegram - 设置 Telegram 机器人
  35. │ ├─ resetSetting - 使用默认值覆盖数据库配置
  36. │ ├─ importSetting - 导入数据库配置
  37. │ ├─ resetNodePassword - 重置所有节点通讯密钥
  38. │ ├─ resetNodeBandwidth - 重置所有节点流量
  39. │ ├─ resetPort - 重置所有用户端口
  40. │ ├─ resetBandwidth - 重置所有用户流量
  41. │ ├─ resetTodayBandwidth - 重置今日流量
  42. │ ├─ resetPassword - 重置所有用户登录密码
  43. │ ├─ resetPasswd - 重置所有用户连接密码
  44. │ ├─ clearSubToken - 清除用户 Sub Token
  45. │ ├─ generateUUID - 为所有用户生成新的 UUID
  46. │ ├─ generateGa - 为所有用户生成新的 Ga Secret
  47. │ ├─ generateApiToken - 为所有用户生成新的 API Token
  48. │ ├─ setTheme - 为所有用户设置新的主题
  49. │ ├─ setLocale - 为所有用户设置新的语言
  50. │ ├─ createAdmin - 创建管理员帐号
  51. │ └─ updateGeoIP2 - 更新 GeoIP2 数据库
  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 importSetting(): void
  90. {
  91. $json_settings = file_get_contents('./config/settings.json');
  92. $settings = json_decode($json_settings, true);
  93. $config = [];
  94. $add_counter = 0;
  95. $update_counter = 0;
  96. $del_counter = 0;
  97. // 检查新增
  98. foreach ($settings as $item) {
  99. $config[] = $item['item'];
  100. $item_name = $item['item'];
  101. $query = (new Config())->where('item', $item['item'])->first();
  102. if ($query === null) {
  103. $new_item = new Config();
  104. $new_item->item = $item['item'];
  105. $new_item->value = $item['value'];
  106. $new_item->class = $item['class'];
  107. $new_item->is_public = $item['is_public'];
  108. $new_item->type = $item['type'];
  109. $new_item->default = $item['default'];
  110. $new_item->mark = $item['mark'];
  111. $new_item->save();
  112. echo '添加新数据库设置:' . $item_name . PHP_EOL;
  113. $add_counter += 1;
  114. continue;
  115. }
  116. if ($query->class !== $item['class']) {
  117. $query->class = $item['class'];
  118. $query->save();
  119. echo '更新数据库设置:' . $item_name . PHP_EOL;
  120. $update_counter += 1;
  121. }
  122. }
  123. // 检查移除
  124. $db_settings = Config::all();
  125. foreach ($db_settings as $db_setting) {
  126. if (! in_array($db_setting->item, $config)) {
  127. $db_setting->delete();
  128. $del_counter += 1;
  129. }
  130. }
  131. if ($add_counter !== 0) {
  132. echo '添加了 ' . $add_counter . ' 项新数据库设置' . PHP_EOL;
  133. }
  134. if ($update_counter !== 0) {
  135. echo '更新了 ' . $update_counter . ' 项数据库设置' . PHP_EOL;
  136. }
  137. if ($del_counter !== 0) {
  138. echo '移除了 ' . $del_counter . ' 项数据库设置' . PHP_EOL;
  139. }
  140. }
  141. public function resetNodePassword(): void
  142. {
  143. $nodes = Node::all();
  144. foreach ($nodes as $node) {
  145. $node->password = Tools::genRandomChar(32);
  146. $node->save();
  147. }
  148. echo '已重置所有节点密码' . PHP_EOL;
  149. }
  150. public function resetNodeBandwidth(): void
  151. {
  152. $nodes = Node::all();
  153. foreach ($nodes as $node) {
  154. $node->node_bandwidth = 0;
  155. $node->save();
  156. }
  157. echo '已重置所有节点流量' . PHP_EOL;
  158. }
  159. /**
  160. * 重置所有用户端口
  161. */
  162. public function resetPort(): void
  163. {
  164. $users = ModelsUser::all();
  165. if (count($users) === 0 || count($users) >= 65535) {
  166. echo '无效的用户数量' . PHP_EOL;
  167. return;
  168. }
  169. (new ModelsUser())->update([
  170. 'port' => 0,
  171. ]);
  172. foreach ($users as $user) {
  173. $user->port = Tools::getSsPort();
  174. $user->save();
  175. }
  176. echo '已重置所有用户端口' . PHP_EOL;
  177. }
  178. /**
  179. * 重置所有用户流量
  180. */
  181. public function resetBandwidth(): void
  182. {
  183. (new ModelsUser())->where('is_banned', 0)->update([
  184. 'd' => 0,
  185. 'u' => 0,
  186. 'transfer_today' => 0,
  187. ]);
  188. echo '已重置所有用户流量' . PHP_EOL;
  189. }
  190. /**
  191. * 重置今日流量
  192. */
  193. public function resetTodayBandwidth(): void
  194. {
  195. (new ModelsUser())->query()->update(['transfer_today' => 0]);
  196. echo '已重置今日流量' . PHP_EOL;
  197. }
  198. /**
  199. * 重置所有用户登录密码
  200. */
  201. public function resetPassword(): void
  202. {
  203. $users = ModelsUser::all();
  204. foreach ($users as $user) {
  205. $user->pass = Hash::passwordHash(Tools::genRandomChar(32));
  206. $user->save();
  207. }
  208. echo '已重置所有用户登录密码' . PHP_EOL;
  209. }
  210. /**
  211. * 重置所有用户连接密码
  212. */
  213. public function resetPasswd(): void
  214. {
  215. $users = ModelsUser::all();
  216. foreach ($users as $user) {
  217. $user->passwd = Tools::genRandomChar(16);
  218. $user->save();
  219. }
  220. echo '已重置所有用户连接密码' . PHP_EOL;
  221. }
  222. /**
  223. * 清除用户 Sub Token
  224. */
  225. public function clearSubToken(): void
  226. {
  227. Link::query()->truncate();
  228. echo '已清除所有用户 Sub Token' . PHP_EOL;
  229. }
  230. /**
  231. * 为所有用户生成新的 UUID
  232. */
  233. public function generateUUID(): void
  234. {
  235. $users = ModelsUser::all();
  236. foreach ($users as $user) {
  237. $user->uuid = Uuid::uuid4();
  238. $user->save();
  239. }
  240. echo '已为所有用户生成新的 UUID' . PHP_EOL;
  241. }
  242. /**
  243. * 二次验证
  244. */
  245. public function generateGa(): void
  246. {
  247. $users = ModelsUser::all();
  248. foreach ($users as $user) {
  249. try {
  250. $user->ga_token = MFA::generateGaToken();
  251. $user->save();
  252. } catch (Exception $e) {
  253. echo $e->getMessage();
  254. }
  255. }
  256. echo '已为所有用户生成新的 Ga Secret' . PHP_EOL;
  257. }
  258. /**
  259. * 为所有用户生成新的 Api Token
  260. */
  261. public function generateApiToken(): void
  262. {
  263. $users = ModelsUser::all();
  264. foreach ($users as $user) {
  265. $user->api_token = Tools::genRandomChar(32);
  266. $user->save();
  267. }
  268. echo '已为所有用户生成新的 Api Token' . PHP_EOL;
  269. }
  270. /**
  271. * 为所有用户设置新的主题
  272. */
  273. public function setTheme(): void
  274. {
  275. fwrite(STDOUT, '请输入要设置的主题名称: ');
  276. $theme = trim(fgets(STDIN));
  277. $users = ModelsUser::all();
  278. foreach ($users as $user) {
  279. $user->theme = $theme;
  280. $user->save();
  281. }
  282. echo '已为所有用户设置新的主题: ' . $theme . PHP_EOL;
  283. }
  284. /**
  285. * Set locale for all users
  286. */
  287. public function setLocale(): void
  288. {
  289. fwrite(STDOUT, 'Please input the new locale: ');
  290. $locale = trim(fgets(STDIN));
  291. $users = ModelsUser::all();
  292. foreach ($users as $user) {
  293. $user->locale = $locale;
  294. $user->save();
  295. }
  296. echo 'Set locale for all users successfully.' . PHP_EOL;
  297. }
  298. /**
  299. * 创建 Admin 账户
  300. *
  301. * @throws Exception
  302. */
  303. public function createAdmin(): void
  304. {
  305. $y = '';
  306. $email = '';
  307. $passwd = '';
  308. if (count($this->argv) === 3) {
  309. // ask for input
  310. echo '(1/3) 请输入管理员邮箱:' . PHP_EOL;
  311. // get input
  312. $email = trim(fgets(STDIN));
  313. // write input back
  314. echo '(2/3) 请输入管理员账户密码:' . PHP_EOL;
  315. $passwd = trim(fgets(STDIN));
  316. echo '(3/3) 按 Y 或 y 确认创建:';
  317. $y = trim(fgets(STDIN));
  318. } elseif (count($this->argv) === 5) {
  319. [,,, $email, $passwd] = $this->argv;
  320. $y = 'y';
  321. }
  322. if (strtolower($y) === 'y') {
  323. // do reg user
  324. $user = new ModelsUser();
  325. $user->user_name = 'Admin';
  326. $user->email = $email;
  327. $user->remark = '';
  328. $user->pass = Hash::passwordHash($passwd);
  329. $user->passwd = Tools::genRandomChar(16);
  330. $user->uuid = Uuid::uuid4();
  331. $user->api_token = Tools::genRandomChar(32);
  332. $user->port = Tools::getSsPort();
  333. $user->u = 0;
  334. $user->d = 0;
  335. $user->transfer_enable = 0;
  336. $user->ref_by = 0;
  337. $user->is_admin = 1;
  338. $user->reg_date = date('Y-m-d H:i:s');
  339. $user->money = 0;
  340. $user->im_type = 0;
  341. $user->im_value = '';
  342. $user->class = 0;
  343. $user->node_iplimit = 0;
  344. $user->node_speedlimit = 0;
  345. $user->theme = $_ENV['theme'];
  346. $user->locale = $_ENV['locale'];
  347. $user->ga_token = MFA::generateGaToken();
  348. $user->ga_enable = 0;
  349. if ($user->save()) {
  350. echo '创建成功,请在主页登录' . PHP_EOL;
  351. } else {
  352. echo '创建失败,请检查数据库配置' . PHP_EOL;
  353. }
  354. } else {
  355. echo '已取消创建' . PHP_EOL;
  356. }
  357. }
  358. public function updateGeoIP2(): void
  359. {
  360. if ($_ENV['maxmind_account_id'] !== '' && $_ENV['maxmind_license_key'] !== '') {
  361. echo 'Updating GeoIP2 database...' . PHP_EOL;
  362. $client = new Client([
  363. 'account_id' => $_ENV['maxmind_account_id'],
  364. 'license_key' => $_ENV['maxmind_license_key'],
  365. 'dir' => BASE_PATH . '/storage/',
  366. 'editions' => ['GeoLite2-City', 'GeoLite2-Country'],
  367. ]);
  368. try {
  369. $client->run();
  370. echo 'Successfully updated GeoIP2 database.' . PHP_EOL;
  371. } catch (Exception $e) {
  372. echo 'Update GeoIP2 database failed.' . PHP_EOL;
  373. echo $e->getMessage() . PHP_EOL;
  374. }
  375. } else {
  376. echo 'Please configure maxmind_account_id & maxmind_license_key in config/.config.php' . PHP_EOL;
  377. }
  378. }
  379. }