1
0

BaseController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. if (abs($value) > $gb) {
  41. return round($value / $gb, 2) . "GB";
  42. } else if (abs($value) > $mb) {
  43. return round($value / $mb, 2) . "MB";
  44. } else if (abs($value) > $kb) {
  45. return round($value / $kb, 2) . "KB";
  46. } else {
  47. return round($value, 2);
  48. }
  49. }
  50. public static function toMB($traffic)
  51. {
  52. $mb = 1048576;
  53. return $traffic * $mb;
  54. }
  55. public static function toGB($traffic)
  56. {
  57. $gb = 1048576 * 1024;
  58. return $traffic * $gb;
  59. }
  60. public static function flowToGB($traffic)
  61. {
  62. $gb = 1048576 * 1024;
  63. return $traffic / $gb;
  64. }
  65. // 加密方式
  66. public function methodList()
  67. {
  68. return SsConfig::where('type', 1)->get();
  69. }
  70. // 协议
  71. public function protocolList()
  72. {
  73. return SsConfig::where('type', 2)->get();
  74. }
  75. // 混淆
  76. public function obfsList()
  77. {
  78. return SsConfig::where('type', 3)->get();
  79. }
  80. // 系统配置
  81. public function systemConfig()
  82. {
  83. $config = Config::get();
  84. $data = [];
  85. foreach ($config as $vo) {
  86. $data[$vo->name] = $vo->value;
  87. }
  88. return $data;
  89. }
  90. // 获取一个随机端口
  91. public function getRandPort()
  92. {
  93. $port = mt_rand(10000,30000);
  94. $exists_port = User::query()->pluck('port')->toArray();
  95. if (in_array($port, $exists_port)) {
  96. $this->getRandPort();
  97. }
  98. return $port;
  99. }
  100. // 类似Linux中的tail命令
  101. public function tail($file, $n, $base = 5)
  102. {
  103. $fp = fopen($file, "r+");
  104. assert($n > 0);
  105. $pos = $n + 1;
  106. $lines = array();
  107. while (count($lines) <= $n) {
  108. try {
  109. fseek($fp, -$pos, SEEK_END);
  110. } catch (Exception $e) {
  111. fseek(0);
  112. break;
  113. }
  114. $pos *= $base;
  115. while (!feof($fp)) {
  116. array_unshift($lines, fgets($fp));
  117. }
  118. }
  119. return array_slice($lines, 0, $n);
  120. }
  121. }