V2rayController.php 3.4 KB

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