ProxyService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. 'area' => $node->country->name,
  84. 'host' => $node->host,
  85. 'group' => sysConfig('website_name'),
  86. 'udp' => $node->is_udp,
  87. ];
  88. if ($node->relay_node_id) {
  89. $parentConfig = $this->getProxyConfig($node->relayNode);
  90. $config = array_merge($config, Arr::except($parentConfig, ['id', 'name', 'host', 'group', 'udp']));
  91. if ($parentConfig['type'] === 'trojan') {
  92. $config['sni'] = $parentConfig['host'];
  93. }
  94. $config['port'] = $node->port;
  95. } else {
  96. switch ($node->type) {
  97. case 0:
  98. $config = array_merge($config, [
  99. 'type' => 'shadowsocks',
  100. 'passwd' => $user->passwd,
  101. ], $node->profile);
  102. if ($node->port && $node->port !== 0) {
  103. $config['port'] = $node->port;
  104. } else {
  105. $config['port'] = $user->port;
  106. }
  107. break;
  108. case 2:
  109. $config = array_merge($config, [
  110. 'type' => 'vmess',
  111. 'port' => $node->port,
  112. 'uuid' => $user->vmess_id,
  113. ], $node->profile);
  114. break;
  115. case 3:
  116. $config = array_merge($config, [
  117. 'type' => 'trojan',
  118. 'port' => $node->port,
  119. 'passwd' => $user->passwd,
  120. 'sni' => '',
  121. ], $node->profile);
  122. break;
  123. case 1:
  124. case 4:
  125. default:
  126. $config = array_merge($config, ['type' => 'shadowsocksr'], $node->profile);
  127. if ($node->profile['passwd'] && $node->port) {
  128. //单端口使用中转的端口
  129. $config['port'] = $node->port;
  130. $config['protocol_param'] = $user->port.':'.$user->passwd;
  131. } else {
  132. $config['port'] = $user->port;
  133. $config['passwd'] = $user->passwd;
  134. if ($node->type === 1) {
  135. $config['method'] = $user->method;
  136. $config['protocol'] = $user->protocol;
  137. $config['obfs'] = $user->obfs;
  138. }
  139. }
  140. break;
  141. }
  142. }
  143. return $config;
  144. }
  145. public function failedProxyReturn(string $text, ?int $type = 0): void
  146. {
  147. $url = sysConfig('website_url');
  148. $data = [
  149. 'name' => $text,
  150. 'type' => [0 => 'shadowsocks', 1 => 'shadowsocksr', 2 => 'vmess', 3 => 'trojan'][$type],
  151. 'host' => $url,
  152. 'port' => 0,
  153. 'udp' => 0,
  154. ];
  155. $addition = match ($type) {
  156. 0 => ['method' => 'none', 'passwd' => 'error'],
  157. 1 => ['method' => 'none', 'passwd' => 'error', 'obfs' => 'origin', 'obfs_param' => '', 'protocol' => 'plain', 'protocol_param' => ''],
  158. 2 => ['uuid' => '0', 'v2_alter_id' => 0, 'method' => 'auto'],
  159. 3 => ['passwd' => 'error']
  160. };
  161. $this->setServers([array_merge($data, $addition)]);
  162. }
  163. private function setServers(array $servers): void
  164. {
  165. self::$servers = $servers;
  166. }
  167. private function getClientConfig(string $target): string
  168. {
  169. foreach (glob(app_path('Utils/Clients').'/*.php') as $file) {
  170. $class = 'App\\Utils\\Clients\\'.basename($file, '.php');
  171. $reflectionClass = new ReflectionClass($class);
  172. foreach ($reflectionClass->getConstant('AGENT') as $agent) {
  173. if (str_contains($target, $agent)) {
  174. return (new $class)->getConfig($this->getServers(), $this->getUser(), $target);
  175. }
  176. }
  177. }
  178. return URLSchemes::build($this->getServers()); // Origin
  179. }
  180. public function getProxyCode(string $target, ?int $type = null): ?string
  181. {// 客户端用代理信息
  182. $servers = $this->getNodeList($type);
  183. if (empty($servers)) {
  184. return null;
  185. }
  186. $this->setServers($servers);
  187. return $this->getClientConfig($target);
  188. }
  189. public function getUserProxyConfig(array $server, bool $is_url): string
  190. { // 用户显示用代理信息
  191. $type = $is_url ? new URLSchemes : new Text;
  192. return match ($server['type']) {
  193. 'shadowsocks' => $type->buildShadowsocks($server),
  194. 'shadowsocksr' => $type->buildShadowsocksr($server),
  195. 'vmess' => $type->buildVmess($server),
  196. 'trojan' => $type->buildTrojan($server),
  197. default => throw new Exception('Unsupported proxy type: '.$server['type']),
  198. };
  199. }
  200. }