ProxyService.php 6.7 KB

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