AppController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Http\Controllers\Client;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\UserService;
  5. use Illuminate\Http\Request;
  6. use App\Models\Server;
  7. use Symfony\Component\Yaml\Yaml;
  8. class AppController extends Controller
  9. {
  10. public function data(Request $request)
  11. {
  12. $server = [];
  13. $user = $request->user;
  14. $userService = new UserService();
  15. if ($userService->isAvailable($user)) {
  16. $servers = Server::where('show', 1)
  17. ->orderBy('sort', 'ASC')
  18. ->get();
  19. foreach ($servers as $item) {
  20. $groupId = json_decode($item['group_id']);
  21. if (in_array($user->group_id, $groupId)) {
  22. array_push($server, $item);
  23. }
  24. }
  25. }
  26. $config = Yaml::parseFile(base_path() . '/resources/rules/app.clash.yaml');
  27. $proxy = [];
  28. $proxies = [];
  29. foreach ($server as $item) {
  30. $array = [];
  31. $array['name'] = $item->name;
  32. $array['type'] = 'vmess';
  33. $array['server'] = $item->host;
  34. $array['port'] = $item->port;
  35. $array['uuid'] = $user->uuid;
  36. $array['alterId'] = $user->v2ray_alter_id;
  37. $array['cipher'] = 'auto';
  38. if ($item->tls) {
  39. $tlsSettings = json_decode($item->tlsSettings);
  40. $array['tls'] = true;
  41. if (isset($tlsSettings->allowInsecure)) $array['skip-cert-verify'] = ($tlsSettings->allowInsecure ? true : false );
  42. }
  43. if ($item->network == 'ws') {
  44. $array['network'] = $item->network;
  45. if ($item->networkSettings) {
  46. $wsSettings = json_decode($item->networkSettings);
  47. if (isset($wsSettings->path)) $array['ws-path'] = $wsSettings->path;
  48. if (isset($wsSettings->headers->Host)) $array['ws-headers'] = [
  49. 'Host' => $wsSettings->headers->Host
  50. ];
  51. }
  52. }
  53. array_push($proxy, $array);
  54. array_push($proxies, $item->name);
  55. }
  56. $config['Proxy'] = array_merge($config['Proxy'] ? $config['Proxy'] : [], $proxy);
  57. foreach ($config['Proxy Group'] as $k => $v) {
  58. $config['Proxy Group'][$k]['proxies'] = array_merge($config['Proxy Group'][$k]['proxies'], $proxies);
  59. }
  60. die(Yaml::dump($config));
  61. }
  62. public function config(Request $request)
  63. {
  64. if (empty($request->input('server_id'))) {
  65. abort(500, '参数错误');
  66. }
  67. $user = $request->user;
  68. if ($user->expired_at < time() && $user->expired_at !== NULL) {
  69. abort(500, '订阅计划已过期');
  70. }
  71. $server = Server::where('show', 1)
  72. ->where('id', $request->input('server_id'))
  73. ->first();
  74. if (!$server) {
  75. abort(500, '服务器不存在');
  76. }
  77. $json = json_decode(self::CLIENT_CONFIG);
  78. //socks
  79. $json->inbound->port = (int)self::SOCKS_PORT;
  80. //http
  81. $json->inboundDetour[0]->port = (int)self::HTTP_PORT;
  82. //other
  83. $json->outbound->settings->vnext[0]->address = (string)$server->host;
  84. $json->outbound->settings->vnext[0]->port = (int)$server->port;
  85. $json->outbound->settings->vnext[0]->users[0]->id = (string)$user->uuid;
  86. $json->outbound->settings->vnext[0]->users[0]->alterId = (int)$user->v2ray_alter_id;
  87. $json->outbound->settings->vnext[0]->remark = (string)$server->name;
  88. $json->outbound->streamSettings->network = $server->network;
  89. if ($server->networkSettings) {
  90. switch ($server->network) {
  91. case 'tcp':
  92. $json->outbound->streamSettings->tcpSettings = json_decode($server->networkSettings);
  93. break;
  94. case 'kcp':
  95. $json->outbound->streamSettings->kcpSettings = json_decode($server->networkSettings);
  96. break;
  97. case 'ws':
  98. $json->outbound->streamSettings->wsSettings = json_decode($server->networkSettings);
  99. break;
  100. case 'http':
  101. $json->outbound->streamSettings->httpSettings = json_decode($server->networkSettings);
  102. break;
  103. case 'domainsocket':
  104. $json->outbound->streamSettings->dsSettings = json_decode($server->networkSettings);
  105. break;
  106. case 'quic':
  107. $json->outbound->streamSettings->quicSettings = json_decode($server->networkSettings);
  108. break;
  109. }
  110. }
  111. if ($request->input('is_global')) {
  112. $json->routing->settings->rules[0]->outboundTag = 'proxy';
  113. }
  114. if ($server->tls) {
  115. $json->outbound->streamSettings->security = "tls";
  116. }
  117. die(json_encode($json, JSON_UNESCAPED_UNICODE));
  118. }
  119. }