Tool.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Command;
  4. use App\Models\Node;
  5. use App\Models\Setting;
  6. use App\Models\User as ModelsUser;
  7. use App\Utils\GA;
  8. use App\Utils\Hash;
  9. use App\Utils\Tools;
  10. use Exception;
  11. use Ramsey\Uuid\Uuid;
  12. final class Tool extends Command
  13. {
  14. public $description = <<<EOL
  15. ├─=: php xcat Tool [选项]
  16. │ ├─ setTelegram - 设置 Telegram 机器人
  17. │ ├─ resetAllSettings - 使用默认值覆盖设置中心设置
  18. │ ├─ exportAllSettings - 导出所有设置
  19. │ ├─ importAllSettings - 导入所有设置
  20. │ ├─ upgradeDatabase - 升级(如果不存在的话初始化) 数据库
  21. │ ├─ resetNodePassword - 重置所有节点通讯密钥
  22. │ ├─ getCookie - 获取指定用户的 Cookie
  23. │ ├─ resetPort - 重置单个用户端口
  24. │ ├─ createAdmin - 创建管理员帐号
  25. │ ├─ resetAllPort - 重置所有用户端口
  26. │ ├─ resetTraffic - 重置所有用户流量
  27. │ ├─ generateUUID - 为所有用户生成新的 UUID
  28. │ ├─ generateGa - 为所有用户生成新的 Ga Secret
  29. │ ├─ setTheme - 为所有用户设置新的主题
  30. EOL;
  31. public function boot(): void
  32. {
  33. if (count($this->argv) === 2) {
  34. echo $this->description;
  35. } else {
  36. $methodName = $this->argv[2];
  37. if (method_exists($this, $methodName)) {
  38. $this->$methodName();
  39. } else {
  40. echo '方法不存在.' . PHP_EOL;
  41. }
  42. }
  43. }
  44. public function setTelegram(): void
  45. {
  46. $WebhookUrl = $_ENV['baseUrl'] . '/telegram_callback?token=' . $_ENV['telegram_request_token'];
  47. $telegram = new \Telegram\Bot\Api($_ENV['telegram_token']);
  48. $telegram->removeWebhook();
  49. if ($telegram->setWebhook(['url' => $WebhookUrl])) {
  50. echo 'Bot @' . $telegram->getMe()->getUsername() . ' 设置成功!' . PHP_EOL;
  51. } else {
  52. echo '设置失败!' . PHP_EOL;
  53. }
  54. }
  55. public function resetAllSettings(): void
  56. {
  57. $settings = Setting::all();
  58. foreach ($settings as $setting) {
  59. $setting->value = $setting->default;
  60. $setting->save();
  61. }
  62. echo '已使用默认值覆盖所有设置.' . PHP_EOL;
  63. }
  64. public function exportAllSettings(): void
  65. {
  66. $settings = Setting::all();
  67. foreach ($settings as $setting) {
  68. // 因为主键自增所以即便设置为 null 也会在导入时自动分配 id
  69. // 同时避免多位开发者 pull request 时 settings.json 文件 id 重复所可能导致的冲突
  70. $setting->id = null;
  71. // 避免开发者调试配置泄露
  72. $setting->value = $setting->default;
  73. }
  74. $json_settings = \json_encode($settings, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  75. file_put_contents('./config/settings.json', $json_settings);
  76. echo '已导出所有设置.' . PHP_EOL;
  77. }
  78. public function importAllSettings(): void
  79. {
  80. $json_settings = file_get_contents('./config/settings.json');
  81. $settings = \json_decode($json_settings, true);
  82. $config = [];
  83. $add_counter = 0;
  84. $del_counter = 0;
  85. // 检查新增
  86. foreach ($settings as $item) {
  87. $config[] = $item['item'];
  88. $item_name = $item['item'];
  89. $query = Setting::where('item', '=', $item['item'])->first();
  90. if ($query === null) {
  91. $new_item = new Setting();
  92. $new_item->id = null;
  93. $new_item->item = $item['item'];
  94. $new_item->value = $item['value'];
  95. $new_item->class = $item['class'];
  96. $new_item->is_public = $item['is_public'];
  97. $new_item->type = $item['type'];
  98. $new_item->default = $item['default'];
  99. $new_item->mark = $item['mark'];
  100. $new_item->save();
  101. echo "添加新设置:${item_name}" . PHP_EOL;
  102. $add_counter += 1;
  103. }
  104. }
  105. // 检查移除
  106. $db_settings = Setting::all();
  107. foreach ($db_settings as $db_setting) {
  108. if (! \in_array($db_setting->item, $config)) {
  109. $db_setting->delete();
  110. $del_counter += 1;
  111. }
  112. }
  113. if ($add_counter !== 0) {
  114. echo "总计添加了 ${add_counter} 条新设置." . PHP_EOL;
  115. } else {
  116. echo '没有任何新设置需要添加.' . PHP_EOL;
  117. }
  118. if ($del_counter !== 0) {
  119. echo "总计移除了 ${del_counter} 条设置." . PHP_EOL;
  120. }
  121. }
  122. public function upgradeDatabase(): void
  123. {
  124. $phinx = new \Phinx\Console\PhinxApplication();
  125. $phinx->run();
  126. }
  127. public function resetNodePassword(): void
  128. {
  129. $nodes = Node::all();
  130. foreach ($nodes as $node) {
  131. $node->password = Tools::genRandomChar(32);
  132. $node->save();
  133. }
  134. echo '已重置所有节点密码.' . PHP_EOL;
  135. }
  136. /**
  137. * 重置用户端口
  138. */
  139. public function resetPort(): void
  140. {
  141. fwrite(STDOUT, '请输入用户id: ');
  142. $user = ModelsUser::find(trim(fgets(STDIN)));
  143. if ($user !== null) {
  144. $user->port = Tools::getAvPort();
  145. if ($user->save()) {
  146. echo '重置成功!';
  147. }
  148. } else {
  149. echo 'not found user.';
  150. }
  151. }
  152. /**
  153. * 重置所有用户端口
  154. */
  155. public function resetAllPort(): void
  156. {
  157. $users = ModelsUser::all();
  158. foreach ($users as $user) {
  159. $origin_port = $user->port;
  160. $user->port = Tools::getAvPort();
  161. echo '$origin_port=' . $origin_port . '&$user->port=' . $user->port . PHP_EOL;
  162. $user->save();
  163. }
  164. }
  165. /**
  166. * 重置所有用户流量
  167. */
  168. public function resetTraffic(): void
  169. {
  170. try {
  171. ModelsUser::where('enable', 1)->update([
  172. 'd' => 0,
  173. 'u' => 0,
  174. 'last_day_t' => 0,
  175. ]);
  176. } catch (Exception $e) {
  177. echo $e->getMessage();
  178. return;
  179. }
  180. echo 'reset traffic successful';
  181. }
  182. /**
  183. * 为所有用户生成新的UUID
  184. */
  185. public function generateUUID(): void
  186. {
  187. $users = ModelsUser::all();
  188. $current_timestamp = \time();
  189. foreach ($users as $user) {
  190. /** @var ModelsUser $user */
  191. $user->generateUUID($current_timestamp);
  192. }
  193. echo 'generate UUID successful';
  194. }
  195. /**
  196. * 二次验证
  197. */
  198. public function generateGa(): void
  199. {
  200. $users = ModelsUser::all();
  201. foreach ($users as $user) {
  202. $ga = new GA();
  203. $secret = $ga->createSecret();
  204. $user->ga_token = $secret;
  205. $user->save();
  206. }
  207. echo 'generate Ga Secret successful';
  208. }
  209. /**
  210. * 创建 Admin 账户
  211. */
  212. public function createAdmin(): void
  213. {
  214. if (count($this->argv) === 3) {
  215. // ask for input
  216. fwrite(STDOUT, '(1/3) 请输入管理员邮箱:') . PHP_EOL;
  217. // get input
  218. $email = trim(fgets(STDIN));
  219. if ($email === null) {
  220. die("必须输入管理员邮箱.\r\n");
  221. }
  222. // write input back
  223. fwrite(STDOUT, '(2/3) 请输入管理员账户密码:') . PHP_EOL;
  224. $passwd = trim(fgets(STDIN));
  225. if ($passwd === null) {
  226. die("必须输入管理员密码.\r\n");
  227. }
  228. fwrite(STDOUT, '(3/3) 按 Y 或 y 确认创建:');
  229. $y = trim(fgets(STDIN));
  230. } elseif (count($this->argv) === 5) {
  231. [,,, $email, $passwd] = $this->argv;
  232. $y = 'y';
  233. }
  234. if (strtolower($y) === 'y') {
  235. $current_timestamp = \time();
  236. // create admin user
  237. $configs = Setting::getClass('register');
  238. // do reg user
  239. $user = new ModelsUser();
  240. $user->user_name = 'admin';
  241. $user->email = $email;
  242. $user->remark = 'admin';
  243. $user->pass = Hash::passwordHash($passwd);
  244. $user->passwd = Tools::genRandomChar(16);
  245. $user->uuid = Uuid::uuid3(Uuid::NAMESPACE_DNS, $email . '|' . $current_timestamp);
  246. $user->port = Tools::getLastPort() + 1;
  247. $user->t = 0;
  248. $user->u = 0;
  249. $user->d = 0;
  250. $user->transfer_enable = Tools::toGB($configs['sign_up_for_free_traffic']);
  251. $user->invite_num = $configs['sign_up_for_invitation_codes'];
  252. $user->ref_by = 0;
  253. $user->is_admin = 1;
  254. $user->expire_in = date('Y-m-d H:i:s', \time() + $configs['sign_up_for_free_time'] * 86400);
  255. $user->reg_date = date('Y-m-d H:i:s');
  256. $user->money = 0;
  257. $user->im_type = 1;
  258. $user->im_value = '';
  259. $user->class = 0;
  260. $user->node_speedlimit = 0;
  261. $user->theme = $_ENV['theme'];
  262. $ga = new GA();
  263. $secret = $ga->createSecret();
  264. $user->ga_token = $secret;
  265. $user->ga_enable = 0;
  266. if ($user->save()) {
  267. echo '创建成功,请在主页登录' . PHP_EOL;
  268. } else {
  269. echo '创建失败,请检查数据库配置' . PHP_EOL;
  270. }
  271. } else {
  272. echo '已取消创建' . PHP_EOL;
  273. }
  274. }
  275. /**
  276. * 获取 USERID 的 Cookie
  277. */
  278. public function getCookie(): void
  279. {
  280. if (count($this->argv) === 4) {
  281. $user = ModelsUser::find($this->argv[3]);
  282. $expire_in = 86400 + \time();
  283. echo Hash::cookieHash($user->pass, $expire_in) . ' ' . $expire_in;
  284. }
  285. }
  286. /**
  287. * 为所有用户设置新的主题
  288. */
  289. public function setTheme(): void
  290. {
  291. fwrite(STDOUT, '请输入要设置的主题名称: ');
  292. $theme = trim(fgets(STDIN));
  293. $users = ModelsUser::all();
  294. foreach ($users as $user) {
  295. $user->theme = $theme;
  296. $user->save();
  297. }
  298. }
  299. }