ClientController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\User;
  6. use App\Models\Plan;
  7. use App\Models\Server;
  8. use Symfony\Component\Yaml\Yaml;
  9. class ClientController extends Controller
  10. {
  11. public function subscribe (Request $request) {
  12. $user = $request->user;
  13. $server = [];
  14. if ($user->expired_at > time()) {
  15. $servers = Server::where('show', 1)->get();
  16. foreach ($servers as $item) {
  17. $groupId = json_decode($item['group_id']);
  18. if (in_array($user->group_id, $groupId)) {
  19. array_push($server, $item);
  20. }
  21. }
  22. }
  23. if(isset($_SERVER['HTTP_USER_AGENT'])) {
  24. if(strpos($_SERVER['HTTP_USER_AGENT'], 'Quantumult%20X') !== false) {
  25. die($this->quantumultX($user, $server));
  26. }
  27. if(strpos($_SERVER['HTTP_USER_AGENT'], 'Quantumult') !== false) {
  28. die($this->quantumult($user, $server));
  29. }
  30. if(strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'clash') !== false) {
  31. die($this->clash($user, $server));
  32. }
  33. }
  34. die($this->origin($user, $server));
  35. }
  36. private function quantumultX ($user, $server) {
  37. $uri = '';
  38. foreach($server as $item) {
  39. $uri .= "vmess=".$item->host.":".$item->port.", method=none, password=".$user->v2ray_uuid.", fast-open=false, udp-relay=false, tag=".$item->name;
  40. if ($item->network == 'ws') {
  41. $uri .= ', obfs=ws';
  42. if ($item->settings) {
  43. $wsSettings = json_decode($item->settings);
  44. if ($wsSettings->path) $uri .= ', obfs-uri='.$wsSettings->path;
  45. }
  46. }
  47. $uri .= "\r\n";
  48. }
  49. return base64_encode($uri);
  50. }
  51. private function quantumult ($user, $server) {
  52. $uri = '';
  53. header('subscription-userinfo: upload='.$user->u.'; download='.$user->d.';total='.$user->transfer_enable);
  54. foreach($server as $item) {
  55. $str = '';
  56. $str .= $item->name.'= vmess, '.$item->host.', '.$item->port.', chacha20-ietf-poly1305, "'.$user->v2ray_uuid.'", over-tls='.($item->tls?"true":"false").', certificate=0, group='.config('v2board.app_name', 'V2Board');
  57. if ($item->network === 'ws') {
  58. $str .= ', obfs=ws';
  59. if ($item->settings) {
  60. $wsSettings = json_decode($item->settings);
  61. if ($wsSettings->path) $str .= ', obfs-path="'.$wsSettings->path.'"';
  62. if ($wsSettings->headers->Host) $str .= ', obfs-header="Host:'.$wsSettings->headers->Host.'"';
  63. }
  64. }
  65. $uri .= "vmess://".base64_encode($str)."\r\n";
  66. }
  67. return base64_encode($uri);
  68. }
  69. private function origin ($user, $server) {
  70. $uri = '';
  71. foreach($server as $item) {
  72. $config = [
  73. "ps" => $item->name,
  74. "add" => $item->host,
  75. "port" => $item->port,
  76. "id" => $user->v2ray_uuid,
  77. "aid" => "2",
  78. "net" => $item->network,
  79. "type" => "chacha20-poly1305",
  80. "host" => "",
  81. "tls" => $item->tls?"tls":"",
  82. ];
  83. if ($item->network == 'ws') {
  84. $wsSettings = json_decode($item->settings);
  85. if ($wsSettings->path) $config['path'] = $wsSettings->path;
  86. }
  87. $uri .= "vmess://".base64_encode(json_encode($config))."\r\n";
  88. }
  89. return base64_encode($uri);
  90. }
  91. private function clash ($user, $server) {
  92. $proxy = [];
  93. $proxyGroup = [];
  94. $proxies = [];
  95. foreach ($server as $item) {
  96. $array = [];
  97. $array['name'] = $item->name;
  98. $array['type'] = 'vmess';
  99. $array['server'] = $item->host;
  100. $array['port'] = $item->port;
  101. $array['uuid'] = $user->v2ray_uuid;
  102. $array['alterId'] = $user->v2ray_alter_id;
  103. $array['cipher'] = 'auto';
  104. if ($item->tls) {
  105. $array['tls'] = true;
  106. }
  107. if ($item->network == 'ws') {
  108. $array['network'] = $item->network;
  109. if ($item->settings) {
  110. $wsSettings = json_decode($item->settings);
  111. if ($wsSettings->path) $array['ws-path'] = $wsSettings->path;
  112. if ($wsSettings->headers->Host) $array['ws-headers'] = [
  113. 'Host' => $wsSettings->headers->Host
  114. ];
  115. }
  116. }
  117. array_push($proxy, $array);
  118. array_push($proxies, $item->name);
  119. }
  120. array_push($proxyGroup, [
  121. 'name' => config('v2board.app_name', 'V2Board'),
  122. 'type' => 'select',
  123. 'proxies' => $proxies
  124. ]);
  125. $config = [
  126. 'port' => 7890,
  127. 'socks-port' => 0,
  128. 'allow-lan' => false,
  129. 'mode' => 'Rule',
  130. 'log-level' => 'info',
  131. 'external-controller' => '0.0.0.0:9090',
  132. 'secret' => '',
  133. 'Proxy' => $proxy,
  134. 'Proxy Group' => $proxyGroup,
  135. 'Rule' => [
  136. 'DOMAIN-SUFFIX,google.com,'.config('v2board.app_name', 'V2Board'),
  137. 'DOMAIN-KEYWORD,google,'.config('v2board.app_name', 'V2Board'),
  138. 'DOMAIN,google.com,'.config('v2board.app_name', 'V2Board'),
  139. 'DOMAIN-SUFFIX,ad.com,REJECT',
  140. 'IP-CIDR,127.0.0.0/8,DIRECT',
  141. 'GEOIP,CN,DIRECT',
  142. 'MATCH,'.config('v2board.app_name', 'V2Board')
  143. ]
  144. ];
  145. return Yaml::dump($config);
  146. }
  147. }