IpController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Controllers\Admin;
  3. use App\Models\Ip;
  4. use App\Models\LoginIp;
  5. use App\Models\BlockIp;
  6. use App\Models\UnblockIp;
  7. use App\Models\Node;
  8. use App\Controllers\AdminController;
  9. use App\Utils\QQWry;
  10. use App\Utils\Tools;
  11. use App\Services\Auth;
  12. use Ozdemir\Datatables\Datatables;
  13. use App\Utils\DatatablesHelper;
  14. class IpController extends AdminController
  15. {
  16. public function index($request, $response, $args)
  17. {
  18. $table_config['total_column'] = array("id" => "ID", "userid" => "用户ID",
  19. "user_name" => "用户名", "ip" => "IP",
  20. "location" => "归属地", "datetime" => "时间", "type" => "类型");
  21. $table_config['default_show_column'] = array();
  22. foreach ($table_config['total_column'] as $column => $value) {
  23. array_push($table_config['default_show_column'], $column);
  24. }
  25. $table_config['ajax_url'] = 'login/ajax';
  26. return $this->view()->assign('table_config', $table_config)->display('admin/ip/login.tpl');
  27. }
  28. public function alive($request, $response, $args)
  29. {
  30. $table_config['total_column'] = array("id" => "ID", "userid" => "用户ID",
  31. "user_name" => "用户名", "nodeid" => "节点ID",
  32. "node_name" => "节点名", "ip" => "IP",
  33. "location" => "归属地", "datetime" => "时间", "is_node" => "是否为中转连接");
  34. $table_config['default_show_column'] = array();
  35. foreach ($table_config['total_column'] as $column => $value) {
  36. array_push($table_config['default_show_column'], $column);
  37. }
  38. $table_config['ajax_url'] = 'alive/ajax';
  39. return $this->view()->assign('table_config', $table_config)->display('admin/ip/alive.tpl');
  40. }
  41. public function block($request, $response, $args)
  42. {
  43. $table_config['total_column'] = array("id" => "ID",
  44. "name" => "节点名称", "ip" => "IP",
  45. "location" => "归属地", "datetime" => "时间");
  46. $table_config['default_show_column'] = array();
  47. foreach ($table_config['total_column'] as $column => $value) {
  48. array_push($table_config['default_show_column'], $column);
  49. }
  50. $table_config['ajax_url'] = 'block/ajax';
  51. return $this->view()->assign('table_config', $table_config)->display('admin/ip/block.tpl');
  52. }
  53. public function unblock($request, $response, $args)
  54. {
  55. $table_config['total_column'] = array("id" => "ID", "userid" => "用户ID",
  56. "user_name" => "用户名", "ip" => "IP",
  57. "location" => "归属地", "datetime" => "时间");
  58. $table_config['default_show_column'] = array();
  59. foreach ($table_config['total_column'] as $column => $value) {
  60. array_push($table_config['default_show_column'], $column);
  61. }
  62. $table_config['ajax_url'] = 'unblock/ajax';
  63. return $this->view()->assign('table_config', $table_config)->display('admin/ip/unblock.tpl');
  64. }
  65. public function doUnblock($request, $response, $args)
  66. {
  67. $ip = $request->getParam('ip');
  68. $user = Auth::getUser();
  69. $BIP = BlockIp::where("ip", $ip)->get();
  70. foreach ($BIP as $bi) {
  71. $bi->delete();
  72. }
  73. $UIP = new UnblockIp();
  74. $UIP->userid = $user->id;
  75. $UIP->ip = $ip;
  76. $UIP->datetime = time();
  77. $UIP->save();
  78. $res['ret'] = 1;
  79. $res['msg'] = "发送解封命令解封 ".$ip." 成功";
  80. return $this->echoJson($response, $res);
  81. }
  82. public function ajax_block($request, $response, $args)
  83. {
  84. $datatables = new Datatables(new DatatablesHelper());
  85. $datatables->query('Select blockip.id,node.name,blockip.ip,blockip.ip as location,datetime from blockip,ss_node as node WHERE blockip.nodeid = node.id');
  86. $datatables->edit('datetime', function ($data) {
  87. return date('Y-m-d H:i:s', $data['datetime']);
  88. });
  89. $iplocation = new QQWry();
  90. $datatables->edit('location', function ($data) use ($iplocation) {
  91. $location=$iplocation->getlocation($data['location']);
  92. return iconv('gbk', 'utf-8//IGNORE', $location['country'].$location['area']);
  93. });
  94. $body = $response->getBody();
  95. $body->write($datatables->generate());
  96. }
  97. public function ajax_unblock($request, $response, $args)
  98. {
  99. $datatables = new Datatables(new DatatablesHelper());
  100. $datatables->query('Select unblockip.id,userid,user.user_name,unblockip.ip,unblockip.ip as location,datetime from unblockip,user WHERE unblockip.userid = user.id');
  101. $datatables->edit('datetime', function ($data) {
  102. return date('Y-m-d H:i:s', $data['datetime']);
  103. });
  104. $iplocation = new QQWry();
  105. $datatables->edit('location', function ($data) use ($iplocation) {
  106. $location=$iplocation->getlocation($data['location']);
  107. return iconv('gbk', 'utf-8//IGNORE', $location['country'].$location['area']);
  108. });
  109. $body = $response->getBody();
  110. $body->write($datatables->generate());
  111. }
  112. public function ajax_login($request, $response, $args)
  113. {
  114. $datatables = new Datatables(new DatatablesHelper());
  115. $datatables->query('Select login_ip.id,login_ip.userid,user.user_name,login_ip.ip,login_ip.ip as location,login_ip.datetime,login_ip.type from login_ip,user WHERE login_ip.userid = user.id');
  116. $datatables->edit('datetime', function ($data) {
  117. return date('Y-m-d H:i:s', $data['datetime']);
  118. });
  119. $iplocation = new QQWry();
  120. $datatables->edit('location', function ($data) use ($iplocation) {
  121. $location=$iplocation->getlocation($data['location']);
  122. return iconv('gbk', 'utf-8//IGNORE', $location['country'].$location['area']);
  123. });
  124. $datatables->edit('type', function ($data) {
  125. return $data['type'] == 0 ? '成功' : '失败';
  126. });
  127. $body = $response->getBody();
  128. $body->write($datatables->generate());
  129. }
  130. public function ajax_alive($request, $response, $args)
  131. {
  132. $datatables = new Datatables(new DatatablesHelper());
  133. $datatables->query('Select alive_ip.id,alive_ip.userid,user.user_name,alive_ip.nodeid,ss_node.name as node_name,alive_ip.ip,alive_ip.ip as location,alive_ip.datetime,alive_ip.id as is_node from alive_ip,user,ss_node WHERE alive_ip.userid = user.id and alive_ip.nodeid = ss_node.id and `datetime` > UNIX_TIMESTAMP() - 60');
  134. $datatables->edit('datetime', function ($data) {
  135. return date('Y-m-d H:i:s', $data['datetime']);
  136. });
  137. $iplocation = new QQWry();
  138. $datatables->edit('ip', function ($data){
  139. return Tools::getRealIp($data['ip']);
  140. });
  141. $datatables->edit('is_node', function ($data){
  142. $is_node = Node::where("node_ip", Tools::getRealIp($data['ip']))->first();
  143. if($is_node) {
  144. return "是";
  145. } else {
  146. return "否";
  147. }
  148. });
  149. $datatables->edit('location', function ($data) use ($iplocation) {
  150. $location=$iplocation->getlocation(Tools::getRealIp($data['location']));
  151. return iconv('gbk', 'utf-8//IGNORE', $location['country'].$location['area']);
  152. });
  153. $body = $response->getBody();
  154. $body->write($datatables->generate());
  155. }
  156. }