Controller.php 6.1 KB

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