Controller.php 6.0 KB

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