BaseController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Models\Config;
  4. use App\Http\Models\EmailLog;
  5. use App\Http\Models\SsConfig;
  6. use App\Http\Models\User;
  7. /**
  8. * 基础控制器
  9. * Class BaseController
  10. * @package App\Http\Controllers
  11. */
  12. class BaseController extends Controller
  13. {
  14. // 生成SS密码
  15. public function makeRandStr($length = 4)
  16. {
  17. // 密码字符集,可任意添加你需要的字符
  18. $chars = 'abcdefghijkmnpqrstuvwxyz23456789';
  19. $char = '@';
  20. for ($i = 0; $i < $length; $i++) {
  21. $char .= $chars[mt_rand(0, strlen($chars) - 1)];
  22. }
  23. return $char;
  24. }
  25. // base64加密(处理URL)
  26. function base64url_encode($data)
  27. {
  28. return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
  29. }
  30. // base64解密(处理URL)
  31. function base64url_decode($data)
  32. {
  33. return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
  34. }
  35. // 根据流量值自动转换单位输出
  36. public static function flowAutoShow($value = 0)
  37. {
  38. $kb = 1024;
  39. $mb = 1048576;
  40. $gb = 1073741824;
  41. $tb = $gb * 1024;
  42. $pb = $tb * 1024;
  43. if (abs($value) > $pb) {
  44. return round($value / $pb, 2) . "PB";
  45. } elseif (abs($value) > $tb) {
  46. return round($value / $tb, 2) . "TB";
  47. } elseif (abs($value) > $gb) {
  48. return round($value / $gb, 2) . "GB";
  49. } elseif (abs($value) > $mb) {
  50. return round($value / $mb, 2) . "MB";
  51. } elseif (abs($value) > $kb) {
  52. return round($value / $kb, 2) . "KB";
  53. } else {
  54. return round($value, 2) . "B";
  55. }
  56. }
  57. public static function toMB($traffic)
  58. {
  59. $mb = 1048576;
  60. return $traffic * $mb;
  61. }
  62. public static function toGB($traffic)
  63. {
  64. $gb = 1048576 * 1024;
  65. return $traffic * $gb;
  66. }
  67. public static function flowToGB($traffic)
  68. {
  69. $gb = 1048576 * 1024;
  70. return $traffic / $gb;
  71. }
  72. // 加密方式
  73. public function methodList()
  74. {
  75. return SsConfig::where('type', 1)->get();
  76. }
  77. // 协议
  78. public function protocolList()
  79. {
  80. return SsConfig::where('type', 2)->get();
  81. }
  82. // 混淆
  83. public function obfsList()
  84. {
  85. return SsConfig::where('type', 3)->get();
  86. }
  87. // 系统配置
  88. public function systemConfig()
  89. {
  90. $config = Config::get();
  91. $data = [];
  92. foreach ($config as $vo) {
  93. $data[$vo->name] = $vo->value;
  94. }
  95. return $data;
  96. }
  97. // 获取一个随机端口
  98. public function getRandPort()
  99. {
  100. $port = mt_rand(10000,30000);
  101. $exists_port = User::query()->pluck('port')->toArray();
  102. if (in_array($port, $exists_port)) {
  103. $this->getRandPort();
  104. }
  105. return $port;
  106. }
  107. // 类似Linux中的tail命令
  108. public function tail($file, $n, $base = 5)
  109. {
  110. $fp = fopen($file, "r+");
  111. assert($n > 0);
  112. $pos = $n + 1;
  113. $lines = array();
  114. while (count($lines) <= $n) {
  115. try {
  116. fseek($fp, -$pos, SEEK_END);
  117. } catch (Exception $e) {
  118. fseek(0);
  119. break;
  120. }
  121. $pos *= $base;
  122. while (!feof($fp)) {
  123. array_unshift($lines, fgets($fp));
  124. }
  125. }
  126. return array_slice($lines, 0, $n);
  127. }
  128. /**
  129. * 文件大小转换
  130. *
  131. * @param int $bytes
  132. * @param int $precision
  133. *
  134. * @return string
  135. */
  136. public function formatBytes($bytes, $precision = 2)
  137. {
  138. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  139. $bytes = max($bytes, 0);
  140. $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  141. $pow = min($pow, count($units) - 1);
  142. $bytes /= pow(1024, $pow);
  143. return round($bytes, $precision) . ' ' . $units[$pow];
  144. }
  145. /**
  146. * 写入邮件发送日志
  147. * @param int $user_id 用户ID
  148. * @param string $title 投递类型(投递标题)
  149. * @param string $content 投递内容(简要概述)
  150. * @param int $status 投递状态
  151. * @param string $error 投递失败时记录的异常信息
  152. */
  153. public function sendEmailLog($user_id, $title, $content, $status = 1, $error = '')
  154. {
  155. $emailLogObj = new EmailLog();
  156. $emailLogObj->user_id = $user_id;
  157. $emailLogObj->title = $title;
  158. $emailLogObj->content = $content;
  159. $emailLogObj->status = $status;
  160. $emailLogObj->error = $error;
  161. $emailLogObj->created_at = date('Y-m-d H:i:s');
  162. $emailLogObj->save();
  163. }
  164. }