Tool.php 12 KB

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