Controller.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\Http\UploadedFile;
  11. use Illuminate\Routing\Controller as BaseController;
  12. use RuntimeException;
  13. use Str;
  14. class Controller extends BaseController {
  15. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  16. // 生成网站安全码
  17. public function makeSecurityCode(): string {
  18. return strtolower(makeRandStr(8));
  19. }
  20. // 类似Linux中的tail命令
  21. public function tail($file, $n, $base = 5) {
  22. $fileLines = $this->countLine($file);
  23. if($fileLines < 15000){
  24. return false;
  25. }
  26. $fp = fopen($file, 'rb+');
  27. assert($n > 0);
  28. $pos = $n + 1;
  29. $lines = [];
  30. while(count($lines) <= $n){
  31. try{
  32. fseek($fp, -$pos, SEEK_END);
  33. }catch(Exception $e){
  34. break;
  35. }
  36. $pos *= $base;
  37. while(!feof($fp)){
  38. array_unshift($lines, fgets($fp));
  39. }
  40. }
  41. return array_slice($lines, 0, $n);
  42. }
  43. /**
  44. * 计算文件行数
  45. *
  46. * @param $file
  47. *
  48. * @return int
  49. */
  50. public function countLine($file): int {
  51. $fp = fopen($file, 'rb');
  52. $i = 0;
  53. while(!feof($fp)){
  54. //每次读取2M
  55. if($data = fread($fp, 1024 * 1024 * 2)){
  56. //计算读取到的行数
  57. $num = substr_count($data, "\n");
  58. $i += $num;
  59. }
  60. }
  61. fclose($fp);
  62. return $i;
  63. }
  64. // 获取敏感词
  65. public function sensitiveWords($type): array {
  66. return SensitiveWords::query()->whereType($type)->get()->pluck('words')->toArray();
  67. }
  68. // 将Base64图片转换为本地图片并保存
  69. public function base64ImageSaver($base64_image_content): ?string {
  70. // 匹配出图片的格式
  71. if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
  72. $type = $result[2];
  73. $directory = date('Ymd');
  74. $path = '/assets/images/qrcode/'.$directory.'/';
  75. // 检查是否有该文件夹,如果没有就创建,并给予最高权限
  76. if(!file_exists(public_path($path))
  77. && !mkdir($concurrentDirectory = public_path($path), 0755, true)
  78. && !is_dir($concurrentDirectory)){
  79. throw new RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
  80. }
  81. $fileName = makeRandStr(18, true).".{$type}";
  82. if(file_put_contents(public_path($path.$fileName),
  83. base64_decode(str_replace($result[1], '', $base64_image_content)))){
  84. chmod(public_path($path.$fileName), 0744);
  85. return $path.$fileName;
  86. }
  87. }
  88. return '';
  89. }
  90. // 上传文件处理
  91. public function uploadFile(UploadedFile $file): string {
  92. $fileType = $file->getClientOriginalExtension();
  93. // 验证文件合法性
  94. if(!in_array($fileType, ['jpg', 'png', 'jpeg', 'bmp'])){
  95. return false;
  96. }
  97. $name = date('YmdHis').random_int(1000, 2000).'.'.$fileType;
  98. $move = $file->move(base_path().'/public/upload/image/', $name);
  99. return $move? '/upload/image/'.$name : '';
  100. }
  101. /**
  102. * 节点信息
  103. *
  104. * @param int $uid 用户ID
  105. * @param int $nodeId 节点ID
  106. * @param int $infoType 信息类型:0为链接,1为文字
  107. *
  108. * @return string
  109. */
  110. public function getUserNodeInfo($uid, $nodeId, $infoType): string {
  111. $user = User::whereId($uid)->firstOrFail();
  112. $node = SsNode::whereId($nodeId)->firstOrFail();
  113. $scheme = null;
  114. // 获取分组名称
  115. $group = $node->getLevel->name;
  116. $host = $node->server?: $node->ip;
  117. $data = null;
  118. switch($node->type){
  119. case 2:
  120. // 生成v2ray scheme
  121. if($infoType !== 1){
  122. // 生成v2ray scheme
  123. $data = $this->v2raySubUrl($node->name, $host, $node->v2_port, $user->vmess_id, $node->v2_alter_id,
  124. $node->v2_net, $node->v2_type, $node->v2_host, $node->v2_path, $node->v2_tls? "tls" : "");
  125. }else{
  126. $data = "服务器:".$host.PHP_EOL."IPv6:".($node->ipv6?: "").PHP_EOL."端口:".$node->v2_port.PHP_EOL."加密方式:".$node->v2_method.PHP_EOL."用户ID:".$user->vmess_id.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;
  127. }
  128. break;
  129. case 3:
  130. if($infoType !== 1){
  131. $data = $this->trojanSubUrl($user->passwd, $host, $node->port, $node->name);
  132. }else{
  133. $data = "备注:".$node->name.PHP_EOL."服务器:".$host.PHP_EOL."密码:".$user->passwd.PHP_EOL."端口:".$node->port.PHP_EOL;
  134. }
  135. break;
  136. case 1:
  137. case 4:
  138. $protocol = $node->protocol;
  139. $method = $node->method;
  140. $obfs = $node->obfs;
  141. if($node->single){
  142. $port = $node->port;
  143. $passwd = $node->passwd;
  144. $protocol_param = $user->port.':'.$user->passwd;
  145. }else{
  146. $port = $user->port;
  147. $passwd = $user->passwd;
  148. $protocol_param = $node->protocol_param;
  149. if($node->type === 1){
  150. $protocol = $user->protocol;
  151. $method = $user->method;
  152. $obfs = $user->obfs;
  153. }
  154. }
  155. if($infoType !== 1){
  156. // 生成ss/ssr scheme
  157. $data = $node->compatible? $this->ssSubUrl($host, $port, $method, $passwd,
  158. $group) : $this->ssrSubUrl($host, $port, $protocol, $method, $obfs, $passwd, $node->obfs_param,
  159. $protocol_param, $node->name, $group, $node->is_udp);
  160. }else{
  161. // 生成文本配置信息
  162. $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);
  163. }
  164. break;
  165. default:
  166. }
  167. return $data;
  168. }
  169. public function v2raySubUrl($name, $host, $port, $uuid, $alter_id, $net, $type, $domain, $path, $tls): string {
  170. return 'vmess://'.base64url_encode(json_encode([
  171. "v" => "2",
  172. "ps" => $name,
  173. "add" => $host,
  174. "port" => $port,
  175. "id" => $uuid,
  176. "aid" => $alter_id,
  177. "net" => $net,
  178. "type" => $type,
  179. "host" => $domain,
  180. "path" => $path,
  181. "tls" => $tls? "tls" : ""
  182. ], JSON_PRETTY_PRINT));
  183. }
  184. public function trojanSubUrl($password, $domain, $port, $remark): string {
  185. return 'trojan://'.urlencode($password).'@'.$domain.':'.$port.'#'.urlencode($remark);
  186. }
  187. public function ssSubUrl($host, $port, $method, $passwd, $group): string {
  188. return 'ss://'.base64url_encode($method.':'.$passwd.'@'.$host.':'.$port).'#'.$group;
  189. }
  190. public function ssrSubUrl(
  191. $host, $port, $protocol, $method, $obfs, $passwd, $obfs_param, $protocol_param, $name, $group, $is_udp
  192. ): string {
  193. return 'ssr://'.base64url_encode($host.':'.$port.':'.$protocol.':'.$method.':'.$obfs.':'.base64url_encode($passwd).'/?obfsparam='.base64url_encode($obfs_param).'&protoparam='.base64url_encode($protocol_param).'&remarks='.base64url_encode($name).'&group='.base64url_encode($group).'&udpport='.$is_udp.'&uot=0');
  194. }
  195. }