BaseController.php 4.8 KB

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