Controller.php 7.0 KB

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