Controller.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. // 生成订阅地址的唯一码
  17. public function makeSubscribeCode()
  18. {
  19. $code = makeRandStr(5);
  20. if (UserSubscribe::query()->where('code', $code)->exists()) {
  21. $code = $this->makeSubscribeCode();
  22. }
  23. return $code;
  24. }
  25. // 加密方式
  26. public function methodList()
  27. {
  28. return SsConfig::query()->where('type', 1)->get();
  29. }
  30. // 默认加密方式
  31. public function getDefaultMethod()
  32. {
  33. $config = SsConfig::query()->where('type', 1)->where('is_default', 1)->first();
  34. return $config ? $config->name : 'aes-192-ctr';
  35. }
  36. // 协议
  37. public function protocolList()
  38. {
  39. return SsConfig::query()->where('type', 2)->get();
  40. }
  41. // 默认协议
  42. public function getDefaultProtocol()
  43. {
  44. $config = SsConfig::query()->where('type', 2)->where('is_default', 1)->first();
  45. return $config ? $config->name : 'origin';
  46. }
  47. // 混淆
  48. public function obfsList()
  49. {
  50. return SsConfig::query()->where('type', 3)->get();
  51. }
  52. // 默认混淆
  53. public function getDefaultObfs()
  54. {
  55. $config = SsConfig::query()->where('type', 3)->where('is_default', 1)->first();
  56. return $config ? $config->name : 'plain';
  57. }
  58. // 等级
  59. public function levelList()
  60. {
  61. return Level::query()->get()->sortBy('level');
  62. }
  63. // 系统配置
  64. public function systemConfig()
  65. {
  66. $config = Config::query()->get();
  67. $data = [];
  68. foreach ($config as $vo) {
  69. $data[$vo->name] = $vo->value;
  70. }
  71. return $data;
  72. }
  73. // 获取一个随机端口
  74. public function getRandPort()
  75. {
  76. $config = $this->systemConfig();
  77. $port = mt_rand($config['min_port'], $config['max_port']);
  78. $deny_port = [1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179]; // 不生成的端口
  79. $exists_port = User::query()->pluck('port')->toArray();
  80. if (in_array($port, $exists_port) || in_array($port, $deny_port)) {
  81. $port = $this->getRandPort();
  82. }
  83. return $port;
  84. }
  85. // 获取一个端口
  86. public function getOnlyPort()
  87. {
  88. $config = $this->systemConfig();
  89. $port = $config['min_port'];
  90. $deny_port = [1068, 1109, 1434, 3127, 3128, 3129, 3130, 3332, 4444, 5554, 6669, 8080, 8081, 8082, 8181, 8282, 9996, 17185, 24554, 35601, 60177, 60179]; // 不生成的端口
  91. $exists_port = User::query()->where('port', '>=', $config['min_port'])->pluck('port')->toArray();
  92. while (in_array($port, $exists_port) || in_array($port, $deny_port)) {
  93. $port = $port + 1;
  94. }
  95. return $port;
  96. }
  97. // 类似Linux中的tail命令
  98. public function tail($file, $n, $base = 5)
  99. {
  100. $fileLines = $this->countLine($file);
  101. if ($fileLines < 15000) {
  102. return false;
  103. }
  104. $fp = fopen($file, "r+");
  105. assert($n > 0);
  106. $pos = $n + 1;
  107. $lines = [];
  108. while (count($lines) <= $n) {
  109. try {
  110. fseek($fp, -$pos, SEEK_END);
  111. } catch (\Exception $e) {
  112. fseek(0);
  113. break;
  114. }
  115. $pos *= $base;
  116. while (!feof($fp)) {
  117. array_unshift($lines, fgets($fp));
  118. }
  119. }
  120. return array_slice($lines, 0, $n);
  121. }
  122. /**
  123. * 计算文件行数
  124. */
  125. public function countLine($file)
  126. {
  127. $fp = fopen($file, "r");
  128. $i = 0;
  129. while (!feof($fp)) {
  130. //每次读取2M
  131. if ($data = fread($fp, 1024 * 1024 * 2)) {
  132. //计算读取到的行数
  133. $num = substr_count($data, "\n");
  134. $i += $num;
  135. }
  136. }
  137. fclose($fp);
  138. return $i;
  139. }
  140. /**
  141. * 写入邮件发送日志
  142. *
  143. * @param int $user_id 用户ID
  144. * @param string $title 标题
  145. * @param string $content 内容
  146. * @param int $status 投递状态
  147. * @param string $error 投递失败时记录的异常信息
  148. */
  149. public function sendEmailLog($user_id, $title, $content, $status = 1, $error = '')
  150. {
  151. $emailLogObj = new EmailLog();
  152. $emailLogObj->user_id = $user_id;
  153. $emailLogObj->title = $title;
  154. $emailLogObj->content = $content;
  155. $emailLogObj->status = $status;
  156. $emailLogObj->error = $error;
  157. $emailLogObj->created_at = date('Y-m-d H:i:s');
  158. $emailLogObj->save();
  159. }
  160. // 将Base64图片转换为本地图片并保存
  161. function base64ImageSaver($base64_image_content)
  162. {
  163. //匹配出图片的格式
  164. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)) {
  165. $type = $result[2];
  166. $directory = date('Ymd');
  167. $path = '/assets/images/qrcode/' . $directory . '/';
  168. if (!file_exists(public_path($path))) { //检查是否有该文件夹,如果没有就创建,并给予最高权限
  169. mkdir(public_path($path), 0700, true);
  170. }
  171. $fileName = makeRandStr(18, true) . ".{$type}";
  172. if (file_put_contents(public_path($path . $fileName), base64_decode(str_replace($result[1], '', $base64_image_content)))) {
  173. return $path . $fileName;
  174. } else {
  175. return '';
  176. }
  177. } else {
  178. return '';
  179. }
  180. }
  181. }