AppController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace App\Http\Controllers\Client;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Models\User;
  6. use App\Models\Plan;
  7. use App\Models\Server;
  8. use App\Models\Notice;
  9. use App\Utils\Helper;
  10. class AppController extends Controller
  11. {
  12. CONST CLIENT_CONFIG = '{"policy":{"levels":{"0":{"uplinkOnly":0}}},"dns":{"servers":["114.114.114.114","8.8.8.8"]},"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":[{"type":"field","ip":["geoip:cn"],"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":{"tag":"proxy","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":true,"allowInsecureCiphers":true},"kcpSettings":{"header":{"type":"none"},"mtu":1350,"congestion":false,"tti":20,"uplinkCapacity":5,"writeBufferSize":1,"readBufferSize":1,"downlinkCapacity":20},"wsSettings":{"path":"","headers":{"Host":"server.cc"}}}}}';
  13. CONST SOCKS_PORT = 10010;
  14. CONST HTTP_PORT = 10011;
  15. // TODO: 1.1.1 abolish
  16. public function data(Request $request)
  17. {
  18. $user = $request->user;
  19. $nodes = [];
  20. if ($user->plan_id) {
  21. $user['plan'] = Plan::find($user->plan_id);
  22. if (!$user['plan']) {
  23. abort(500, '订阅计划不存在');
  24. }
  25. if ($user->expired_at > time()) {
  26. $servers = Server::where('show', 1)
  27. ->orderBy('name')
  28. ->get();
  29. foreach ($servers as $item) {
  30. $groupId = json_decode($item['group_id']);
  31. if (in_array($user->group_id, $groupId)) {
  32. array_push($nodes, $item);
  33. }
  34. }
  35. }
  36. }
  37. return response([
  38. 'data' => [
  39. 'nodes' => $nodes,
  40. 'u' => $user->u,
  41. 'd' => $user->d,
  42. 'transfer_enable' => $user->transfer_enable,
  43. 'expired_at' => $user->expired_at,
  44. 'plan' => isset($user['plan']) ? $user['plan'] : false,
  45. 'notice' => Notice::orderBy('created_at', 'DESC')->first()
  46. ]
  47. ]);
  48. }
  49. public function config(Request $request)
  50. {
  51. if (empty($request->input('server_id'))) {
  52. abort(500, '参数错误');
  53. }
  54. $user = $request->user;
  55. if ($user->expired_at < time() && $user->expired_at !== NULL) {
  56. abort(500, '订阅计划已过期');
  57. }
  58. $server = Server::where('show', 1)
  59. ->where('id', $request->input('server_id'))
  60. ->first();
  61. if (!$server) {
  62. abort(500, '服务器不存在');
  63. }
  64. $json = json_decode(self::CLIENT_CONFIG);
  65. //socks
  66. $json->inbound->port = (int)self::SOCKS_PORT;
  67. //http
  68. $json->inboundDetour[0]->port = (int)self::HTTP_PORT;
  69. //other
  70. $json->outbound->settings->vnext[0]->address = (string)$server->host;
  71. $json->outbound->settings->vnext[0]->port = (int)$server->port;
  72. $json->outbound->settings->vnext[0]->users[0]->id = (string)$user->v2ray_uuid;
  73. $json->outbound->settings->vnext[0]->users[0]->alterId = (int)$user->v2ray_alter_id;
  74. $json->outbound->settings->vnext[0]->remark = (string)$server->name;
  75. $json->outbound->streamSettings->network = $server->network;
  76. if ($server->networkSettings) {
  77. switch ($server->network) {
  78. case 'tcp':
  79. $json->outbound->streamSettings->tcpSettings = json_decode($server->networkSettings);
  80. break;
  81. case 'kcp':
  82. $json->outbound->streamSettings->kcpSettings = json_decode($server->networkSettings);
  83. break;
  84. case 'ws':
  85. $json->outbound->streamSettings->wsSettings = json_decode($server->networkSettings);
  86. break;
  87. case 'http':
  88. $json->outbound->streamSettings->httpSettings = json_decode($server->networkSettings);
  89. break;
  90. case 'domainsocket':
  91. $json->outbound->streamSettings->dsSettings = json_decode($server->networkSettings);
  92. break;
  93. case 'quic':
  94. $json->outbound->streamSettings->quicSettings = json_decode($server->networkSettings);
  95. break;
  96. }
  97. }
  98. if ($request->input('is_global')) {
  99. $json->routing->settings->rules[0]->outboundTag = 'proxy';
  100. }
  101. if ($server->tls) {
  102. $json->outbound->streamSettings->security = "tls";
  103. }
  104. die(json_encode($json, JSON_UNESCAPED_UNICODE));
  105. }
  106. }