Index.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use fast\Random;
  5. use think\Queue;
  6. /**
  7. * 首页接口
  8. */
  9. class Index extends Api
  10. {
  11. protected $noNeedLogin = ['*'];
  12. protected $noNeedRight = ['*'];
  13. /**
  14. * 首页
  15. *
  16. */
  17. public function index()
  18. {
  19. $proxy_ip = $this->request->request('local_addr');
  20. $user_ip = $this->request->request('client_addr');
  21. $service = $this->request->request('service');
  22. $is_sps = $this->request->request('sps')=='1';
  23. $account = $this->request->request('user');
  24. $password = $this->request->request('pass');
  25. $target = $this->request->request('target');
  26. if (!$account || !$password) {
  27. return $this->err();
  28. }
  29. $ret = $this->auth->login($account, $password);
  30. if ($ret) {
  31. $user = \app\common\model\User::where(['mobile'=>$account])->find();
  32. if($user->total_traffic < $user->traffic){
  33. // 流量已用完
  34. return $this->err();
  35. }
  36. $user_ip = explode(":",$user_ip)[0];
  37. \app\common\model\User::where(['id'=>$this->auth->id])->update(['loginip'=>$user_ip]);
  38. header("userconns:1000"); // 用户的最大连接数,不限制为0或者不设置这个头部。
  39. header("ipconns:2000"); // 用户IP的最大连接数,不限制为0或者不设置这个头部。
  40. //header("userrate:3000"); // 用户的单个TCP连接速率限制,单位:字节/秒,不限制为0或者不设置这个头部。
  41. //header("iprate:8000"); // 用户IP的单个TCP连接速率限制,单位:字节/秒,不限制为0或者不设置这个头部。
  42. //header("UPSTREAM:http://127.0.0.1:3500?parent-type=tcp"); // 使用的上级,没有为空,或者不设置这个头部。
  43. header("HTTP/1.1 204 No Content");
  44. exit;
  45. return 'true';
  46. } else {
  47. return $this->err();
  48. }
  49. }
  50. /**
  51. * 功能: 流量上报, 生成队列, 后台进程处理流量数据
  52. * @return bool|void
  53. */
  54. public function traffic()
  55. {
  56. $jobData = $this->request->request();
  57. $jobHandlerClassName = "app\job\FlowLog";
  58. $jobName = "FlowLog";
  59. $jobData['time'] = time();
  60. $ret = Queue::push($jobHandlerClassName, $jobData, $jobName);
  61. if ($ret) {
  62. return header('HTTP/1.0 204 Success');
  63. }
  64. return false;
  65. }
  66. // 状态上报
  67. public function status()
  68. {
  69. #接收proxy post过来的数据
  70. $userArr = $this->request->request('user');
  71. $ipArr = $this->request->request('ip');
  72. //无效用户列表
  73. $badUsers=[];
  74. $userArr = explode(',',$userArr);
  75. $badUsers = \app\common\model\User::whereIn('mobile', $userArr)->where("traffic > ifnull(total_traffic, traffic-1)")->column('mobile');
  76. $data = ["user"=>implode(",",$badUsers ?? []),"ip"=>""];
  77. // $data = ["user"=>implode(",",[]),"ip"=>""];
  78. echo json_encode($data);
  79. exit;
  80. }
  81. public function birth()
  82. {
  83. $username = time().Random::numeric(3);
  84. $password = Random::numeric(6);
  85. $email = $password."@qq.com";
  86. $mobile = Random::numeric(6).Random::numeric(5);
  87. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  88. if ($ret) {
  89. $user = $this->auth->getUserinfo();
  90. $data = ['userinfo' => $user];
  91. $this->success(__('Sign up successful'), $data);
  92. } else {
  93. $this->error($this->auth->getError());
  94. }
  95. }
  96. public function err(){
  97. header("HTTP/1.1 404 No Content");
  98. return "";
  99. }
  100. }