UserController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace App\Controllers;
  3. use App\Models\CheckInLog;
  4. use App\Models\InviteCode;
  5. use App\Models\Node;
  6. use App\Models\User;
  7. use App\Models\TrafficLog;
  8. use App\Services\Auth;
  9. use App\Services\Config;
  10. use App\Services\DbConfig;
  11. use App\Utils\Hash;
  12. use App\Utils\Tools;
  13. /**
  14. * HomeController
  15. */
  16. class UserController extends BaseController
  17. {
  18. private $user;
  19. private $node;
  20. public function __construct()
  21. {
  22. $this->user = Auth::getUser();
  23. }
  24. public function view()
  25. {
  26. $userFooter = DbConfig::get('user-footer');
  27. return parent::view()->assign('userFooter', $userFooter);
  28. }
  29. public function index($request, $response, $args)
  30. {
  31. $msg = DbConfig::get('user-index');
  32. if ($msg == null) {
  33. $msg = "在后台修改用户中心公告...";
  34. }
  35. $nodes = Node::all();
  36. $android_add="";
  37. foreach($nodes as $node)
  38. {
  39. if($android_add=="")
  40. {
  41. $ary['server'] = $node->server;
  42. $ary['server_port'] = $this->user->port;
  43. $ary['password'] = $this->user->passwd;
  44. $ary['method'] = $this->user->method;
  45. /* if ($this->user->custom_method) {
  46. $ary['method'] = $this->user->method;
  47. }
  48. */
  49. $ssurl = $ary['method'] . ":" . $ary['password'] . "@" . $ary['server'] . ":" . $ary['server_port'];
  50. $ssqr = "ss://" . base64_encode($ssurl);
  51. $android_add .="'".$ssqr."'";
  52. }
  53. else
  54. {
  55. $ary['server'] = $node->server;
  56. $ary['server_port'] = $this->user->port;
  57. $ary['password'] = $this->user->passwd;
  58. $ary['method'] = $this->user->method;
  59. /* if ($this->user->custom_method) {
  60. $ary['method'] = $this->user->method;
  61. }*/
  62. $ssurl = $ary['method'] . ":" . $ary['password'] . "@" . $ary['server'] . ":" . $ary['server_port'];
  63. $ssqr = "ss://" . base64_encode($ssurl);
  64. $android_add .=",'".$ssqr."'";
  65. }
  66. }
  67. return $this->view()->assign('msg', $msg)->assign("android_add",$android_add)->assign('baseUrl',Config::get('baseUrl'))->display('user/index.tpl');
  68. }
  69. public function node($request, $response, $args)
  70. {
  71. $msg = DbConfig::get('user-node');
  72. $user = Auth::getUser();
  73. $nodes = Node::where('type', 1)->orderBy('sort')->get();
  74. return $this->view()->assign('nodes', $nodes)->assign('user', $user)->assign('msg', $msg)->display('user/node.tpl');
  75. }
  76. public function nodeInfo($request, $response, $args)
  77. {
  78. $user = Auth::getUser();
  79. $id = $args['id'];
  80. $node = Node::find($id);
  81. if ($node == null) {
  82. }
  83. $ary['server'] = $node->server;
  84. $ary['server_port'] = $this->user->port;
  85. $ary['password'] = $this->user->passwd;
  86. $ary['method'] = $this->user->method;
  87. /* if ($this->user->custom_method) {
  88. $ary['method'] = $this->user->method;
  89. }*/
  90. $ary['protocol'] = $this->user->protocol;
  91. $ary['obfs'] = $this->user->obfs;
  92. if ($this->user->obfs=='http_post'||$this->user->obfs=='http_simple'||$this->user->obfs=='tls1.2_ticket_auth') {
  93. $ary['obfs_param'] = $this->user->obfs_param;
  94. }
  95. $json = json_encode($ary);
  96. $json_show = json_encode($ary, JSON_PRETTY_PRINT);
  97. if ($user->obfs=='http_simple' || $user->obfs=='http_post' || $user->obfs=='random_head' || $user->obfs=='tls_simple' || $user->obfs=='tls1.0_session_auth' || $user->obfs=='tls1.2_ticket_auth' || $user->protocol=='verify_simple' || $user->protocol=='verify_deflate' || $user->protocol=='verify_sha1' || $user->protocol=='auth_simple' || $user->protocol=='auth_sha1' || $user->protocol=='auth_sha1_v2')
  98. {
  99. $ssurl = str_replace("_compatible","",$user->obfs).":".str_replace("_compatible","",$user->protocol).":".$ary['method'] . ":" . $ary['password'] . "@" . $ary['server'] . ":" . $ary['server_port']."/".base64_encode($user->obfs_param);
  100. $ssqr_s = "ss://" . base64_encode($ssurl);
  101. $ssqr = "ss://" . base64_encode($ssurl);
  102. }
  103. else
  104. {
  105. $ssurl = str_replace("_compatible","",$user->obfs).":".str_replace("_compatible","",$user->protocol).":".$ary['method'] . ":" . $ary['password'] . "@" . $ary['server'] . ":" . $ary['server_port']."/".base64_encode($user->obfs_param);
  106. $ssqr_s = "ss://" . base64_encode($ssurl);
  107. $ssurl = $ary['method'] . ":" . $ary['password'] . "@" . $ary['server'] . ":" . $ary['server_port'];
  108. $ssqr = "ss://" . base64_encode($ssurl);
  109. }
  110. /* $token_1 = LinkController::GenerateSurgeCode($ary['server'],$ary['server_port'],$this->user->id,0,$ary['method']);
  111. $token_2 = LinkController::GenerateSurgeCode($ary['server'],$ary['server_port'],$this->user->id,1,$ary['method']);*/
  112. $surge_base = Config::get('baseUrl') . "/downloads/ProxyBase.conf";
  113. $surge_proxy = "#!PROXY-OVERRIDE:ProxyBase.conf\n";
  114. $surge_proxy .= "[Proxy]\n";
  115. $surge_proxy .= "Proxy = custom," . $ary['server'] . "," . $ary['server_port'] . "," . $ary['method'] . "," . $ary['password'] . "," . Config::get('baseUrl') . "/downloads/SSEncrypt.module";
  116. return $this->view()->assign('json', $json)->assign('json_show', $json_show)->assign('ssqr', $ssqr)->assign('ssqr_s', $ssqr_s)->assign('surge_base', $surge_base)->assign('surge_proxy', $surge_proxy)->display('user/nodeinfo.tpl');
  117. /* return $this->view()->assign('ary', $ary)->assign('node',$node)->assign('user',$this->user)->assign('json', $json)->assign('link1',Config::get('baseUrl')."/link/".$token_1)->assign('link2',Config::get('baseUrl')."/link/".$token_2)->assign('json_show', $json_show)->assign('ssqr', $ssqr)->assign('ssqr_s', $ssqr_s)->assign('surge_base', $surge_base)->assign('surge_proxy', $surge_proxy)->assign('info_server', $ary['server'])->assign('info_port', $this->user->port)->assign('info_method', $ary['method'])->assign('info_pass', $this->user->passwd)->display('user/nodeinfo.tpl');*/
  118. }
  119. public function profile($request, $response, $args)
  120. {
  121. return $this->view()->display('user/profile.tpl');
  122. }
  123. public function edit($request, $response, $args)
  124. {
  125. return $this->view()->display('user/edit.tpl');
  126. }
  127. public function invite($request, $response, $args)
  128. {
  129. $codes = $this->user->inviteCodes();
  130. return $this->view()->assign('codes', $codes)->display('user/invite.tpl');
  131. }
  132. public function doInvite($request, $response, $args)
  133. {
  134. $n = $this->user->invite_num;
  135. if ($n < 1) {
  136. $res['ret'] = 0;
  137. return $response->getBody()->write(json_encode($res));
  138. }
  139. for ($i = 0; $i < $n; $i++) {
  140. $char = Tools::genRandomChar(32);
  141. $code = new InviteCode();
  142. $code->code = $char;
  143. $code->user_id = $this->user->id;
  144. $code->save();
  145. }
  146. $this->user->invite_num = 0;
  147. $this->user->save();
  148. $res['ret'] = 1;
  149. return $this->echoJson($response, $res);
  150. }
  151. public function sys($request, $response, $args)
  152. {
  153. return $this->view()->assign('ana', "")->display('user/sys.tpl');
  154. }
  155. public function updatePassword($request, $response, $args)
  156. {
  157. $oldpwd = $request->getParam('oldpwd');
  158. $pwd = $request->getParam('pwd');
  159. $repwd = $request->getParam('repwd');
  160. $user = $this->user;
  161. if (!Hash::checkPassword($user->pass, $oldpwd)) {
  162. $res['ret'] = 0;
  163. $res['msg'] = "旧密码错误";
  164. return $response->getBody()->write(json_encode($res));
  165. }
  166. if ($pwd != $repwd) {
  167. $res['ret'] = 0;
  168. $res['msg'] = "两次输入不符合";
  169. return $response->getBody()->write(json_encode($res));
  170. }
  171. if (strlen($pwd) < 8) {
  172. $res['ret'] = 0;
  173. $res['msg'] = "密码太短啦";
  174. return $response->getBody()->write(json_encode($res));
  175. }
  176. $hashPwd = Hash::passwordHash($pwd);
  177. $user->pass = $hashPwd;
  178. $user->save();
  179. $res['ret'] = 1;
  180. $res['msg'] = "ok";
  181. return $this->echoJson($response, $res);
  182. }
  183. public function updateSsPwd($request, $response, $args)
  184. {
  185. $user = Auth::getUser();
  186. $pwd = $request->getParam('sspwd');
  187. $user->updateSsPwd($pwd);
  188. $res['ret'] = 1;
  189. return $this->echoJson($response, $res);
  190. }
  191. public function updateMethod($request, $response, $args)
  192. {
  193. $user = Auth::getUser();
  194. $method = $request->getParam('method');
  195. $method = strtolower($method);
  196. $user->updateMethod($method);
  197. $res['ret'] = 1;
  198. return $this->echoJson($response, $res);
  199. }
  200. public function updateProtocol($request, $response, $args)
  201. {
  202. $user = Auth::getUser();
  203. $protocol = $request->getParam('protocol');
  204. $protocol = strtolower($protocol);
  205. $user->updateProtocol($protocol);
  206. $res['ret'] = 1;
  207. return $this->echoJson($response, $res);
  208. }
  209. public function updateObfs($request, $response, $args)
  210. {
  211. $user = Auth::getUser();
  212. $obfs = $request->getParam('obfs');
  213. $obfs = strtolower($obfs);
  214. $user->updateObfs($obfs);
  215. $res['ret'] = 1;
  216. return $this->echoJson($response, $res);
  217. }
  218. public function logout($request, $response, $args)
  219. {
  220. Auth::logout();
  221. $newResponse = $response->withStatus(302)->withHeader('Location', '/auth/login');
  222. return $newResponse;
  223. }
  224. public function doCheckIn($request, $response, $args)
  225. {
  226. if (!$this->user->isAbleToCheckin()) {
  227. $res['msg'] = "您似乎已经签到过了...";
  228. $res['ret'] = 1;
  229. return $response->getBody()->write(json_encode($res));
  230. }
  231. $traffic = rand(Config::get('checkinMin'), Config::get('checkinMax'));
  232. $trafficToAdd = Tools::toMB($traffic);
  233. $this->user->transfer_enable = $this->user->transfer_enable + $trafficToAdd;
  234. $this->user->last_check_in_time = time();
  235. $this->user->save();
  236. // checkin log
  237. try {
  238. $log = new CheckInLog();
  239. $log->user_id = Auth::getUser()->id;
  240. $log->traffic = $trafficToAdd;
  241. $log->checkin_at = time();
  242. $log->save();
  243. } catch (\Exception $e) {
  244. }
  245. $res['msg'] = sprintf("获得了 %u MB流量.", $traffic);
  246. $res['ret'] = 1;
  247. return $this->echoJson($response, $res);
  248. }
  249. public function kill($request, $response, $args)
  250. {
  251. return $this->view()->display('user/kill.tpl');
  252. }
  253. public function handleKill($request, $response, $args)
  254. {
  255. $user = Auth::getUser();
  256. $passwd = $request->getParam('passwd');
  257. // check passwd
  258. $res = array();
  259. if (!Hash::checkPassword($user->pass, $passwd)) {
  260. $res['ret'] = 0;
  261. $res['msg'] = " 密码错误";
  262. return $this->echoJson($response, $res);
  263. }
  264. Auth::logout();
  265. $user->delete();
  266. $res['ret'] = 1;
  267. $res['msg'] = "GG!您的帐号已经从我们的系统中删除.";
  268. return $this->echoJson($response, $res);
  269. }
  270. public function trafficLog($request, $response, $args)
  271. {
  272. $pageNum = 1;
  273. if (isset($request->getQueryParams()["page"])) {
  274. $pageNum = $request->getQueryParams()["page"];
  275. }
  276. $traffic = TrafficLog::where('user_id', $this->user->id)->orderBy('id', 'desc')->paginate(15, ['*'], 'page', $pageNum);
  277. $traffic->setPath('/user/trafficlog');
  278. return $this->view()->assign('logs', $traffic)->display('user/trafficlog.tpl');
  279. }
  280. }