UniProxyController.php 4.7 KB

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