V2rayController.php 3.5 KB

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