Controller.php 7.0 KB

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