ProxyService.php 6.2 KB

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