1
0

NodeService.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Node;
  4. use Illuminate\Database\Eloquent\Builder;
  5. class NodeService
  6. {
  7. public function getActiveNodeTypes(?Builder $nodes = null): array
  8. {
  9. // / 1. 获取 Node 存在的 type 集合并去重
  10. $nodeTypes = ($nodes ?? Node::whereStatus(1))->pluck('type')->unique()->toArray();
  11. $protocols = config('common.proxy_protocols', []);
  12. $result = [];
  13. foreach ($nodeTypes as $type) {
  14. if (! isset($protocols[$type])) {
  15. continue;
  16. }
  17. // 特殊逻辑:type 4 映射到 key 1,其余保持不变
  18. $key = ($type === 4) ? 1 : $type;
  19. $result[$key] = $protocols[$key];
  20. }
  21. return $result;
  22. }
  23. public function getNodeDeploymentConfig(Node $node): array
  24. {
  25. $webApi = sysConfig('web_api_url') ?: sysConfig('website_url');
  26. return match ($node->type) {
  27. 1,4 => $this->getVnetConfig($node, $webApi),
  28. 2 => $this->getV2RayConfig($node, $webApi),
  29. 3 => $this->getTrojanConfig($node, $webApi),
  30. 5 => $this->getHysteria2Config($node),
  31. default => $this->getDefaultConfig(),
  32. };
  33. }
  34. private function getVnetConfig(Node $node, string $webApi): array
  35. {
  36. return [[
  37. 'name' => 'VNET',
  38. 'commands' => [
  39. 'install' => "(yum install curl 2> /dev/null || apt install curl 2> /dev/null) \\\n&& curl -L -s https://bit.ly/3828OP1 \\\n| WEB_API=\"$webApi\" \\\nNODE_ID=$node->id \\\nNODE_KEY={$node->auth->key} \\\nbash",
  40. 'update' => trans('admin.node.auth.deploy.same'),
  41. 'uninstall' => 'curl -L -s https://bit.ly/3828OP1 | bash -s -- --remove',
  42. 'start' => 'systemctl start vnet',
  43. 'stop' => 'systemctl stop vnet',
  44. 'restart' => 'systemctl restart vnet',
  45. 'status' => 'systemctl status vnet',
  46. 'recent_logs' => 'journalctl -x -n 300 --no-pager -u vnet',
  47. 'real_time_logs' => 'journalctl -u vnet -f',
  48. ],
  49. ]];
  50. }
  51. private function getV2RayConfig(Node $node, string $webApi): array
  52. {
  53. return [
  54. [
  55. 'name' => 'VNET-V2Ray',
  56. 'commands' => [
  57. 'install' => "(yum install curl 2> /dev/null || apt install curl 2> /dev/null) \\\n&& curl -L -s https://bit.ly/3oO3HZy \\\n| WEB_API=\"$webApi\" \\\nNODE_ID=$node->id \\\nNODE_KEY={$node->auth->key} \\\nbash",
  58. 'update' => trans('admin.node.auth.deploy.same'),
  59. 'uninstall' => 'curl -L -s https://bit.ly/3oO3HZy | bash -s -- --remove',
  60. 'start' => 'systemctl start vnet-v2ray',
  61. 'stop' => 'systemctl stop vnet-v2ray',
  62. 'status' => 'systemctl status vnet-v2ray',
  63. 'recent_logs' => 'journalctl -x -n 300 --no-pager -u vnet-v2ray',
  64. 'real_time_logs' => 'journalctl -u vnet-v2ray -f',
  65. ],
  66. ],
  67. [
  68. 'name' => 'V2Ray-Poseidon',
  69. 'commands' => [
  70. 'install' => "(yum install curl 2> /dev/null || apt install curl 2> /dev/null) \\\n&& curl -L -s https://bit.ly/2HswWko \\\n| WEB_API=\"$webApi\" \\\nNODE_ID=$node->id \\\nNODE_KEY={$node->auth->key} \\\nbash",
  71. 'update' => 'curl -L -s https://bit.ly/2HswWko | bash',
  72. 'uninstall' => 'curl -L -s https://mrw.so/5IHPR4 | bash',
  73. 'start' => 'systemctl start v2ray',
  74. 'stop' => 'systemctl stop v2ray',
  75. 'status' => 'systemctl status v2ray',
  76. 'recent_logs' => 'journalctl -x -n 300 --no-pager -u v2ray',
  77. 'real_time_logs' => 'journalctl -u v2ray -f',
  78. ],
  79. ],
  80. ];
  81. }
  82. private function getTrojanConfig(Node $node, string $webApi): array
  83. {
  84. if (empty($node->host)) {
  85. return [
  86. 'requires_host' => true,
  87. 'edit_url' => route('admin.node.edit', $node->id),
  88. ];
  89. }
  90. return [[
  91. 'name' => 'Trojan-Poseidon',
  92. 'commands' => [
  93. 'install' => "(yum install curl 2> /dev/null || apt install curl 2> /dev/null) \\\n&& curl -L -s https://mrw.so/6cMfGy \\\n| WEB_API=\"$webApi\" \\\nNODE_ID=$node->id \\\nNODE_KEY={$node->auth->key} \\\nNODE_HOST=$node->host \\\nbash",
  94. 'update' => 'curl -L -s https://mrw.so/6cMfGy | bash',
  95. 'uninstall' => 'curl -L -s https://mrw.so/5ulpvu | bash',
  96. 'start' => 'systemctl start trojanp',
  97. 'stop' => 'systemctl stop trojanp',
  98. 'status' => 'systemctl status trojanp',
  99. 'recent_logs' => 'journalctl -x -n 300 --no-pager -u trojanp',
  100. 'real_time_logs' => 'journalctl -u trojanp -f',
  101. ],
  102. ]];
  103. }
  104. private function getHysteria2Config(Node $node): array
  105. {
  106. // 生成配置文件内容
  107. $configContent = "listen: :$node->port\n";
  108. if (! empty($node->host)) {
  109. $configContent .= "acme:\n domains:\n - $node->host\n email: ".sysConfig('webmaster_email')."\n";
  110. }
  111. $configContent .= "auth:\n type: http\n http:\n url: ".route('api.hysteria2.auth', $node->id)."\n insecure: ".($node->allow_insecure ? 'true' : 'false')."\n";
  112. $configContent .= "masquerade:\n type: proxy\n proxy:\n url: https://bing.com\n rewriteHost: true\n";
  113. // 添加带宽配置
  114. if ($node->upload_mbps > 0 || $node->download_mbps > 0) {
  115. $configContent .= "bandwidth:\n";
  116. if ($node->upload_mbps > 0) {
  117. $configContent .= " up: $node->upload_mbps mbps\n";
  118. }
  119. if ($node->download_mbps > 0) {
  120. $configContent .= " down: $node->download_mbps mbps\n";
  121. }
  122. }
  123. $configContent .= 'ignoreClientBandwidth: '.($node->ignore_client_bandwidth ? 'true' : 'false')."\ntrafficStats:\n listen: :$node->push_port\n secret: {$node->auth->secret}\n";
  124. return [[
  125. 'name' => 'Hysteria2 Official',
  126. 'commands' => [
  127. 'install' => "(yum install curl 2> /dev/null || apt install curl 2> /dev/null) \\\n&& bash <(curl -fsSL https://get.hy2.sh/) \\\n&& mkdir -p /etc/hysteria \\\n&& cat > /etc/hysteria/config.yaml << EOF\n{$configContent}EOF",
  128. 'update' => 'bash <(curl -fsSL https://get.hy2.sh/)',
  129. 'uninstall' => 'bash <(curl -fsSL https://get.hy2.sh/) --remove',
  130. 'start' => 'systemctl start hysteria-server.service',
  131. 'stop' => 'systemctl stop hysteria-server.service',
  132. 'status' => 'systemctl status hysteria-server.service',
  133. 'recent_logs' => 'journalctl -x -n 300 --no-pager -u hysteria-server.service',
  134. 'real_time_logs' => 'journalctl -u hysteria-server.service -f',
  135. ],
  136. ]];
  137. }
  138. private function getDefaultConfig(): array
  139. {
  140. return [[
  141. 'name' => trans('common.none'),
  142. ]];
  143. }
  144. }