Controller.php 5.6 KB

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