Controller.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\SensitiveWords;
  4. use App\Models\SsNode;
  5. use App\Models\User;
  6. use Exception;
  7. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  8. use Illuminate\Foundation\Bus\DispatchesJobs;
  9. use Illuminate\Foundation\Validation\ValidatesRequests;
  10. use Illuminate\Routing\Controller as BaseController;
  11. class Controller extends BaseController {
  12. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  13. // 生成随机密码
  14. public function makePasswd() {
  15. return makeRandStr();
  16. }
  17. // 生成UUID
  18. public function makeUUID() {
  19. return createGuid();
  20. }
  21. // 生成网站安全码
  22. public function makeSecurityCode() {
  23. return strtolower(makeRandStr(8));
  24. }
  25. // 类似Linux中的tail命令
  26. public function tail($file, $n, $base = 5) {
  27. $fileLines = $this->countLine($file);
  28. if($fileLines < 15000){
  29. return false;
  30. }
  31. $fp = fopen($file, "r+");
  32. assert($n > 0);
  33. $pos = $n + 1;
  34. $lines = [];
  35. while(count($lines) <= $n){
  36. try{
  37. fseek($fp, -$pos, SEEK_END);
  38. }catch(Exception $e){
  39. break;
  40. }
  41. $pos *= $base;
  42. while(!feof($fp)){
  43. array_unshift($lines, fgets($fp));
  44. }
  45. }
  46. return array_slice($lines, 0, $n);
  47. }
  48. /**
  49. * 计算文件行数
  50. *
  51. * @param $file
  52. *
  53. * @return int
  54. */
  55. public function countLine($file) {
  56. $fp = fopen($file, "r");
  57. $i = 0;
  58. while(!feof($fp)){
  59. //每次读取2M
  60. if($data = fread($fp, 1024 * 1024 * 2)){
  61. //计算读取到的行数
  62. $num = substr_count($data, "\n");
  63. $i += $num;
  64. }
  65. }
  66. fclose($fp);
  67. return $i;
  68. }
  69. // 获取敏感词
  70. public function sensitiveWords($type) {
  71. return SensitiveWords::query()->whereType($type)->get()->pluck('words')->toArray();
  72. }
  73. // 将Base64图片转换为本地图片并保存
  74. function base64ImageSaver($base64_image_content) {
  75. // 匹配出图片的格式
  76. if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
  77. $type = $result[2];
  78. $directory = date('Ymd');
  79. $path = '/assets/images/qrcode/'.$directory.'/';
  80. if(!file_exists(public_path($path))){ // 检查是否有该文件夹,如果没有就创建,并给予最高权限
  81. mkdir(public_path($path), 0755, true);
  82. }
  83. $fileName = makeRandStr(18, true).".{$type}";
  84. if(file_put_contents(public_path($path.$fileName),
  85. base64_decode(str_replace($result[1], '', $base64_image_content)))){
  86. chmod(public_path($path.$fileName), 0744);
  87. return $path.$fileName;
  88. }else{
  89. return '';
  90. }
  91. }else{
  92. return '';
  93. }
  94. }
  95. /**
  96. * 节点信息
  97. *
  98. * @param int $uid 用户ID
  99. * @param int $nodeId 节点ID
  100. * @param int $infoType 信息类型:0为链接,1为文字
  101. *
  102. * @return string
  103. */
  104. function getNodeInfo($uid, $nodeId, $infoType) {
  105. $user = User::whereId($uid)->first();
  106. $node = SsNode::whereId($nodeId)->first();
  107. $scheme = null;
  108. // 获取分组名称
  109. $group = $node->getLevel()->first()->name;
  110. $host = $node->server?: $node->ip;
  111. $data = null;
  112. switch($node->type){
  113. case 1:
  114. if($node->single){
  115. $port = $node->port;
  116. $protocol = $node->protocol;
  117. $method = $node->method;
  118. $obfs = $node->obfs;
  119. $passwd = $node->passwd;
  120. $protocol_param = $user->port.':'.$user->passwd;
  121. }else{
  122. $port = $user->port;
  123. $protocol = $user->protocol;
  124. $method = $user->method;
  125. $obfs = $user->obfs;
  126. $passwd = $user->passwd;
  127. $protocol_param = $node->protocol_param;
  128. }
  129. if($infoType != 1){
  130. // 生成ss/ssr scheme
  131. if($node->compatible){
  132. $data = 'ss://'.base64url_encode($method.':'.$passwd.'@'.$host.':'.$port).'#'.$group;
  133. }else{
  134. $data = 'ssr://'.base64url_encode($host.':'.$port.':'.$protocol.':'.$method.':'.$obfs.':'.base64url_encode($passwd).'/?obfsparam='.base64url_encode($node->obfs_param).'&protoparam='.base64url_encode($protocol_param).'&remarks='.base64url_encode($node->name).'&group='.base64url_encode($group).'&udpport=0&uot=0');
  135. }
  136. }else{
  137. // 生成文本配置信息
  138. $data = "服务器:".$host.PHP_EOL."IPv6:".$node->ipv6.PHP_EOL."服务器端口:".$port.PHP_EOL."密码:".$passwd.PHP_EOL."加密:".$method.PHP_EOL.($node->compatible? '' : "协议:".$protocol.PHP_EOL."协议参数:".$protocol_param.PHP_EOL."混淆:".$obfs.PHP_EOL."混淆参数:".$node->obfs_param.PHP_EOL);
  139. }
  140. break;
  141. case 2:
  142. // 生成v2ray scheme
  143. if($infoType != 1){
  144. // 生成v2ray scheme
  145. $data = 'vmess://'.base64url_encode(json_encode([
  146. "v" => "2",
  147. "ps" => $node->name,
  148. "add" => $host,
  149. "port" => $node->v2_port,
  150. "id" => $user->uuid,
  151. "aid" => $node->v2_alter_id,
  152. "net" => $node->v2_net,
  153. "type" => $node->v2_type,
  154. "host" => $node->v2_host,
  155. "path" => $node->v2_path,
  156. "tls" => $node->v2_tls? "tls" : ""
  157. ], JSON_PRETTY_PRINT));
  158. }else{
  159. $data = "服务器:".$host.PHP_EOL."IPv6:".($node->ipv6?: "").PHP_EOL."端口:".$node->v2_port.PHP_EOL."加密方式:".$node->v2_method.PHP_EOL."用户ID:".$user->uuid.PHP_EOL."额外ID:".$node->v2_alter_id.PHP_EOL."传输协议:".$node->v2_net.PHP_EOL."伪装类型:".$node->v2_type.PHP_EOL."伪装域名:".($node->v2_host?: "").PHP_EOL."路径:".($node->v2_path?: "").PHP_EOL."TLS:".($node->v2_tls? "tls" : "").PHP_EOL;
  160. }
  161. break;
  162. case 3:
  163. break;
  164. default:
  165. }
  166. return $data;
  167. }
  168. }