ProxyService.php 6.9 KB

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