BaseController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Models\Config;
  4. use App\Http\Models\EmailLog;
  5. use App\Http\Models\Level;
  6. use App\Http\Models\SsConfig;
  7. use App\Http\Models\User;
  8. /**
  9. * 基础控制器
  10. * Class BaseController
  11. * @package App\Http\Controllers
  12. */
  13. class BaseController extends Controller
  14. {
  15. // 生成SS密码
  16. public function makeRandStr($length = 6)
  17. {
  18. // 密码字符集,可任意添加你需要的字符
  19. $chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789';
  20. $char = '';
  21. for ($i = 0; $i < $length; $i++) {
  22. $char .= $chars[mt_rand(0, strlen($chars) - 1)];
  23. }
  24. return $char;
  25. }
  26. // base64加密(处理URL)
  27. function base64url_encode($data)
  28. {
  29. return strtr(base64_encode($data), array('+' => '-', '/' => '_', '=' => ''));
  30. }
  31. // base64解密(处理URL)
  32. function base64url_decode($data)
  33. {
  34. return base64_decode(strtr($data, '-_', '+/'));
  35. }
  36. // 根据流量值自动转换单位输出
  37. public static function flowAutoShow($value = 0)
  38. {
  39. $kb = 1024;
  40. $mb = 1048576;
  41. $gb = 1073741824;
  42. $tb = $gb * 1024;
  43. $pb = $tb * 1024;
  44. if (abs($value) > $pb) {
  45. return round($value / $pb, 2) . "PB";
  46. } elseif (abs($value) > $tb) {
  47. return round($value / $tb, 2) . "TB";
  48. } elseif (abs($value) > $gb) {
  49. return round($value / $gb, 2) . "GB";
  50. } elseif (abs($value) > $mb) {
  51. return round($value / $mb, 2) . "MB";
  52. } elseif (abs($value) > $kb) {
  53. return round($value / $kb, 2) . "KB";
  54. } else {
  55. return round($value, 2) . "B";
  56. }
  57. }
  58. public static function toMB($traffic)
  59. {
  60. $mb = 1048576;
  61. return $traffic * $mb;
  62. }
  63. public static function toGB($traffic)
  64. {
  65. $gb = 1048576 * 1024;
  66. return $traffic * $gb;
  67. }
  68. public static function flowToGB($traffic)
  69. {
  70. $gb = 1048576 * 1024;
  71. return $traffic / $gb;
  72. }
  73. // 加密方式
  74. public function methodList()
  75. {
  76. return SsConfig::query()->where('type', 1)->get();
  77. }
  78. // 协议
  79. public function protocolList()
  80. {
  81. return SsConfig::query()->where('type', 2)->get();
  82. }
  83. // 混淆
  84. public function obfsList()
  85. {
  86. return SsConfig::query()->where('type', 3)->get();
  87. }
  88. // 等级
  89. public function levelList()
  90. {
  91. return Level::query()->get()->sortBy('level');
  92. }
  93. // 系统配置
  94. public function systemConfig()
  95. {
  96. $config = Config::query()->get();
  97. $data = [];
  98. foreach ($config as $vo) {
  99. $data[$vo->name] = $vo->value;
  100. }
  101. return $data;
  102. }
  103. // 获取一个随机端口
  104. public function getRandPort()
  105. {
  106. $config = $this->systemConfig();
  107. $port = mt_rand($config['min_port'], $config['max_port']);
  108. $deny_port = [1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179]; // 不生成的端口
  109. $exists_port = User::query()->pluck('port')->toArray();
  110. if (in_array($port, $exists_port) || in_array($port, $deny_port)) {
  111. $port = $this->getRandPort();
  112. }
  113. return $port;
  114. }
  115. // 类似Linux中的tail命令
  116. public function tail($file, $n, $base = 5)
  117. {
  118. $fileLines = $this->countLine($file);
  119. if ($fileLines < 15000) {
  120. return false;
  121. }
  122. $fp = fopen($file, "r+");
  123. assert($n > 0);
  124. $pos = $n + 1;
  125. $lines = array();
  126. while (count($lines) <= $n) {
  127. try {
  128. fseek($fp, -$pos, SEEK_END);
  129. } catch (Exception $e) {
  130. fseek(0);
  131. break;
  132. }
  133. $pos *= $base;
  134. while (!feof($fp)) {
  135. array_unshift($lines, fgets($fp));
  136. }
  137. }
  138. return array_slice($lines, 0, $n);
  139. }
  140. /**
  141. * 计算文件行数
  142. */
  143. function countLine($file)
  144. {
  145. $fp = fopen($file, "r");
  146. $i = 0;
  147. while (!feof($fp)) {
  148. //每次读取2M
  149. if ($data = fread($fp, 1024 * 1024 * 2)) {
  150. //计算读取到的行数
  151. $num = substr_count($data, "\n");
  152. $i += $num;
  153. }
  154. }
  155. fclose($fp);
  156. return $i;
  157. }
  158. /**
  159. * 文件大小转换
  160. *
  161. * @param int $bytes
  162. * @param int $precision
  163. *
  164. * @return string
  165. */
  166. public function formatBytes($bytes, $precision = 2)
  167. {
  168. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  169. $bytes = max($bytes, 0);
  170. $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  171. $pow = min($pow, count($units) - 1);
  172. $bytes /= pow(1024, $pow);
  173. return round($bytes, $precision) . ' ' . $units[$pow];
  174. }
  175. // 禁止注册的邮箱后缀
  176. public function forbidDomain()
  177. {
  178. return [
  179. 'gov.cn',
  180. 'edu.cn'
  181. ];
  182. }
  183. /**
  184. * 写入邮件发送日志
  185. * @param int $user_id 用户ID
  186. * @param string $title 投递类型(投递标题)
  187. * @param string $content 投递内容(简要概述)
  188. * @param int $status 投递状态
  189. * @param string $error 投递失败时记录的异常信息
  190. */
  191. public function sendEmailLog($user_id, $title, $content, $status = 1, $error = '')
  192. {
  193. $emailLogObj = new EmailLog();
  194. $emailLogObj->user_id = $user_id;
  195. $emailLogObj->title = $title;
  196. $emailLogObj->content = $content;
  197. $emailLogObj->status = $status;
  198. $emailLogObj->error = $error;
  199. $emailLogObj->created_at = date('Y-m-d H:i:s');
  200. $emailLogObj->save();
  201. }
  202. }