ProxyService.php 7.6 KB

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