UniProxyController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Http\Controllers\V1\Server;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\ServerService;
  5. use App\Services\UserService;
  6. use App\Utils\CacheKey;
  7. use App\Utils\Helper;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Cache;
  10. class UniProxyController extends Controller
  11. {
  12. private $nodeType;
  13. private $nodeInfo;
  14. private $nodeId;
  15. private $serverService;
  16. public function __construct(Request $request)
  17. {
  18. $token = $request->input('token');
  19. if (empty($token)) {
  20. abort(500, 'token is null');
  21. }
  22. if ($token !== config('v2board.server_token')) {
  23. abort(500, 'token is error');
  24. }
  25. $this->nodeType = $request->input('node_type');
  26. if ($this->nodeType === 'v2ray') $this->nodeType = 'vmess';
  27. $this->nodeId = $request->input('node_id');
  28. $this->serverService = new ServerService();
  29. $this->nodeInfo = $this->serverService->getServer($this->nodeId, $this->nodeType);
  30. if (!$this->nodeInfo) abort(500, 'server is not exist');
  31. }
  32. // 后端获取用户
  33. public function user(Request $request)
  34. {
  35. ini_set('memory_limit', -1);
  36. Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_LAST_CHECK_AT', $this->nodeInfo->id), time(), 3600);
  37. $users = $this->serverService->getAvailableUsers($this->nodeInfo->group_id);
  38. $users = $users->toArray();
  39. $response['users'] = $users;
  40. $eTag = sha1(json_encode($response));
  41. if (strpos($request->header('If-None-Match'), $eTag) !== false ) {
  42. abort(304);
  43. }
  44. return response($response)->header('ETag', "\"{$eTag}\"");
  45. }
  46. // 后端提交数据
  47. public function push(Request $request)
  48. {
  49. $data = file_get_contents('php://input');
  50. $data = json_decode($data, true);
  51. Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_ONLINE_USER', $this->nodeInfo->id), count($data), 3600);
  52. Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_LAST_PUSH_AT', $this->nodeInfo->id), time(), 3600);
  53. $userService = new UserService();
  54. $userService->trafficFetch($this->nodeInfo->toArray(), $this->nodeType, $data);
  55. return response([
  56. 'data' => true
  57. ]);
  58. }
  59. // 后端获取配置
  60. public function config(Request $request)
  61. {
  62. switch ($this->nodeType) {
  63. case 'shadowsocks':
  64. $response = [
  65. 'server_port' => $this->nodeInfo->server_port,
  66. 'cipher' => $this->nodeInfo->cipher,
  67. 'obfs' => $this->nodeInfo->obfs,
  68. 'obfs_settings' => $this->nodeInfo->obfs_settings
  69. ];
  70. if ($this->nodeInfo->cipher === '2022-blake3-aes-128-gcm') {
  71. $response['server_key'] = Helper::getServerKey($this->nodeInfo->created_at, 16);
  72. }
  73. if ($this->nodeInfo->cipher === '2022-blake3-aes-256-gcm') {
  74. $response['server_key'] = Helper::getServerKey($this->nodeInfo->created_at, 32);
  75. }
  76. break;
  77. case 'vmess':
  78. $response = [
  79. 'server_port' => $this->nodeInfo->server_port,
  80. 'network' => $this->nodeInfo->network,
  81. 'networkSettings' => $this->nodeInfo->networkSettings,
  82. 'tls' => $this->nodeInfo->tls
  83. ];
  84. break;
  85. case 'trojan':
  86. $response = [
  87. 'host' => $this->nodeInfo->host,
  88. 'server_port' => $this->nodeInfo->server_port,
  89. 'server_name' => $this->nodeInfo->server_name,
  90. ];
  91. break;
  92. case 'hysteria':
  93. $response = [
  94. 'host' => $this->nodeInfo->host,
  95. 'server_port' => $this->nodeInfo->server_port,
  96. 'server_name' => $this->nodeInfo->server_name,
  97. 'up_mbps' => $this->nodeInfo->up_mbps,
  98. 'down_mbps' => $this->nodeInfo->down_mbps,
  99. 'obfs' => Helper::getServerKey($this->nodeInfo->created_at, 16)
  100. ];
  101. break;
  102. }
  103. $response['base_config'] = [
  104. 'push_interval' => (int)config('v2board.server_push_interval', 60),
  105. 'pull_interval' => (int)config('v2board.server_pull_interval', 60)
  106. ];
  107. if ($this->nodeInfo['route_id']) {
  108. $response['routes'] = $this->serverService->getRoutes($this->nodeInfo['route_id']);
  109. }
  110. $eTag = sha1(json_encode($response));
  111. if (strpos($request->header('If-None-Match'), $eTag) !== false ) {
  112. abort(304);
  113. }
  114. return response($response)->header('ETag', "\"{$eTag}\"");
  115. }
  116. }