ProxyService.php 6.9 KB

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