Controller.php 5.8 KB

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