BaseController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Http\Controllers\Api\WebApi;
  3. use App\Models\Rule;
  4. use App\Models\RuleGroup;
  5. use App\Models\RuleGroupNode;
  6. use App\Models\RuleLog;
  7. use App\Models\SsNode;
  8. use App\Models\SsNodeInfo;
  9. use App\Models\SsNodeIp;
  10. use App\Models\SsNodeOnlineLog;
  11. use App\Models\User;
  12. use App\Models\UserTrafficLog;
  13. use Illuminate\Http\JsonResponse;
  14. use Illuminate\Http\Request;
  15. use Response;
  16. class BaseController {
  17. // 上报节点心跳信息
  18. public function setNodeStatus(Request $request, $id): JsonResponse {
  19. $cpu = intval($request->input('cpu')) / 100;
  20. $mem = intval($request->input('mem')) / 100;
  21. $disk = intval($request->input('disk')) / 100;
  22. if(is_null($request->input('uptime'))){
  23. return $this->returnData('上报节点心跳信息失败,请检查字段');
  24. }
  25. $obj = new SsNodeInfo();
  26. $obj->node_id = $id;
  27. $obj->uptime = intval($request->input('uptime'));
  28. //$obj->load = $request->input('load');
  29. $obj->load = implode(' ', [$cpu, $mem, $disk]);
  30. $obj->log_time = time();
  31. $obj->save();
  32. if($obj->id){
  33. return $this->returnData('上报节点心跳信息成功', 'success', 200);
  34. }
  35. return $this->returnData('上报节点心跳信息失败,请检查字段');
  36. }
  37. // 返回数据
  38. public function returnData($message, $status = 'fail', $code = 400, $data = '', $addition = false): JsonResponse {
  39. $data = ['status' => $status, 'code' => $code, 'data' => $data, 'message' => $message];
  40. if($addition){
  41. $data = array_merge($data, $addition);
  42. }
  43. return Response::json($data);
  44. }
  45. // 上报节点在线人数
  46. public function setNodeOnline(Request $request, $id): JsonResponse {
  47. $inputArray = $request->all();
  48. $onlineCount = 0;
  49. foreach($inputArray as $input){
  50. if(!array_key_exists('ip', $input) || !array_key_exists('uid', $input)){
  51. return $this->returnData('上报节点在线情况失败,请检查字段');
  52. }
  53. if(!isset($input['ip'], $input['uid'])){
  54. return $this->returnData('上报节点在线情况失败,请检查字段');
  55. }
  56. $obj = new SsNodeIp();
  57. $obj->node_id = $id;
  58. $obj->user_id = $input['uid'];
  59. $obj->ip = $input['ip'];
  60. $obj->port = User::find($input['uid'])->port;
  61. $obj->created_at = time();
  62. $obj->save();
  63. if(!$obj->id){
  64. return $this->returnData('上报节点在线情况失败,请检查字段');
  65. }
  66. $onlineCount++;
  67. }
  68. $obj = new SsNodeOnlineLog();
  69. $obj->node_id = $id;
  70. $obj->online_user = $onlineCount;
  71. $obj->log_time = time();
  72. $obj->save();
  73. if($obj->id){
  74. return $this->returnData('上报节点在线情况成功', 'success', 200);
  75. }
  76. return $this->returnData('上报节点在线情况失败,请检查字段');
  77. }
  78. // 上报用户流量日志
  79. public function setUserTraffic(Request $request, $id): JsonResponse {
  80. $inputArray = $request->all();
  81. foreach($inputArray as $input){
  82. if(!array_key_exists('uid', $input)){
  83. return $this->returnData('上报用户流量日志失败,请检查字段');
  84. }
  85. $rate = SsNode::find($id)->traffic_rate;
  86. $obj = new UserTrafficLog();
  87. $obj->user_id = intval($input['uid']);
  88. $obj->u = intval($input['upload']) * $rate;
  89. $obj->d = intval($input['download']) * $rate;
  90. $obj->node_id = $id;
  91. $obj->rate = $rate;
  92. $obj->traffic = flowAutoShow($obj->u + $obj->d);
  93. $obj->log_time = time();
  94. $obj->save();
  95. if(!$obj->id){
  96. return $this->returnData('上报用户流量日志失败,请检查字段');
  97. }
  98. }
  99. return $this->returnData('上报用户流量日志成功', 'success', 200);
  100. }
  101. // 获取节点的审计规则
  102. public function getNodeRule($id): JsonResponse {
  103. $nodeRule = RuleGroupNode::whereNodeId($id)->first();
  104. $data = [];
  105. //节点未设置任何审计规则
  106. if($nodeRule){
  107. $ruleGroup = RuleGroup::query()->whereId($nodeRule->rule_group_id)->first();
  108. if($ruleGroup){
  109. $rules = explode(',', $ruleGroup->rules);
  110. foreach($rules as $ruleId){
  111. $rule = Rule::query()->whereId($ruleId)->first();
  112. if($rule){
  113. $new = [
  114. 'id' => $rule->id,
  115. 'type' => $rule->type_api_label,
  116. 'pattern' => $rule->pattern
  117. ];
  118. $data[] = $new;
  119. }
  120. }
  121. return $this->returnData('获取节点审计规则成功', 'success', 200,
  122. ['mode' => $ruleGroup->type? 'reject' : 'allow', 'rules' => $data]);
  123. }
  124. }
  125. //放行
  126. return $this->returnData('获取节点审计规则成功', 'success', 200, ['mode' => 'all', 'rules' => $data]);
  127. }
  128. // 上报用户触发的审计规则记录
  129. public function addRuleLog(Request $request, $id): JsonResponse {
  130. if($request->has(['uid', 'rule_id', 'reason'])){
  131. $obj = new RuleLog();
  132. $obj->user_id = $request->input('uid');
  133. $obj->node_id = $id;
  134. $obj->rule_id = $request->input('rule_id');
  135. $obj->reason = $request->input('reason');
  136. $obj->save();
  137. if($obj->id){
  138. return $this->returnData('上报用户触发审计规则记录成功', 'success', 200);
  139. }
  140. }
  141. return $this->returnData('上报用户触发审计规则记录失败');
  142. }
  143. }