BaseController.php 3.9 KB

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