Controller.php 7.0 KB

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