$pb) { return round($value / $pb, 2) . "PB"; } elseif (abs($value) > $tb) { return round($value / $tb, 2) . "TB"; } elseif (abs($value) > $gb) { return round($value / $gb, 2) . "GB"; } elseif (abs($value) > $mb) { return round($value / $mb, 2) . "MB"; } elseif (abs($value) > $kb) { return round($value / $kb, 2) . "KB"; } else { return round($value, 2) . "B"; } } public static function toMB($traffic) { $mb = 1048576; return $traffic * $mb; } public static function toGB($traffic) { $gb = 1048576 * 1024; return $traffic * $gb; } public static function flowToGB($traffic) { $gb = 1048576 * 1024; return $traffic / $gb; } // 加密方式 public function methodList() { return SsConfig::where('type', 1)->get(); } // 协议 public function protocolList() { return SsConfig::where('type', 2)->get(); } // 混淆 public function obfsList() { return SsConfig::where('type', 3)->get(); } // 系统配置 public function systemConfig() { $config = Config::get(); $data = []; foreach ($config as $vo) { $data[$vo->name] = $vo->value; } return $data; } // 获取一个随机端口 public function getRandPort() { $port = mt_rand(10000, 40000); $deny_port = [17185, 28281]; $exists_port = User::query()->pluck('port')->toArray(); if (in_array($port, $exists_port) || in_array($port, $deny_port)) { $port = $this->getRandPort(); } return $port; } // 类似Linux中的tail命令 public function tail($file, $n, $base = 5) { $fp = fopen($file, "r+"); assert($n > 0); $pos = $n + 1; $lines = array(); while (count($lines) <= $n) { try { fseek($fp, -$pos, SEEK_END); } catch (Exception $e) { fseek(0); break; } $pos *= $base; while (!feof($fp)) { array_unshift($lines, fgets($fp)); } } return array_slice($lines, 0, $n); } /** * 文件大小转换 * * @param int $bytes * @param int $precision * * @return string */ public function formatBytes($bytes, $precision = 2) { $units = array('B', 'KB', 'MB', 'GB', 'TB'); $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) - 1); $bytes /= pow(1024, $pow); return round($bytes, $precision) . ' ' . $units[$pow]; } /** * 写入邮件发送日志 * @param int $user_id 用户ID * @param string $title 投递类型(投递标题) * @param string $content 投递内容(简要概述) * @param int $status 投递状态 * @param string $error 投递失败时记录的异常信息 */ public function sendEmailLog($user_id, $title, $content, $status = 1, $error = '') { $emailLogObj = new EmailLog(); $emailLogObj->user_id = $user_id; $emailLogObj->title = $title; $emailLogObj->content = $content; $emailLogObj->status = $status; $emailLogObj->error = $error; $emailLogObj->created_at = date('Y-m-d H:i:s'); $emailLogObj->save(); } }