ProxyService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace App\Services;
  3. use App\Helpers\ClientConfig;
  4. use App\Models\Node;
  5. use App\Models\User;
  6. use App\Utils\Clients\Text;
  7. use App\Utils\Clients\URLSchemes;
  8. use Arr;
  9. class ProxyService
  10. {
  11. use ClientConfig;
  12. private static ?User $user;
  13. private static array $servers;
  14. public function __construct(?User $user = null)
  15. {
  16. $this->setUser($user ?? auth()->user());
  17. }
  18. public function setUser(?User $user = null): void
  19. {
  20. self::$user = $user;
  21. }
  22. public function getUser(): ?User
  23. {
  24. return self::$user;
  25. }
  26. public function getServers(): array
  27. {
  28. return self::$servers;
  29. }
  30. public function getProxyText(string $target, ?int $type = null): string
  31. {
  32. $servers = $this->getNodeList($type);
  33. if (empty($servers)) {
  34. return $this->failedProxyReturn(trans('errors.subscribe.none'), $type);
  35. }
  36. if (sysConfig('rand_subscribe')) {// 打乱数组
  37. $servers = Arr::shuffle($servers);
  38. }
  39. $max = (int) sysConfig('subscribe_max');
  40. if ($max && count($servers) > $max) { // 订阅数量限制
  41. $servers = Arr::random($servers, $max);
  42. }
  43. $this->setServers($servers);
  44. return $this->clientConfig($target);
  45. }
  46. public function getNodeList(?int $type = null, bool $isConfig = true): array
  47. {
  48. $query = self::$user->nodes()->whereIn('is_display', [2, 3]); // 获取这个账号可用节点
  49. if ($type) {
  50. if ($type === 1) {
  51. $query = $query->whereIn('type', [1, 4]);
  52. } else {
  53. $query = $query->whereType($type);
  54. }
  55. }
  56. $nodes = $query->orderByDesc('sort')->orderBy('id')->get();
  57. if ($isConfig) {
  58. $servers = [];
  59. foreach ($nodes as $node) {
  60. $servers[] = $this->getProxyConfig($node);
  61. }
  62. return $servers;
  63. }
  64. return $nodes;
  65. }
  66. public function getProxyConfig(Node $node): array
  67. { // 提取节点信息
  68. $user = self::$user;
  69. $config = [
  70. 'id' => $node->id,
  71. 'name' => $node->name,
  72. 'area' => $node->country->name,
  73. 'host' => $node->host,
  74. 'group' => sysConfig('website_name'),
  75. 'udp' => $node->is_udp,
  76. ];
  77. if ($node->relay_node_id) {
  78. $parentConfig = $this->getProxyConfig($node->relayNode);
  79. $config = array_merge($config, Arr::except($parentConfig, ['id', 'name', 'host', 'group', 'udp']));
  80. if ($parentConfig['type'] === 'trojan') {
  81. $config['sni'] = $parentConfig['host'];
  82. }
  83. $config['port'] = $node->port;
  84. } else {
  85. switch ($node->type) {
  86. case 0:
  87. $config = array_merge($config, [
  88. 'type' => 'shadowsocks',
  89. 'passwd' => $user->passwd,
  90. ], $node->profile);
  91. if ($node->port && $node->port !== 0) {
  92. $config['port'] = $node->port;
  93. } else {
  94. $config['port'] = $user->port;
  95. }
  96. break;
  97. case 2:
  98. $config = array_merge($config, [
  99. 'type' => 'v2ray',
  100. 'port' => $node->port,
  101. 'uuid' => $user->vmess_id,
  102. ], $node->profile);
  103. break;
  104. case 3:
  105. $config = array_merge($config, [
  106. 'type' => 'trojan',
  107. 'port' => $node->port,
  108. 'passwd' => $user->passwd,
  109. 'sni' => '',
  110. ], $node->profile);
  111. break;
  112. case 1:
  113. case 4:
  114. default:
  115. $config = array_merge($config, ['type' => 'shadowsocksr'], $node->profile);
  116. if ($node->profile['passwd'] && $node->port) {
  117. //单端口使用中转的端口
  118. $config['port'] = $node->port;
  119. $config['protocol_param'] = $user->port.':'.$user->passwd;
  120. } else {
  121. $config['port'] = $user->port;
  122. $config['passwd'] = $user->passwd;
  123. if ($node->type === 1) {
  124. $config['method'] = $user->method;
  125. $config['protocol'] = $user->protocol;
  126. $config['obfs'] = $user->obfs;
  127. }
  128. }
  129. break;
  130. }
  131. }
  132. return $config;
  133. }
  134. public function failedProxyReturn(string $text, ?int $type = 1): string
  135. {
  136. $url = sysConfig('website_url');
  137. return match ($type) {
  138. 1 => 'vmess://'.base64url_encode(json_encode(['v' => '2', 'ps' => $text, 'add' => $url, 'port' => 0, 'id' => 0, 'aid' => 0, 'net' => 'tcp', 'type' => 'none', 'host' => $url, 'path' => '/', 'tls' => 'tls'], JSON_PRETTY_PRINT)),
  139. 2 => 'trojan://[email protected]:0?peer=0.0.0.0#'.rawurlencode($text),
  140. default => 'ssr://'.base64url_encode('0.0.0.0:0:origin:none:plain:MDAwMA/?obfsparam=&protoparam=&remarks='.base64url_encode($text).'&group='.base64url_encode(sysConfig('website_name')).'&udpport=0&uot=0'),
  141. }.PHP_EOL;
  142. }
  143. private function setServers(array $servers): void
  144. {
  145. self::$servers = $servers;
  146. }
  147. public function getProxyCode(string $target, ?int $type = null): ?string
  148. {// 客户端用代理信息
  149. $servers = $this->getNodeList($type);
  150. if (empty($servers)) {
  151. return null;
  152. }
  153. $this->setServers($servers);
  154. return $this->clientConfig($target);
  155. }
  156. public function getUserProxyConfig(array $server, bool $is_url): string
  157. { // 用户显示用代理信息
  158. $type = $is_url ? new URLSchemes() : new Text();
  159. return match ($server['type']) {
  160. 'shadowsocks' => $type->buildShadowsocks($server),
  161. 'shadowsocksr' => $type->buildShadowsocksr($server),
  162. 'v2ray' => $type->buildVmess($server),
  163. 'trojan' => $type->buildTrojan($server),
  164. };
  165. }
  166. }