Controller.php 5.8 KB

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