AppController.php 5.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 App\Utils\Helper;
  9. class AppController extends Controller
  10. {
  11. CONST CLIENT_CONFIG = '{"policy":{"levels":{"0":{"uplinkOnly":0}}},"dns":{"servers":["1.1.1.1"]},"outboundDetour":[{"protocol":"freedom","tag":"direct","settings":{}}],"inbound":{"listen":"0.0.0.0","port":31211,"protocol":"socks","settings":{"auth":"noauth","udp":true,"ip":"127.0.0.1"}},"inboundDetour":[{"listen":"0.0.0.0","allocate":{"strategy":"always","refresh":5,"concurrency":3},"port":31210,"protocol":"http","tag":"httpDetour","domainOverride":["http","tls"],"streamSettings":{},"settings":{"timeout":0}}],"routing":{"strategy":"rules","settings":{"domainStrategy":"IPIfNonMatch","rules":[{"port":"1-52","type":"field","outboundTag":"direct"},{"port":"54-79","type":"field","outboundTag":"direct"},{"port":"81-442","type":"field","outboundTag":"direct"},{"port":"444-65535","type":"field","outboundTag":"direct"},{"type":"field","ip":["0.0.0.0/8","10.0.0.0/8","100.64.0.0/10","127.0.0.0/8","169.254.0.0/16","172.16.0.0/12","192.0.0.0/24","192.0.2.0/24","192.168.0.0/16","198.18.0.0/15","198.51.100.0/24","203.0.113.0/24","::1/128","fc00::/7","fe80::/10"],"outboundTag":"direct"}]}},"outbound":{"sendThrough":"0.0.0.0","mux":{"enabled":false,"concurrency":8},"protocol":"vmess","settings":{"vnext":[{"address":"server","port":443,"users":[{"id":"uuid","alterId":2,"security":"auto","level":0}],"remark":"remark"}]},"streamSettings":{"network":"tcp","tcpSettings":{"header":{"type":"none"}},"security":"none","tlsSettings":{"allowInsecure":false},"kcpSettings":{"header":{"type":"none"},"mtu":1350,"congestion":false,"tti":20,"uplinkCapacity":5,"writeBufferSize":1,"readBufferSize":1,"downlinkCapacity":20},"wsSettings":{"path":"","headers":{"Host":"server.cc"}}}}}';
  12. CONST SOCKS_PORT = 10010;
  13. CONST HTTP_PORT = 10011;
  14. public function data (Request $request) {
  15. $user = $request->user;
  16. $nodes = [];
  17. if ($user->plan_id) {
  18. $user['plan'] = Plan::find($user->plan_id);
  19. if (!$user['plan']) {
  20. abort(500, '订阅计划不存在');
  21. }
  22. if ($user->expired_at > time()) {
  23. $servers = Server::where('show', 1)
  24. ->orderBy('name')
  25. ->get();
  26. foreach ($servers as $item) {
  27. $groupId = json_decode($item['group_id']);
  28. if (in_array($user->group_id, $groupId)) {
  29. array_push($nodes, $item);
  30. }
  31. }
  32. }
  33. }
  34. return response([
  35. 'data' => [
  36. 'nodes' => $nodes,
  37. 'u' => $user->u,
  38. 'd' => $user->d,
  39. 'transfer_enable' => $user->transfer_enable,
  40. 'expired_at' => $user->expired_at,
  41. 'plan' => isset($user['plan']) ? $user['plan'] : false
  42. ]
  43. ]);
  44. }
  45. public function config (Request $request) {
  46. if (empty($request->input('server_id'))) {
  47. abort(500, '参数错误');
  48. }
  49. $user = $request->user;
  50. if ($user->expired_at < time()) {
  51. abort(500, '订阅计划已过期');
  52. }
  53. $server = Server::where('show', 1)
  54. ->where('id', $request->input('server_id'))
  55. ->first();
  56. if (!$server) {
  57. abort(500, '服务器不存在');
  58. }
  59. $json = json_decode(self::CLIENT_CONFIG);
  60. //socks
  61. $json->inbound->port = (int)self::SOCKS_PORT;
  62. //http
  63. $json->inboundDetour[0]->port = (int)self::HTTP_PORT;
  64. //other
  65. $json->outbound->settings->vnext[0]->address = (string)$server->host;
  66. $json->outbound->settings->vnext[0]->port = (int)$server->port;
  67. $json->outbound->settings->vnext[0]->users[0]->id = (string)$user->v2ray_uuid;
  68. $json->outbound->settings->vnext[0]->users[0]->alterId = (int)$user->v2ray_alter_id;
  69. $json->outbound->settings->vnext[0]->remark = (string)$server->name;
  70. $json->outbound->streamSettings->network = $server->network;
  71. if ($server->settings) {
  72. switch ($server->network) {
  73. case 'tcp': $json->outbound->streamSettings->tcpSettings = json_decode($server->settings);
  74. break;
  75. case 'kcp': $json->outbound->streamSettings->kcpSettings = json_decode($server->settings);
  76. break;
  77. case 'ws': $json->outbound->streamSettings->wsSettings = json_decode($server->settings);
  78. break;
  79. case 'http': $json->outbound->streamSettings->httpSettings = json_decode($server->settings);
  80. break;
  81. case 'domainsocket': $json->outbound->streamSettings->dsSettings = json_decode($server->settings);
  82. break;
  83. case 'quic': $json->outbound->streamSettings->quicSettings = json_decode($server->settings);
  84. break;
  85. }
  86. }
  87. if ($server->tls) {
  88. $json->outbound->streamSettings->security = "tls";
  89. }
  90. die(json_encode($json, JSON_UNESCAPED_UNICODE));
  91. }
  92. }