V2rayController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Http\Controllers\Admin\Server;
  3. use App\Http\Requests\Admin\ServerV2raySave;
  4. use App\Http\Requests\Admin\ServerV2raySort;
  5. use App\Http\Requests\Admin\ServerV2rayUpdate;
  6. use App\Services\ServerService;
  7. use App\Utils\CacheKey;
  8. use Illuminate\Http\Request;
  9. use App\Http\Controllers\Controller;
  10. use App\Models\Server;
  11. use Illuminate\Support\Facades\Cache;
  12. use Illuminate\Support\Facades\DB;
  13. class V2rayController extends Controller
  14. {
  15. public function save(ServerV2raySave $request)
  16. {
  17. $params = $request->validated();
  18. $params['group_id'] = json_encode($params['group_id']);
  19. if (isset($params['tags'])) {
  20. $params['tags'] = json_encode($params['tags']);
  21. }
  22. if (isset($params['dnsSettings'])) {
  23. if (!is_object(json_decode($params['dnsSettings']))) {
  24. abort(500, 'DNS规则配置格式不正确');
  25. }
  26. }
  27. if (isset($params['ruleSettings'])) {
  28. if (!is_object(json_decode($params['ruleSettings']))) {
  29. abort(500, '审计规则配置格式不正确');
  30. }
  31. }
  32. if (isset($params['networkSettings'])) {
  33. if (!is_object(json_decode($params['networkSettings']))) {
  34. abort(500, '传输协议配置格式不正确');
  35. }
  36. }
  37. if (isset($params['tlsSettings'])) {
  38. if (!is_object(json_decode($params['tlsSettings']))) {
  39. abort(500, 'TLS配置格式不正确');
  40. }
  41. }
  42. if ($request->input('id')) {
  43. $server = Server::find($request->input('id'));
  44. if (!$server) {
  45. abort(500, '服务器不存在');
  46. }
  47. try {
  48. $server->update($params);
  49. } catch (\Exception $e) {
  50. abort(500, '保存失败');
  51. }
  52. return response([
  53. 'data' => true
  54. ]);
  55. }
  56. if (!Server::create($params)) {
  57. abort(500, '创建失败');
  58. }
  59. return response([
  60. 'data' => true
  61. ]);
  62. }
  63. public function drop(Request $request)
  64. {
  65. if ($request->input('id')) {
  66. $server = Server::find($request->input('id'));
  67. if (!$server) {
  68. abort(500, '节点ID不存在');
  69. }
  70. }
  71. return response([
  72. 'data' => $server->delete()
  73. ]);
  74. }
  75. public function update(ServerV2rayUpdate $request)
  76. {
  77. $params = $request->only([
  78. 'show',
  79. ]);
  80. $server = Server::find($request->input('id'));
  81. if (!$server) {
  82. abort(500, '该服务器不存在');
  83. }
  84. try {
  85. $server->update($params);
  86. } catch (\Exception $e) {
  87. abort(500, '保存失败');
  88. }
  89. return response([
  90. 'data' => true
  91. ]);
  92. }
  93. public function copy(Request $request)
  94. {
  95. $server = Server::find($request->input('id'));
  96. $server->show = 0;
  97. if (!$server) {
  98. abort(500, '服务器不存在');
  99. }
  100. if (!Server::create($server->toArray())) {
  101. abort(500, '复制失败');
  102. }
  103. return response([
  104. 'data' => true
  105. ]);
  106. }
  107. public function viewConfig(Request $request)
  108. {
  109. $serverService = new ServerService();
  110. $config = $serverService->getVmessConfig($request->input('node_id'), 23333);
  111. return response([
  112. 'data' => $config
  113. ]);
  114. }
  115. }