From abe78182fe5e14f25adc0ebc209bc8c29231843d Mon Sep 17 00:00:00 2001 From: mercury Date: Wed, 10 Apr 2024 01:21:51 +0400 Subject: [PATCH] xtls pagination --- app/bot.php | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/app/bot.php b/app/bot.php index 3d84448..ae2f042 100644 --- a/app/bot.php +++ b/app/bot.php @@ -6,6 +6,7 @@ class Bot public $adguard; public $update; public $ip; + public $limit; public function __construct($key, $i18n) { @@ -354,8 +355,8 @@ class Bot case preg_match('~^/domain$~', $this->input['callback'], $m): $this->domain(); break; - case preg_match('~^/xray$~', $this->input['callback'], $m): - $this->xray(); + case preg_match('~^/xray(?: (\d+))?$~', $this->input['callback'], $m): + $this->xray($m[1] ?: 0); break; case preg_match('~^/generateSecretXray$~', $this->input['callback'], $m): $this->generateSecretXray(); @@ -3559,7 +3560,7 @@ DNS-over-HTTPS with IP: $this->xray(); } - public function xray() + public function xray($page = 0) { if (!$this->ssh('pgrep xray', 'xr')) { $this->generateSecretXray(); @@ -3590,28 +3591,46 @@ DNS-over-HTTPS with IP: $on++; } } + $type = $this->getPacConf()['xtlslist']; $data[] = [ [ - 'text' => $this->i18n('on') . " $on", + 'text' => $this->i18n('on') . " $on " . (!$type ? "✅" : ''), 'callback_data' => "/listXr 0", ], [ - 'text' => $this->i18n('off') . " $off", + 'text' => $this->i18n('off') . " $off " . ($type ? "✅" : ''), 'callback_data' => "/listXr 1", ], ]; - $type = $this->getPacConf()['xtlslist']; - $c['inbounds'][0]['settings']['clients'] = array_filter($c['inbounds'][0]['settings']['clients'], fn($e) => !$type ? empty($e['off']) : !empty($e['off'])); - usort($c['inbounds'][0]['settings']['clients'], fn($a, $b) => ($a['time'] ?: PHP_INT_MAX) <=> ($b['time'] ?: PHP_INT_MAX)); - foreach ($c['inbounds'][0]['settings']['clients'] as $k => $v) { + $clients = array_filter($c['inbounds'][0]['settings']['clients'], fn($e) => !$type ? empty($e['off']) : !empty($e['off'])); + uasort($clients, fn($a, $b) => ($a['time'] ?: PHP_INT_MAX) <=> ($b['time'] ?: PHP_INT_MAX)); + + $all = (int) ceil(count($clients) / $this->limit); + $page = min($page, $all - 1); + $page = $page == -2 ? $all - 1 : $page; + $clients = $page != -1 ? array_slice($clients, $page * $this->limit, $this->limit, true) : $clients; + foreach ($clients as $k => $v) { $time = $v['time'] ? $this->getTime($v['time']) : ''; $data[] = [ [ - 'text' => $this->i18n($v['off'] ? 'off' : 'on') . " {$v['email']}" . ($time ? ": $time" : ''), + 'text' => "{$v['email']}" . ($time ? ": $time" : ''), 'callback_data' => "/userXr $k", ], ]; } + if ($page != -1 && $all > 1) { + $data[] = [ + [ + 'text' => '<<', + 'callback_data' => "/xray " . ($page - 1 >= 0 ? $page - 1 : $all - 1), + ], + [ + 'text' => '>>', + 'callback_data' => "/xray " . ($page < $all - 1 ? $page + 1 : 0), + ] + ]; + } + $data[] = [ [ 'text' => $this->i18n('back'),