Index.php 3.8 KB

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