UniProxyController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 $serverService;
  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->nodeType = $request->input('node_type');
  29. $this->nodeId = $request->input('node_id');
  30. $this->serverService = new ServerService();
  31. $this->nodeInfo = $this->serverService->getServer($this->nodeId, $this->nodeType);
  32. if (!$this->nodeInfo) abort(500, 'server is not exist');
  33. }
  34. // 后端获取用户
  35. public function user(Request $request)
  36. {
  37. ini_set('memory_limit', -1);
  38. Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_LAST_CHECK_AT', $this->nodeInfo->id), time(), 3600);
  39. $users = $this->serverService->getAvailableUsers($this->nodeInfo->group_id);
  40. $users = $users->toArray();
  41. $response['users'] = $users;
  42. $eTag = sha1(json_encode($response));
  43. if (strpos($request->header('If-None-Match'), $eTag) !== false ) {
  44. abort(304);
  45. }
  46. return response($response)->header('ETag', "\"{$eTag}\"");
  47. }
  48. // 后端提交数据
  49. public function push(Request $request)
  50. {
  51. $data = file_get_contents('php://input');
  52. $data = json_decode($data, true);
  53. Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_ONLINE_USER', $this->nodeInfo->id), count($data), 3600);
  54. Cache::put(CacheKey::get('SERVER_' . strtoupper($this->nodeType) . '_LAST_PUSH_AT', $this->nodeInfo->id), time(), 3600);
  55. $userService = new UserService();
  56. foreach (array_keys($data) as $k) {
  57. $u = $data[$k][0];
  58. $d = $data[$k][1];
  59. $userService->trafficFetch($u, $d, $k, $this->nodeInfo->toArray(), $this->nodeType);
  60. }
  61. return response([
  62. 'data' => true
  63. ]);
  64. }
  65. // 后端获取配置
  66. public function config(Request $request)
  67. {
  68. switch ($this->nodeType) {
  69. case 'shadowsocks':
  70. $response = [
  71. 'server_port' => $this->nodeInfo->server_port,
  72. 'cipher' => $this->nodeInfo->cipher,
  73. 'obfs' => $this->nodeInfo->obfs,
  74. 'obfs_settings' => $this->nodeInfo->obfs_settings
  75. ];
  76. if ($this->nodeInfo->cipher === '2022-blake3-aes-128-gcm') {
  77. $response['server_key'] = Helper::getShadowsocksServerKey($this->nodeInfo->created_at, 16);
  78. }
  79. if ($this->nodeInfo->cipher === '2022-blake3-aes-256-gcm') {
  80. $response['server_key'] = Helper::getShadowsocksServerKey($this->nodeInfo->created_at, 32);
  81. }
  82. break;
  83. case 'v2ray':
  84. $response = [
  85. 'server_port' => $this->nodeInfo->server_port,
  86. 'network' => $this->nodeInfo->network,
  87. 'networkSettings' => $this->nodeInfo->networkSettings,
  88. 'tls' => $this->nodeInfo->tls
  89. ];
  90. break;
  91. case 'trojan':
  92. $response = [
  93. 'host' => $this->nodeInfo->host,
  94. 'server_port' => $this->nodeInfo->server_port,
  95. 'server_name' => $this->nodeInfo->server_name
  96. ];
  97. break;
  98. }
  99. $response['base_config'] = [
  100. 'push_interval' => 120,
  101. 'pull_interval' => 120
  102. ];
  103. $response['routes'] = $this->serverService->getRoutes($this->nodeInfo['route_id']);
  104. $eTag = sha1(json_encode($response));
  105. if (strpos($request->header('If-None-Match'), $eTag) !== false ) {
  106. abort(304);
  107. }
  108. return response($response)->header('ETag', "\"{$eTag}\"");
  109. }
  110. }