User.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Command;
  3. use Exception;
  4. use App\Utils\GA;
  5. use App\Utils\Hash;
  6. use App\Utils\Tools;
  7. use Ramsey\Uuid\Uuid;
  8. use App\Services\Config;
  9. use App\Models\Setting;
  10. use App\Models\User as ModelsUser;
  11. use App\Controllers\AuthController;
  12. class User extends Command
  13. {
  14. public $description = ''
  15. . '├─=: php xcat User [选项]' . PHP_EOL
  16. . '│ ├─ getCookie - 获取指定用户的 Cookie' . PHP_EOL
  17. . '│ ├─ resetPort - 重置单个用户端口' . PHP_EOL
  18. . '│ ├─ createAdmin - 创建管理员帐号' . PHP_EOL
  19. . '│ ├─ resetAllPort - 重置所有用户端口' . PHP_EOL
  20. . '│ ├─ resetTraffic - 重置所有用户流量' . PHP_EOL
  21. . '│ ├─ generateUUID - 为所有用户生成新的 UUID' . PHP_EOL
  22. . '│ ├─ generateGa - 为所有用户生成新的 Ga Secret' . PHP_EOL;
  23. public function boot()
  24. {
  25. if (count($this->argv) === 2) {
  26. echo $this->description;
  27. } else {
  28. $methodName = $this->argv[2];
  29. if (method_exists($this, $methodName)) {
  30. $this->$methodName();
  31. } else {
  32. echo '方法不存在.' . PHP_EOL;
  33. }
  34. }
  35. }
  36. /**
  37. * 重置用户端口
  38. *
  39. * @return void
  40. */
  41. public function resetPort()
  42. {
  43. fwrite(STDOUT, '请输入用户id: ');
  44. $user = ModelsUser::find(trim(fgets(STDIN)));
  45. if ($user !== null) {
  46. $user->port = Tools::getAvPort();
  47. if ($user->save()) {
  48. echo '重置成功!' . PHP_EOL;
  49. }
  50. } else {
  51. echo 'not found user.' . PHP_EOL;
  52. }
  53. }
  54. /**
  55. * 重置所有用户端口
  56. *
  57. * @return void
  58. */
  59. public function resetAllPort()
  60. {
  61. $users = ModelsUser::all();
  62. foreach ($users as $user) {
  63. $origin_port = $user->port;
  64. $user->port = Tools::getAvPort();
  65. echo '$origin_port=' . $origin_port . '&$user->port=' . $user->port . PHP_EOL;
  66. $user->save();
  67. }
  68. }
  69. /**
  70. * 重置所有用户流量
  71. *
  72. * @return void
  73. */
  74. public function resetTraffic()
  75. {
  76. try {
  77. ModelsUser::where('enable', 1)->update([
  78. 'd' => 0,
  79. 'u' => 0,
  80. 'last_day_t' => 0,
  81. ]);
  82. } catch (Exception $e) {
  83. echo $e->getMessage();
  84. return;
  85. }
  86. echo 'reset traffic successful' . PHP_EOL;
  87. }
  88. /**
  89. * 为所有用户生成新的UUID
  90. *
  91. * @return void
  92. */
  93. public function generateUUID()
  94. {
  95. $users = ModelsUser::all();
  96. $current_timestamp = time();
  97. foreach ($users as $user) {
  98. /** @var ModelsUser $user */
  99. $user->generateUUID($current_timestamp);
  100. }
  101. echo 'generate UUID successful' . PHP_EOL;
  102. }
  103. /**
  104. * 二次验证
  105. *
  106. * @return void
  107. */
  108. public function generateGa()
  109. {
  110. $users = ModelsUser::all();
  111. foreach ($users as $user) {
  112. $ga = new GA();
  113. $secret = $ga->createSecret();
  114. $user->ga_token = $secret;
  115. $user->save();
  116. }
  117. echo 'generate Ga Secret successful' . PHP_EOL;
  118. }
  119. /**
  120. * 创建 Admin 账户
  121. *
  122. * @return void
  123. */
  124. public function createAdmin()
  125. {
  126. if (count($this->argv) === 3) {
  127. // ask for input
  128. fwrite(STDOUT, '(1/3) 请输入管理员邮箱:') . PHP_EOL;
  129. // get input
  130. $email = trim(fgets(STDIN));
  131. if ($email == null) {
  132. die("必须输入管理员邮箱.\r\n");
  133. }
  134. // write input back
  135. fwrite(STDOUT, "(2/3) 请输入管理员账户密码:") . PHP_EOL;
  136. $passwd = trim(fgets(STDIN));
  137. if ($passwd == null) {
  138. die("必须输入管理员密码.\r\n");
  139. }
  140. fwrite(STDOUT, "(3/3) 按 Y 或 y 确认创建:");
  141. $y = trim(fgets(STDIN));
  142. } elseif (count($this->argv) === 5) {
  143. [,,, $email, $passwd] = $this->argv;
  144. $y = 'y';
  145. }
  146. if (strtolower($y) == 'y') {
  147. try {
  148. AuthController::register_helper('admin', $email, $passwd, '', '1', '', 0, false);
  149. } catch (\Exception $e) {
  150. $error_msg = $e->getMessage();
  151. }
  152. if (!empty($error_msg)) {
  153. echo PHP_EOL . '创建失败,以下是错误信息:' . PHP_EOL;
  154. die($error_msg);
  155. }
  156. echo PHP_EOL . '创建成功,请在主页登录' . PHP_EOL;
  157. } else {
  158. echo PHP_EOL . '已取消创建' . PHP_EOL;
  159. }
  160. }
  161. /**
  162. * 获取 USERID 的 Cookie
  163. *
  164. * @return void
  165. */
  166. public function getCookie()
  167. {
  168. if (count($this->argv) === 4) {
  169. $user = ModelsUser::find($this->argv[3]);
  170. $expire_in = 86400 + time();
  171. echo Hash::cookieHash($user->pass, $expire_in) . ' ' . $expire_in;
  172. }
  173. }
  174. }