Controller.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Models\UserSubscribe;
  4. use Illuminate\Foundation\Bus\DispatchesJobs;
  5. use Illuminate\Routing\Controller as BaseController;
  6. use Illuminate\Foundation\Validation\ValidatesRequests;
  7. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  8. use App\Http\Models\Config;
  9. use App\Http\Models\EmailLog;
  10. use App\Http\Models\Level;
  11. use App\Http\Models\SsConfig;
  12. use App\Http\Models\User;
  13. class Controller extends BaseController
  14. {
  15. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  16. // 生成订阅地址的唯一码
  17. public function makeSubscribeCode()
  18. {
  19. $code = makeRandStr(5);
  20. if (UserSubscribe::query()->where('code', $code)->exists()) {
  21. $code = $this->makeSubscribeCode();
  22. }
  23. return $code;
  24. }
  25. // 加密方式
  26. public function methodList()
  27. {
  28. return SsConfig::query()->where('type', 1)->get();
  29. }
  30. // 协议
  31. public function protocolList()
  32. {
  33. return SsConfig::query()->where('type', 2)->get();
  34. }
  35. // 混淆
  36. public function obfsList()
  37. {
  38. return SsConfig::query()->where('type', 3)->get();
  39. }
  40. // 等级
  41. public function levelList()
  42. {
  43. return Level::query()->get()->sortBy('level');
  44. }
  45. // 系统配置
  46. public function systemConfig()
  47. {
  48. $config = Config::query()->get();
  49. $data = [];
  50. foreach ($config as $vo) {
  51. $data[$vo->name] = $vo->value;
  52. }
  53. return $data;
  54. }
  55. // 获取一个随机端口
  56. public function getRandPort()
  57. {
  58. $config = $this->systemConfig();
  59. $port = mt_rand($config['min_port'], $config['max_port']);
  60. $deny_port = [1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179]; // 不生成的端口
  61. $exists_port = User::query()->pluck('port')->toArray();
  62. if (in_array($port, $exists_port) || in_array($port, $deny_port)) {
  63. $port = $this->getRandPort();
  64. }
  65. return $port;
  66. }
  67. // 类似Linux中的tail命令
  68. public function tail($file, $n, $base = 5)
  69. {
  70. $fileLines = $this->countLine($file);
  71. if ($fileLines < 15000) {
  72. return false;
  73. }
  74. $fp = fopen($file, "r+");
  75. assert($n > 0);
  76. $pos = $n + 1;
  77. $lines = [];
  78. while (count($lines) <= $n) {
  79. try {
  80. fseek($fp, -$pos, SEEK_END);
  81. } catch (\Exception $e) {
  82. fseek(0);
  83. break;
  84. }
  85. $pos *= $base;
  86. while (!feof($fp)) {
  87. array_unshift($lines, fgets($fp));
  88. }
  89. }
  90. return array_slice($lines, 0, $n);
  91. }
  92. /**
  93. * 计算文件行数
  94. */
  95. public function countLine($file)
  96. {
  97. $fp = fopen($file, "r");
  98. $i = 0;
  99. while (!feof($fp)) {
  100. //每次读取2M
  101. if ($data = fread($fp, 1024 * 1024 * 2)) {
  102. //计算读取到的行数
  103. $num = substr_count($data, "\n");
  104. $i += $num;
  105. }
  106. }
  107. fclose($fp);
  108. return $i;
  109. }
  110. /**
  111. * 写入邮件发送日志
  112. *
  113. * @param int $user_id 用户ID
  114. * @param string $title 标题
  115. * @param string $content 内容
  116. * @param int $status 投递状态
  117. * @param string $error 投递失败时记录的异常信息
  118. */
  119. public function sendEmailLog($user_id, $title, $content, $status = 1, $error = '')
  120. {
  121. $emailLogObj = new EmailLog();
  122. $emailLogObj->user_id = $user_id;
  123. $emailLogObj->title = $title;
  124. $emailLogObj->content = $content;
  125. $emailLogObj->status = $status;
  126. $emailLogObj->error = $error;
  127. $emailLogObj->created_at = date('Y-m-d H:i:s');
  128. $emailLogObj->save();
  129. }
  130. }