Controller.php 6.3 KB

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