V2rayController.php 3.2 KB

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