ProxyServer.php 6.7 KB

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